From fdf2abb7028cff75a0ffc9e02bd9c9ce86d8a47b Mon Sep 17 00:00:00 2001 From: layhuts Date: Wed, 1 Jul 2026 11:08:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9form-field-array?= =?UTF-8?q?=E6=94=AF=E6=8C=81dependencies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui-kit/form-ui/__tests__/form-api.test.ts | 70 ++++ .../src/components/form-field-array.vue | 182 ++++++---- packages/@core/ui-kit/form-ui/src/form-api.ts | 233 +++++++++--- .../form-ui/src/form-render/form-field.vue | 7 +- .../ui-kit/form-ui/src/form-render/form.vue | 99 +---- .../ui-kit/form-ui/src/form-render/schema.ts | 341 ++++++++++++++++++ packages/@core/ui-kit/form-ui/src/index.ts | 1 + packages/@core/ui-kit/form-ui/src/types.ts | 57 ++- .../src/router/routes/modules/examples.ts | 9 + .../src/views/demos/form-array/README.md | 325 +++++++++++++++++ .../src/views/demos/form-array/index.vue | 187 ++++++++++ 11 files changed, 1315 insertions(+), 196 deletions(-) create mode 100644 packages/@core/ui-kit/form-ui/src/form-render/schema.ts create mode 100644 playground/src/views/demos/form-array/README.md create mode 100644 playground/src/views/demos/form-array/index.vue diff --git a/packages/@core/ui-kit/form-ui/__tests__/form-api.test.ts b/packages/@core/ui-kit/form-ui/__tests__/form-api.test.ts index a4c05b54..50ae2b75 100644 --- a/packages/@core/ui-kit/form-ui/__tests__/form-api.test.ts +++ b/packages/@core/ui-kit/form-ui/__tests__/form-api.test.ts @@ -101,6 +101,47 @@ describe('formApi', () => { expect(formActions.values).toEqual(originalValuesSnapshot); }); + it('should format child schema values inside array fields', async () => { + formApi.setState({ + schema: [ + { + children: [ + { + component: 'text', + fieldName: 'name', + valueFormat: ( + value: any, + setValue: any, + _values: any, + ctx: any, + ) => { + setValue('normalizedName', value?.trim()); + setValue('$root.firstRow', ctx?.rowIndex); + }, + }, + ], + fieldName: 'contacts', + type: 'array', + } as any, + ], + }); + + const formActions: any = { + meta: {}, + values: { + contacts: [{ name: ' Ada ' }, { name: ' Grace ' }], + }, + }; + + await formApi.mount(formActions, new Map()); + + const values = await formApi.getValues(); + expect(values).toEqual({ + contacts: [{ normalizedName: 'Ada' }, { normalizedName: 'Grace' }], + firstRow: 1, + }); + }); + it('should set field value', async () => { const setFieldValueMock = vi.fn(); const formActions: any = { @@ -229,6 +270,35 @@ describe('updateSchema', () => { expect(instance.state?.schema?.[1]?.label).toBe('Age'); }); + it('should update child schema by parent path', () => { + instance.state = { + schema: [ + { + children: [ + { component: 'text', fieldName: 'name', label: 'Name' }, + { component: 'text', fieldName: 'phone', label: 'Phone' }, + ], + fieldName: 'contacts', + type: 'array', + } as any, + ], + }; + + instance.updateSchema([ + { + fieldName: 'contacts.name', + label: 'Full Name', + }, + ]); + + expect((instance.state?.schema?.[0] as any)?.children?.[0]?.label).toBe( + 'Full Name', + ); + expect((instance.state?.schema?.[0] as any)?.children?.[1]?.label).toBe( + 'Phone', + ); + }); + it('should log an error if fieldName is missing in some items', () => { const newSchema: any[] = [ { component: 'textarea', fieldName: 'name' }, diff --git a/packages/@core/ui-kit/form-ui/src/components/form-field-array.vue b/packages/@core/ui-kit/form-ui/src/components/form-field-array.vue index 1d7a0730..27e15542 100644 --- a/packages/@core/ui-kit/form-ui/src/components/form-field-array.vue +++ b/packages/@core/ui-kit/form-ui/src/components/form-field-array.vue @@ -1,5 +1,5 @@