Skip to content
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 8 commits into from
Nov 30, 2023

Conversation

crazyair
Copy link
Contributor

@crazyair crazyair commented Nov 29, 2023

Copy link

vercel bot commented Nov 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
field-form ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 30, 2023 2:41am

Copy link

codecov bot commented Nov 29, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (75dbcb3) 99.43% compared to head (3891f26) 99.43%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #637   +/-   ##
=======================================
  Coverage   99.43%   99.43%           
=======================================
  Files          18       18           
  Lines        1236     1240    +4     
  Branches      283      285    +2     
=======================================
+ Hits         1229     1233    +4     
  Misses          7        7           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/useWatch.ts Outdated
@@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要用下划线命名,rawValues

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提出来了

src/useWatch.ts Outdated
options.preserve ? getFieldsValue(true) : getFieldsValue(),
namePathRef.current,
);
const initialValue =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个和上面的逻辑很相似,看看能不能抽出来

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

就等着呢

@@ -76,6 +76,11 @@ function useWatch<TForm extends FormInstance>(
form?: TForm | WatchOptions<TForm>,
): GetGeneric<TForm>;

function useWatch<TForm extends FormInstance, TSelected = unknown>(
Copy link
Contributor Author

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 类型方式

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不过还是支持了

src/useWatch.ts Outdated
@@ -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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> getWatchValue

options.preserve ? getFieldsValue(true) : getFieldsValue(),
namePathRef.current,
);
const initialValue = getWatchValue(getFieldsValue(), getFieldsValue(true));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还记得 Form.List 下需要设置 getFieldsValue({ strict: true }) 吗,这个truestrict不能同时设置,需要加个options.strict

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

暂时不用,有需求再加

@zombieJ zombieJ merged commit 8bbfd34 into react-component:master Nov 30, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants