Skip to content

Commit

Permalink
docs: add faq about valuePropName (ant-design#44609)
Browse files Browse the repository at this point in the history
* docs: add faq about valuePropName

* docs: more info

* docs: clean up
  • Loading branch information
zombieJ authored Sep 4, 2023
1 parent c29e91d commit c7a0928
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
12 changes: 12 additions & 0 deletions components/checkbox/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,15 @@ interface Option {
## Design Token

<ComponentTokenTable component="Checkbox"></ComponentTokenTable>

## FAQ

### Why not work in Form.Item?

Form.Item default bind value to `value` property, but Checkbox value property is `checked`. You can use `valuePropName` to change bind property.

```tsx | pure
<Form.Item name="fieldA" valuePropName="checked">
<Checkbox />
</Form.Item>
```
12 changes: 12 additions & 0 deletions components/checkbox/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ interface Option {
## 主题变量(Design Token)

<ComponentTokenTable component="Checkbox"></ComponentTokenTable>

## FAQ

### 为什么在 Form.Item 下不能绑定数据?

Form.Item 默认绑定值属性到 `value` 上,而 Checkbox 的值属性为 `checked`。你可以通过 `valuePropName` 来修改绑定的值属性。

```tsx | pure
<Form.Item name="fieldA" valuePropName="checked">
<Checkbox />
</Form.Item>
```
12 changes: 11 additions & 1 deletion components/form/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Form field component for data bidirectional binding, validation, layout, and so
| validateFirst | Whether stop validate on first rule of error for this field. Will parallel validate when `parallel` configured | boolean \| `parallel` | false | `parallel`: 4.5.0 |
| validateStatus | The validation status. If not provided, it will be generated by validation rule. options: `success` `warning` `error` `validating` | string | - | |
| validateTrigger | When to validate the value of children node | string \| string\[] | `onChange` | |
| valuePropName | Props of children node, for example, the prop of Switch is 'checked'. This prop is an encapsulation of `getValueProps`, which will be invalid after customizing `getValueProps` | string | `value` | |
| valuePropName | Props of children node, for example, the prop of Switch or Checkbox is `checked`. This prop is an encapsulation of `getValueProps`, which will be invalid after customizing `getValueProps` | string | `value` | |
| wrapperCol | The layout for input controls, same as `labelCol`. You can set `wrapperCol` on Form which will not affect nest Item. If both exists, use Item first | [object](/components/grid/#col) | - | |

After wrapped by `Form.Item` with `name` property, `value`(or other property defined by `valuePropName`) `onChange`(or other property defined by `trigger`) props will be added to form controls, the flow of form data will be handled by Form which will cause:
Expand Down Expand Up @@ -533,6 +533,16 @@ type Rule = RuleConfig | ((form: FormInstance) => RuleConfig);

## FAQ

### Why can't Switch、Checkbox bind data?

Form.Item default bind value to `value` prop, but Switch or Checkbox value prop is `checked`. You can use `valuePropName` to change bind value prop.

```tsx | pure
<Form.Item name="fieldA" valuePropName="checked">
<Switch />
</Form.Item>
```

### Custom validator not working

It may be caused by your `validator` if it has some errors that prevents `callback` to be called. You can use `async` instead or use `try...catch` to catch the error:
Expand Down
12 changes: 11 additions & 1 deletion components/form/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const validateMessages = {
| validateFirst | 当某一规则校验不通过时,是否停止剩下的规则的校验。设置 `parallel` 时会并行校验 | boolean \| `parallel` | false | `parallel`: 4.5.0 |
| validateStatus | 校验状态,如不设置,则会根据校验规则自动生成,可选:'success' 'warning' 'error' 'validating' | string | - | |
| validateTrigger | 设置字段校验的时机 | string \| string\[] | `onChange` | |
| valuePropName | 子节点的值的属性,如 Switch 的是 'checked'。该属性为 `getValueProps` 的封装,自定义 `getValueProps` 后会失效 | string | `value` | |
| valuePropName | 子节点的值的属性,如 Switch、Checkbox 的是 `checked`。该属性为 `getValueProps` 的封装,自定义 `getValueProps` 后会失效 | string | `value` | |
| wrapperCol | 需要为输入控件设置布局样式时,使用该属性,用法同 `labelCol`。你可以通过 Form 的 `wrapperCol` 进行统一设置,不会作用于嵌套 Item。当和 Form 同时设置时,以 Item 为准 | [object](/components/grid-cn#col) | - | |

被设置了 `name` 属性的 `Form.Item` 包装的控件,表单控件会自动添加 `value`(或 `valuePropName` 指定的其他属性) `onChange`(或 `trigger` 指定的其他属性),数据同步将被 Form 接管,这会导致以下结果:
Expand Down Expand Up @@ -532,6 +532,16 @@ type Rule = RuleConfig | ((form: FormInstance) => RuleConfig);

## FAQ

### Switch、Checkbox 为什么不能绑定数据?

Form.Item 默认绑定值属性到 `value` 上,而 Switch、Checkbox 等组件的值属性为 `checked`。你可以通过 `valuePropName` 来修改绑定的值属性。

```tsx | pure
<Form.Item name="fieldA" valuePropName="checked">
<Switch />
</Form.Item>
```

### 自定义 validator 没有效果

这是由于你的 `validator` 有错误导致 `callback` 没有执行到。你可以选择通过 `async` 返回一个 promise 或者使用 `try...catch` 进行错误捕获:
Expand Down
12 changes: 12 additions & 0 deletions components/switch/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@ Common props ref:[Common props](/docs/react/common-props)
## Design Token

<ComponentTokenTable component="Switch"></ComponentTokenTable>

## FAQ

### Why not work in Form.Item?

Form.Item default bind value to `value` property, but Switch value property is `checked`. You can use `valuePropName` to change bind property.

```tsx | pure
<Form.Item name="fieldA" valuePropName="checked">
<Switch />
</Form.Item>
```
12 changes: 12 additions & 0 deletions components/switch/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,15 @@ demo:
## 主题变量(Design Token)

<ComponentTokenTable component="Switch"></ComponentTokenTable>

## FAQ

### 为什么在 Form.Item 下不能绑定数据?

Form.Item 默认绑定值属性到 `value` 上,而 Switch 的值属性为 `checked`。你可以通过 `valuePropName` 来修改绑定的值属性。

```tsx | pure
<Form.Item name="fieldA" valuePropName="checked">
<Switch />
</Form.Item>
```

0 comments on commit c7a0928

Please sign in to comment.