Skip to content
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
5 changes: 3 additions & 2 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ class Field extends React.PureComponent<InternalFieldProps, FieldState> implemen
this.dirty = true;

this.triggerMetaEvent();
const curValue = this.getValue();

let newValue: StoreValue;
if (getValueFromEvent) {
Expand All @@ -620,9 +621,9 @@ class Field extends React.PureComponent<InternalFieldProps, FieldState> implemen
}

if (normalize) {
newValue = normalize(newValue, value, getFieldsValue(true));
newValue = normalize(newValue, curValue, getFieldsValue(true));
}
if (newValue !== value) {
if (newValue !== curValue) {
dispatch({
type: 'updateValue',
namePath,
Expand Down
29 changes: 27 additions & 2 deletions tests/field.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React from 'react';
import Form, { Field } from '../src';
import type { FormInstance } from '../src';
import { act, fireEvent, render } from '@testing-library/react';
import { Input } from './common/InfoField';
import { getInput } from './common';
import timeout from './common/timeout';

describe('Form.Field', () => {
Expand Down Expand Up @@ -73,4 +71,31 @@ describe('Form.Field', () => {
onValuesChange.mockReset();
}
});

// https://github.com/react-component/field-form/issues/753
it('uses the latest value for consecutive changes', () => {
const form = React.createRef<FormInstance>();
const MockInput = ({ onChange }: { onChange?: (value: string) => void }) => (
<button
type="button"
onClick={() => {
onChange?.('');
onChange?.('A');
}}
>
change
</button>
);

const { getByRole } = render(
<Form ref={form}>
<Field name="input" initialValue="A">
<MockInput />
</Field>
</Form>,
);

fireEvent.click(getByRole('button'));
expect(form.current?.getFieldValue('input')).toBe('A');
});
});
Loading