2021-01-26 15:48:48 +00:00
|
|
|
|
import { defineComponent, inject, VNode, PropType, CSSProperties } from 'vue';
|
2020-08-31 08:53:19 +00:00
|
|
|
|
import classNames from '../_util/classNames';
|
2020-03-28 07:50:01 +00:00
|
|
|
|
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
|
|
|
|
|
import FileOutlined from '@ant-design/icons-vue/FileOutlined';
|
|
|
|
|
import CaretDownFilled from '@ant-design/icons-vue/CaretDownFilled';
|
|
|
|
|
import MinusSquareOutlined from '@ant-design/icons-vue/MinusSquareOutlined';
|
|
|
|
|
import PlusSquareOutlined from '@ant-design/icons-vue/PlusSquareOutlined';
|
2020-08-30 14:23:36 +00:00
|
|
|
|
import VcTree from '../vc-tree';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import animation from '../_util/openAnimation';
|
|
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-10-22 06:25:02 +00:00
|
|
|
|
import { getOptionProps, getComponent, getSlot } from '../_util/props-util';
|
|
|
|
|
import initDefaultProps from '../_util/props-util/initDefaultProps';
|
2019-04-10 05:28:07 +00:00
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
2020-09-30 02:47:18 +00:00
|
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2018-09-26 14:57:01 +00:00
|
|
|
|
|
2020-08-30 14:23:36 +00:00
|
|
|
|
const TreeNode = VcTree.TreeNode;
|
2020-11-18 09:37:15 +00:00
|
|
|
|
|
|
|
|
|
export interface TreeDataItem {
|
|
|
|
|
key?: string | number;
|
|
|
|
|
title?: string;
|
|
|
|
|
isLeaf?: boolean;
|
|
|
|
|
selectable?: boolean;
|
|
|
|
|
children?: TreeDataItem[];
|
|
|
|
|
disableCheckbox?: boolean;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
class?: string;
|
2021-01-26 15:48:48 +00:00
|
|
|
|
style?: CSSProperties;
|
2020-11-18 09:37:15 +00:00
|
|
|
|
checkable?: boolean;
|
2021-01-26 15:48:48 +00:00
|
|
|
|
icon?: VNode;
|
|
|
|
|
slots?: Record<string, string>;
|
|
|
|
|
switcherIcon?: VNode;
|
2021-02-06 05:30:08 +00:00
|
|
|
|
// support custom field
|
|
|
|
|
[key: string]: any;
|
2020-11-18 09:37:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface DefaultEvent {
|
|
|
|
|
nativeEvent: MouseEvent;
|
2021-01-26 15:48:48 +00:00
|
|
|
|
node: Record<string, any>;
|
2020-11-18 09:37:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface CheckEvent extends DefaultEvent {
|
|
|
|
|
checked: boolean;
|
2021-01-26 15:48:48 +00:00
|
|
|
|
checkedNodes: Array<Record<string, any>>;
|
|
|
|
|
checkedNodesPositions: { node: Record<string, any>; pos: string | number }[];
|
2020-11-18 09:37:15 +00:00
|
|
|
|
event: string;
|
|
|
|
|
halfCheckedKeys: (string | number)[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ExpendEvent extends DefaultEvent {
|
|
|
|
|
expanded: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface SelectEvent extends DefaultEvent {
|
|
|
|
|
event: string;
|
|
|
|
|
selected: boolean;
|
2021-01-26 15:48:48 +00:00
|
|
|
|
selectedNodes: Array<Record<string, any>>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface TreeDragEvent {
|
|
|
|
|
event: DragEvent;
|
|
|
|
|
expandedKeys: (string | number)[];
|
|
|
|
|
node: Record<string, any>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface DropEvent {
|
|
|
|
|
dragNode: Record<string, any>;
|
|
|
|
|
dragNodesKeys: (string | number)[];
|
|
|
|
|
dropPosition: number;
|
|
|
|
|
dropToGap: boolean;
|
|
|
|
|
event: DragEvent;
|
|
|
|
|
node: Record<string, any>;
|
2020-11-18 09:37:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
function TreeProps() {
|
2018-09-26 14:57:01 +00:00
|
|
|
|
return {
|
2020-10-10 10:16:28 +00:00
|
|
|
|
showLine: PropTypes.looseBool,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 是否支持多选 */
|
2020-10-10 10:16:28 +00:00
|
|
|
|
multiple: PropTypes.looseBool,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 是否自动展开父节点 */
|
2020-10-10 10:16:28 +00:00
|
|
|
|
autoExpandParent: PropTypes.looseBool,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** checkable状态下节点选择完全受控(父子节点选中状态不再关联)*/
|
2020-10-10 10:16:28 +00:00
|
|
|
|
checkStrictly: PropTypes.looseBool,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 是否支持选中 */
|
2020-10-10 10:16:28 +00:00
|
|
|
|
checkable: PropTypes.looseBool,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 是否禁用树 */
|
2020-10-10 10:16:28 +00:00
|
|
|
|
disabled: PropTypes.looseBool,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 默认展开所有树节点 */
|
2020-10-10 10:16:28 +00:00
|
|
|
|
defaultExpandAll: PropTypes.looseBool,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 默认展开对应树节点 */
|
2020-10-10 10:16:28 +00:00
|
|
|
|
defaultExpandParent: PropTypes.looseBool,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 默认展开指定的树节点 */
|
2020-11-18 09:37:15 +00:00
|
|
|
|
defaultExpandedKeys: PropTypes.arrayOf(
|
|
|
|
|
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
|
),
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** (受控)展开指定的树节点 */
|
2020-11-18 09:37:15 +00:00
|
|
|
|
expandedKeys: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** (受控)选中复选框的树节点 */
|
2019-01-12 03:33:27 +00:00
|
|
|
|
checkedKeys: PropTypes.oneOfType([
|
2020-11-18 09:37:15 +00:00
|
|
|
|
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
|
2019-01-12 03:33:27 +00:00
|
|
|
|
PropTypes.shape({
|
2020-11-18 09:37:15 +00:00
|
|
|
|
checked: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
|
|
|
|
|
halfChecked: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
|
2019-01-12 03:33:27 +00:00
|
|
|
|
}).loose,
|
|
|
|
|
]),
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 默认选中复选框的树节点 */
|
2020-11-18 09:37:15 +00:00
|
|
|
|
defaultCheckedKeys: PropTypes.arrayOf(
|
|
|
|
|
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
|
),
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** (受控)设置选中的树节点 */
|
2020-11-18 09:37:15 +00:00
|
|
|
|
selectedKeys: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** 默认选中的树节点 */
|
2020-11-18 09:37:15 +00:00
|
|
|
|
defaultSelectedKeys: PropTypes.arrayOf(
|
|
|
|
|
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
|
),
|
2020-10-10 10:16:28 +00:00
|
|
|
|
selectable: PropTypes.looseBool,
|
2020-08-12 10:07:41 +00:00
|
|
|
|
|
2018-09-26 14:57:01 +00:00
|
|
|
|
/** filter some AntTreeNodes as you need. it should return true */
|
|
|
|
|
filterAntTreeNode: PropTypes.func,
|
|
|
|
|
/** 异步加载数据 */
|
|
|
|
|
loadData: PropTypes.func,
|
2020-11-18 09:37:15 +00:00
|
|
|
|
loadedKeys: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
|
2018-09-26 14:57:01 +00:00
|
|
|
|
// onLoaded: (loadedKeys: string[], info: { event: 'load', node: AntTreeNode; }) => void,
|
|
|
|
|
/** 响应右键点击 */
|
|
|
|
|
// onRightClick: (options: AntTreeNodeMouseEvent) => void,
|
|
|
|
|
/** 设置节点可拖拽(IE>8)*/
|
2020-10-10 10:16:28 +00:00
|
|
|
|
draggable: PropTypes.looseBool,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
// /** 开始拖拽时调用 */
|
|
|
|
|
// onDragStart: (options: AntTreeNodeMouseEvent) => void,
|
|
|
|
|
// /** dragenter 触发时调用 */
|
|
|
|
|
// onDragEnter: (options: AntTreeNodeMouseEvent) => void,
|
|
|
|
|
// /** dragover 触发时调用 */
|
|
|
|
|
// onDragOver: (options: AntTreeNodeMouseEvent) => void,
|
|
|
|
|
// /** dragleave 触发时调用 */
|
|
|
|
|
// onDragLeave: (options: AntTreeNodeMouseEvent) => void,
|
|
|
|
|
// /** drop 触发时调用 */
|
|
|
|
|
// onDrop: (options: AntTreeNodeMouseEvent) => void,
|
2020-10-10 10:16:28 +00:00
|
|
|
|
showIcon: PropTypes.looseBool,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
icon: PropTypes.func,
|
2019-04-10 05:28:07 +00:00
|
|
|
|
switcherIcon: PropTypes.any,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
|
filterTreeNode: PropTypes.func,
|
|
|
|
|
openAnimation: PropTypes.any,
|
2020-11-18 09:37:15 +00:00
|
|
|
|
treeData: {
|
|
|
|
|
type: Array as PropType<TreeDataItem[]>,
|
|
|
|
|
},
|
2019-11-15 07:05:38 +00:00
|
|
|
|
/**
|
|
|
|
|
* @default{title,key,children}
|
|
|
|
|
* 替换treeNode中 title,key,children字段为treeData中对应的字段
|
|
|
|
|
*/
|
|
|
|
|
replaceFields: PropTypes.object,
|
2020-10-10 10:16:28 +00:00
|
|
|
|
blockNode: PropTypes.looseBool,
|
2020-08-12 10:07:41 +00:00
|
|
|
|
/** 展开/收起节点时触发 */
|
|
|
|
|
onExpand: PropTypes.func,
|
|
|
|
|
/** 点击复选框触发 */
|
|
|
|
|
onCheck: PropTypes.func,
|
|
|
|
|
/** 点击树节点触发 */
|
|
|
|
|
onSelect: PropTypes.func,
|
|
|
|
|
/** 单击树节点触发 */
|
|
|
|
|
onClick: PropTypes.func,
|
|
|
|
|
/** 双击树节点触发 */
|
|
|
|
|
onDoubleclick: PropTypes.func,
|
|
|
|
|
onDblclick: PropTypes.func,
|
|
|
|
|
'onUpdate:selectedKeys': PropTypes.func,
|
|
|
|
|
'onUpdate:checkedKeys': PropTypes.func,
|
|
|
|
|
'onUpdate:expandedKeys': PropTypes.func,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
export { TreeProps };
|
2018-09-26 14:57:01 +00:00
|
|
|
|
|
2020-10-22 06:25:02 +00:00
|
|
|
|
export default defineComponent({
|
2018-09-26 14:57:01 +00:00
|
|
|
|
name: 'ATree',
|
2020-07-16 10:31:20 +00:00
|
|
|
|
inheritAttrs: false,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
props: initDefaultProps(TreeProps(), {
|
|
|
|
|
checkable: false,
|
|
|
|
|
showIcon: false,
|
|
|
|
|
openAnimation: {
|
2020-07-16 10:31:20 +00:00
|
|
|
|
...animation,
|
|
|
|
|
appear: null,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
|
blockNode: false,
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}),
|
2020-07-16 10:31:20 +00:00
|
|
|
|
setup() {
|
|
|
|
|
return {
|
2020-10-22 06:25:02 +00:00
|
|
|
|
tree: null,
|
2020-09-30 02:47:18 +00:00
|
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-07-16 10:31:20 +00:00
|
|
|
|
};
|
2018-09-26 14:57:01 +00:00
|
|
|
|
},
|
|
|
|
|
TreeNode,
|
|
|
|
|
methods: {
|
2020-11-18 09:37:15 +00:00
|
|
|
|
renderSwitcherIcon(prefixCls: string, switcherIcon: VNode, { isLeaf, loading, expanded }) {
|
2019-04-10 05:28:07 +00:00
|
|
|
|
const { showLine } = this.$props;
|
2018-12-11 12:33:37 +00:00
|
|
|
|
if (loading) {
|
2020-03-28 07:50:01 +00:00
|
|
|
|
return <LoadingOutlined class={`${prefixCls}-switcher-loading-icon`} />;
|
2018-12-11 12:33:37 +00:00
|
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
|
|
if (isLeaf) {
|
2020-03-28 07:50:01 +00:00
|
|
|
|
return showLine ? <FileOutlined class={`${prefixCls}-switcher-line-icon`} /> : null;
|
2018-12-11 12:33:37 +00:00
|
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const switcherCls = `${prefixCls}-switcher-icon`;
|
|
|
|
|
if (switcherIcon) {
|
|
|
|
|
return cloneElement(switcherIcon, {
|
2020-08-12 10:07:41 +00:00
|
|
|
|
class: switcherCls,
|
2020-03-07 11:45:13 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return showLine ? (
|
2020-07-16 10:31:20 +00:00
|
|
|
|
expanded ? (
|
|
|
|
|
<MinusSquareOutlined class={`${prefixCls}-switcher-line-icon`} />
|
|
|
|
|
) : (
|
2020-03-28 07:50:01 +00:00
|
|
|
|
<PlusSquareOutlined class={`${prefixCls}-switcher-line-icon`} />
|
2020-07-16 10:31:20 +00:00
|
|
|
|
)
|
2020-03-07 11:45:13 +00:00
|
|
|
|
) : (
|
2020-03-28 07:50:01 +00:00
|
|
|
|
<CaretDownFilled class={switcherCls} />
|
2020-03-07 11:45:13 +00:00
|
|
|
|
);
|
2018-12-11 12:33:37 +00:00
|
|
|
|
},
|
2020-11-18 09:37:15 +00:00
|
|
|
|
updateTreeData(treeData: TreeDataItem[]) {
|
2020-07-16 10:31:20 +00:00
|
|
|
|
const { $slots } = this;
|
2019-11-15 07:05:38 +00:00
|
|
|
|
const defaultFields = { children: 'children', title: 'title', key: 'key' };
|
|
|
|
|
const replaceFields = { ...defaultFields, ...this.$props.replaceFields };
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return treeData.map(item => {
|
2019-11-15 07:05:38 +00:00
|
|
|
|
const key = item[replaceFields.key];
|
|
|
|
|
const children = item[replaceFields.children];
|
2020-11-18 09:37:15 +00:00
|
|
|
|
const { slots = {}, class: cls, style, ...restProps } = item;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
const treeNodeProps = {
|
|
|
|
|
...restProps,
|
2020-11-18 09:37:15 +00:00
|
|
|
|
icon: $slots[slots.icon] || restProps.icon,
|
|
|
|
|
switcherIcon: $slots[slots.switcherIcon] || restProps.switcherIcon,
|
|
|
|
|
title: $slots[slots.title] || $slots.title || restProps[replaceFields.title],
|
2018-09-26 14:57:01 +00:00
|
|
|
|
dataRef: item,
|
|
|
|
|
key,
|
|
|
|
|
class: cls,
|
|
|
|
|
style,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2018-09-26 14:57:01 +00:00
|
|
|
|
if (children) {
|
2019-03-18 07:10:20 +00:00
|
|
|
|
return { ...treeNodeProps, children: this.updateTreeData(children) };
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return treeNodeProps;
|
|
|
|
|
});
|
2018-09-26 14:57:01 +00:00
|
|
|
|
},
|
2020-11-18 09:37:15 +00:00
|
|
|
|
setTreeRef(node: VNode) {
|
2020-07-16 10:31:20 +00:00
|
|
|
|
this.tree = node;
|
|
|
|
|
},
|
2020-11-18 09:37:15 +00:00
|
|
|
|
handleCheck(checkedObj: (number | string)[], eventObj: CheckEvent) {
|
2020-08-12 10:07:41 +00:00
|
|
|
|
this.$emit('update:checkedKeys', checkedObj);
|
|
|
|
|
this.$emit('check', checkedObj, eventObj);
|
|
|
|
|
},
|
2020-11-18 09:37:15 +00:00
|
|
|
|
handleExpand(expandedKeys: (number | string)[], eventObj: ExpendEvent) {
|
2020-08-12 10:07:41 +00:00
|
|
|
|
this.$emit('update:expandedKeys', expandedKeys);
|
|
|
|
|
this.$emit('expand', expandedKeys, eventObj);
|
|
|
|
|
},
|
2020-11-18 09:37:15 +00:00
|
|
|
|
handleSelect(selectedKeys: (number | string)[], eventObj: SelectEvent) {
|
2020-08-12 10:07:41 +00:00
|
|
|
|
this.$emit('update:selectedKeys', selectedKeys);
|
|
|
|
|
this.$emit('select', selectedKeys, eventObj);
|
|
|
|
|
},
|
2018-09-26 14:57:01 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
render() {
|
|
|
|
|
const props = getOptionProps(this);
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const { prefixCls: customizePrefixCls, showIcon, treeNodes, blockNode } = props;
|
2019-09-11 14:35:25 +00:00
|
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-10 05:28:07 +00:00
|
|
|
|
const prefixCls = getPrefixCls('tree', customizePrefixCls);
|
2020-07-16 10:31:20 +00:00
|
|
|
|
const switcherIcon = getComponent(this, 'switcherIcon');
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const checkable = props.checkable;
|
|
|
|
|
let treeData = props.treeData || treeNodes;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
if (treeData) {
|
2019-03-18 07:10:20 +00:00
|
|
|
|
treeData = this.updateTreeData(treeData);
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
2020-07-16 10:31:20 +00:00
|
|
|
|
const { class: className, ...restAttrs } = this.$attrs;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
const vcTreeProps = {
|
2020-07-16 10:31:20 +00:00
|
|
|
|
...props,
|
|
|
|
|
prefixCls,
|
|
|
|
|
checkable: checkable ? <span class={`${prefixCls}-checkbox-inner`} /> : checkable,
|
|
|
|
|
children: getSlot(this),
|
|
|
|
|
switcherIcon: nodeProps => this.renderSwitcherIcon(prefixCls, switcherIcon, nodeProps),
|
|
|
|
|
ref: this.setTreeRef,
|
|
|
|
|
...restAttrs,
|
|
|
|
|
class: classNames(className, {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
[`${prefixCls}-icon-hide`]: !showIcon,
|
|
|
|
|
[`${prefixCls}-block-node`]: blockNode,
|
2020-07-16 10:31:20 +00:00
|
|
|
|
}),
|
2020-08-12 10:07:41 +00:00
|
|
|
|
onCheck: this.handleCheck,
|
|
|
|
|
onExpand: this.handleExpand,
|
|
|
|
|
onSelect: this.handleSelect,
|
2021-01-26 15:48:48 +00:00
|
|
|
|
} as Record<string, any>;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
if (treeData) {
|
2020-07-16 10:31:20 +00:00
|
|
|
|
vcTreeProps.treeData = treeData;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
2020-08-27 10:34:23 +00:00
|
|
|
|
return <VcTree {...vcTreeProps} __propsSymbol__={[]} />;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
},
|
2020-10-22 06:25:02 +00:00
|
|
|
|
});
|