Skip to content

Commit c6dab30

Browse files
authored
fix: form onValues second params values (#582)
* fix: form onValues second params values close ant-design/ant-design#41053 * test: improve list case * fix: review * fix: list nest list * test: add list case * fix: up
1 parent b701987 commit c6dab30

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/Field.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
RuleError,
1919
} from './interface';
2020
import FieldContext, { HOOK_MARK } from './FieldContext';
21+
import ListContext from './ListContext';
2122
import { toArray } from './utils/typeUtil';
2223
import { validateRules } from './utils/validateUtil';
2324
import {
@@ -625,7 +626,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
625626

626627
function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>) {
627628
const fieldContext = React.useContext(FieldContext);
628-
629+
const listContext = React.useContext(ListContext);
629630
const namePath = name !== undefined ? getNamePath(name) : undefined;
630631

631632
let key: string = 'keep';
@@ -644,7 +645,15 @@ function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>)
644645
warning(false, '`preserve` should not apply on Form.List fields.');
645646
}
646647

647-
return <Field key={key} name={namePath} {...restProps} fieldContext={fieldContext} />;
648+
return (
649+
<Field
650+
key={key}
651+
name={namePath}
652+
isListField={!!listContext}
653+
{...restProps}
654+
fieldContext={fieldContext}
655+
/>
656+
);
648657
}
649658

650659
export default WrapperField;

src/List.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const List: React.FunctionComponent<ListProps> = ({
4343
isListField,
4444
}) => {
4545
const context = React.useContext(FieldContext);
46+
const wrapperListContext = React.useContext(ListContext);
4647
const keyRef = React.useRef({
4748
keys: [],
4849
id: 0,
@@ -91,7 +92,7 @@ const List: React.FunctionComponent<ListProps> = ({
9192
validateTrigger={validateTrigger}
9293
initialValue={initialValue}
9394
isList
94-
isListField={isListField}
95+
isListField={isListField ?? !!wrapperListContext}
9596
>
9697
{({ value = [], onChange }, meta) => {
9798
const { getFieldValue } = context;

tests/list.test.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,51 @@ describe('Form.List', () => {
628628
expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { list: [{ first: 'light' }] });
629629
});
630630

631+
it('Nest list remove should trigger correct onValuesChange when no spread field props', () => {
632+
const onValuesChange = jest.fn();
633+
634+
const [wrapper] = generateForm(
635+
(fields, operation) => (
636+
<div>
637+
{fields.map(field => (
638+
<div key={field.key}>
639+
<Field name={[field.name, 'first']}>
640+
<Input />
641+
</Field>
642+
<Field name={[field.name, 'second']}>
643+
<Input />
644+
</Field>
645+
</div>
646+
))}
647+
<button
648+
type="button"
649+
onClick={() => {
650+
operation.add();
651+
}}
652+
/>
653+
<button
654+
type="button"
655+
onClick={() => {
656+
operation.remove(1);
657+
}}
658+
/>
659+
</div>
660+
),
661+
{
662+
onValuesChange,
663+
initialValues: {
664+
list: [{ first: 'light' }],
665+
},
666+
},
667+
);
668+
wrapper.find('button').first().simulate('click');
669+
expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), {
670+
list: [{ first: 'light' }, undefined],
671+
});
672+
wrapper.find('button').last().simulate('click');
673+
expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { list: [{ first: 'light' }] });
674+
});
675+
631676
describe('isFieldTouched edge case', () => {
632677
it('virtual object', () => {
633678
const formRef = React.createRef<FormInstance>();

0 commit comments

Comments
 (0)