Ant Design 是一个流行的React UI组件库,它提供了丰富的表单组件,用于快速构建用户界面。以下是一些常见的 Ant Design 表单问题及其解决方法的示例代码:
import { Form, Input, Button } from 'antd';
import { useState } from 'react';
const MyForm = () => {
const [form] = Form.useForm();
const [inputValue, setInputValue] = useState('');
const handleFormSubmit = () => {
const values = form.getFieldsValue();
setInputValue(values.input);
};
return (
输入值:{inputValue}
);
};
export default MyForm;
import { Form, Input, Button } from 'antd';
const MyForm = () => {
const onFinish = (values) => {
console.log('Success:', values);
};
const onFinishFailed = (errorInfo) => {
console.log('Failed:', errorInfo);
};
const validateInput = (rule, value, callback) => {
if (value && value.length < 6) {
callback('输入至少6个字符!');
} else {
callback();
}
};
return (
);
};
export default MyForm;
import { Form, Input, Button } from 'antd';
const MyForm = () => {
const [disableField, setDisableField] = useState(false);
const toggleDisableField = () => {
setDisableField(!disableField);
};
return (
);
};
export default MyForm;
这些示例提供了一些常见的 Ant Design 表单问题的解决方法,包括获取表单的输入值、动态校验表单字段和禁用表单字段。你可以根据自己的需求进行相应的修改和扩展。