Skip to content

Commit 744dc9c

Browse files
committed
shouldUpdate accepts true
1 parent 76ea536 commit 744dc9c

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ We use typescript to create the Type definition. You can view directly in IDE. B
7373

7474
## Field
7575

76-
| Prop | Description | Type | Default |
77-
| ----------------- | --------------------------------------- | ------------------------------------- | -------- |
78-
| dependencies | Will re-render if dependencies changed | [NamePath](#namepath)[] | - |
79-
| getValueFromEvent | Specify how to get value from event | (..args: any[]) => any | - |
80-
| name | Field name path | [NamePath](#namepath) | - |
81-
| normalize | Normalize value before update | (value, prevValue, prevValues) => any | - |
82-
| rules | Validate rules | [Rule](#rule)[] | - |
83-
| shouldUpdate | Check if Field should update | (prevValues, nextValues): boolean | - |
84-
| trigger | Collect value update by event trigger | string | onChange |
85-
| validateTrigger | Config trigger point with rule validate | string \| string[] | onChange |
76+
| Prop | Description | Type | Default |
77+
| ----------------- | --------------------------------------- | -------------------------------------------- | -------- |
78+
| dependencies | Will re-render if dependencies changed | [NamePath](#namepath)[] | - |
79+
| getValueFromEvent | Specify how to get value from event | (..args: any[]) => any | - |
80+
| name | Field name path | [NamePath](#namepath) | - |
81+
| normalize | Normalize value before update | (value, prevValue, prevValues) => any | - |
82+
| rules | Validate rules | [Rule](#rule)[] | - |
83+
| shouldUpdate | Check if Field should update | true \| (prevValues, nextValues): boolean | - |
84+
| trigger | Collect value update by event trigger | string | onChange |
85+
| validateTrigger | Config trigger point with rule validate | string \| string[] | onChange |
8686

8787
## List
8888

src/Field.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export interface FieldProps {
4747
name?: NamePath;
4848
normalize?: (value: StoreValue, prevValue: StoreValue, allValues: Store) => StoreValue;
4949
rules?: Rule[];
50-
shouldUpdate?: (prevValues: Store, nextValues: Store, info: { source?: string }) => boolean;
50+
shouldUpdate?:
51+
| true
52+
| ((prevValues: Store, nextValues: Store, info: { source?: string }) => boolean);
5153
trigger?: string;
5254
validateTrigger?: string | string[] | false;
5355
}
@@ -224,7 +226,10 @@ class Field extends React.Component<FieldProps, FieldState> implements FieldEnti
224226
dependencies.some(dependency =>
225227
containsNamePath(namePathList, getNamePath(dependency)),
226228
) ||
227-
(shouldUpdate ? shouldUpdate(prevStore, values, info) : prevValue !== curValue)
229+
shouldUpdate === true ||
230+
(typeof shouldUpdate === 'function'
231+
? shouldUpdate(prevStore, values, info)
232+
: prevValue !== curValue)
228233
) {
229234
this.reRender();
230235
}

tests/index.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,29 @@ describe('Basic', () => {
319319

320320
errorSpy.mockRestore();
321321
});
322+
323+
it('shouldUpdate return true will update whatever notification updated', async () => {
324+
let renderCount = 0;
325+
326+
const wrapper = mount(
327+
<Form>
328+
<Field rules={[{ required: true }]}>
329+
<Input />
330+
</Field>
331+
<Field shouldUpdate>
332+
{() => {
333+
renderCount += 1;
334+
return null;
335+
}}
336+
</Field>
337+
</Form>,
338+
);
339+
340+
const baseRenderCount = renderCount;
341+
changeValue(getField(wrapper), '');
342+
expect(renderCount).toEqual(baseRenderCount + 1);
343+
344+
await timeout();
345+
expect(renderCount).toEqual(baseRenderCount + 2);
346+
});
322347
});

0 commit comments

Comments
 (0)