Skip to content

Commit 2c0fa4b

Browse files
authored
fix: change not trigger on Date (#734)
1 parent e5738d4 commit 2c0fa4b

File tree

4 files changed

+69
-31
lines changed

4 files changed

+69
-31
lines changed

src/Field.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
620620
if (normalize) {
621621
newValue = normalize(newValue, value, getFieldsValue(true));
622622
}
623-
if (!isEqual(newValue, value)) {
623+
if (newValue !== value) {
624624
dispatch({
625625
type: 'updateValue',
626626
namePath,

tests/control.test.tsx

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,4 @@ describe('Form.Control', () => {
4040
await changeValue(getInput(container), ['bamboo', '']);
4141
matchError(container, "'test' is required");
4242
});
43-
44-
it('value no change', async () => {
45-
const fn = jest.fn();
46-
const { container } = render(
47-
<Form onFieldsChange={fn}>
48-
<InfoField name="test" normalize={value => value?.replace(/\D/g, '') || undefined} />
49-
</Form>,
50-
);
51-
52-
await changeValue(getInput(container), 'bamboo');
53-
expect(fn).toHaveBeenCalledTimes(0);
54-
await changeValue(getInput(container), '1');
55-
expect(fn).toHaveBeenCalledTimes(1);
56-
});
5743
});

tests/field.test.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import React from 'react';
22
import Form, { Field } from '../src';
33
import type { FormInstance } from '../src';
4-
import { render } from '@testing-library/react';
4+
import { act, fireEvent, render } from '@testing-library/react';
5+
import { Input } from './common/InfoField';
6+
import { getInput } from './common';
7+
import timeout from './common/timeout';
58

69
describe('Form.Field', () => {
710
it('field remount should trigger constructor again', () => {
@@ -37,4 +40,37 @@ describe('Form.Field', () => {
3740
// expect((wrapper.find('Field').instance() as any).cancelRegisterFunc).toBeTruthy();
3841
expect(formRef.getFieldsValue()).toEqual({ light: 'bamboo' });
3942
});
43+
44+
// https://github.com/ant-design/ant-design/issues/51611
45+
it('date type as change', async () => {
46+
const onValuesChange = jest.fn();
47+
48+
const MockDateInput = (props: { onChange?: (val: Date) => void }) => (
49+
<button
50+
onClick={() => {
51+
props.onChange?.(new Date());
52+
}}
53+
>
54+
Mock
55+
</button>
56+
);
57+
58+
const { container } = render(
59+
<Form onValuesChange={onValuesChange}>
60+
<Field name="date">
61+
<MockDateInput />
62+
</Field>
63+
</Form>,
64+
);
65+
66+
// Trigger
67+
for (let i = 0; i < 3; i += 1) {
68+
fireEvent.click(container.querySelector('button'));
69+
await act(async () => {
70+
await timeout();
71+
});
72+
expect(onValuesChange).toHaveBeenCalled();
73+
onValuesChange.mockReset();
74+
}
75+
});
4076
});

tests/legacy/field-props.test.tsx

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import type { FormInstance } from '../../src';
33
import Form, { Field } from '../../src';
4-
import { Input } from '../common/InfoField';
4+
import InfoField, { Input } from '../common/InfoField';
55
import { changeValue, getInput, matchArray } from '../common';
66
import { render } from '@testing-library/react';
77

@@ -55,22 +55,38 @@ describe('legacy.field-props', () => {
5555
expect(form.current?.getFieldValue('normal')).toBe('21');
5656
});
5757

58-
it('normalize', async () => {
59-
const form = React.createRef<FormInstance>();
60-
const { container } = render(
61-
<div>
62-
<Form ref={form}>
63-
<Field name="normal" normalize={v => v && v.toUpperCase()}>
64-
<Input />
65-
</Field>
66-
</Form>
67-
</div>,
68-
);
58+
describe('normalize', () => {
59+
it('basic', async () => {
60+
const form = React.createRef<FormInstance>();
61+
const { container } = render(
62+
<div>
63+
<Form ref={form}>
64+
<Field name="normal" normalize={v => v && v.toUpperCase()}>
65+
<Input />
66+
</Field>
67+
</Form>
68+
</div>,
69+
);
6970

70-
await changeValue(getInput(container), 'a');
71+
await changeValue(getInput(container), 'a');
72+
73+
expect(form.current?.getFieldValue('normal')).toBe('A');
74+
expect(getInput(container).value).toBe('A');
75+
});
76+
77+
it('value no change', async () => {
78+
const fn = jest.fn();
79+
const { container } = render(
80+
<Form onFieldsChange={fn}>
81+
<InfoField name="test" normalize={value => value?.replace(/\D/g, '') || undefined} />
82+
</Form>,
83+
);
7184

72-
expect(form.current?.getFieldValue('normal')).toBe('A');
73-
expect(getInput(container).value).toBe('A');
85+
await changeValue(getInput(container), 'bamboo');
86+
expect(fn).toHaveBeenCalledTimes(0);
87+
await changeValue(getInput(container), '1');
88+
expect(fn).toHaveBeenCalledTimes(1);
89+
});
7490
});
7591

7692
it('support jsx message', async () => {

0 commit comments

Comments
 (0)