diff --git a/src/Field.tsx b/src/Field.tsx index 60d72a2c..928767ae 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'; @@ -644,7 +645,15 @@ function WrapperField({ name, ...restProps }: FieldProps) warning(false, '`preserve` should not apply on Form.List fields.'); } - return ; + return ( + + ); } export default WrapperField; diff --git a/src/List.tsx b/src/List.tsx index 64db2e20..839e8908 100644 --- a/src/List.tsx +++ b/src/List.tsx @@ -43,6 +43,7 @@ const List: React.FunctionComponent = ({ isListField, }) => { const context = React.useContext(FieldContext); + const wrapperListContext = React.useContext(ListContext); const keyRef = React.useRef({ keys: [], id: 0, @@ -91,7 +92,7 @@ const List: React.FunctionComponent = ({ validateTrigger={validateTrigger} initialValue={initialValue} isList - isListField={isListField} + isListField={isListField ?? !!wrapperListContext} > {({ value = [], onChange }, meta) => { const { getFieldValue } = context; diff --git a/tests/list.test.tsx b/tests/list.test.tsx index 06a66132..c808f046 100644 --- a/tests/list.test.tsx +++ b/tests/list.test.tsx @@ -628,6 +628,51 @@ describe('Form.List', () => { 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) => ( +
+ {fields.map(field => ( +
+ + + + + + +
+ ))} +
+ ), + { + onValuesChange, + initialValues: { + list: [{ first: 'light' }], + }, + }, + ); + 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' }] }); + }); + describe('isFieldTouched edge case', () => { it('virtual object', () => { const formRef = React.createRef();