2024-04-08 14:04:08 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { useMemo } from 'react';
|
2020-09-11 21:27:51 +08:00
|
|
|
import classNames from 'classnames';
|
2023-07-25 19:47:05 +08:00
|
|
|
import type { CSSMotionProps } from 'rc-motion';
|
2021-06-04 14:44:41 +08:00
|
|
|
import CSSMotion, { CSSMotionList } from 'rc-motion';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
2022-07-19 22:53:38 +08:00
|
|
|
import initCollapseMotion from '../_util/motion';
|
2024-04-08 14:04:08 +08:00
|
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
2023-05-05 20:52:44 +08:00
|
|
|
import { FormItemPrefixContext } from './context';
|
2023-05-06 15:49:37 +08:00
|
|
|
import type { ValidateStatus } from './FormItem';
|
2022-07-04 13:32:48 +08:00
|
|
|
import useDebounce from './hooks/useDebounce';
|
2022-11-24 21:43:35 +08:00
|
|
|
import useStyle from './style';
|
|
|
|
|
2020-09-11 21:27:51 +08:00
|
|
|
const EMPTY_LIST: React.ReactNode[] = [];
|
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
interface ErrorEntity {
|
|
|
|
error: React.ReactNode;
|
|
|
|
errorStatus?: ValidateStatus;
|
|
|
|
key: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toErrorEntity(
|
|
|
|
error: React.ReactNode,
|
|
|
|
prefix: string,
|
2023-01-16 16:44:45 +08:00
|
|
|
errorStatus?: ValidateStatus,
|
2024-06-22 21:59:12 +08:00
|
|
|
index = 0,
|
2021-06-04 14:44:41 +08:00
|
|
|
): ErrorEntity {
|
|
|
|
return {
|
|
|
|
key: typeof error === 'string' ? error : `${prefix}-${index}`,
|
|
|
|
error,
|
|
|
|
errorStatus,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-11 21:27:51 +08:00
|
|
|
export interface ErrorListProps {
|
2022-08-24 17:07:50 +08:00
|
|
|
fieldId?: string;
|
2020-09-11 21:27:51 +08:00
|
|
|
help?: React.ReactNode;
|
2021-06-04 14:44:41 +08:00
|
|
|
helpStatus?: ValidateStatus;
|
|
|
|
errors?: React.ReactNode[];
|
|
|
|
warnings?: React.ReactNode[];
|
2021-06-07 14:32:07 +08:00
|
|
|
className?: string;
|
2022-07-19 16:01:31 +08:00
|
|
|
onVisibleChanged?: (visible: boolean) => void;
|
2020-09-11 21:27:51 +08:00
|
|
|
}
|
|
|
|
|
2023-07-25 19:47:05 +08:00
|
|
|
const ErrorList: React.FC<ErrorListProps> = ({
|
2020-09-11 21:27:51 +08:00
|
|
|
help,
|
2021-06-04 14:44:41 +08:00
|
|
|
helpStatus,
|
|
|
|
errors = EMPTY_LIST,
|
|
|
|
warnings = EMPTY_LIST,
|
2021-06-07 14:32:07 +08:00
|
|
|
className: rootClassName,
|
2022-08-24 17:07:50 +08:00
|
|
|
fieldId,
|
2022-07-19 16:01:31 +08:00
|
|
|
onVisibleChanged,
|
2023-07-25 19:47:05 +08:00
|
|
|
}) => {
|
2021-06-04 14:44:41 +08:00
|
|
|
const { prefixCls } = React.useContext(FormItemPrefixContext);
|
2020-09-11 21:27:51 +08:00
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
const baseClassName = `${prefixCls}-item-explain`;
|
2020-09-11 21:27:51 +08:00
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const rootCls = useCSSVarCls(prefixCls);
|
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
|
2022-11-24 21:43:35 +08:00
|
|
|
|
2023-07-25 19:47:05 +08:00
|
|
|
const collapseMotion: CSSMotionProps = useMemo(() => initCollapseMotion(prefixCls), [prefixCls]);
|
2022-07-19 22:53:38 +08:00
|
|
|
|
2022-07-04 13:32:48 +08:00
|
|
|
// We have to debounce here again since somewhere use ErrorList directly still need no shaking
|
|
|
|
// ref: https://github.com/ant-design/ant-design/issues/36336
|
|
|
|
const debounceErrors = useDebounce(errors);
|
|
|
|
const debounceWarnings = useDebounce(warnings);
|
|
|
|
|
2023-01-16 16:44:45 +08:00
|
|
|
const fullKeyList = React.useMemo<ErrorEntity[]>(() => {
|
2021-06-04 14:44:41 +08:00
|
|
|
if (help !== undefined && help !== null) {
|
2023-01-16 16:44:45 +08:00
|
|
|
return [toErrorEntity(help, 'help', helpStatus)];
|
2020-09-11 21:27:51 +08:00
|
|
|
}
|
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
return [
|
2022-07-04 13:32:48 +08:00
|
|
|
...debounceErrors.map((error, index) => toErrorEntity(error, 'error', 'error', index)),
|
|
|
|
...debounceWarnings.map((warning, index) =>
|
|
|
|
toErrorEntity(warning, 'warning', 'warning', index),
|
|
|
|
),
|
2021-06-04 14:44:41 +08:00
|
|
|
];
|
2022-07-04 13:32:48 +08:00
|
|
|
}, [help, helpStatus, debounceErrors, debounceWarnings]);
|
2020-09-11 21:27:51 +08:00
|
|
|
|
2024-11-14 17:09:23 +08:00
|
|
|
const filledKeyFullKeyList = React.useMemo<ErrorEntity[]>(() => {
|
|
|
|
const keysCount: Record<string, number> = {};
|
|
|
|
fullKeyList.forEach(({ key }) => {
|
|
|
|
keysCount[key] = (keysCount[key] || 0) + 1;
|
|
|
|
});
|
|
|
|
return fullKeyList.map((entity, index) => ({
|
|
|
|
...entity,
|
|
|
|
key: keysCount[entity.key] > 1 ? `${entity.key}-fallback-${index}` : entity.key,
|
|
|
|
}));
|
|
|
|
}, [fullKeyList]);
|
|
|
|
|
2022-08-24 17:07:50 +08:00
|
|
|
const helpProps: { id?: string } = {};
|
|
|
|
|
|
|
|
if (fieldId) {
|
|
|
|
helpProps.id = `${fieldId}_help`;
|
|
|
|
}
|
|
|
|
|
2023-11-14 10:08:34 +08:00
|
|
|
return wrapCSSVar(
|
2020-09-11 21:27:51 +08:00
|
|
|
<CSSMotion
|
2022-07-19 16:01:31 +08:00
|
|
|
motionDeadline={collapseMotion.motionDeadline}
|
2022-11-24 21:43:35 +08:00
|
|
|
motionName={`${prefixCls}-show-help`}
|
2024-11-14 17:09:23 +08:00
|
|
|
visible={!!filledKeyFullKeyList.length}
|
2022-07-19 16:01:31 +08:00
|
|
|
onVisibleChanged={onVisibleChanged}
|
2020-09-11 21:27:51 +08:00
|
|
|
>
|
2022-11-19 13:47:33 +08:00
|
|
|
{(holderProps) => {
|
2021-06-04 14:44:41 +08:00
|
|
|
const { className: holderClassName, style: holderStyle } = holderProps;
|
|
|
|
|
|
|
|
return (
|
2021-06-07 14:32:07 +08:00
|
|
|
<div
|
2022-08-24 17:07:50 +08:00
|
|
|
{...helpProps}
|
2023-12-14 14:58:53 +08:00
|
|
|
className={classNames(
|
|
|
|
baseClassName,
|
|
|
|
holderClassName,
|
|
|
|
cssVarCls,
|
|
|
|
rootCls,
|
|
|
|
rootClassName,
|
|
|
|
hashId,
|
|
|
|
)}
|
2021-06-07 14:32:07 +08:00
|
|
|
style={holderStyle}
|
2022-08-24 17:07:50 +08:00
|
|
|
role="alert"
|
2021-06-07 14:32:07 +08:00
|
|
|
>
|
2021-06-04 14:44:41 +08:00
|
|
|
<CSSMotionList
|
2024-11-14 17:09:23 +08:00
|
|
|
keys={filledKeyFullKeyList}
|
2022-11-24 21:43:35 +08:00
|
|
|
{...initCollapseMotion(prefixCls)}
|
|
|
|
motionName={`${prefixCls}-show-help-item`}
|
2021-06-04 14:44:41 +08:00
|
|
|
component={false}
|
|
|
|
>
|
2022-11-19 13:47:33 +08:00
|
|
|
{(itemProps) => {
|
2021-06-04 14:44:41 +08:00
|
|
|
const {
|
|
|
|
key,
|
|
|
|
error,
|
|
|
|
errorStatus,
|
|
|
|
className: itemClassName,
|
|
|
|
style: itemStyle,
|
|
|
|
} = itemProps;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key={key}
|
|
|
|
className={classNames(itemClassName, {
|
|
|
|
[`${baseClassName}-${errorStatus}`]: errorStatus,
|
|
|
|
})}
|
|
|
|
style={itemStyle}
|
|
|
|
>
|
|
|
|
{error}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</CSSMotionList>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
2023-11-14 10:08:34 +08:00
|
|
|
</CSSMotion>,
|
2020-09-11 21:27:51 +08:00
|
|
|
);
|
2023-07-25 19:47:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ErrorList;
|