-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(form): add customized form controls example (#3112)
- Loading branch information
Showing
4 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.