Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forms): add index to deeply nested input id #1638

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/core/forms/src/components/FormGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
:form-options="options"
:hint="field.hint && getFieldType(field) === 'field-input' ? fieldHint(field) : undefined"
:model="model"
:schema="field"
:schema="schema"
:vfg="vfg"
@model-updated="onModelUpdated"
@validated="onFieldValidated"
Expand Down Expand Up @@ -97,6 +97,11 @@ export default {
name: 'FormGroup',
components: fieldComponents,
mixins: [formMixin],
inject: {
'vfg-array-item-index': {
default: undefined,
},
},
props: {
vfg: {
type: Object,
Expand Down Expand Up @@ -127,6 +132,30 @@ export default {
child: ref(),
}
},
computed: {
schema() {
if (!this.field?.schema || !Array.isArray(this.field.schema.fields)) {
return this.field
}

const index = this['vfg-array-item-index']

if (typeof index !== 'number' || Number.isNaN(index)) {
return this.field
}

return {
...this.field,
schema: {
...this.field.schema,
fields: this.field.schema.fields.map(field => ({
...field,
...(field.id ? { id: `${field.id}-${index}` } : undefined),
})),
},
}
},
},
methods: {
// Should field type have a label?
fieldTypeHasLabel(field) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/forms/src/components/fields/FieldArray.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<component
:is="schema.itemContainerComponent"
v-if="schema.items && schema.itemContainerComponent"
:index="index"
:model="item"
:schema="generateSchema(value, schema.items, index)"
@remove-item="removeElement(index)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
</template>

<script lang="ts" setup>
import { provide } from 'vue'
import { TrashIcon } from '@kong/icons'

defineProps({
const props = defineProps({
model: {
type: Object,
default: () => null,
Expand All @@ -26,11 +27,17 @@ defineProps({
type: Object,
default: () => null,
},
index: {
type: Number,
default: undefined,
},
})

defineEmits<{
(e: 'remove-item'): void,
}>()

provide('vfg-array-item-index', props.index)
</script>

<style lang='scss'>
Expand Down
Loading