Skip to content

Commit 8e4c986

Browse files
committed
docs: 新增 Cascader form 用例
1 parent bd5d230 commit 8e4c986

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

packages/react-vant/src/components/cascader/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import { Cascader } from 'react-vant';
1616

1717
<code title="基础用法" src="./demo/base.tsx" />
1818

19+
### Form 用法
20+
21+
<code title="Form 用法" src="./demo/form.tsx" />
22+
1923
### 异步加载选项
2024

2125
可以监听 `onChange` 事件并动态设置 `options`,实现异步加载选项。
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react'
2+
import { Cascader, Button, Input, Form, Toast, Space } from 'react-vant'
3+
import options from './options'
4+
5+
export default () => {
6+
const [form] = Form.useForm()
7+
const onSumbit = async () => {
8+
const values = await form.getFieldsValue()
9+
console.log(values)
10+
Toast.info(JSON.stringify(values))
11+
}
12+
const setValue = () => {
13+
form.setFieldsValue({ area: ['330000', '330100', '330104'] })
14+
}
15+
return (
16+
<Form form={form}>
17+
<Form.Item name='area' label='地区' isLink>
18+
<Cascader
19+
popup={{ round: true }}
20+
title='请选择所在地区'
21+
options={options}
22+
>
23+
{(_, selectedRows, actions) => (
24+
<Input
25+
value={selectedRows.map(el => el.text).join(',')}
26+
readOnly
27+
placeholder='请选择所在地区'
28+
onClick={() => actions.open()}
29+
/>
30+
)}
31+
</Cascader>
32+
</Form.Item>
33+
<Space block justify='center' style={{ marginTop: 20 }}>
34+
<Button type='primary' onClick={onSumbit}>
35+
提交
36+
</Button>
37+
<Button onClick={setValue}>设置默认值</Button>
38+
</Space>
39+
</Form>
40+
)
41+
}

0 commit comments

Comments
 (0)