Skip to content

Commit

Permalink
fix: preserve false should not trigger shouldUpdate rerender (#714)
Browse files Browse the repository at this point in the history
* test: test driven

* enhance: shouldUpdate false should not trigger rerender
  • Loading branch information
zombieJ authored Aug 8, 2024
1 parent 9a98132 commit 3493250
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
* - Reset A, need clean B, C
*/
case 'remove': {
if (shouldUpdate) {
if (
shouldUpdate &&
requireUpdate(shouldUpdate, prevStore, store, prevValue, curValue, info)
) {
this.reRender();
return;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/dependencies.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,43 @@ describe('Form.Dependencies', () => {
// sync end
expect(spy).toHaveBeenCalledTimes(3);
});

it('shouldUpdate false should not update', () => {
let counter = 0;
const formRef = React.createRef<FormInstance>();

const { container } = render(
<Form ref={formRef}>
<Field name="little" preserve={false}>
<Input />
</Field>

<Field shouldUpdate={(prev, next) => prev.little !== next.little}>
{(_, __, form) => {
// Fill to hide
if (!form.getFieldValue('little')) {
return <InfoField name="bamboo" preserve={false} />;
}

return null;
}}
</Field>

<Field shouldUpdate={() => false}>
{() => {
console.log('render!');
counter += 1;
return null;
}}
</Field>
</Form>,
);
expect(counter).toEqual(1);
expect(container.querySelectorAll('input')).toHaveLength(2);

// hide should not re-render
fireEvent.change(getInput(container, 0), { target: { value: '1' } });
expect(container.querySelectorAll('input')).toHaveLength(1);
expect(counter).toEqual(1);
});
});

0 comments on commit 3493250

Please sign in to comment.