From 9fed30e8d28239a7d8567521c09046519a402d41 Mon Sep 17 00:00:00 2001 From: elevensky Date: Wed, 5 Apr 2023 18:22:14 +0800 Subject: [PATCH 1/6] fix: form onValues second params values close https://github.com/ant-design/ant-design/issues/41053 --- src/Field.tsx | 7 ++++++- src/List.tsx | 1 + src/ListContext.ts | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Field.tsx b/src/Field.tsx index 60d72a2c..65189966 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -18,6 +18,7 @@ import type { RuleError, } from './interface'; import FieldContext, { HOOK_MARK } from './FieldContext'; +import ListContext from './ListContext'; import { toArray } from './utils/typeUtil'; import { validateRules } from './utils/validateUtil'; import { @@ -625,7 +626,7 @@ class Field extends React.Component implements F function WrapperField({ name, ...restProps }: FieldProps) { const fieldContext = React.useContext(FieldContext); - + const listContext = React.useContext(ListContext); const namePath = name !== undefined ? getNamePath(name) : undefined; let key: string = 'keep'; @@ -633,6 +634,10 @@ function WrapperField({ name, ...restProps }: FieldProps) key = `_${(namePath || []).join('_')}`; } + if (restProps.isListField === undefined && !restProps.isList && listContext) { + restProps.isListField = listContext.inList; + } + // Warning if it's a directly list field. // We can still support multiple level field preserve. if ( diff --git a/src/List.tsx b/src/List.tsx index 64db2e20..95f59f3c 100644 --- a/src/List.tsx +++ b/src/List.tsx @@ -64,6 +64,7 @@ const List: React.FunctionComponent = ({ const pathName = namePath[len]; return [keyManager.keys[pathName], namePath.slice(len + 1)]; }, + inList: true, }), [prefixName], ); diff --git a/src/ListContext.ts b/src/ListContext.ts index cb5d0791..d5ef8f8c 100644 --- a/src/ListContext.ts +++ b/src/ListContext.ts @@ -3,6 +3,7 @@ import type { InternalNamePath } from './interface'; export interface ListContextProps { getKey: (namePath: InternalNamePath) => [React.Key, InternalNamePath]; + inList: boolean; } const ListContext = React.createContext(null); From c89c7d256d85d2b39d6b15792a673f8664ba7cea Mon Sep 17 00:00:00 2001 From: elevensky Date: Wed, 12 Apr 2023 17:20:54 +0800 Subject: [PATCH 2/6] test: improve list case --- tests/list.test.tsx | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/list.test.tsx b/tests/list.test.tsx index 06a66132..66150188 100644 --- a/tests/list.test.tsx +++ b/tests/list.test.tsx @@ -604,10 +604,21 @@ describe('Form.List', () => { (fields, operation) => (
{fields.map(field => ( - - - +
+ + + + + + +
))} +
+ ), + { + onValuesChange, + initialValues: { + list: [{ first: 'light' }, { first: 'bamboo' }], + }, + }, + ); + + wrapper.find('button').simulate('click'); + expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { list: [{ first: 'light' }] }); + }); + + it('Nest list remove should trigger correct onValuesChange when no spread field props', () => { + const onValuesChange = jest.fn(); + const [wrapper] = generateForm( (fields, operation) => (
From 45e167657e7239d1319f12b9bc92e16014e9c45e Mon Sep 17 00:00:00 2001 From: elevensky Date: Sun, 23 Apr 2023 19:42:35 +0800 Subject: [PATCH 6/6] fix: up --- tests/list.test.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/list.test.tsx b/tests/list.test.tsx index d6cd61f4..c808f046 100644 --- a/tests/list.test.tsx +++ b/tests/list.test.tsx @@ -666,6 +666,9 @@ describe('Form.List', () => { }, ); wrapper.find('button').first().simulate('click'); + expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { + list: [{ first: 'light' }, undefined], + }); wrapper.find('button').last().simulate('click'); expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { list: [{ first: 'light' }] }); });