重点代码:
const [form] = Form.useForm(); // form-可以任意名称,区分不同Form表单
<Form form={form} ></Form>
import { Form, Input, message, Spin } from 'antd';
const nodeList: React.FC = () => {
const [form] = Form.useForm(); // 重点这里
const handleOk = () => {
// 表单校验
form
.validateFields()
.then(async (values) => {
// 校验成功
const result: API.normalResult = await createNodeAPI({...values, clusterId: getData.clusterId})
if(result && result.success){
message.success('新增成功');
getNodeData({ clusterId: getData.clusterId });
setIsModalOpen(false);
form.resetFields()
}
})
.catch((err) => {
// 校验失败
console.log('err: ', err);
});
};
const handleCancel = () => {
form.resetFields() // 重置表单
setIsModalOpen(false);
};
return (
<Modal
key="addnodemodal"
title="新增节点"
forceRender={true}
destroyOnClose={true}
open={isModalOpen}
onOk={handleOk}
onCancel={handleCancel}
// footer={null}
>
<div>
<Form
form={form} // 重点这里
key="addnodeform"
name="新增节点"
preserve={false}
labelCol={{ span: 8 }}
wrapperCol={{ span: 16 }}
initialValues={{ remember: true }}
// onFinish={onFinish}
autoComplete="off"
>
<Form.Item
label="ip"
name="ip"
rules={[{ required: true, message: '请输入ip!' }]}
>
<Input />
</Form.Item>
<Form.Item
label="sshUser"
name="sshUser"
rules={[{ required: true, message: '请输入sshUser!' }]}
>
<Input />
</Form.Item>
<Form.Item
label="sshPassword"
name="sshPassword"
rules={[{ required: true, message: '请输入sshPassword!' }]}
>
<Input />
</Form.Item>
<Form.Item
label="sshPort"
name="sshPort"
rules={[{ required: true, message: 'sshPort!' }]}
>
<Input />
</Form.Item>
{/* <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
<Button key='addnodebtn' type="primary" htmlType="submit">
确定
</Button>
</Form.Item> */}
</Form>
</div>
</Modal>
)
}