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): fix Field onChange reset value #2197

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions src/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,17 @@ const Form: FormComponent = React.forwardRef((props: FormProps, ref) => {
const [_formValue, setFormValue] = useState(formDefaultValue);
const [_formError, setFormError] = useState(formError || {});

const formValueRef = useRef(formValue);

formValueRef.current = formValue;

const _formValueRef = useRef(_formValue);

_formValueRef.current = _formValue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这解决方案,很奇怪,并且可读性不太好,我更推荐使用 useReducer


const getFormValue = useCallback(() => {
return isUndefined(formValue) ? _formValue : formValue;
}, [_formValue, formValue]);
return isUndefined(formValueRef.current) ? _formValueRef.current : formValueRef.current;
}, []);

const getFormError = useCallback(() => {
return isUndefined(formError) ? _formError : formError;
Expand Down
45 changes: 44 additions & 1 deletion src/Form/test/FormSpec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useCallback } from 'react';
import { render } from '@testing-library/react';
import ReactTestUtils from 'react-dom/test-utils';
import _isNil from 'lodash/isNil';
Expand Down Expand Up @@ -652,4 +652,47 @@ describe('Form', () => {
formRef.current.check();
});
});

it('Should accepter onChange effect current field', () => {
const Accepter = ({ value, onChange }) => {
const handleClick = useCallback(() => {
onChange(value + 1);
}, []);
return (
<div>
{value}
<button aria-label="add btn" onClick={handleClick}>
+
</button>
</div>
);
};

const App = () => {
const [value, setValue] = React.useState({
name: '',
age: 0
});
return (
<Form formValue={value} onChange={setValue}>
<FormControl name="name" aria-label="name" />
<FormControl name="age" accepter={Accepter} />
</Form>
);
};

const { getByLabelText } = render(<App />);

ReactTestUtils.Simulate.change(getByLabelText('name'), {
target: {
value: 'abc'
}
});

assert.isTrue(getByLabelText('name').value === 'abc');

ReactTestUtils.Simulate.click(getByLabelText('add btn'));

assert.isTrue(getByLabelText('name').value === 'abc');
});
});