Skip to content

Commit cb183e6

Browse files
author
lichlli
committed
fix: field value updates may fail multiple times
1 parent 6611c31 commit cb183e6

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/Field.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
606606
this.dirty = true;
607607

608608
this.triggerMetaEvent();
609+
const curValue = this.getValue();
609610

610611
let newValue: StoreValue;
611612
if (getValueFromEvent) {
@@ -615,9 +616,9 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
615616
}
616617

617618
if (normalize) {
618-
newValue = normalize(newValue, value, getFieldsValue(true));
619+
newValue = normalize(newValue, curValue, getFieldsValue(true));
619620
}
620-
if (newValue !== value) {
621+
if (newValue !== curValue) {
621622
dispatch({
622623
type: 'updateValue',
623624
namePath,

tests/field.test.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Form, { Field } from '../src';
33
import type { FormInstance } from '../src';
44
import { act, fireEvent, render } from '@testing-library/react';
55
import { Input } from './common/InfoField';
6-
import { getInput } from './common';
76
import timeout from './common/timeout';
87

98
describe('Form.Field', () => {
@@ -73,4 +72,38 @@ describe('Form.Field', () => {
7372
onValuesChange.mockReset();
7473
}
7574
});
75+
76+
// https://github.com/react-component/field-form/issues/753
77+
it('value change multiple times', async () => {
78+
const form = React.createRef<FormInstance>();
79+
const MockBtnInput = props => (
80+
<>
81+
<Input {...props} />
82+
<button
83+
onClick={() => {
84+
props.onChange?.('');
85+
props.onChange?.('A');
86+
}}
87+
>
88+
change
89+
</button>
90+
</>
91+
);
92+
const { container } = render(
93+
<Form ref={form}>
94+
<Field name="input">
95+
<MockBtnInput />
96+
</Field>
97+
</Form>,
98+
);
99+
100+
// Trigger
101+
for (let i = 0; i < 3; i += 1) {
102+
fireEvent.click(container.querySelector('button'));
103+
await act(async () => {
104+
await timeout();
105+
});
106+
expect(form.current?.getFieldValue('input')).toBe('A');
107+
}
108+
});
76109
});

0 commit comments

Comments
 (0)