@agentscope-ai/design 1.0.19 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/lib/components/commonComponents/CodeBlock/index.js +21 -12
  2. package/llms/all.llms.txt +4976 -0
  3. package/llms/components/commonComponents/Alert/index.zh-CN.llms.txt +32 -0
  4. package/llms/components/commonComponents/AlertDialog/index.zh-CN.llms.txt +171 -0
  5. package/llms/components/commonComponents/Anchor/index.zh-CN.llms.txt +51 -0
  6. package/llms/components/commonComponents/Audio/index.zh-CN.llms.txt +7 -0
  7. package/llms/components/commonComponents/Avatar/index.zh-CN.llms.txt +37 -0
  8. package/llms/components/commonComponents/Breadcrumb/index.zh-CN.llms.txt +117 -0
  9. package/llms/components/commonComponents/Button/index.zh-CN.llms.txt +54 -0
  10. package/llms/components/commonComponents/Card/index.zh-CN.llms.txt +61 -0
  11. package/llms/components/commonComponents/Checkbox/index.zh-CN.llms.txt +69 -0
  12. package/llms/components/commonComponents/CodeBlock/index.zh-CN.llms.txt +13 -0
  13. package/llms/components/commonComponents/Collapse/index.zh-CN.llms.txt +56 -0
  14. package/llms/components/commonComponents/CollapsePanel/index.zh-CN.llms.txt +69 -0
  15. package/llms/components/commonComponents/DatePicker/index.zh-CN.llms.txt +221 -0
  16. package/llms/components/commonComponents/Descriptions/index.zh-CN.llms.txt +37 -0
  17. package/llms/components/commonComponents/Drawer/index.zh-CN.llms.txt +62 -0
  18. package/llms/components/commonComponents/Dropdown/index.zh-CN.llms.txt +149 -0
  19. package/llms/components/commonComponents/Empty/index.zh-CN.llms.txt +41 -0
  20. package/llms/components/commonComponents/FloatButton/index.zh-CN.llms.txt +51 -0
  21. package/llms/components/commonComponents/Form/index.zh-CN.llms.txt +558 -0
  22. package/llms/components/commonComponents/IconButton/index.zh-CN.llms.txt +45 -0
  23. package/llms/components/commonComponents/Image/index.zh-CN.llms.txt +165 -0
  24. package/llms/components/commonComponents/Input/index.zh-CN.llms.txt +133 -0
  25. package/llms/components/commonComponents/InputNumber/index.zh-CN.llms.txt +51 -0
  26. package/llms/components/commonComponents/Message/index.zh-CN.llms.txt +98 -0
  27. package/llms/components/commonComponents/Modal/index.zh-CN.llms.txt +168 -0
  28. package/llms/components/commonComponents/Notification/index.zh-CN.llms.txt +94 -0
  29. package/llms/components/commonComponents/Pagination/index.zh-CN.llms.txt +45 -0
  30. package/llms/components/commonComponents/Popconfirm/index.zh-CN.llms.txt +55 -0
  31. package/llms/components/commonComponents/Popover/index.zh-CN.llms.txt +43 -0
  32. package/llms/components/commonComponents/Progress/index.zh-CN.llms.txt +50 -0
  33. package/llms/components/commonComponents/PromptsEditor/index.zh-CN.llms.txt +15 -0
  34. package/llms/components/commonComponents/Radio/index.zh-CN.llms.txt +66 -0
  35. package/llms/components/commonComponents/RadioButton/index.zh-CN.llms.txt +73 -0
  36. package/llms/components/commonComponents/Result/index.zh-CN.llms.txt +10 -0
  37. package/llms/components/commonComponents/Select/index.zh-CN.llms.txt +96 -0
  38. package/llms/components/commonComponents/Skeleton/index.zh-CN.llms.txt +59 -0
  39. package/llms/components/commonComponents/Slider/index.zh-CN.llms.txt +62 -0
  40. package/llms/components/commonComponents/Spinner/index.zh-CN.llms.txt +15 -0
  41. package/llms/components/commonComponents/Statistic/index.zh-CN.llms.txt +58 -0
  42. package/llms/components/commonComponents/Steps/index.zh-CN.llms.txt +57 -0
  43. package/llms/components/commonComponents/Switch/index.zh-CN.llms.txt +39 -0
  44. package/llms/components/commonComponents/Table/index.zh-CN.llms.txt +254 -0
  45. package/llms/components/commonComponents/Tabs/index.zh-CN.llms.txt +68 -0
  46. package/llms/components/commonComponents/Tag/index.zh-CN.llms.txt +33 -0
  47. package/llms/components/commonComponents/TimePicker/index.zh-CN.llms.txt +111 -0
  48. package/llms/components/commonComponents/Tooltip/index.zh-CN.llms.txt +45 -0
  49. package/llms/components/commonComponents/Upload/index.zh-CN.llms.txt +95 -0
  50. package/llms/components/commonComponents/Video/index.zh-CN.llms.txt +11 -0
  51. package/llms/docs/guide/fromAntd.zh-CN.llms.txt +156 -0
  52. package/llms/docs/guide/fromScratch.zh-CN.llms.txt +83 -0
  53. package/llms/docs/guide/iconfont.zh-CN.llms.txt +69 -0
  54. package/llms/docs/guide/overview.zh-CN.llms.txt +51 -0
  55. package/llms/docs/guide/sparkIcons.zh-CN.llms.txt +25 -0
  56. package/llms/docs/guide/theme.zh-CN.llms.txt +61 -0
  57. package/llms/docs/guide/tokens&variables.zh-CN.llms.txt +433 -0
  58. package/llms/docs/guide/vibe-coding.zh-CN.llms.txt +29 -0
  59. package/llms/index.llms.txt +58 -0
  60. package/package.json +4 -2
