-
-
Notifications
You must be signed in to change notification settings - Fork 272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: useWatch support selector #637
Changes from 2 commits
f7e543a
82c6a65
88e7b31
4800c58
b30a2d4
dacb1c6
2f26933
3891f26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## useWatch-selector | ||
|
||
<code src="../examples/useWatch-selector.tsx"></code> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import Form, { Field } from 'rc-field-form'; | ||
import Input from './components/Input'; | ||
|
||
type FieldType = { | ||
name?: string; | ||
age?: number; | ||
}; | ||
|
||
export default () => { | ||
const [form] = Form.useForm<FieldType>(); | ||
const values = Form.useWatch(values => ({ newName: values.name, newAge: values.age }), form); | ||
console.log('values', values); | ||
return ( | ||
<> | ||
<Form form={form} initialValues={{ name: 'aaa' }}> | ||
name | ||
<Field name="name"> | ||
<Input /> | ||
</Field> | ||
age | ||
<Field name="age"> | ||
<Input /> | ||
</Field> | ||
values:{JSON.stringify(values)} | ||
</Form> | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,11 @@ function useWatch<TForm extends FormInstance>( | |
form?: TForm | WatchOptions<TForm>, | ||
): GetGeneric<TForm>; | ||
|
||
function useWatch<TForm extends FormInstance, TSelected = unknown>( | ||
selector: (values: GetGeneric<TForm>) => TSelected, | ||
form?: TForm | WatchOptions<TForm>, | ||
): TSelected; | ||
|
||
function useWatch<TForm extends FormInstance>( | ||
dependencies: NamePath, | ||
form?: TForm | WatchOptions<TForm>, | ||
|
@@ -86,8 +91,10 @@ function useWatch<ValueType = Store>( | |
form?: FormInstance | WatchOptions<FormInstance>, | ||
): ValueType; | ||
|
||
function useWatch(...args: [NamePath, FormInstance | WatchOptions<FormInstance>]) { | ||
const [dependencies = [], _form = {}] = args; | ||
function useWatch( | ||
...args: [NamePath | ((values: Store) => any), FormInstance | WatchOptions<FormInstance>] | ||
) { | ||
const [dependencies, _form = {}] = args; | ||
const options = isFormInstance(_form) ? { form: _form } : _form; | ||
const form = options.form; | ||
|
||
|
@@ -126,7 +133,11 @@ function useWatch(...args: [NamePath, FormInstance | WatchOptions<FormInstance>] | |
const { registerWatch } = getInternalHooks(HOOK_MARK); | ||
|
||
const cancelRegister = registerWatch((values, allValues) => { | ||
const newValue = getValue(options.preserve ? allValues : values, namePathRef.current); | ||
const _values = options.preserve ? allValues : values; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不要用下划线命名, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 提出来了 |
||
const newValue = | ||
typeof dependencies === 'function' | ||
? dependencies(_values) | ||
: getValue(_values, namePathRef.current); | ||
const nextValueStr = stringify(newValue); | ||
|
||
// Compare stringify in case it's nest object | ||
|
@@ -136,11 +147,12 @@ function useWatch(...args: [NamePath, FormInstance | WatchOptions<FormInstance>] | |
} | ||
}); | ||
|
||
const _values = options.preserve ? getFieldsValue(true) : getFieldsValue(); | ||
// TODO: We can improve this perf in future | ||
const initialValue = getValue( | ||
options.preserve ? getFieldsValue(true) : getFieldsValue(), | ||
namePathRef.current, | ||
); | ||
const initialValue = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个和上面的逻辑很相似,看看能不能抽出来 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 就等着呢 |
||
typeof dependencies === 'function' | ||
? dependencies(_values) | ||
: getValue(_values, namePathRef.current); | ||
|
||
// React 18 has the bug that will queue update twice even the value is not changed | ||
// ref: https://github.com/facebook/react/issues/27213 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为用了 TSelected,所以不建议
const values = useWatch<{name?: string}>(values => xxx)
设置 stroe 类型方式There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不过还是支持了