2021-06-07 17:35:03 +08:00
|
|
|
import { useInjectFormItemPrefix } from './context';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { VueNode } from '../_util/type';
|
2021-06-26 14:27:26 +08:00
|
|
|
import { defineComponent, onBeforeUnmount, ref, watch } from 'vue';
|
2021-06-07 17:35:03 +08:00
|
|
|
import classNames from '../_util/classNames';
|
|
|
|
import Transition, { getTransitionProps } from '../_util/transition';
|
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
|
|
|
|
export interface ErrorListProps {
|
|
|
|
errors?: VueNode[];
|
|
|
|
/** @private Internal Usage. Do not use in your production */
|
|
|
|
help?: VueNode;
|
|
|
|
/** @private Internal Usage. Do not use in your production */
|
|
|
|
onDomErrorVisibleChange?: (visible: boolean) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'ErrorList',
|
|
|
|
props: ['errors', 'help', 'onDomErrorVisibleChange'],
|
|
|
|
setup(props) {
|
|
|
|
const { prefixCls: rootPrefixCls } = useConfigInject('', props);
|
|
|
|
const { prefixCls, status } = useInjectFormItemPrefix();
|
|
|
|
const visible = ref(!!(props.errors && props.errors.length));
|
|
|
|
const innerStatus = ref(status.value);
|
2021-06-11 22:06:25 +08:00
|
|
|
const timeout = ref();
|
2021-06-07 17:35:03 +08:00
|
|
|
const cacheErrors = ref([...props.errors]);
|
|
|
|
watch([() => [...props.errors], () => props.help], newValues => {
|
2021-11-29 17:53:09 +08:00
|
|
|
clearTimeout(timeout.value);
|
2021-06-07 17:35:03 +08:00
|
|
|
if (props.help) {
|
|
|
|
visible.value = !!(props.errors && props.errors.length);
|
|
|
|
if (visible.value) {
|
|
|
|
cacheErrors.value = newValues[0];
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-29 17:53:09 +08:00
|
|
|
timeout.value = setTimeout(() => {
|
2021-06-07 17:35:03 +08:00
|
|
|
visible.value = !!(props.errors && props.errors.length);
|
|
|
|
if (visible.value) {
|
|
|
|
cacheErrors.value = newValues[0];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
2021-11-29 17:53:09 +08:00
|
|
|
clearTimeout(timeout.value);
|
2021-06-07 17:35:03 +08:00
|
|
|
});
|
|
|
|
// Memo status in same visible
|
|
|
|
watch([visible, status], () => {
|
2021-12-10 15:52:13 +08:00
|
|
|
if (visible.value) {
|
2021-06-07 17:35:03 +08:00
|
|
|
innerStatus.value = status.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
watch(
|
|
|
|
visible,
|
|
|
|
() => {
|
|
|
|
if (visible.value) {
|
|
|
|
props.onDomErrorVisibleChange?.(true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ immediate: true, flush: 'post' },
|
|
|
|
);
|
|
|
|
return () => {
|
|
|
|
const baseClassName = `${prefixCls.value}-item-explain`;
|
|
|
|
const transitionProps = getTransitionProps(`${rootPrefixCls.value}-show-help`, {
|
|
|
|
onAfterLeave: () => {
|
|
|
|
props.onDomErrorVisibleChange?.(false);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<Transition {...transitionProps}>
|
|
|
|
{visible.value ? (
|
|
|
|
<div
|
|
|
|
class={classNames(baseClassName, {
|
|
|
|
[`${baseClassName}-${innerStatus.value}`]: innerStatus.value,
|
|
|
|
})}
|
|
|
|
key="help"
|
|
|
|
>
|
|
|
|
{cacheErrors.value?.map((error: any, index: number) => (
|
|
|
|
<div key={index} role="alert">
|
|
|
|
{error}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</Transition>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|