Skip to content

Commit

Permalink
fix: normalize as the same should not trigger onValuesChange (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair authored Oct 30, 2024
1 parent 38c629a commit df61d03
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,13 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
if (normalize) {
newValue = normalize(newValue, value, getFieldsValue(true));
}

dispatch({
type: 'updateValue',
namePath,
value: newValue,
});

if (!isEqual(newValue, value)) {
dispatch({
type: 'updateValue',
namePath,
value: newValue,
});
}
if (originTriggerFunc) {
originTriggerFunc(...args);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/control.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,18 @@ describe('Form.Control', () => {
await changeValue(getInput(container), ['bamboo', '']);
matchError(container, "'test' is required");
});

it('value no change', async () => {
const fn = jest.fn();
const { container } = render(
<Form onFieldsChange={fn}>
<InfoField name="test" normalize={value => value?.replace(/\D/g, '') || undefined} />
</Form>,
);

await changeValue(getInput(container), 'bamboo');
expect(fn).toHaveBeenCalledTimes(0);
await changeValue(getInput(container), '1');
expect(fn).toHaveBeenCalledTimes(1);
});
});

0 comments on commit df61d03

Please sign in to comment.