Skip to content

Commit

Permalink
feat: support selector (ant-design#46180)
Browse files Browse the repository at this point in the history
* feat: support selector

* fix: ts text

* feat: md

* feat: snap
  • Loading branch information
crazyair authored Nov 30, 2023
1 parent e6bf2e2 commit f5e0408
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19514,6 +19514,9 @@ Array [
<pre>
Name Value:
</pre>
<pre>
Custom Value: name:
</pre>
</article>,
]
`;
Expand Down
3 changes: 3 additions & 0 deletions components/form/__tests__/__snapshots__/demo.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8256,6 +8256,9 @@ Array [
<pre>
Name Value:
</pre>
<pre>
Custom Value:
</pre>
</article>,
]
`;
Expand Down
3 changes: 3 additions & 0 deletions components/form/demo/useWatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Form, Input, InputNumber, Typography } from 'antd';
const Demo: React.FC = () => {
const [form] = Form.useForm<{ name: string; age: number }>();
const nameValue = Form.useWatch('name', form);
// The selector is static and does not support closures.
const customValue = Form.useWatch((values) => `name: ${values.name || ''}`, form);

return (
<>
Expand All @@ -18,6 +20,7 @@ const Demo: React.FC = () => {

<Typography>
<pre>Name Value: {nameValue}</pre>
<pre>Custom Value: {customValue}</pre>
</Typography>
</>
);
Expand Down
4 changes: 3 additions & 1 deletion components/form/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,9 @@ export default () => {

### Form.useWatch

`type Form.useWatch = (namePath: NamePath, formInstance?: FormInstance | WatchOptions): Value`
`type Form.useWatch = (namePath: NamePath | (selector: (values: Store) => any), formInstance?: FormInstance | WatchOptions): Value`

`5.12.0` add `selector`

Watch the value of a field. You can use this to interact with other hooks like `useSWR` to reduce development costs:

Expand Down
4 changes: 3 additions & 1 deletion components/form/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ export default () => {

### Form.useWatch

`type Form.useWatch = (namePath: NamePath, formInstance?: FormInstance | WatchOptions): Value`
`type Form.useWatch = (namePath: NamePath | (selector: (values: Store)) => any, formInstance?: FormInstance | WatchOptions): Value`

`5.12.0` 新增 `selector`

用于直接获取 form 中字段对应的值。通过该 Hooks 可以与诸如 `useSWR` 进行联动从而降低维护成本:

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"rc-dialog": "~9.3.4",
"rc-drawer": "~6.5.2",
"rc-dropdown": "~4.1.0",
"rc-field-form": "~1.40.0",
"rc-field-form": "~1.41.0",
"rc-image": "~7.5.1",
"rc-input": "~1.3.6",
"rc-input-number": "~8.4.0",
Expand Down

0 comments on commit f5e0408

Please sign in to comment.