2022-07-14 11:01:39 +08:00
|
|
|
import { useContext } from 'react';
|
|
|
|
import type { ValidateStatus } from 'antd/es/form/FormItem';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
import { devUseWarning } from '../../_util/warning';
|
2022-07-14 11:01:39 +08:00
|
|
|
import { FormItemInputContext } from '../context';
|
|
|
|
|
|
|
|
type UseFormItemStatus = () => {
|
|
|
|
status?: ValidateStatus;
|
2023-03-31 11:42:34 +08:00
|
|
|
errors: React.ReactNode[];
|
|
|
|
warnings: React.ReactNode[];
|
2022-07-14 11:01:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const useFormItemStatus: UseFormItemStatus = () => {
|
2023-03-31 11:42:34 +08:00
|
|
|
const { status, errors = [], warnings = [] } = useContext(FormItemInputContext);
|
2022-07-14 11:01:39 +08:00
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Form.Item');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
warning(
|
|
|
|
status !== undefined,
|
|
|
|
'usage',
|
|
|
|
'Form.Item.useStatus should be used under Form.Item component. For more information: https://u.ant.design/form-item-usestatus',
|
|
|
|
);
|
|
|
|
}
|
2022-07-14 11:01:39 +08:00
|
|
|
|
2023-03-31 11:42:34 +08:00
|
|
|
return { status, errors, warnings };
|
2022-07-14 11:01:39 +08:00
|
|
|
};
|
|
|
|
|
2022-11-22 11:40:17 +08:00
|
|
|
// Only used for compatible package. Not promise this will work on future version.
|
|
|
|
(useFormItemStatus as any).Context = FormItemInputContext;
|
|
|
|
|
2022-07-14 11:01:39 +08:00
|
|
|
export default useFormItemStatus;
|