From f5e0408649de722557b8685f57b1ace914a00cd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E6=9E=AB?= <7971419+crazyair@users.noreply.github.com> Date: Thu, 30 Nov 2023 17:34:50 +0800 Subject: [PATCH] feat: support selector (#46180) * feat: support selector * fix: ts text * feat: md * feat: snap --- .../form/__tests__/__snapshots__/demo-extend.test.ts.snap | 3 +++ components/form/__tests__/__snapshots__/demo.test.tsx.snap | 3 +++ components/form/demo/useWatch.tsx | 3 +++ components/form/index.en-US.md | 4 +++- components/form/index.zh-CN.md | 4 +++- package.json | 2 +- 6 files changed, 16 insertions(+), 3 deletions(-) diff --git a/components/form/__tests__/__snapshots__/demo-extend.test.ts.snap b/components/form/__tests__/__snapshots__/demo-extend.test.ts.snap index a2cd43e046..3593baf2b4 100644 --- a/components/form/__tests__/__snapshots__/demo-extend.test.ts.snap +++ b/components/form/__tests__/__snapshots__/demo-extend.test.ts.snap @@ -19514,6 +19514,9 @@ Array [
       Name Value:
     
+
+      Custom Value: name:
+    
, ] `; diff --git a/components/form/__tests__/__snapshots__/demo.test.tsx.snap b/components/form/__tests__/__snapshots__/demo.test.tsx.snap index 8938d79790..293fe3eda8 100644 --- a/components/form/__tests__/__snapshots__/demo.test.tsx.snap +++ b/components/form/__tests__/__snapshots__/demo.test.tsx.snap @@ -8256,6 +8256,9 @@ Array [
       Name Value:
     
+
+      Custom Value:
+    
, ] `; diff --git a/components/form/demo/useWatch.tsx b/components/form/demo/useWatch.tsx index 068d5e6180..50932710cd 100644 --- a/components/form/demo/useWatch.tsx +++ b/components/form/demo/useWatch.tsx @@ -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 ( <> @@ -18,6 +20,7 @@ const Demo: React.FC = () => {
Name Value: {nameValue}
+
Custom Value: {customValue}
); diff --git a/components/form/index.en-US.md b/components/form/index.en-US.md index f63847bd0b..7c38471d7b 100644 --- a/components/form/index.en-US.md +++ b/components/form/index.en-US.md @@ -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: diff --git a/components/form/index.zh-CN.md b/components/form/index.zh-CN.md index f5fbfd6e39..290857abab 100644 --- a/components/form/index.zh-CN.md +++ b/components/form/index.zh-CN.md @@ -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` 进行联动从而降低维护成本: diff --git a/package.json b/package.json index a92ff275a3..c114ae72f9 100644 --- a/package.json +++ b/package.json @@ -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",