ant-design-vue/components/vc-tree/MotionTreeNode.tsx

127 lines
3.8 KiB
Vue
Raw Normal View History

2021-08-17 06:36:46 +00:00
import TreeNode from './TreeNode';
2021-08-17 14:05:24 +00:00
import type { FlattenNode } from './interface';
import type { TreeNodeRequiredProps } from './utils/treeUtil';
import { getTreeNodeProps } from './utils/treeUtil';
2021-08-17 06:36:46 +00:00
import { useInjectTreeContext } from './contextTypes';
2021-08-19 15:29:07 +00:00
import { computed, getCurrentInstance, nextTick, PropType } from 'vue';
2021-08-17 14:05:24 +00:00
import { defineComponent, onBeforeUnmount, onMounted, ref, Transition, watch } from 'vue';
2021-08-17 06:36:46 +00:00
import { treeNodeProps } from './props';
2021-08-19 15:29:07 +00:00
import { collapseMotion } from '../_util/transition';
2021-08-17 06:36:46 +00:00
export default defineComponent({
name: 'MotionTreeNode',
inheritAttrs: false,
props: {
...treeNodeProps,
active: Boolean,
motion: Object,
motionNodes: { type: Array as PropType<FlattenNode[]> },
onMotionStart: Function,
onMotionEnd: Function,
motionType: String,
treeNodeRequiredProps: { type: Object as PropType<TreeNodeRequiredProps> },
},
2021-08-17 14:03:49 +00:00
slots: ['title', 'icon', 'switcherIcon', 'checkable'],
2021-08-17 06:36:46 +00:00
setup(props, { attrs, slots }) {
const visible = ref(true);
const context = useInjectTreeContext();
const motionedRef = ref(false);
2021-08-19 15:29:07 +00:00
const transitionClass = ref('');
const transitionStyle = ref({});
const transitionProps = computed(() => {
if (props.motion) {
return props.motion;
} else {
return collapseMotion(transitionStyle, transitionClass);
}
});
const onMotionEnd = (type?: 'appear' | 'leave') => {
if (type === 'appear') {
transitionProps.value?.onAfterAppear?.();
} else if (type === 'leave') {
transitionProps.value?.onAfterLeave?.();
}
2021-08-17 06:36:46 +00:00
if (!motionedRef.value) {
props.onMotionEnd();
}
motionedRef.value = true;
};
watch(
() => props.motionNodes,
() => {
if (props.motionNodes && props.motionType === 'hide' && visible.value) {
2021-08-19 15:29:07 +00:00
nextTick(() => {
visible.value = false;
});
2021-08-17 06:36:46 +00:00
}
},
2021-08-19 15:29:07 +00:00
{ immediate: true, flush: 'post' },
2021-08-17 06:36:46 +00:00
);
onMounted(() => {
props.motionNodes && props.onMotionStart();
});
onBeforeUnmount(() => {
props.motionNodes && onMotionEnd();
});
2021-08-19 15:29:07 +00:00
2021-08-17 06:36:46 +00:00
return () => {
const { motion, motionNodes, motionType, active, treeNodeRequiredProps, ...otherProps } =
props;
if (motionNodes) {
return (
<Transition
2021-08-19 15:29:07 +00:00
{...transitionProps.value}
2021-08-17 06:36:46 +00:00
appear={motionType === 'show'}
2021-08-19 15:29:07 +00:00
onAfterAppear={() => onMotionEnd('appear')}
onAfterLeave={() => onMotionEnd('leave')}
2021-08-17 06:36:46 +00:00
>
2021-08-19 15:29:07 +00:00
<div
v-show={visible.value}
class={[`${context.value.prefixCls}-treenode-motion`, transitionClass.value]}
style={transitionStyle.value}
>
2021-08-17 06:36:46 +00:00
{motionNodes.map((treeNode: FlattenNode) => {
const {
data: { ...restProps },
title,
key,
isStart,
isEnd,
} = treeNode;
delete restProps.children;
const treeNodeProps = getTreeNodeProps(key, treeNodeRequiredProps);
return (
<TreeNode
v-slots={slots}
{...restProps}
{...treeNodeProps}
title={title}
active={active}
data={treeNode.data}
key={key}
isStart={isStart}
isEnd={isEnd}
/>
);
})}
</div>
</Transition>
);
}
return (
<TreeNode
v-slots={slots}
domRef={ref}
class={attrs.class}
style={attrs.style}
{...otherProps}
active={active}
/>
);
};
},
});