2021-09-25 08:51:32 +00:00
|
|
|
|
import type { PropType, ExtractPropTypes } from 'vue';
|
2021-10-29 08:47:25 +00:00
|
|
|
|
import { watchEffect, ref, defineComponent, computed } from 'vue';
|
2020-08-31 08:53:19 +00:00
|
|
|
|
import classNames from '../_util/classNames';
|
2021-11-10 02:06:59 +00:00
|
|
|
|
import VcTree from '../vc-tree';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import PropTypes from '../_util/vue-types';
|
2021-09-25 08:51:32 +00:00
|
|
|
|
import { filterEmpty } from '../_util/props-util';
|
2020-10-22 06:25:02 +00:00
|
|
|
|
import initDefaultProps from '../_util/props-util/initDefaultProps';
|
2022-05-21 05:17:26 +00:00
|
|
|
|
import type { DataNode, EventDataNode, FieldNames, Key, ScrollTo } from '../vc-tree/interface';
|
2021-09-25 08:51:32 +00:00
|
|
|
|
import type { TreeNodeProps } from '../vc-tree/props';
|
|
|
|
|
import { treeProps as vcTreeProps } from '../vc-tree/props';
|
|
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
2022-04-27 14:37:22 +00:00
|
|
|
|
import type { SwitcherIconProps } from './utils/iconUtil';
|
2021-09-25 08:51:32 +00:00
|
|
|
|
import renderSwitcherIcon from './utils/iconUtil';
|
|
|
|
|
import dropIndicatorRender from './utils/dropIndicator';
|
|
|
|
|
import devWarning from '../vc-util/devWarning';
|
|
|
|
|
import { warning } from '../vc-util/warning';
|
2022-03-26 14:52:54 +00:00
|
|
|
|
import omit from '../_util/omit';
|
2018-09-26 14:57:01 +00:00
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
|
export interface AntdTreeNodeAttribute {
|
|
|
|
|
eventKey: string;
|
|
|
|
|
prefixCls: string;
|
|
|
|
|
class: string;
|
|
|
|
|
expanded: boolean;
|
|
|
|
|
selected: boolean;
|
|
|
|
|
checked: boolean;
|
|
|
|
|
halfChecked: boolean;
|
|
|
|
|
children: any;
|
|
|
|
|
title: any;
|
|
|
|
|
pos: string;
|
|
|
|
|
dragOver: boolean;
|
|
|
|
|
dragOverGapTop: boolean;
|
|
|
|
|
dragOverGapBottom: boolean;
|
|
|
|
|
isLeaf: boolean;
|
|
|
|
|
selectable: boolean;
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
disableCheckbox: boolean;
|
2020-11-18 09:37:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
|
export type AntTreeNodeProps = TreeNodeProps;
|
|
|
|
|
|
|
|
|
|
// [Legacy] Compatible for v2
|
|
|
|
|
export type TreeDataItem = DataNode;
|
|
|
|
|
|
|
|
|
|
export interface AntTreeNodeBaseEvent {
|
|
|
|
|
node: EventDataNode;
|
2020-11-18 09:37:15 +00:00
|
|
|
|
nativeEvent: MouseEvent;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
|
export interface AntTreeNodeCheckedEvent extends AntTreeNodeBaseEvent {
|
|
|
|
|
event: 'check';
|
|
|
|
|
checked?: boolean;
|
|
|
|
|
checkedNodes?: DataNode[];
|
2020-11-18 09:37:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
|
export interface AntTreeNodeSelectedEvent extends AntTreeNodeBaseEvent {
|
|
|
|
|
event: 'select';
|
|
|
|
|
selected?: boolean;
|
|
|
|
|
selectedNodes?: DataNode[];
|
2020-11-18 09:37:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
|
export interface AntTreeNodeExpandedEvent extends AntTreeNodeBaseEvent {
|
|
|
|
|
expanded?: boolean;
|
2021-01-26 15:48:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
|
export interface AntTreeNodeMouseEvent {
|
|
|
|
|
node: EventDataNode;
|
2021-01-26 15:48:48 +00:00
|
|
|
|
event: DragEvent;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
|
export interface AntTreeNodeDragEnterEvent extends AntTreeNodeMouseEvent {
|
|
|
|
|
expandedKeys: Key[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AntTreeNodeDropEvent {
|
|
|
|
|
node: EventDataNode;
|
|
|
|
|
dragNode: EventDataNode;
|
|
|
|
|
dragNodesKeys: Key[];
|
2021-01-26 15:48:48 +00:00
|
|
|
|
dropPosition: number;
|
2021-09-25 08:51:32 +00:00
|
|
|
|
dropToGap?: boolean;
|
|
|
|
|
event: MouseEvent;
|
2020-11-18 09:37:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
|
export const treeProps = () => {
|
2022-03-26 14:52:54 +00:00
|
|
|
|
const baseTreeProps = vcTreeProps();
|
2018-09-26 14:57:01 +00:00
|
|
|
|
return {
|
2022-03-26 14:52:54 +00:00
|
|
|
|
...baseTreeProps,
|
2022-01-21 13:58:10 +00:00
|
|
|
|
showLine: {
|
|
|
|
|
type: [Boolean, Object] as PropType<boolean | { showLeafIcon: boolean }>,
|
|
|
|
|
default: undefined,
|
|
|
|
|
},
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 是否支持多选 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
multiple: { type: Boolean, default: undefined },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 是否自动展开父节点 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
autoExpandParent: { type: Boolean, default: undefined },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** checkable状态下节点选择完全受控(父子节点选中状态不再关联)*/
|
2021-09-25 08:51:32 +00:00
|
|
|
|
checkStrictly: { type: Boolean, default: undefined },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 是否支持选中 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
checkable: { type: Boolean, default: undefined },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 是否禁用树 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
disabled: { type: Boolean, default: undefined },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 默认展开所有树节点 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
defaultExpandAll: { type: Boolean, default: undefined },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 默认展开对应树节点 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
defaultExpandParent: { type: Boolean, default: undefined },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 默认展开指定的树节点 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
defaultExpandedKeys: { type: Array as PropType<Key[]> },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** (受控)展开指定的树节点 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
expandedKeys: { type: Array as PropType<Key[]> },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** (受控)选中复选框的树节点 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
checkedKeys: {
|
|
|
|
|
type: [Array, Object] as PropType<Key[] | { checked: Key[]; halfChecked: Key[] }>,
|
|
|
|
|
},
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 默认选中复选框的树节点 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
defaultCheckedKeys: { type: Array as PropType<Key[]> },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** (受控)设置选中的树节点 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
selectedKeys: { type: Array as PropType<Key[]> },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 默认选中的树节点 */
|
2021-09-25 08:51:32 +00:00
|
|
|
|
defaultSelectedKeys: { type: Array as PropType<Key[]> },
|
|
|
|
|
selectable: { type: Boolean, default: undefined },
|
2020-08-12 10:07:41 +00:00
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
|
loadedKeys: { type: Array as PropType<Key[]> },
|
|
|
|
|
draggable: { type: Boolean, default: undefined },
|
|
|
|
|
showIcon: { type: Boolean, default: undefined },
|
|
|
|
|
icon: { type: Function as PropType<(nodeProps: AntdTreeNodeAttribute) => any> },
|
2019-04-10 05:28:07 +00:00
|
|
|
|
switcherIcon: PropTypes.any,
|
2022-03-26 14:52:54 +00:00
|
|
|
|
prefixCls: String,
|
2019-11-15 07:05:38 +00:00
|
|
|
|
/**
|
|
|
|
|
* @default{title,key,children}
|
2021-09-25 08:51:32 +00:00
|
|
|
|
* deprecated, please use `fieldNames` instead
|
2019-11-15 07:05:38 +00:00
|
|
|
|
* 替换treeNode中 title,key,children字段为treeData中对应的字段
|
|
|
|
|
*/
|
2021-09-25 08:51:32 +00:00
|
|
|
|
replaceFields: { type: Object as PropType<FieldNames> },
|
|
|
|
|
blockNode: { type: Boolean, default: undefined },
|
|
|
|
|
openAnimation: PropTypes.any,
|
2022-03-26 14:52:54 +00:00
|
|
|
|
onDoubleclick: baseTreeProps.onDblclick,
|
|
|
|
|
'onUpdate:selectedKeys': Function as PropType<(keys: Key[]) => void>,
|
|
|
|
|
'onUpdate:checkedKeys': Function as PropType<(keys: Key[]) => void>,
|
|
|
|
|
'onUpdate:expandedKeys': Function as PropType<(keys: Key[]) => void>,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2021-09-25 08:51:32 +00:00
|
|
|
|
};
|
2018-09-26 14:57:01 +00:00
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
|
export type TreeProps = Partial<ExtractPropTypes<ReturnType<typeof treeProps>>>;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
|
2020-10-22 06:25:02 +00:00
|
|
|
|
export default defineComponent({
|
2022-09-26 13:33:41 +00:00
|
|
|
|
compatConfig: { MODE: 3 },
|
2018-09-26 14:57:01 +00:00
|
|
|
|
name: 'ATree',
|
2020-07-16 10:31:20 +00:00
|
|
|
|
inheritAttrs: false,
|
2021-09-25 08:51:32 +00:00
|
|
|
|
props: initDefaultProps(treeProps(), {
|
2018-09-26 14:57:01 +00:00
|
|
|
|
checkable: false,
|
2021-09-25 08:51:32 +00:00
|
|
|
|
selectable: true,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
showIcon: false,
|
2020-03-07 11:45:13 +00:00
|
|
|
|
blockNode: false,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}),
|
2021-09-25 08:51:32 +00:00
|
|
|
|
slots: ['icon', 'title', 'switcherIcon', 'titleRender'],
|
2022-03-26 14:52:54 +00:00
|
|
|
|
// emits: [
|
|
|
|
|
// 'update:selectedKeys',
|
|
|
|
|
// 'update:checkedKeys',
|
|
|
|
|
// 'update:expandedKeys',
|
|
|
|
|
// 'expand',
|
|
|
|
|
// 'select',
|
|
|
|
|
// 'check',
|
|
|
|
|
// 'doubleclick',
|
|
|
|
|
// 'dblclick',
|
|
|
|
|
// ],
|
2021-09-25 08:51:32 +00:00
|
|
|
|
setup(props, { attrs, expose, emit, slots }) {
|
|
|
|
|
warning(
|
|
|
|
|
!(props.treeData === undefined && slots.default),
|
|
|
|
|
'`children` of Tree is deprecated. Please use `treeData` instead.',
|
|
|
|
|
);
|
|
|
|
|
const { prefixCls, direction, virtual } = useConfigInject('tree', props);
|
|
|
|
|
const treeRef = ref();
|
2022-05-21 05:17:26 +00:00
|
|
|
|
const scrollTo: ScrollTo = scroll => {
|
|
|
|
|
treeRef.value?.scrollTo(scroll);
|
|
|
|
|
};
|
2021-09-25 08:51:32 +00:00
|
|
|
|
expose({
|
|
|
|
|
treeRef,
|
|
|
|
|
onNodeExpand: (...args) => {
|
|
|
|
|
treeRef.value?.onNodeExpand(...args);
|
|
|
|
|
},
|
2022-05-21 05:17:26 +00:00
|
|
|
|
scrollTo,
|
2021-10-29 08:47:25 +00:00
|
|
|
|
selectedKeys: computed(() => treeRef.value?.selectedKeys),
|
|
|
|
|
checkedKeys: computed(() => treeRef.value?.checkedKeys),
|
|
|
|
|
halfCheckedKeys: computed(() => treeRef.value?.halfCheckedKeys),
|
|
|
|
|
loadedKeys: computed(() => treeRef.value?.loadedKeys),
|
|
|
|
|
loadingKeys: computed(() => treeRef.value?.loadingKeys),
|
|
|
|
|
expandedKeys: computed(() => treeRef.value?.expandedKeys),
|
2021-09-25 08:51:32 +00:00
|
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
|
watchEffect(() => {
|
|
|
|
|
devWarning(
|
|
|
|
|
props.replaceFields === undefined,
|
|
|
|
|
'Tree',
|
|
|
|
|
'`replaceFields` is deprecated, please use fieldNames instead',
|
2020-03-07 11:45:13 +00:00
|
|
|
|
);
|
2021-09-25 08:51:32 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const handleCheck: TreeProps['onCheck'] = (checkedObjOrKeys, eventObj) => {
|
|
|
|
|
emit('update:checkedKeys', checkedObjOrKeys);
|
|
|
|
|
emit('check', checkedObjOrKeys, eventObj);
|
|
|
|
|
};
|
|
|
|
|
const handleExpand: TreeProps['onExpand'] = (expandedKeys, eventObj) => {
|
|
|
|
|
emit('update:expandedKeys', expandedKeys);
|
|
|
|
|
emit('expand', expandedKeys, eventObj);
|
|
|
|
|
};
|
|
|
|
|
const handleSelect: TreeProps['onSelect'] = (selectedKeys, eventObj) => {
|
|
|
|
|
emit('update:selectedKeys', selectedKeys);
|
|
|
|
|
emit('select', selectedKeys, eventObj);
|
|
|
|
|
};
|
|
|
|
|
return () => {
|
|
|
|
|
const {
|
|
|
|
|
showIcon,
|
|
|
|
|
showLine,
|
|
|
|
|
switcherIcon = slots.switcherIcon,
|
|
|
|
|
icon = slots.icon,
|
|
|
|
|
blockNode,
|
|
|
|
|
checkable,
|
|
|
|
|
selectable,
|
|
|
|
|
fieldNames = props.replaceFields,
|
|
|
|
|
motion = props.openAnimation,
|
|
|
|
|
itemHeight = 28,
|
2022-03-26 14:52:54 +00:00
|
|
|
|
onDoubleclick,
|
|
|
|
|
onDblclick,
|
2021-09-25 08:51:32 +00:00
|
|
|
|
} = props as TreeProps;
|
|
|
|
|
const newProps = {
|
|
|
|
|
...attrs,
|
2022-03-26 14:52:54 +00:00
|
|
|
|
...omit(props, [
|
|
|
|
|
'onUpdate:checkedKeys',
|
|
|
|
|
'onUpdate:expandedKeys',
|
|
|
|
|
'onUpdate:selectedKeys',
|
|
|
|
|
'onDoubleclick',
|
|
|
|
|
]),
|
2021-09-25 08:51:32 +00:00
|
|
|
|
showLine: Boolean(showLine),
|
|
|
|
|
dropIndicatorRender,
|
|
|
|
|
fieldNames,
|
|
|
|
|
icon,
|
|
|
|
|
itemHeight,
|
|
|
|
|
};
|
2022-04-27 14:37:22 +00:00
|
|
|
|
const children = slots.default ? filterEmpty(slots.default()) : undefined;
|
2021-09-25 08:51:32 +00:00
|
|
|
|
return (
|
|
|
|
|
<VcTree
|
|
|
|
|
{...newProps}
|
|
|
|
|
virtual={virtual.value}
|
|
|
|
|
motion={motion}
|
|
|
|
|
ref={treeRef}
|
|
|
|
|
prefixCls={prefixCls.value}
|
|
|
|
|
class={classNames(
|
|
|
|
|
{
|
|
|
|
|
[`${prefixCls.value}-icon-hide`]: !showIcon,
|
|
|
|
|
[`${prefixCls.value}-block-node`]: blockNode,
|
|
|
|
|
[`${prefixCls.value}-unselectable`]: !selectable,
|
|
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
|
|
|
|
},
|
|
|
|
|
attrs.class,
|
|
|
|
|
)}
|
|
|
|
|
direction={direction.value}
|
|
|
|
|
checkable={checkable}
|
|
|
|
|
selectable={selectable}
|
2022-04-27 14:37:22 +00:00
|
|
|
|
switcherIcon={(nodeProps: SwitcherIconProps) =>
|
2021-09-25 08:51:32 +00:00
|
|
|
|
renderSwitcherIcon(prefixCls.value, switcherIcon, showLine, nodeProps)
|
|
|
|
|
}
|
|
|
|
|
onCheck={handleCheck}
|
|
|
|
|
onExpand={handleExpand}
|
|
|
|
|
onSelect={handleSelect}
|
2022-03-26 14:52:54 +00:00
|
|
|
|
onDblclick={onDblclick || onDoubleclick}
|
2021-09-25 08:51:32 +00:00
|
|
|
|
v-slots={{
|
|
|
|
|
...slots,
|
|
|
|
|
checkable: () => <span class={`${prefixCls.value}-checkbox-inner`} />,
|
|
|
|
|
}}
|
2022-04-27 14:37:22 +00:00
|
|
|
|
children={children}
|
2021-09-25 08:51:32 +00:00
|
|
|
|
></VcTree>
|
|
|
|
|
);
|
|
|
|
|
};
|
2018-09-26 14:57:01 +00:00
|
|
|
|
},
|
2020-10-22 06:25:02 +00:00
|
|
|
|
});
|