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: form onValues second params values #582

Merged
merged 6 commits into from
Apr 24, 2023
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
13 changes: 11 additions & 2 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -625,7 +626,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F

function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>) {
const fieldContext = React.useContext(FieldContext);

const listContext = React.useContext(ListContext);
const namePath = name !== undefined ? getNamePath(name) : undefined;

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

return <Field key={key} name={namePath} {...restProps} fieldContext={fieldContext} />;
return (
<Field
key={key}
name={namePath}
isListField={!!listContext}
{...restProps}
fieldContext={fieldContext}
/>
);
}

export default WrapperField;
3 changes: 2 additions & 1 deletion src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const List: React.FunctionComponent<ListProps> = ({
isListField,
}) => {
const context = React.useContext(FieldContext);
const wrapperListContext = React.useContext(ListContext);
const keyRef = React.useRef({
keys: [],
id: 0,
Expand Down Expand Up @@ -91,7 +92,7 @@ const List: React.FunctionComponent<ListProps> = ({
validateTrigger={validateTrigger}
initialValue={initialValue}
isList
isListField={isListField}
isListField={isListField ?? !!wrapperListContext}
>
{({ value = [], onChange }, meta) => {
const { getFieldValue } = context;
Expand Down
45 changes: 45 additions & 0 deletions tests/list.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
<div>
{fields.map(field => (
<div key={field.key}>
elevensky marked this conversation as resolved.
Show resolved Hide resolved
<Field name={[field.name, 'first']}>
<Input />
</Field>
<Field name={[field.name, 'second']}>
<Input />
</Field>
</div>
))}
<button
type="button"
onClick={() => {
operation.add();
}}
/>
<button
type="button"
onClick={() => {
operation.remove(1);
}}
/>
</div>
),
{
onValuesChange,
initialValues: {
list: [{ first: 'light' }],
},
},
);
wrapper.find('button').first().simulate('click');
elevensky marked this conversation as resolved.
Show resolved Hide resolved
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<FormInstance>();
Expand Down