@@ -0,0 +1,558 @@
1
+
2
+
3
+ <DemoTitle title="Form" desc="高性能表单控件,自带数据域管理。包含数据录入、校验以及对应样式。">
4
+ #### API
5
+
6
+ <AntdApiRef url="https://ant.design/components/form-cn/#api"></AntdApiRef>
7
+
8
+
9
+ ## antd API
10
+
11
+ 通用属性参考:[通用属性](/docs/react/common-props)
12
+
13
+ ### Form
14
+
15
+ | 参数 | 说明 | 类型 | 默认值 | 版本 |
16
+ |--------|--------|--------|--------|--------|
17
+ | colon | 配置 Form.Item 的 | boolean | true | |
18
+ | disabled | 设置表单组件禁用,仅对 antd 组件有效 | boolean | false | 4.21.0 |
19
+ | component | 设置 Form 渲染元素,为 | ComponentType \| false | form | |
20
+ | fields | 通过状态管理(如 redux)控制表单字段,如非强需求不推荐使用。查看 | | - | |
21
+ | form | 经 | | - | |
22
+ | feedbackIcons | 当 | | - | 5.9.0 |
23
+ | initialValues | 表单默认值,只有初始化以及重置时生效 | object | - | |
24
+ | labelAlign | label 标签的文本对齐方式 | left | right | |
25
+ | labelWrap | label 标签的文本换行方式 | boolean | false | 4.18.0 |
26
+ | labelCol | label 标签布局,同 | | - | |
27
+ | layout | 表单布局 | horizontal | horizontal | |
28
+ | name | 表单名称,会作为表单字段 | string | - | |
29
+ | preserve | 当字段被删除时保留字段值。你可以通过 | boolean | true | 4.4.0 |
30
+ | requiredMark | 必选样式,可以切换为必选或者可选展示样式。此为 Form 配置,Form.Item 无法单独配置 | boolean \| | true | renderProps |
31
+ | scrollToFirstError | 提交失败自动滚动到第一个错误字段 | boolean \| | false | focus: 5.24.0 |
32
+ | size | 设置字段组件的尺寸(仅限 antd 组件) | small | - | |
33
+ | validateMessages | 验证提示模板,说明 | | - | |
34
+ | validateTrigger | 统一设置字段触发验证的时机 | string \| string[] | onChange | 4.3.0 |
35
+ | variant | 表单内控件变体 | outlined | outlined | 5.13.0 \| |
36
+ | wrapperCol | 需要为输入控件设置布局样式时,使用该属性,用法同 labelCol | | - | |
37
+ | onFieldsChange | 字段更新时触发回调事件 | function(changedFields, allFields) | - | |
38
+ | onFinish | 提交表单且数据验证成功后回调事件 | function(values) | - | |
39
+ | onFinishFailed | 提交表单且数据验证失败后回调事件 | function({ values, errorFields, outOfDate }) | - | |
40
+ | onValuesChange | 字段值更新时触发回调事件 | function(changedValues, allValues) | - | |
41
+ | clearOnDestroy | 当表单被卸载时清空表单值 | boolean | false | 5.18.0 |
42
+
43
+
44
+ 支持原生 form 除 `onSubmit` 外的所有属性。
45
+
46
+ ### validateMessages
47
+
48
+ Form 为验证提供了[默认的错误提示信息](https://github.com/ant-design/ant-design/blob/6234509d18bac1ac60fbb3f92a5b2c6a6361295a/components/locale/en_US.ts#L88-L134),你可以通过配置 `validateMessages` 属性,修改对应的提示模板。一种常见的使用方式,是配置国际化提示信息:
49
+
50
+
51
+ ```jsx
52
+ const validateMessages = {
53
+ required: "'${name}' 是必选字段",
54
+ // ...
55
+ };
56
+
57
+ <Form validateMessages={validateMessages} />;
58
+ ```
59
+
60
+
61
+ 此外,[ConfigProvider](/components/config-provider-cn) 也提供了全局化配置方案,允许统一配置错误提示模板:
62
+
63
+
64
+ ```jsx
65
+ const validateMessages = {
66
+ required: "'${name}' 是必选字段",
67
+ // ...
68
+ };
69
+
70
+ <ConfigProvider form={{ validateMessages }}>
71
+ <Form />
72
+ </ConfigProvider>;
73
+ ```
74
+
75
+
76
+ ## Form.Item
77
+
78
+ 表单字段组件,用于数据双向绑定、校验、布局等。
79
+
80
+ | 参数 | 说明 | 类型 | 默认值 | 版本 |
81
+ |--------|--------|--------|--------|--------|
82
+ | colon | 配合 | boolean | true | |
83
+ | dependencies | 设置依赖字段,说明 | | - | |
84
+ | extra | 额外的提示信息,和 | ReactNode | - | |
85
+ | getValueFromEvent | 设置如何将 event 的值转换成字段值 | (..args: any[]) => any | - | |
86
+ | getValueProps | 为子元素添加额外的属性 (不建议通过 | (value: any) => Record | - | 4.2.0 |
87
+ | hasFeedback | 配合 | boolean \| { icons: | false | icons: 5.9.0 |
88
+ | help | 提示信息,如不设置,则会根据校验规则自动生成 | ReactNode | - | |
89
+ | hidden | 是否隐藏字段(依然会收集和校验字段) | boolean | false | 4.4.0 |
90
+ | htmlFor | 设置子元素 label | string | - | |
91
+ | initialValue | 设置子元素默认值,如果与 Form 的 | string | - | 4.2.0 |
92
+ | label | label | ReactNode | - | null: 5.22.0 |
93
+ | labelAlign | 标签文本对齐方式 | left | right | |
94
+ | labelCol | label | | - | |
95
+ | messageVariables | 默认验证字段的信息,查看 | Record | - | 4.7.0 |
96
+ | name | 字段名,支持数组 | | - | |
97
+ | normalize | 组件获取值后进行转换,再放入 Form 中。不支持异步 | (value, prevValue, prevValues) => any | - | |
98
+ | noStyle | 为 | boolean | false | |
99
+ | preserve | 当字段被删除时保留字段值 | boolean | true | 4.4.0 |
100
+ | required | 必填样式设置。如不设置,则会根据校验规则自动生成 | boolean | false | |
101
+ | rules | 校验规则,设置字段的校验逻辑。点击 | | - | |
102
+ | shouldUpdate | 自定义字段更新逻辑,说明 | boolean \| (prevValue, curValue) => boolean | false | |
103
+ | tooltip | 配置提示信息 | ReactNode \| | - | 4.7.0 |
104
+ | trigger | 设置收集字段值变更的时机。点击 | string | onChange | |
105
+ | validateFirst | 当某一规则校验不通过时,是否停止剩下的规则的校验。设置 | boolean \| | false | parallel |
106
+ | validateDebounce | 设置防抖,延迟毫秒数后进行校验 | number | - | 5.9.0 |
107
+ | validateStatus | 校验状态,如不设置,则会根据校验规则自动生成,可选:'success' 'warning' 'error' 'validating' | string | - | |
108
+ | validateTrigger | 设置字段校验的时机 | string \| string[] | onChange | |
109
+ | valuePropName | 子节点的值的属性。注意:Switch、Checkbox 的 valuePropName 应该是 | string | value | |
110
+ | wrapperCol | 需要为输入控件设置布局样式时,使用该属性,用法同 | | - | |
111
+ | layout | 表单项布局 | horizontal | - | 5.18.0 |
112
+
113
+
114
+ 被设置了 `name` 属性的 `Form.Item` 包装的控件,表单控件会自动添加 `value`(或 `valuePropName` 指定的其他属性) `onChange`(或 `trigger` 指定的其他属性),数据同步将被 Form 接管,这会导致以下结果:
115
+
116
+
117
+ 1. 你**不再需要也不应该**用 `onChange` 来做数据收集同步(你可以使用 Form 的 `onValuesChange`),但还是可以继续监听 `onChange` 事件。
118
+ 2. 你不能用控件的 `value` 或 `defaultValue` 等属性来设置表单域的值,默认值可以用 Form 里的 `initialValues` 来设置。注意 `initialValues` 不能被 `setState` 动态更新,你需要用 `setFieldsValue` 来更新。
119
+ 3. 你不应该用 `setState`,可以使用 `form.setFieldsValue` 来动态改变表单值。
120
+
121
+ ### dependencies
122
+
123
+ 当字段间存在依赖关系时使用。如果一个字段设置了 `dependencies` 属性。那么它所依赖的字段更新时,该字段将自动触发更新与校验。一种常见的场景,就是注册用户表单的“密码”与“确认密码”字段。“确认密码”校验依赖于“密码”字段,设置 `dependencies` 后,“密码”字段更新会重新触发“校验密码”的校验逻辑。你可以参考[具体例子](#form-demo-dependencies)。
124
+
125
+ `dependencies` 不应和 `shouldUpdate` 一起使用,因为这可能带来更新逻辑的混乱。
126
+
127
+ ### FeedbackIcons
128
+
129
+ `({ status: ValidateStatus, errors: ReactNode, warnings: ReactNode }) => Record<ValidateStatus, ReactNode>`
130
+
131
+ ### shouldUpdate
132
+
133
+ Form 通过增量更新方式,只更新被修改的字段相关组件以达到性能优化目的。大部分场景下,你只需要编写代码或者与 [`dependencies`](#dependencies) 属性配合校验即可。而在某些特定场景,例如修改某个字段值后出现新的字段选项、或者纯粹希望表单任意变化都对某一个区域进行渲染。你可以通过 `shouldUpdate` 修改 Form.Item 的更新逻辑。
134
+
135
+ 当 `shouldUpdate` 为 `true` 时,Form 的任意变化都会使该 Form.Item 重新渲染。这对于自定义渲染一些区域十分有帮助,要注意 Form.Item 里包裹的子组件必须由函数返回,否则 `shouldUpdate` 不会起作用:
136
+
137
+ 相关issue:[#34500](https://github.com/ant-design/ant-design/issues/34500)
138
+
139
+
140
+ ```jsx
141
+ <Form.Item shouldUpdate>
142
+ {() => {
143
+ return <pre>{JSON.stringify(form.getFieldsValue(), null, 2)}</pre>;
144
+ }}
145
+ </Form.Item>
146
+ ```
147
+
148
+
149
+ 你可以参考[示例](#form-demo-inline-login)查看具体使用场景。
150
+
151
+ 当 `shouldUpdate` 为方法时,表单的每次数值更新都会调用该方法,提供原先的值与当前的值以供你比较是否需要更新。这对于是否根据值来渲染额外字段十分有帮助:
152
+
153
+
154
+ ```jsx
155
+ <Form.Item shouldUpdate={(prevValues, curValues) => prevValues.additional !== curValues.additional}>
156
+ {() => {
157
+ return (
158
+ <Form.Item name="other">
159
+ <Input />
160
+ </Form.Item>
161
+ );
162
+ }}
163
+ </Form.Item>
164
+ ```
165
+
166
+
167
+ 你可以参考[示例](#form-demo-control-hooks)查看具体使用场景。
168
+
169
+ ### messageVariables
170
+
171
+ 你可以通过 `messageVariables` 修改 Form.Item 的默认验证信息。
172
+
173
+
174
+ ```jsx
175
+ <Form>
176
+ <Form.Item
177
+ messageVariables={{ another: 'good' }}
178
+ label="user"
179
+ rules={[{ required: true, message: '${another} is required' }]}
180
+ >
181
+ <Input />
182
+ </Form.Item>
183
+ <Form.Item
184
+ messageVariables={{ label: 'good' }}
185
+ label={<span>user</span>}
186
+ rules={[{ required: true, message: '${label} is required' }]}
187
+ >
188
+ <Input />
189
+ </Form.Item>
190
+ </Form>
191
+ ```
192
+
193
+
194
+ 自 `5.20.2` 起,当你希望不要转译 `${}` 时,你可以通过 `\\${}` 来略过:
195
+
196
+
197
+ ```jsx
198
+ { required: true, message: '${label} is convert, \\${label} is not convert' }
199
+
200
+ // good is convert, ${label} is not convert
201
+ ```
202
+
203
+
204
+ ## Form.List
205
+
206
+ 为字段提供数组化管理。
207
+
208
+ | 参数 | 说明 | 类型 | 默认值 | 版本 |
209
+ |--------|--------|--------|--------|--------|
210
+ | children | 渲染函数 | (fields: Field[], operation: { add, remove, move }, meta: { errors }) => React.ReactNode | - | |
211
+ | initialValue | 设置子元素默认值,如果与 Form 的 | any[] | - | 4.9.0 |
212
+ | name | 字段名,支持数组。List 本身也是字段,因而 | | - | |
213
+ | rules | 校验规则,仅支持自定义规则。需要配合 | { validator, message }[] | - | 4.7.0 |
214
+
215
+
216
+
217
+ ```tsx
218
+ <Form.List>
219
+ {(fields) =>
220
+ fields.map((field) => (
221
+ <Form.Item {...field}>
222
+ <Input />
223
+ </Form.Item>
224
+ ))
225
+ }
226
+ </Form.List>
227
+ ```
228
+
229
+
230
+ 注意:Form.List 下的字段不应该配置 `initialValue`,你始终应该通过 Form.List 的 `initialValue` 或者 Form 的 `initialValues` 来配置。
231
+
232
+ ## operation
233
+
234
+ Form.List 渲染表单相关操作函数。
235
+
236
+ | 参数 | 说明 | 类型 | 默认值 | 版本 |
237
+ |--------|--------|--------|--------|--------|
238
+ | add | 新增表单项 | (defaultValue?: any, insertIndex?: number) => void | insertIndex | 4.6.0 |
239
+ | move | 移动表单项 | (from: number, to: number) => void | - | |
240
+ | remove | 删除表单项 | (index: number \| number[]) => void | number[] | 4.5.0 |
241
+
242
+
243
+ ## Form.ErrorList
244
+
245
+ 4.7.0 新增。错误展示组件,仅限配合 Form.List 的 rules 一同使用。参考[示例](#form-demo-dynamic-form-item)。
246
+
247
+ | 参数 | 说明 | 类型 | 默认值 |
248
+ |--------|--------|--------|--------|
249
+ | errors | 错误列表 | ReactNode[] | - |
250
+
251
+
252
+ ## Form.Provider
253
+
254
+ 提供表单间联动功能,其下设置 `name` 的 Form 更新时,会自动触发对应事件。查看[示例](#form-demo-form-context)。
255
+
256
+ | 参数 | 说明 | 类型 | 默认值 |
257
+ |--------|--------|--------|--------|
258
+ | onFormChange | 子表单字段更新时触发 | function(formName: string, info: { changedFields, forms }) | - |
259
+ | onFormFinish | 子表单提交时触发 | function(formName: string, info: { values, forms }) | - |
260
+
261
+
262
+
263
+ ```jsx
264
+ <Form.Provider
265
+ onFormFinish={(name) => {
266
+ if (name === 'form1') {
267
+ // Do something...
268
+ }
269
+ }}
270
+ >
271
+ <Form name="form1">...</Form>
272
+ <Form name="form2">...</Form>
273
+ </Form.Provider>
274
+ ```
275
+
276
+
277
+ ### FormInstance
278
+
279
+ | 名称 | 说明 | 类型 | 版本 |
280
+ |--------|--------|--------|--------|
281
+ | getFieldError | 获取对应字段名的错误信息 | (name: | |
282
+ | getFieldInstance | 获取对应字段实例 | (name: | 4.4.0 |
283
+ | getFieldsError | 获取一组字段名对应的错误信息,返回为数组形式 | (nameList?: | |
284
+ | getFieldsValue | 获取一组字段名对应的值,会按照对应结构返回。默认返回现存字段值,当调用 | | |
285
+ | getFieldValue | 获取对应字段名的值 | (name: | |
286
+ | isFieldsTouched | 检查一组字段是否被用户操作过, | (nameList?: | |
287
+ | isFieldTouched | 检查对应字段是否被用户操作过 | (name: | |
288
+ | isFieldValidating | 检查对应字段是否正在校验 | (name: | |
289
+ | resetFields | 重置一组字段到 | (fields?: | |
290
+ | scrollToField | 滚动到对应字段位置 | (name: | focus: 5.24.0 |
291
+ | setFields | 设置一组字段状态 | (fields: | |
292
+ | setFieldValue | 设置表单的值(该值将直接传入 form store 中并且 | (name: | 4.22.0 |
293
+ | setFieldsValue | 设置表单的值(该值将直接传入 form store 中并且 | (values) => void | |
294
+ | submit | 提交表单,与点击 | () => void | |
295
+ | validateFields | 触发表单验证,设置 | (nameList?: | |
296
+
297
+
298
+ #### validateFields
299
+
300
+
301
+ ```tsx
302
+ export interface ValidateConfig {
303
+ // 5.5.0 新增。仅校验内容而不会将错误信息展示到 UI 上。
304
+ validateOnly?: boolean;
305
+ // 5.9.0 新增。对提供的 `nameList` 与其子路径进行递归校验。
306
+ recursive?: boolean;
307
+ // 5.11.0 新增。校验 dirty 的字段(touched + validated)。
308
+ // 使用 `dirty` 可以很方便的仅校验用户操作过和被校验过的字段。
309
+ dirty?: boolean;
310
+ }
311
+ ```
312
+
313
+
314
+ 返回示例:
315
+
316
+
317
+ ```jsx
318
+ validateFields()
319
+ .then((values) => {
320
+ /*
321
+ values:
322
+ {
323
+ username: 'username',
324
+ password: 'password',
325
+ }
326
+ */
327
+ })
328
+ .catch((errorInfo) => {
329
+ /*
330
+ errorInfo:
331
+ {
332
+ values: {
333
+ username: 'username',
334
+ password: 'password',
335
+ },
336
+ errorFields: [
337
+ { name: ['password'], errors: ['Please input your Password!'] },
338
+ ],
339
+ outOfDate: false,
340
+ }
341
+ */
342
+ });
343
+ ```
344
+
345
+
346
+ ## Hooks
347
+
348
+ ### Form.useForm
349
+
350
+ `type Form.useForm = (): [FormInstance]`
351
+
352
+ 创建 Form 实例,用于管理所有数据状态。
353
+
354
+ ### Form.useFormInstance
355
+
356
+ `type Form.useFormInstance = (): FormInstance`
357
+
358
+ `4.20.0` 新增,获取当前上下文正在使用的 Form 实例,常见于封装子组件消费无需透传 Form 实例:
359
+
360
+
361
+ ```tsx
362
+ const Sub = () => {
363
+ const form = Form.useFormInstance();
364
+
365
+ return <Button onClick={() => form.setFieldsValue({})} />;
366
+ };
367
+
368
+ export default () => {
369
+ const [form] = Form.useForm();
370
+
371
+ return (
372
+ <Form form={form}>
373
+ <Sub />
374
+ </Form>
375
+ );
376
+ };
377
+ ```
378
+
379
+
380
+ ### Form.useWatch
381
+
382
+ `type Form.useWatch = (namePath: NamePath | (selector: (values: Store)) => any, formInstance?: FormInstance | WatchOptions): Value`
383
+
384
+ `5.12.0` 新增 `selector`
385
+
386
+ 用于直接获取 form 中字段对应的值。通过该 Hooks 可以与诸如 `useSWR` 进行联动从而降低维护成本:
387
+
388
+
389
+ ```tsx
390
+ const Demo = () => {
391
+ const [form] = Form.useForm();
392
+ const userName = Form.useWatch('username', form);
393
+
394
+ const { data: options } = useSWR(`/api/user/${userName}`, fetcher);
395
+
396
+ return (
397
+ <Form form={form}>
398
+ <Form.Item name="username">
399
+ <AutoComplete options={options} />
400
+ </Form.Item>
401
+ </Form>
402
+ );
403
+ };
404
+ ```
405
+
406
+
407
+ 如果你的组件被包裹在 `Form.Item` 内部,你可以省略第二个参数,`Form.useWatch` 会自动找到上层最近的 `FormInstance`。
408
+
409
+ `useWatch` 默认只监听在 Form 中注册的字段,如果需要监听非注册字段,可以通过配置 `preserve` 进行监听:
410
+
411
+
412
+ ```tsx
413
+ const Demo = () => {
414
+ const [form] = Form.useForm();
415
+
416
+ const age = Form.useWatch('age', { form, preserve: true });
417
+ console.log(age);
418
+
419
+ return (
420
+ <div>
421
+ <Button onClick={() => form.setFieldValue('age', 2)}>Update</Button>
422
+ <Form form={form}>
423
+ <Form.Item name="name">
424
+ <Input />
425
+ </Form.Item>
426
+ </Form>
427
+ </div>
428
+ );
429
+ };
430
+ ```
431
+
432
+
433
+ ### Form.Item.useStatus
434
+
435
+ `type Form.Item.useStatus = (): { status: ValidateStatus | undefined, errors: ReactNode[], warnings: ReactNode[] }`
436
+
437
+ `4.22.0` 新增,可用于获取当前 Form.Item 的校验状态,如果上层没有 Form.Item,`status` 将会返回 `undefined`。`5.4.0` 新增 `errors` 和 `warnings`,可用于获取当前 Form.Item 的错误信息和警告信息:
438
+
439
+
440
+ ```tsx
441
+ const CustomInput = ({ value, onChange }) => {
442
+ const { status, errors } = Form.Item.useStatus();
443
+ return (
444
+ <input
445
+ value={value}
446
+ onChange={onChange}
447
+ className={`custom-input-${status}`}
448
+ placeholder={(errors.length && errors[0]) || ''}
449
+ />
450
+ );
451
+ };
452
+
453
+ export default () => (
454
+ <Form>
455
+ <Form.Item name="username">
456
+ <CustomInput />
457
+ </Form.Item>
458
+ </Form>
459
+ );
460
+ ```
461
+
462
+
463
+ #### 与其他获取数据的方式的区别
464
+
465
+ Form 仅会对变更的 Field 进行刷新,从而避免完整的组件刷新可能引发的性能问题。因而你无法在 render 阶段通过 `form.getFieldsValue` 来实时获取字段值,而 `useWatch` 提供了一种特定字段访问的方式,从而使得在当前组件中可以直接消费字段的值。同时,如果为了更好的渲染性能,你可以通过 Field 的 renderProps 仅更新需要更新的部分。而当当前组件更新或者 effect 都不需要消费字段值时,则可以通过 `onValuesChange` 将数据抛出,从而避免组件更新。
466
+
467
+ ## Interface
468
+
469
+ ### NamePath
470
+
471
+ `string | number | (string | number)[]`
472
+
473
+ ### GetFieldsValue
474
+
475
+ `getFieldsValue` 提供了多种重载方法:
476
+
477
+ #### getFieldsValue(nameList?: true | [NamePath](#namepath)[], filterFunc?: FilterFunc)
478
+
479
+ 当不提供 `nameList` 时,返回所有注册字段,这也包含 List 下所有的值(即便 List 下没有绑定 Item)。
480
+
481
+ 当 `nameList` 为 `true` 时,返回 store 中所有的值,包含未注册字段。例如通过 `setFieldsValue` 设置了不存在的 Item 的值,也可以通过 `true` 全部获取。
482
+
483
+ 当 `nameList` 为数组时,返回规定路径的值。需要注意的是,`nameList` 为嵌套数组。例如你需要某路径值应该如下:
484
+
485
+
486
+ ```tsx
487
+ // 单个路径
488
+ form.getFieldsValue([['user', 'age']]);
489
+
490
+ // 多个路径
491
+ form.getFieldsValue([
492
+ ['user', 'age'],
493
+ ['preset', 'account'],
494
+ ]);
495
+ ```
496
+
497
+
498
+ #### getFieldsValue({ strict?: boolean, filter?: FilterFunc })
499
+
500
+ `5.8.0` 新增接受配置参数。当 `strict` 为 `true` 时会仅匹配 Item 的值。例如 `{ list: [{ bamboo: 1, little: 2 }] }` 中,如果 List 仅绑定了 `bamboo` 字段,那么 `getFieldsValue({ strict: true })` 会只获得 `{ list: [{ bamboo: 1 }] }`。
501
+
502
+ ### FilterFunc
503
+
504
+ 用于过滤一些字段值,`meta` 会返回字段相关信息。例如可以用来获取仅被用户修改过的值等等。
505
+
506
+
507
+ ```tsx
508
+ type FilterFunc = (meta: { touched: boolean; validating: boolean }) => boolean;
509
+ ```
510
+
511
+
512
+ ### FieldData
513
+
514
+ | 名称 | 说明 | 类型 |
515
+ |--------|--------|--------|
516
+ | errors | 错误信息 | string[] |
517
+ | warnings | 警告信息 | string[] |
518
+ | name | 字段名称 | |
519
+ | touched | 是否被用户操作过 | boolean |
520
+ | validating | 是否正在校验 | boolean |
521
+ | value | 字段对应值 | any |
522
+
523
+
524
+ ### Rule
525
+
526
+ Rule 支持接收 object 进行配置,也支持 function 来动态获取 form 的数据:
527
+
528
+
529
+ ```tsx
530
+ type Rule = RuleConfig | ((form: FormInstance) => RuleConfig);
531
+ ```
532
+
533
+
534
+ | 名称 | 说明 | 类型 | 版本 |
535
+ |--------|--------|--------|--------|
536
+ | defaultField | 仅在 | | |
537
+ | enum | 是否匹配枚举中的值(需要将 | any[] | |
538
+ | fields | 仅在 | Record<string, | |
539
+ | len | string 类型时为字符串长度;number 类型时为确定数字; array 类型时为数组长度 | number | |
540
+ | max | 必须设置 | number | |
541
+ | message | 错误信息,不设置时会通过 | string \| ReactElement | |
542
+ | min | 必须设置 | number | |
543
+ | pattern | 正则表达式匹配 | RegExp | |
544
+ | required | 是否为必选字段 | boolean | |
545
+ | transform | 将字段值转换成目标值后进行校验 | (value) => any | |
546
+ | type | 类型,常见有 | string | |
547
+ | validateTrigger | 设置触发验证时机,必须是 Form.Item 的 | string \| string[] | |
548
+ | validator | 自定义校验,接收 Promise 作为返回值。 | ( | |
549
+ | warningOnly | 仅警告,不阻塞表单提交 | boolean | 4.17.0 |
550
+ | whitespace | 如果字段仅包含空格则校验不通过,只在 | boolean | |
551
+
552
+
553
+ ### WatchOptions
554
+
555
+ | 名称 | 说明 | 类型 | 默认值 | 版本 |
556
+ |--------|--------|--------|--------|--------|
557
+ | form | 指定 Form 实例 | FormInstance | 当前 context 中的 Form | 5.4.0 |
558
+ | preserve | 是否监视没有对应的 | boolean | false | 5.4.0 |
@@ -0,0 +1,45 @@
1
+
2
+
3
+ <DemoTitle title="IconButton" desc="以图标为核心的轻量按钮组件,用于触发操作。">
4
+ #### API
5
+
6
+ | 属性名 | 描述 | 类型 | 默认值 |
7
+ |--------|--------|--------|--------|
8
+ | icon | icon | ReactNode \| string | |
9
+ | iconType | 百炼图标iconfont的key,依赖百炼的iconFont,例如bl-icon-add | string | |
10
+ | bordered | 边框 | boolean | true |
11
+ | shape | 形状 | 'default' \| 'circle' | 'default' |
12
+
13
+
14
+ ## antd API
15
+
16
+ 通用属性参考:[通用属性](/docs/react/common-props)
17
+
18
+ 通过设置 Button 的属性来产生不同的按钮样式,推荐顺序为:`type` -> `shape` -> `size` -> `loading` -> `disabled`。
19
+
20
+ 按钮的属性说明如下:
21
+
22
+ | 属性 | 说明 | 类型 | 默认值 | 版本 |
23
+ |--------|--------|--------|--------|--------|
24
+ | autoInsertSpace | 我们默认提供两个汉字之间的空格,可以设置 | boolean | true | 5.17.0 |
25
+ | block | 将按钮宽度调整为其父宽度的选项 | boolean | false | |
26
+ | classNames | 语义化结构 class | | - | 5.4.0 |
27
+ | color | 设置按钮的颜色 | default | - | default |
28
+ | danger | 语法糖,设置危险按钮。当设置 | boolean | false | |
29
+ | disabled | 设置按钮失效状态 | boolean | false | |
30
+ | ghost | 幽灵属性,使按钮背景透明 | boolean | false | |
31
+ | href | 点击跳转的地址,指定此属性 button 的行为和 a 链接一致 | string | - | |
32
+ | htmlType | 设置 | submit | button | |
33
+ | iconPosition | 设置按钮图标组件的位置 | start | start | 5.17.0 |
34
+ | loading | 设置按钮载入状态 | boolean \| { delay: number, icon: ReactNode } | false | icon: 5.23.0 |
35
+ | styles | 语义化结构 style | | - | 5.4.0 |
36
+ | target | 相当于 a 链接的 target 属性,href 存在时生效 | string | - | |
37
+ | onClick | 点击按钮时的回调 | (event: React.MouseEvent) => void | - | |
38
+ | variant | 设置按钮的变体 | outlined | - | 5.21.0 |
39
+
40
+
41
+ 支持原生 button 的其他所有属性。
42
+
43
+ ### PresetColors
44
+
45
+ type PresetColors = 'blue' | 'purple' | 'cyan' | 'green' | 'magenta' | 'pink' | 'red' | 'orange' | 'yellow' | 'volcano' | 'geekblue' | 'lime' | 'gold';