2021-06-07 09:35:03 +00:00
|
|
|
import { useInjectFormItemPrefix } from './context';
|
2021-06-26 01:35:40 +00:00
|
|
|
import type { VueNode } from '../_util/type';
|
2022-03-12 01:56:32 +00:00
|
|
|
import { computed, defineComponent, ref, watch } from 'vue';
|
|
|
|
import { getTransitionGroupProps, TransitionGroup } from '../_util/transition';
|
2021-06-07 09:35:03 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
2022-03-12 01:56:32 +00:00
|
|
|
import collapseMotion from '../_util/collapseMotion';
|
2021-06-07 09:35:03 +00:00
|
|
|
|
|
|
|
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',
|
2022-03-12 01:56:32 +00:00
|
|
|
props: ['errors', 'help', 'onDomErrorVisibleChange', 'helpStatus', 'warnings'],
|
2021-06-07 09:35:03 +00:00
|
|
|
setup(props) {
|
|
|
|
const { prefixCls: rootPrefixCls } = useConfigInject('', props);
|
|
|
|
const { prefixCls, status } = useInjectFormItemPrefix();
|
2022-03-12 01:56:32 +00:00
|
|
|
const baseClassName = computed(() => `${prefixCls.value}-item-explain`);
|
|
|
|
const visible = computed(() => !!(props.errors && props.errors.length));
|
2021-06-07 09:35:03 +00:00
|
|
|
const innerStatus = ref(status.value);
|
2022-03-12 01:56:32 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
// Memo status in same visible
|
|
|
|
watch([visible, status], () => {
|
2021-12-10 07:52:13 +00:00
|
|
|
if (visible.value) {
|
2021-06-07 09:35:03 +00:00
|
|
|
innerStatus.value = status.value;
|
|
|
|
}
|
|
|
|
});
|
2022-03-12 01:56:32 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
return () => {
|
2022-03-12 01:56:32 +00:00
|
|
|
const colMItem = collapseMotion(`${rootPrefixCls.value}-show-help-item`);
|
|
|
|
const transitionGroupProps = getTransitionGroupProps(
|
|
|
|
`${rootPrefixCls.value}-show-help-item`,
|
|
|
|
colMItem,
|
|
|
|
);
|
|
|
|
(transitionGroupProps as any).class = baseClassName.value;
|
2022-03-15 06:34:25 +00:00
|
|
|
return props.errors?.length ? (
|
2022-03-12 01:56:32 +00:00
|
|
|
<TransitionGroup {...transitionGroupProps} tag="div">
|
|
|
|
{props.errors?.map((error: any, index: number) => (
|
2021-06-07 09:35:03 +00:00
|
|
|
<div
|
2022-03-12 01:56:32 +00:00
|
|
|
key={index}
|
|
|
|
role="alert"
|
|
|
|
class={innerStatus.value ? `${baseClassName.value}-${innerStatus.value}` : ''}
|
2021-06-07 09:35:03 +00:00
|
|
|
>
|
2022-03-12 01:56:32 +00:00
|
|
|
{error}
|
2021-06-07 09:35:03 +00:00
|
|
|
</div>
|
2022-03-12 01:56:32 +00:00
|
|
|
))}
|
|
|
|
</TransitionGroup>
|
2022-03-15 06:34:25 +00:00
|
|
|
) : null;
|
2021-06-07 09:35:03 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|