Skip to content

Commit 4fcb7da

Browse files
crazyairzombieJ
andauthored
enhance: useWatch selector init (#762)
* feat: useWatch support dynamic names * fix: lint * feat: useWatch init object * feat: review * feat: review --------- Co-authored-by: 二货机器人 <[email protected]>
1 parent 2b0f66b commit 4fcb7da

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

docs/examples/useWatch-selector.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-shadow */
12
import React from 'react';
23
import Form, { Field } from 'rc-field-form';
34
import Input from './components/Input';
@@ -10,11 +11,15 @@ type FieldType = {
1011

1112
export default () => {
1213
const [form] = Form.useForm<FieldType>();
14+
const firstEmptyObject = Form.useWatch(values => ({ newName: values.name }), form);
15+
console.log('firstEmptyObject', firstEmptyObject);
16+
1317
const values = Form.useWatch(
1418
values => ({ init: values.init, newName: values.name, newAge: values.age }),
1519
{ form, preserve: true },
1620
);
1721
console.log('values', values);
22+
1823
return (
1924
<>
2025
<Form form={form} initialValues={{ init: 'init', name: 'aaa' }}>

src/useWatch.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ function useWatch(
9292
const options = isFormInstance(_form) ? { form: _form } : _form;
9393
const form = options.form;
9494

95-
const [value, setValue] = useState<any>();
95+
const [value, setValue] = useState<any>(() =>
96+
typeof dependencies === 'function' ? dependencies({}) : undefined,
97+
);
9698

9799
const valueStr = useMemo(() => stringify(value), [value]);
98100
const valueStrRef = useRef(valueStr);
@@ -117,8 +119,8 @@ function useWatch(
117119
// ============================= Update =============================
118120
const triggerUpdate = useEvent((values?: any, allValues?: any) => {
119121
const watchValue = options.preserve
120-
? (allValues ?? getFieldsValue(true))
121-
: (values ?? getFieldsValue());
122+
? allValues ?? getFieldsValue(true)
123+
: values ?? getFieldsValue();
122124

123125
const nextValue =
124126
typeof dependencies === 'function'

tests/useWatch.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,22 @@ describe('useWatch', () => {
518518
await changeValue(input[0], 'bamboo2');
519519
expect(container.querySelector<HTMLDivElement>('.values')?.textContent).toEqual('bamboo2');
520520
});
521+
it('selector by first no undefined', async () => {
522+
const list: any[] = [];
523+
const Demo = () => {
524+
const [form] = Form.useForm<{ name?: string }>();
525+
const data = Form.useWatch(values => values, form);
526+
list.push(data);
527+
return (
528+
<Form form={form}>
529+
<Field name="name" initialValue="bamboo">
530+
<Input />
531+
</Field>
532+
</Form>
533+
);
534+
};
535+
render(<Demo />);
536+
expect(list[0]).toEqual({});
537+
expect(list[1]).toEqual({ name: 'bamboo' });
538+
});
521539
});

0 commit comments

Comments
 (0)