Skip to content

Commit

Permalink
fix(Form.Control): shouldResetWithUnmount isn't working (#3055)
Browse files Browse the repository at this point in the history
when several components unmount at the same time,
React will call them one by one, and the later ones will overwrite the earlier ones
  • Loading branch information
myNameIsDu committed Feb 8, 2023
1 parent 602b8c2 commit 98c7e1d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ const Form: FormComponent = React.forwardRef((props: FormProps, ref: React.Ref<F
* so use Ref to get future error
*/
const formError = omit(realFormErrorRef.current, [name]);
realFormErrorRef.current = formError;
setFormError(formError);
onCheck?.(formError);
},
Expand All @@ -325,6 +326,7 @@ const Form: FormComponent = React.forwardRef((props: FormProps, ref: React.Ref<F
* so use Ref to get future value
*/
const formValue = omit(realFormValueRef.current, [name]);
realFormValueRef.current = formValue;
setFormValue(formValue);
onChange?.(formValue);
},
Expand Down
19 changes: 13 additions & 6 deletions src/FormControl/test/FormControlSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ describe('FormControl', () => {
expect(alert).to.exist;
expect(input).to.have.attr('aria-errormessage', alert.getAttribute('id') as string);
});

it('Should remove value and error when shouldResetWithUnmount is true', () => {
let refValue = { username: '', email: '' };
let refValue = { username: '', email: '', password: '' };
let refError = {};
const model = Schema.Model({
password: Schema.Types.StringType().maxLength(2, 'The length cannot exceed 2'),
username: Schema.Types.StringType().maxLength(2, 'The length cannot exceed 2'),
email: Schema.Types.StringType().maxLength(2, 'The length cannot exceed 2')
});
Expand All @@ -245,6 +245,7 @@ describe('FormControl', () => {
formError={error}
formValue={value}
>
{email || <FormControl id="password" name="password" shouldResetWithUnmount />}
{email || <FormControl id="username" name="username" shouldResetWithUnmount />}
<FormControl id="email" name="email" />
</Form>
Expand All @@ -255,13 +256,19 @@ describe('FormControl', () => {
fireEvent.change(container.querySelector('#username') as HTMLInputElement, {
target: { value: 'username' }
});
assert.deepEqual(refValue, { username: 'username', email: '' });
assert.deepEqual(refError, { username: 'The length cannot exceed 2' });
fireEvent.change(container.querySelector('#password') as HTMLInputElement, {
target: { value: 'password' }
});
expect(refValue).to.deep.equal({ username: 'username', password: 'password', email: '' });
expect(refError).to.deep.equal({
username: 'The length cannot exceed 2',
password: 'The length cannot exceed 2'
});
fireEvent.change(container.querySelector('#email') as HTMLInputElement, {
target: { value: 'email' }
});
assert.deepEqual(refValue, { email: 'email' } as any);
assert.deepEqual(refError, { email: 'The length cannot exceed 2' });
expect(refValue).to.deep.equal({ email: 'email' });
expect(refError).to.deep.equal({ email: 'The length cannot exceed 2' });
});

describe('rule', () => {
Expand Down

1 comment on commit 98c7e1d

@vercel
Copy link

@vercel vercel bot commented on 98c7e1d Feb 8, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.