mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-05 05:28:20 +08:00
b9a6b7b578
* demo: update demo * add form * clear * add Select * add * fix style * fix style * fix * revert
97 lines
2.4 KiB
TypeScript
97 lines
2.4 KiB
TypeScript
import React from 'react';
|
|
import { Button, Form, Input, Select } from 'antd';
|
|
import type { FormInstance } from 'antd/es/form';
|
|
|
|
const { Option } = Select;
|
|
|
|
const layout = {
|
|
labelCol: { span: 8 },
|
|
wrapperCol: { span: 16 },
|
|
};
|
|
|
|
const tailLayout = {
|
|
wrapperCol: { offset: 8, span: 16 },
|
|
};
|
|
|
|
const App: React.FC = () => {
|
|
const formRef = React.useRef<FormInstance>(null);
|
|
|
|
const onGenderChange = (value: string) => {
|
|
switch (value) {
|
|
case 'male':
|
|
formRef.current?.setFieldsValue({ note: 'Hi, man!' });
|
|
break;
|
|
case 'female':
|
|
formRef.current?.setFieldsValue({ note: 'Hi, lady!' });
|
|
break;
|
|
case 'other':
|
|
formRef.current?.setFieldsValue({ note: 'Hi there!' });
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
};
|
|
|
|
const onFinish = (values: any) => {
|
|
console.log(values);
|
|
};
|
|
|
|
const onReset = () => {
|
|
formRef.current?.resetFields();
|
|
};
|
|
|
|
const onFill = () => {
|
|
formRef.current?.setFieldsValue({ note: 'Hello world!', gender: 'male' });
|
|
};
|
|
|
|
return (
|
|
<Form
|
|
{...layout}
|
|
ref={formRef}
|
|
name="control-ref"
|
|
onFinish={onFinish}
|
|
style={{ maxWidth: 600 }}
|
|
>
|
|
<Form.Item name="note" label="Note" rules={[{ required: true }]}>
|
|
<Input />
|
|
</Form.Item>
|
|
<Form.Item name="gender" label="Gender" rules={[{ required: true }]}>
|
|
<Select
|
|
placeholder="Select a option and change input text above"
|
|
onChange={onGenderChange}
|
|
allowClear
|
|
>
|
|
<Option value="male">male</Option>
|
|
<Option value="female">female</Option>
|
|
<Option value="other">other</Option>
|
|
</Select>
|
|
</Form.Item>
|
|
<Form.Item
|
|
noStyle
|
|
shouldUpdate={(prevValues, currentValues) => prevValues.gender !== currentValues.gender}
|
|
>
|
|
{({ getFieldValue }) =>
|
|
getFieldValue('gender') === 'other' ? (
|
|
<Form.Item name="customizeGender" label="Customize Gender" rules={[{ required: true }]}>
|
|
<Input />
|
|
</Form.Item>
|
|
) : null
|
|
}
|
|
</Form.Item>
|
|
<Form.Item {...tailLayout}>
|
|
<Button type="primary" htmlType="submit">
|
|
Submit
|
|
</Button>
|
|
<Button htmlType="button" onClick={onReset}>
|
|
Reset
|
|
</Button>
|
|
<Button type="link" htmlType="button" onClick={onFill}>
|
|
Fill form
|
|
</Button>
|
|
</Form.Item>
|
|
</Form>
|
|
);
|
|
};
|
|
|
|
export default App;
|