-
-
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f7e543a
feat: api
crazyair 82c6a65
feat: test
crazyair 88e7b31
feat: 代码优化
crazyair 4800c58
feat: 添加显示声明类型支持
crazyair b30a2d4
feat: 下划线优化
crazyair dacb1c6
feat: 下划线优化
crazyair 2f26933
Merge branch 'master' of https://github.com/react-component/field-for…
crazyair 3891f26
feat: watchValue
crazyair File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## useWatch-selector | ||
|
||
<code src="../examples/useWatch-selector.tsx"></code> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import Form, { Field } from 'rc-field-form'; | ||
import Input from './components/Input'; | ||
|
||
type FieldType = { | ||
init?: string; | ||
name?: string; | ||
age?: number; | ||
}; | ||
|
||
export default () => { | ||
const [form] = Form.useForm<FieldType>(); | ||
const values = Form.useWatch( | ||
values => ({ init: values.init, newName: values.name, newAge: values.age }), | ||
{ form, preserve: true }, | ||
); | ||
console.log('values', values); | ||
return ( | ||
<> | ||
<Form form={form} initialValues={{ init: 'init', name: 'aaa' }}> | ||
name | ||
<Field name="name"> | ||
<Input /> | ||
</Field> | ||
age | ||
<Field name="age"> | ||
<Input /> | ||
</Field> | ||
no-watch | ||
<Field name="no-watch"> | ||
<Input /> | ||
</Field> | ||
values:{JSON.stringify(values)} | ||
</Form> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,18 @@ function useWatch<TForm extends FormInstance>( | |
form?: TForm | WatchOptions<TForm>, | ||
): GetGeneric<TForm>; | ||
|
||
// ------- selector type ------- | ||
function useWatch<TForm extends FormInstance, TSelected = unknown>( | ||
selector: (values: GetGeneric<TForm>) => TSelected, | ||
form?: TForm | WatchOptions<TForm>, | ||
): TSelected; | ||
|
||
function useWatch<ValueType = Store, TSelected = unknown>( | ||
selector: (values: ValueType) => TSelected, | ||
form?: FormInstance | WatchOptions<FormInstance>, | ||
): TSelected; | ||
// ------- selector type end ------- | ||
|
||
function useWatch<TForm extends FormInstance>( | ||
dependencies: NamePath, | ||
form?: TForm | WatchOptions<TForm>, | ||
|
@@ -86,8 +98,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; | ||
|
||
|
@@ -125,8 +139,15 @@ function useWatch(...args: [NamePath, FormInstance | WatchOptions<FormInstance>] | |
const { getFieldsValue, getInternalHooks } = formInstance; | ||
const { registerWatch } = getInternalHooks(HOOK_MARK); | ||
|
||
const getNewValue = (values: any, allValues: any) => { | ||
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 = options.preserve ? allValues : values; | ||
return typeof dependencies === 'function' | ||
? dependencies(newValue) | ||
: getValue(newValue, namePathRef.current); | ||
}; | ||
|
||
const cancelRegister = registerWatch((values, allValues) => { | ||
const newValue = getValue(options.preserve ? allValues : values, namePathRef.current); | ||
const newValue = getNewValue(values, allValues); | ||
const nextValueStr = stringify(newValue); | ||
|
||
// Compare stringify in case it's nest object | ||
|
@@ -137,10 +158,7 @@ function useWatch(...args: [NamePath, FormInstance | WatchOptions<FormInstance>] | |
}); | ||
|
||
// TODO: We can improve this perf in future | ||
const initialValue = getValue( | ||
options.preserve ? getFieldsValue(true) : getFieldsValue(), | ||
namePathRef.current, | ||
); | ||
const initialValue = getNewValue(getFieldsValue(), getFieldsValue(true)); | ||
|
||
// React 18 has the bug that will queue update twice even the value is not changed | ||
// ref: https://github.com/facebook/react/issues/27213 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
不过还是支持了