mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-29 13:47:02 +08:00
37e042358d
* feat: remove form status style && input support status * test: update snapshot * feat: update status prop in config provider * fix: form item validateStatus support * chore: code clean * feat: status classname change * test: update snapshot * refactor: move formItemStatusContext to form folder * refactor: merge utils * refactor: rename statusUtils * chore: code clean * test: fix coverage * chore: remove status prop of Form.Item * chore: code clean * docs: update demo * test: fix lint * feat: status only success and warning * test: fix lint * docs: update deamo
1.8 KiB
1.8 KiB
order | title | ||||
---|---|---|---|---|---|
19 |
|
zh-CN
Form
具有自动收集数据并校验的功能,但如果您不需要这个功能,或者默认的行为无法满足业务需求,可以选择自行处理数据。
en-US
Form
will collect and validate form data automatically. But if you don't need this feature or the default behavior cannot satisfy your business, you can handle form data manually.
import React, { useState } from 'react';
import { Form, InputNumber } from 'antd';
type ValidateStatus = Parameters<typeof Form.Item>[0]['validateStatus'];
function validatePrimeNumber(number: number): {
validateStatus: ValidateStatus;
errorMsg: string | null;
} {
if (number === 11) {
return {
validateStatus: 'success',
errorMsg: null,
};
}
return {
validateStatus: 'error',
errorMsg: 'The prime between 8 and 12 is 11!',
};
}
const formItemLayout = {
labelCol: { span: 7 },
wrapperCol: { span: 12 },
};
const RawForm = () => {
const [number, setNumber] = useState<{
value: number;
validateStatus?: ValidateStatus;
errorMsg?: string | null;
}>({
value: 11,
});
const tips =
'A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself.';
const onNumberChange = (value: number) => {
setNumber({
...validatePrimeNumber(value),
value,
});
};
return (
<Form>
<Form.Item
{...formItemLayout}
label="Prime between 8 & 12"
validateStatus={number.validateStatus}
help={number.errorMsg || tips}
>
<InputNumber min={8} max={12} value={number.value} onChange={onNumberChange} />
</Form.Item>
</Form>
);
};
ReactDOM.render(<RawForm />, mountNode);