2023-07-28 16:17:43 +08:00
|
|
|
import React from 'react';
|
2023-05-11 15:43:00 +08:00
|
|
|
import type { FormInstance } from 'antd';
|
|
|
|
import { Button, Form, Input, Space } from 'antd';
|
|
|
|
|
2024-02-23 11:01:56 +08:00
|
|
|
interface SubmitButtonProps {
|
|
|
|
form: FormInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SubmitButton: React.FC<React.PropsWithChildren<SubmitButtonProps>> = ({ form, children }) => {
|
|
|
|
const [submittable, setSubmittable] = React.useState<boolean>(false);
|
2023-05-11 15:43:00 +08:00
|
|
|
|
|
|
|
// Watch all values
|
|
|
|
const values = Form.useWatch([], form);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2024-02-23 11:01:56 +08:00
|
|
|
form
|
|
|
|
.validateFields({ validateOnly: true })
|
|
|
|
.then(() => setSubmittable(true))
|
|
|
|
.catch(() => setSubmittable(false));
|
|
|
|
}, [form, values]);
|
2023-05-11 15:43:00 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Button type="primary" htmlType="submit" disabled={!submittable}>
|
2024-02-23 11:01:56 +08:00
|
|
|
{children}
|
2023-05-11 15:43:00 +08:00
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-07-06 23:54:03 +08:00
|
|
|
const App: React.FC = () => {
|
2023-05-11 15:43:00 +08:00
|
|
|
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>
|
2024-02-23 11:01:56 +08:00
|
|
|
<SubmitButton form={form}>Submit</SubmitButton>
|
2023-05-11 15:43:00 +08:00
|
|
|
<Button htmlType="reset">Reset</Button>
|
|
|
|
</Space>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|