Skip to content

Commit

Permalink
docs(Form): 将 FQA 中的 【如何根据某个字段变化动态展示数据】 改为 【字段联动的表单示例】
Browse files Browse the repository at this point in the history
  • Loading branch information
l123wx committed Oct 18, 2024
1 parent f8c9abc commit b4afb58
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
47 changes: 47 additions & 0 deletions src/form/_example/form-field-linkage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react';
import Form from '../Form';
import Radio from '../../radio';
import Button from '../../button';

const { FormItem } = Form;

export default function FormExample() {
const [form] = Form.useForm();
const setMessage = () => {
form.setFieldsValue({
type: 'cold',
ice: '1',
});
};

return (
<Form form={form} colon labelWidth={100}>
<FormItem label="类型" name="type" initialData="hot">
<Radio.Group>
<Radio value="hot">热饮</Radio>
<Radio value="cold">冷饮</Radio>
</Radio.Group>
</FormItem>
<FormItem shouldUpdate={(prev, next) => prev.type !== next.type}>
{({ getFieldValue }) => {
if (getFieldValue('type') === 'cold') {
return (
<FormItem label="冰量" key="ice" name="ice">
<Radio.Group>
<Radio value="0">正常冰</Radio>
<Radio value="1">少冰</Radio>
<Radio value="2">去冰</Radio>
</Radio.Group>
</FormItem>
);
}
return null;
}}
</FormItem>

<FormItem style={{ marginLeft: 100 }}>
<Button onClick={setMessage}>选择冷饮-少冰</Button>
</FormItem>
</Form>
);
}
26 changes: 6 additions & 20 deletions src/form/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

{{ form-list }}

### 字段联动的表单

在某些特定场景,例如修改某个字段值后出现新的字段选项、或者纯粹希望表单任意变化都对某一个区域进行渲染。你可以通过 `shouldUpdate` 修改 `FormItem` 的更新逻辑。

{{ form-field-linkage }}

### 自定义表单控件

可以使用 `Form.FormItem` 包裹自定义组件并在组件中接受 `value``onChange` 的入参,实现自定义表单控件。
Expand Down Expand Up @@ -76,26 +82,6 @@ Form 组件设计的初衷是为了解放开发者配置大量的 `value`、`onC
</Form.FormItem>
```

### 如何根据某个字段变化动态展示数据

而在某些特定场景,例如修改某个字段值后出现新的字段选项、或者纯粹希望表单任意变化都对某一个区域进行渲染。你可以通过 `shouldUpdate` 修改 `FormItem` 的更新逻辑。

```js
<Form.FormItem shouldUpdate={(prev, next) => prev.additional !== next.additional}>
{({ getFieldValue }) => {
if (getFieldValue('additional') === 'test') {
return (
<Form.FormItem name="test">
<Input />
</Form.FormItem>
);
}
return null;
}}
<Input />
</Form.FormItem>
```

## API

### Form Props
Expand Down

0 comments on commit b4afb58

Please sign in to comment.