Skip to content

Commit d09343c

Browse files
authored
feat:兼容 form 第一次为 undefined (#507)
* feat: done * feat: test * feat: test * feat: test * feat: test * feat: test * feat: test * feat: args * feat: test
1 parent d789287 commit d09343c

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/useWatch.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ function useWatch<TForm extends FormInstance>(dependencies: NamePath, form?: TFo
5858

5959
function useWatch<ValueType = Store>(dependencies: NamePath, form?: FormInstance): ValueType;
6060

61-
function useWatch(dependencies: NamePath = [], form?: FormInstance) {
61+
function useWatch(...args: [NamePath, FormInstance]) {
62+
const [dependencies = [], form] = args;
6263
const [value, setValue] = useState<any>();
6364

6465
const valueStr = useMemo(() => stringify(value), [value]);
@@ -72,7 +73,7 @@ function useWatch(dependencies: NamePath = [], form?: FormInstance) {
7273
// Warning if not exist form instance
7374
if (process.env.NODE_ENV !== 'production') {
7475
warning(
75-
isValidForm,
76+
args.length === 2 ? (form ? isValidForm : true) : isValidForm,
7677
'useWatch requires a form instance since it can not auto detect from context.',
7778
);
7879
}
@@ -111,7 +112,7 @@ function useWatch(dependencies: NamePath = [], form?: FormInstance) {
111112

112113
// We do not need re-register since namePath content is the same
113114
// eslint-disable-next-line react-hooks/exhaustive-deps
114-
[],
115+
[isValidForm],
115116
);
116117

117118
return value;

tests/useWatch.test.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React, { useRef, useState } from 'react';
22
import { mount } from 'enzyme';
33
import type { FormInstance } from '../src';
44
import { List } from '../src';
@@ -219,6 +219,7 @@ describe('useWatch', () => {
219219
expect(errorSpy).toHaveBeenCalledWith(
220220
'Warning: useWatch requires a form instance since it can not auto detect from context.',
221221
);
222+
errorSpy.mockRestore();
222223
});
223224

224225
it('no more render time', () => {
@@ -392,4 +393,38 @@ describe('useWatch', () => {
392393
const str = stringify(obj);
393394
expect(typeof str === 'number').toBeTruthy();
394395
});
396+
it('first undefined', () => {
397+
const errorSpy = jest.spyOn(console, 'error');
398+
const Demo = () => {
399+
const formRef = useRef();
400+
const name = Form.useWatch('name', formRef.current);
401+
const [, setUpdate] = useState({});
402+
403+
return (
404+
<>
405+
<div className="setUpdate" onClick={() => setUpdate({})} />
406+
<div className="value">{name}</div>
407+
<Form ref={formRef} initialValues={{ name: 'default' }}>
408+
<Field name="name" key="a">
409+
<Input />
410+
</Field>
411+
</Form>
412+
</>
413+
);
414+
};
415+
416+
const wrapper = mount(<Demo />);
417+
expect(wrapper.find('.value').text()).toEqual('');
418+
wrapper.find('.setUpdate').at(0).simulate('click');
419+
expect(wrapper.find('.value').text()).toEqual('default');
420+
wrapper
421+
.find('input')
422+
.at(0)
423+
.simulate('change', { target: { value: 'bamboo' } });
424+
expect(wrapper.find('.value').text()).toEqual('bamboo');
425+
expect(errorSpy).not.toHaveBeenCalledWith(
426+
'Warning: useWatch requires a form instance since it can not auto detect from context.',
427+
);
428+
errorSpy.mockRestore();
429+
});
395430
});

0 commit comments

Comments
 (0)