ant-design/components/form/hooks/useFormItemStatus.ts
JiaQi b73b37eee9
feat(Form.Item): useStatus supports get error messages and warning messages (#41554)
* feat: useStatus supports get error messages

* feat: useStatus supports get warning messages

* chore: update example

* chore: add test case
2023-03-31 11:42:34 +08:00

28 lines
865 B
TypeScript

import { useContext } from 'react';
import type { ValidateStatus } from 'antd/es/form/FormItem';
import { FormItemInputContext } from '../context';
import warning from '../../_util/warning';
type UseFormItemStatus = () => {
status?: ValidateStatus;
errors: React.ReactNode[];
warnings: React.ReactNode[];
};
const useFormItemStatus: UseFormItemStatus = () => {
const { status, errors = [], warnings = [] } = useContext(FormItemInputContext);
warning(
status !== undefined,
'Form.Item',
'Form.Item.useStatus should be used under Form.Item component. For more information: https://u.ant.design/form-item-usestatus',
);
return { status, errors, warnings };
};
// Only used for compatible package. Not promise this will work on future version.
(useFormItemStatus as any).Context = FormItemInputContext;
export default useFormItemStatus;