refactor: tree

pull/4577/head
tangjinzhou 3 years ago
parent 448788eaa5
commit 8c5a23f377

@ -1,4 +1,4 @@
import type { VNode, PropType, DefineComponent, ExtractPropTypes } from 'vue'; import type { VNode, PropType, DefineComponent, ExtractPropTypes, CSSProperties } from 'vue';
import { ref } from 'vue'; import { ref } from 'vue';
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
import classNames from '../_util/classNames'; import classNames from '../_util/classNames';
@ -16,7 +16,7 @@ import dropIndicatorRender from './utils/dropIndicator';
export interface AntdTreeNodeAttribute { export interface AntdTreeNodeAttribute {
eventKey: string; eventKey: string;
prefixCls: string; prefixCls: string;
className: string; class: string;
expanded: boolean; expanded: boolean;
selected: boolean; selected: boolean;
checked: boolean; checked: boolean;
@ -239,6 +239,7 @@ export default defineComponent({
onExpand={handleExpand} onExpand={handleExpand}
onSelect={handleSelect} onSelect={handleSelect}
v-slots={{ v-slots={{
...slots,
checkable: () => <span class={`${prefixCls.value}-checkbox-inner`} />, checkable: () => <span class={`${prefixCls.value}-checkbox-inner`} />,
}} }}
children={filterEmpty(slots.default?.())} children={filterEmpty(slots.default?.())}

@ -1046,13 +1046,14 @@ export default defineComponent({
onNodeDragLeave, onNodeDragLeave,
onNodeDragEnd, onNodeDragEnd,
onNodeDrop, onNodeDrop,
slots: slots,
}} }}
> >
<div <div
role="tree" role="tree"
class={classNames(prefixCls, className, { class={classNames(prefixCls, className, {
[`${prefixCls}-show-line`]: showLine, [`${prefixCls}-show-line`]: showLine,
[`${prefixCls}-focused`]: focused, [`${prefixCls}-focused`]: focused.value,
[`${prefixCls}-active-focused`]: activeKey.value !== null, [`${prefixCls}-active-focused`]: activeKey.value !== null,
})} })}
> >

@ -5,6 +5,7 @@ import { convertNodePropsToEventData } from './utils/treeUtil';
import { computed, defineComponent, getCurrentInstance, onMounted, onUpdated, ref } from 'vue'; import { computed, defineComponent, getCurrentInstance, onMounted, onUpdated, ref } from 'vue';
import { treeNodeProps } from './props'; import { treeNodeProps } from './props';
import classNames from '../_util/classNames'; import classNames from '../_util/classNames';
import { warning } from '../vc-util/warning';
const ICON_OPEN = 'open'; const ICON_OPEN = 'open';
const ICON_CLOSE = 'close'; const ICON_CLOSE = 'close';
@ -17,7 +18,11 @@ export default defineComponent({
props: treeNodeProps, props: treeNodeProps,
isTreeNode: 1, isTreeNode: 1,
slots: ['title', 'icon', 'switcherIcon', 'checkable'], slots: ['title', 'icon', 'switcherIcon', 'checkable'],
setup(props, { attrs, expose, slots }) { setup(props, { attrs, slots }) {
warning(
!('slots' in props.data),
'treeData slots is deprecated, please use `v-slot:icon` or `v-slot:title`, `v-slot:switcherIcon` instead',
);
const dragNodeHighlight = ref(false); const dragNodeHighlight = ref(false);
const context = useInjectTreeContext(); const context = useInjectTreeContext();
const instance = getCurrentInstance(); const instance = getCurrentInstance();
@ -198,7 +203,10 @@ export default defineComponent({
}; };
const renderSwitcherIconDom = (isLeaf: boolean) => { const renderSwitcherIconDom = (isLeaf: boolean) => {
const { switcherIcon: switcherIconFromProps = slots.switcherIcon } = props; const {
switcherIcon: switcherIconFromProps = slots.switcherIcon ||
context.value.slots?.[props.data?.slots?.switcherIcon],
} = props;
const { switcherIcon: switcherIconFromCtx } = context.value; const { switcherIcon: switcherIconFromCtx } = context.value;
const switcherIcon = switcherIconFromProps || switcherIconFromCtx; const switcherIcon = switcherIconFromProps || switcherIconFromCtx;
@ -327,7 +335,13 @@ export default defineComponent({
// Icon + Title // Icon + Title
const renderSelector = () => { const renderSelector = () => {
const { title = slots.title, selected, icon = slots.icon, loading, data } = props; const {
title = slots.title || context.value.slots?.[props.data?.slots?.title],
selected,
icon = slots.icon,
loading,
data,
} = props;
const { const {
prefixCls, prefixCls,
showIcon, showIcon,
@ -345,7 +359,7 @@ export default defineComponent({
let $icon; let $icon;
if (showIcon) { if (showIcon) {
const currentIcon = icon || treeIcon; const currentIcon = icon || context.value.slots?.[data?.slots?.icon] || treeIcon;
$icon = currentIcon ? ( $icon = currentIcon ? (
<span class={classNames(`${prefixCls}-iconEle`, `${prefixCls}-icon__customize`)}> <span class={classNames(`${prefixCls}-iconEle`, `${prefixCls}-icon__customize`)}>
@ -377,7 +391,7 @@ export default defineComponent({
class={classNames( class={classNames(
`${wrapClass}`, `${wrapClass}`,
`${wrapClass}-${nodeState.value || 'normal'}`, `${wrapClass}-${nodeState.value || 'normal'}`,
!disabled && (selected || dragNodeHighlight) && `${prefixCls}-node-selected`, !disabled && (selected || dragNodeHighlight.value) && `${prefixCls}-node-selected`,
!disabled && mergedDraggable && 'draggable', !disabled && mergedDraggable && 'draggable',
)} )}
draggable={(!disabled && mergedDraggable) || undefined} draggable={(!disabled && mergedDraggable) || undefined}

@ -80,6 +80,7 @@ export interface TreeContextProps {
onNodeDragLeave: NodeDragEventHandler; onNodeDragLeave: NodeDragEventHandler;
onNodeDragEnd: NodeDragEventHandler; onNodeDragEnd: NodeDragEventHandler;
onNodeDrop: NodeDragEventHandler; onNodeDrop: NodeDragEventHandler;
slots: Record<string, any>;
} }
const TreeContextKey: InjectionKey<ComputedRef<TreeContextProps>> = Symbol('TreeContextKey'); const TreeContextKey: InjectionKey<ComputedRef<TreeContextProps>> = Symbol('TreeContextKey');

@ -16,6 +16,7 @@ export interface DataNode {
/** Set style of TreeNode. This is not recommend if you don't have any force requirement */ /** Set style of TreeNode. This is not recommend if you don't have any force requirement */
class?: string; class?: string;
style?: CSSProperties; style?: CSSProperties;
slots?: Record<string, string>;
} }
export interface EventDataNode extends DataNode { export interface EventDataNode extends DataNode {

Loading…
Cancel
Save