mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-13 07:39:10 +08:00
51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
|
import type { FormInstance } from 'antd';
|
||
|
import { Button, Form, Input, Space } from 'antd';
|
||
|
import React from 'react';
|
||
|
|
||
|
const SubmitButton = ({ form }: { form: FormInstance }) => {
|
||
|
const [submittable, setSubmittable] = React.useState(false);
|
||
|
|
||
|
// Watch all values
|
||
|
const values = Form.useWatch([], form);
|
||
|
|
||
|
React.useEffect(() => {
|
||
|
form.validateFields({ validateOnly: true }).then(
|
||
|
() => {
|
||
|
setSubmittable(true);
|
||
|
},
|
||
|
() => {
|
||
|
setSubmittable(false);
|
||
|
},
|
||
|
);
|
||
|
}, [values]);
|
||
|
|
||
|
return (
|
||
|
<Button type="primary" htmlType="submit" disabled={!submittable}>
|
||
|
Submit
|
||
|
</Button>
|
||
|
);
|
||
|
};
|
||
|
|
||
|
const App = () => {
|
||
|
const [form] = Form.useForm();
|
||
|
|
||
|
return (
|
||
|
<Form form={form} name="validateOnly" layout="vertical" autoComplete="off">
|
||
|
<Form.Item name="name" label="Name" rules={[{ required: true }]}>
|
||
|
<Input />
|
||
|
</Form.Item>
|
||
|
<Form.Item name="age" label="Age" rules={[{ required: true }]}>
|
||
|
<Input />
|
||
|
</Form.Item>
|
||
|
<Form.Item>
|
||
|
<Space>
|
||
|
<SubmitButton form={form} />
|
||
|
<Button htmlType="reset">Reset</Button>
|
||
|
</Space>
|
||
|
</Form.Item>
|
||
|
</Form>
|
||
|
);
|
||
|
};
|
||
|
|
||
|
export default App;
|