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): emit event for nested objects #1634

Merged
merged 2 commits 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
12 changes: 11 additions & 1 deletion packages/core/forms/src/components/FormGenerator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
:options="options"
:vfg="vfg"
@model-updated="onModelUpdated"
@refresh-model="onRefreshModel"
@validated="onFieldValidated"
/>
</template>
Expand Down Expand Up @@ -70,6 +71,7 @@
:options="options"
:vfg="vfg"
@model-updated="onModelUpdated"
@refresh-model="onRefreshModel"
@validated="onFieldValidated"
/>
</template>
Expand Down Expand Up @@ -100,6 +102,7 @@
:options="options"
:vfg="vfg"
@model-updated="onModelUpdated"
@refresh-model="onRefreshModel"
@validated="onFieldValidated"
/>
</template>
Expand Down Expand Up @@ -128,6 +131,7 @@
:options="options"
:vfg="vfg"
@model-updated="onModelUpdated"
@refresh-model="onRefreshModel"
@validated="onFieldValidated"
/>
</template>
Expand Down Expand Up @@ -224,7 +228,7 @@ export default {
},
},
},
emits: ['validated', 'modelUpdated'],
emits: ['validated', 'modelUpdated', 'refreshModel'],

data() {
return {
Expand Down Expand Up @@ -325,6 +329,12 @@ export default {
this.$emit('validated', isValid, this.errors, this)
},

onRefreshModel() {
// This is for updating a deeply nested array element
// See `modelUpdated` in `FieldArray.vue`
this.$emit('refreshModel')
},

onModelUpdated(newVal, schema) {
this.$emit('modelUpdated', newVal, schema)
},
Expand Down
8 changes: 7 additions & 1 deletion packages/core/forms/src/components/FormGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
:schema="field"
:vfg="vfg"
@model-updated="onModelUpdated"
@refresh-model="onRefreshModel"
@validated="onFieldValidated"
/>
<div
Expand Down Expand Up @@ -121,7 +122,7 @@ export default {
},
},
},
emits: ['validated', 'modelUpdated'],
emits: ['validated', 'modelUpdated', 'refreshModel'],
data() {
return {
child: ref(),
Expand Down Expand Up @@ -182,6 +183,11 @@ export default {
fieldErrors(field) {
return this.errors.filter((e) => e.field.fieldName === field.fieldName).map((item) => item.error)
},
onRefreshModel() {
// This is for updating a deeply nested array element
// See `modelUpdated` in `FieldArray.vue`
this.$emit('refreshModel')
},
onModelUpdated(newVal, schema) {
this.$emit('modelUpdated', newVal, schema)
},
Expand Down
14 changes: 13 additions & 1 deletion packages/core/forms/src/components/fields/FieldArray.vue
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export default {
default: 'x',
},
},
emits: ['refreshModel'],
methods: {
generateSchema(rootValue, schema, index) {
// Instead of using schema directly, we make a copy to avoid schema object mutation side effects
Expand Down Expand Up @@ -211,7 +212,18 @@ export default {
getFieldType(fieldSchema) {
return 'field-' + fieldSchema.type
},
modelUpdated() {},
modelUpdated() {
// Ideally the event handler should be:
// `modelUpdated(model, schema) { this.$emit('modelUpdated', model, schema) }`
// but currently `schema` is the plain key of the field:
// when the field is nested, `schema` does not contain any info about its parent field,
// so in the consuming components, we cannot know which field is being updated.
// For example, if each element of the array is an object with a `name` field,
// then if the user updates the `name` field, the `schema` will be `name` here,
// so the consuming component will think the root `name` field is updated,
// which is the name of the plugin and should not be updated.
this.$emit('refreshModel')
},
handleInput(val, index) {
this.value = this.value.map((item, i) => i === index ? val : item)
},
Expand Down
7 changes: 7 additions & 0 deletions packages/core/forms/src/components/fields/FieldObject.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
:model="value"
:options="formOptions"
:schema="schema.schema"
@model-updated="updateModel"
/>
<div v-else>
<table
Expand Down Expand Up @@ -78,6 +79,8 @@ import abstractField from './abstractField'
export default {
mixins: [abstractField],

emits: ['modelUpdated'],

data() {
return {
attributes: this.schema ? this.schema.attributes : undefined,
Expand Down Expand Up @@ -124,6 +127,10 @@ export default {
this.keyTypes = { ...this.keyTypes }
this.newKeyName = ''
},

updateModel(model, schema) {
this.$emit('modelUpdated', model, schema)
},
},
}
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
:options="formOptions"
:schema="formSchema"
@model-updated="onModelUpdated"
@refresh-model="getModel"
>
<template #plugin-config-empty-state>
<div class="plugin-config-empty-state">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,9 @@ const buildFormSchema = (parentKey: string, response: Record<string, any>, initi

// Field type is an input, determine input type, such as 'text', or 'number'
if (initialFormSchema[field].type === 'input') {
if (['string', 'number'].includes(typeof initialFormSchema[field].default)) {
initialFormSchema[field].placeholder = `Default: ${initialFormSchema[field].default}`
}
Leopoldthecoder marked this conversation as resolved.
Show resolved Hide resolved
const { type: elementsType } = scheme.elements || {}

if (elementsType) {
Expand Down
Loading