Skip to content

Commit

Permalink
docs(form): add customized form controls example (#3112)
Browse files Browse the repository at this point in the history
  • Loading branch information
miownag authored Sep 24, 2024
1 parent eda4be3 commit ab0b227
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 2 deletions.
87 changes: 87 additions & 0 deletions src/form/_example/customized-form-controls.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React from 'react';
import { Form, Input, Button, MessagePlugin, Space, Select } from 'tdesign-react';
import type { FormProps } from 'tdesign-react';

interface ICourseSelect {
value?: {
type?: string;
name?: string;
};
onChange?: (v: { type?: string; name?: string }) => void;
}

const { FormItem } = Form;

function CourseSelect(props: ICourseSelect) {
const { value, onChange } = props;

return (
<Space>
<Select
options={[
{
label: '数学',
value: 'math',
},
{
label: '英语',
value: 'english',
},
]}
value={value?.type}
onChange={(v) => {
onChange?.({
...value,
type: v as string,
});
}}
placeholder="请选择课程类型"
/>
<Input
value={value?.name}
onChange={(v) => {
onChange?.({
...value,
name: v,
});
}}
placeholder="请输入课程名称"
/>
</Space>
);
}

export default function BaseForm() {
const [form] = Form.useForm();

const onSubmit: FormProps['onSubmit'] = (e) => {
console.log(e);
if (e.validateResult === true) {
MessagePlugin.info('提交成功');
}
};
const setData = () => {
form.setFieldsValue?.({
course: {
type: 'math',
name: '线性代数',
},
});
};

return (
<Form form={form} onSubmit={onSubmit} colon labelWidth={100}>
<FormItem label="课程" name="course">
<CourseSelect />
</FormItem>
<FormItem style={{ marginLeft: 100 }}>
<Button type="submit" theme="primary">
提交
</Button>
<Button theme="primary" onClick={setData} style={{ marginLeft: 12 }}>
设置信息
</Button>
</FormItem>
</Form>
);
}
10 changes: 8 additions & 2 deletions src/form/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

### 复杂嵌套数据结构表单

可给 `name` 传入数组整理成对象嵌套数据结构
可给 `name` 传入数组整理成对象嵌套数据结构

{{ nested-data }}

### 动态增减嵌套表单

可使用 `Form.FormList` 组件创建动态表单
可使用 `Form.FormList` 组件创建动态表单

{{ form-list }}

### 自定义表单控件

可以使用 `Form.FormItem` 包裹自定义组件并在组件中接受 `value``onChange` 的入参,实现自定义表单控件。

{{ customized-form-controls }}

## Hooks

### Form.useForm
Expand Down
Loading

0 comments on commit ab0b227

Please sign in to comment.