用react hooks+typescript结合antdesign二次封装表单组件,实现表单联动
时间: 2024-04-24 16:04:53 浏览: 198
React+Antd-可视化拖拽表单
好的,我了解了您的问题。对于这个问题,您需要使用 React Hooks 和 TypeScript 来结合 Ant Design 进行表单组件的二次封装和联动。
首先,您需要在 React 中定义一个表单组件,并使用 useState 钩子来处理输入值的状态。例如:
```
import React, { useState } from 'react';
import { Form, Input, Button } from 'antd';
interface MyFormProps {
initialValues?: MyFormValues;
onSubmit?: (values: MyFormValues) => void;
}
interface MyFormValues {
name: string;
age: number;
email: string;
}
const MyForm: React.FC<MyFormProps> = ({ initialValues = {}, onSubmit }) => {
const [name, setName] = useState<string>(initialValues.name ?? '');
const [age, setAge] = useState<number>(initialValues.age ?? 0);
const [email, setEmail] = useState<string>(initialValues.email ?? '');
const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
if (onSubmit) {
onSubmit({ name, age, email });
}
};
const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setName(event.target.value);
};
const handleAgeChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setAge(parseInt(event.target.value, 10));
};
const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setEmail(event.target.value);
};
return (
<Form layout="vertical" onSubmit={handleSubmit}>
<Form.Item label="Name">
<Input value={name} onChange={handleNameChange} />
</Form.Item>
<Form.Item label="Age">
<Input type="number" value={age} onChange={handleAgeChange} />
</Form.Item>
<Form.Item label="Email">
<Input type="email" value={email} onChange={handleEmailChange} />
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
);
};
export default MyForm;
```
接下来,您需要在 Form.Item 组件中使用 getFieldDecorator 来封装每个表单项,并使用 onChange 钩子来处理联动事件。例如:
```
import React from 'react';
import { Form, Input, Button } from 'antd';
interface MyFormProps {
initialValues?: MyFormValues;
onSubmit?: (values: MyFormValues) => void;
}
interface MyFormValues {
name: string;
age: number;
email: string;
}
const MyForm: React.FC<MyFormProps> = ({ initialValues = {}, onSubmit }) => {
const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
if (onSubmit) {
onSubmit(form.getFieldsValue());
}
};
const form = Form.useForm()[0];
const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
form.setFieldsValue({ name: event.target.value });
};
const handleAgeChange = (event: React.ChangeEvent<HTMLInputElement>) => {
form.setFieldsValue({ age: parseInt(event.target.value, 10) });
};
const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => {
form.setFieldsValue({ email: event.target.value });
};
return (
<Form layout="vertical" onSubmit={handleSubmit} form={form}>
<Form.Item label="Name">
{form.getFieldDecorator('name', {
initialValue: initialValues.name,
})(<Input onChange={handleNameChange} />)}
</Form.Item>
<Form.Item label="Age">
{form.getFieldDecorator('age', {
initialValue: initialValues.age,
})(<Input type="number" onChange={handleAgeChange} />)}
</Form.Item>
<Form.Item label="Email">
{form.getFieldDecorator('email', {
initialValue: initialValues.email,
})(<Input type="email" onChange={handleEmailChange} />)}
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
);
};
export default MyForm;
```
如上所示,您可以使用 getFieldDecorator 来封装每个表单项,并使用 initialValue 属性来设置初始值。在 onChange 钩子中,您可以使用 setFieldsValue 来联动更新其他表单项的值。
这就是使用 React Hooks 和 TypeScript 结合 Ant Design 进行表单组件的二次封装和联动的示例。希望对您有所帮助!
阅读全文