2020-07-16 18:31:20 +08:00
|
|
|
|
import { inject } from 'vue';
|
|
|
|
|
import classNames from 'classnames';
|
2020-03-28 15:50:01 +08: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';
|
2019-01-12 11:33:27 +08:00
|
|
|
|
import { Tree as VcTree, TreeNode } from '../vc-tree';
|
|
|
|
|
import animation from '../_util/openAnimation';
|
|
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-07-16 18:31:20 +08:00
|
|
|
|
import { initDefaultProps, getOptionProps, getComponent, getSlot } from '../_util/props-util';
|
2019-04-10 13:28:07 +08:00
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
|
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2018-09-26 22:57:01 +08:00
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
|
function TreeProps() {
|
2018-09-26 22:57:01 +08:00
|
|
|
|
return {
|
|
|
|
|
showLine: PropTypes.bool,
|
|
|
|
|
/** 是否支持多选 */
|
|
|
|
|
multiple: PropTypes.bool,
|
|
|
|
|
/** 是否自动展开父节点 */
|
|
|
|
|
autoExpandParent: PropTypes.bool,
|
|
|
|
|
/** checkable状态下节点选择完全受控(父子节点选中状态不再关联)*/
|
|
|
|
|
checkStrictly: PropTypes.bool,
|
|
|
|
|
/** 是否支持选中 */
|
|
|
|
|
checkable: PropTypes.bool,
|
|
|
|
|
/** 是否禁用树 */
|
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
|
/** 默认展开所有树节点 */
|
|
|
|
|
defaultExpandAll: PropTypes.bool,
|
|
|
|
|
/** 默认展开对应树节点 */
|
|
|
|
|
defaultExpandParent: PropTypes.bool,
|
|
|
|
|
/** 默认展开指定的树节点 */
|
2018-12-26 16:33:24 +08:00
|
|
|
|
defaultExpandedKeys: PropTypes.array,
|
2018-09-26 22:57:01 +08:00
|
|
|
|
/** (受控)展开指定的树节点 */
|
2018-12-26 16:33:24 +08:00
|
|
|
|
expandedKeys: PropTypes.array,
|
2018-09-26 22:57:01 +08:00
|
|
|
|
/** (受控)选中复选框的树节点 */
|
2019-01-12 11:33:27 +08:00
|
|
|
|
checkedKeys: PropTypes.oneOfType([
|
|
|
|
|
PropTypes.array,
|
|
|
|
|
PropTypes.shape({
|
|
|
|
|
checked: PropTypes.array,
|
|
|
|
|
halfChecked: PropTypes.array,
|
|
|
|
|
}).loose,
|
|
|
|
|
]),
|
2018-09-26 22:57:01 +08:00
|
|
|
|
/** 默认选中复选框的树节点 */
|
2018-12-26 16:33:24 +08:00
|
|
|
|
defaultCheckedKeys: PropTypes.array,
|
2018-09-26 22:57:01 +08:00
|
|
|
|
/** (受控)设置选中的树节点 */
|
2018-12-26 16:33:24 +08:00
|
|
|
|
selectedKeys: PropTypes.array,
|
2018-09-26 22:57:01 +08:00
|
|
|
|
/** 默认选中的树节点 */
|
2018-12-26 16:33:24 +08:00
|
|
|
|
defaultSelectedKeys: PropTypes.array,
|
2018-09-26 22:57:01 +08:00
|
|
|
|
selectable: PropTypes.bool,
|
2020-08-12 18:07:41 +08:00
|
|
|
|
|
2018-09-26 22:57:01 +08:00
|
|
|
|
/** filter some AntTreeNodes as you need. it should return true */
|
|
|
|
|
filterAntTreeNode: PropTypes.func,
|
|
|
|
|
/** 异步加载数据 */
|
|
|
|
|
loadData: PropTypes.func,
|
2018-12-26 16:33:24 +08:00
|
|
|
|
loadedKeys: PropTypes.array,
|
2018-09-26 22:57:01 +08:00
|
|
|
|
// onLoaded: (loadedKeys: string[], info: { event: 'load', node: AntTreeNode; }) => void,
|
|
|
|
|
/** 响应右键点击 */
|
|
|
|
|
// onRightClick: (options: AntTreeNodeMouseEvent) => void,
|
|
|
|
|
/** 设置节点可拖拽(IE>8)*/
|
|
|
|
|
draggable: PropTypes.bool,
|
|
|
|
|
// /** 开始拖拽时调用 */
|
|
|
|
|
// onDragStart: (options: AntTreeNodeMouseEvent) => void,
|
|
|
|
|
// /** dragenter 触发时调用 */
|
|
|
|
|
// onDragEnter: (options: AntTreeNodeMouseEvent) => void,
|
|
|
|
|
// /** dragover 触发时调用 */
|
|
|
|
|
// onDragOver: (options: AntTreeNodeMouseEvent) => void,
|
|
|
|
|
// /** dragleave 触发时调用 */
|
|
|
|
|
// onDragLeave: (options: AntTreeNodeMouseEvent) => void,
|
|
|
|
|
// /** drop 触发时调用 */
|
|
|
|
|
// onDrop: (options: AntTreeNodeMouseEvent) => void,
|
|
|
|
|
showIcon: PropTypes.bool,
|
|
|
|
|
icon: PropTypes.func,
|
2019-04-10 13:28:07 +08:00
|
|
|
|
switcherIcon: PropTypes.any,
|
2018-09-26 22:57:01 +08:00
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
|
filterTreeNode: PropTypes.func,
|
|
|
|
|
openAnimation: PropTypes.any,
|
|
|
|
|
treeData: PropTypes.array,
|
2019-11-15 15:05:38 +08:00
|
|
|
|
/**
|
|
|
|
|
* @default{title,key,children}
|
|
|
|
|
* 替换treeNode中 title,key,children字段为treeData中对应的字段
|
|
|
|
|
*/
|
|
|
|
|
replaceFields: PropTypes.object,
|
2020-03-07 19:45:13 +08:00
|
|
|
|
blockNode: PropTypes.bool,
|
2020-08-12 18:07:41 +08: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 11:33:27 +08:00
|
|
|
|
};
|
2018-09-26 22:57:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
|
export { TreeProps };
|
2018-09-26 22:57:01 +08:00
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
name: 'ATree',
|
2020-07-16 18:31:20 +08:00
|
|
|
|
inheritAttrs: false,
|
2018-09-26 22:57:01 +08:00
|
|
|
|
props: initDefaultProps(TreeProps(), {
|
|
|
|
|
checkable: false,
|
|
|
|
|
showIcon: false,
|
|
|
|
|
openAnimation: {
|
2020-07-16 18:31:20 +08:00
|
|
|
|
...animation,
|
|
|
|
|
appear: null,
|
2018-09-26 22:57:01 +08:00
|
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
|
blockNode: false,
|
2018-09-26 22:57:01 +08:00
|
|
|
|
}),
|
2020-07-16 18:31:20 +08:00
|
|
|
|
setup() {
|
|
|
|
|
return {
|
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
|
|
|
|
};
|
2018-09-26 22:57:01 +08:00
|
|
|
|
},
|
|
|
|
|
TreeNode,
|
|
|
|
|
methods: {
|
2020-03-28 15:50:01 +08:00
|
|
|
|
renderSwitcherIcon(prefixCls, switcherIcon, { isLeaf, loading, expanded }) {
|
2019-04-10 13:28:07 +08:00
|
|
|
|
const { showLine } = this.$props;
|
2018-12-11 20:33:37 +08:00
|
|
|
|
if (loading) {
|
2020-03-28 15:50:01 +08:00
|
|
|
|
return <LoadingOutlined class={`${prefixCls}-switcher-loading-icon`} />;
|
2018-12-11 20:33:37 +08:00
|
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
|
|
|
|
|
|
if (isLeaf) {
|
2020-03-28 15:50:01 +08:00
|
|
|
|
return showLine ? <FileOutlined class={`${prefixCls}-switcher-line-icon`} /> : null;
|
2018-12-11 20:33:37 +08:00
|
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const switcherCls = `${prefixCls}-switcher-icon`;
|
|
|
|
|
if (switcherIcon) {
|
|
|
|
|
return cloneElement(switcherIcon, {
|
2020-08-12 18:07:41 +08:00
|
|
|
|
class: switcherCls,
|
2020-03-07 19:45:13 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return showLine ? (
|
2020-07-16 18:31:20 +08:00
|
|
|
|
expanded ? (
|
|
|
|
|
<MinusSquareOutlined class={`${prefixCls}-switcher-line-icon`} />
|
|
|
|
|
) : (
|
2020-03-28 15:50:01 +08:00
|
|
|
|
<PlusSquareOutlined class={`${prefixCls}-switcher-line-icon`} />
|
2020-07-16 18:31:20 +08:00
|
|
|
|
)
|
2020-03-07 19:45:13 +08:00
|
|
|
|
) : (
|
2020-03-28 15:50:01 +08:00
|
|
|
|
<CaretDownFilled class={switcherCls} />
|
2020-03-07 19:45:13 +08:00
|
|
|
|
);
|
2018-12-11 20:33:37 +08:00
|
|
|
|
},
|
2019-03-18 15:10:20 +08:00
|
|
|
|
updateTreeData(treeData) {
|
2020-07-16 18:31:20 +08:00
|
|
|
|
const { $slots } = this;
|
2019-11-15 15:05:38 +08:00
|
|
|
|
const defaultFields = { children: 'children', title: 'title', key: 'key' };
|
|
|
|
|
const replaceFields = { ...defaultFields, ...this.$props.replaceFields };
|
2019-01-12 11:33:27 +08:00
|
|
|
|
return treeData.map(item => {
|
2019-11-15 15:05:38 +08:00
|
|
|
|
const key = item[replaceFields.key];
|
|
|
|
|
const children = item[replaceFields.children];
|
2020-07-16 18:31:20 +08:00
|
|
|
|
const { slots = {}, scopedSlots = {}, class: cls, style, ...restProps } = item;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
const treeNodeProps = {
|
|
|
|
|
...restProps,
|
2020-07-16 18:31:20 +08:00
|
|
|
|
icon: $slots[scopedSlots.icon] || $slots[slots.icon] || restProps.icon,
|
2020-03-07 19:45:13 +08:00
|
|
|
|
switcherIcon:
|
2020-07-16 18:31:20 +08:00
|
|
|
|
$slots[scopedSlots.switcherIcon] ||
|
2020-03-07 19:45:13 +08:00
|
|
|
|
$slots[slots.switcherIcon] ||
|
|
|
|
|
restProps.switcherIcon,
|
2020-07-16 18:31:20 +08:00
|
|
|
|
title: $slots[scopedSlots.title] || $slots[slots.title] || restProps[replaceFields.title],
|
2018-09-26 22:57:01 +08:00
|
|
|
|
dataRef: item,
|
|
|
|
|
key,
|
|
|
|
|
class: cls,
|
|
|
|
|
style,
|
2019-01-12 11:33:27 +08:00
|
|
|
|
};
|
2018-09-26 22:57:01 +08:00
|
|
|
|
if (children) {
|
2019-03-18 15:10:20 +08:00
|
|
|
|
return { ...treeNodeProps, children: this.updateTreeData(children) };
|
2018-09-26 22:57:01 +08:00
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
|
return treeNodeProps;
|
|
|
|
|
});
|
2018-09-26 22:57:01 +08:00
|
|
|
|
},
|
2020-07-16 18:31:20 +08:00
|
|
|
|
setTreeRef(node) {
|
|
|
|
|
this.tree = node;
|
|
|
|
|
},
|
2020-08-12 18:07:41 +08:00
|
|
|
|
handleCheck(checkedObj, eventObj) {
|
|
|
|
|
this.$emit('update:checkedKeys', checkedObj);
|
|
|
|
|
this.$emit('check', checkedObj, eventObj);
|
|
|
|
|
},
|
|
|
|
|
handleExpand(expandedKeys, eventObj) {
|
|
|
|
|
this.$emit('update:expandedKeys', expandedKeys);
|
|
|
|
|
this.$emit('expand', expandedKeys, eventObj);
|
|
|
|
|
},
|
|
|
|
|
handleSelect(selectedKeys, eventObj) {
|
|
|
|
|
this.$emit('update:selectedKeys', selectedKeys);
|
|
|
|
|
this.$emit('select', selectedKeys, eventObj);
|
|
|
|
|
},
|
2018-09-26 22:57:01 +08:00
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
|
render() {
|
|
|
|
|
const props = getOptionProps(this);
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const { prefixCls: customizePrefixCls, showIcon, treeNodes, blockNode } = props;
|
2019-09-11 22:35:25 +08:00
|
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-10 13:28:07 +08:00
|
|
|
|
const prefixCls = getPrefixCls('tree', customizePrefixCls);
|
2020-07-16 18:31:20 +08:00
|
|
|
|
const switcherIcon = getComponent(this, 'switcherIcon');
|
2019-01-12 11:33:27 +08:00
|
|
|
|
const checkable = props.checkable;
|
|
|
|
|
let treeData = props.treeData || treeNodes;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
if (treeData) {
|
2019-03-18 15:10:20 +08:00
|
|
|
|
treeData = this.updateTreeData(treeData);
|
2018-09-26 22:57:01 +08:00
|
|
|
|
}
|
2020-07-16 18:31:20 +08:00
|
|
|
|
const { class: className, ...restAttrs } = this.$attrs;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
const vcTreeProps = {
|
2020-07-16 18:31:20 +08: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 19:45:13 +08:00
|
|
|
|
[`${prefixCls}-icon-hide`]: !showIcon,
|
|
|
|
|
[`${prefixCls}-block-node`]: blockNode,
|
2020-07-16 18:31:20 +08:00
|
|
|
|
}),
|
2020-08-12 18:07:41 +08:00
|
|
|
|
onCheck: this.handleCheck,
|
|
|
|
|
onExpand: this.handleExpand,
|
|
|
|
|
onSelect: this.handleSelect,
|
2019-01-12 11:33:27 +08:00
|
|
|
|
};
|
2018-09-26 22:57:01 +08:00
|
|
|
|
if (treeData) {
|
2020-07-16 18:31:20 +08:00
|
|
|
|
vcTreeProps.treeData = treeData;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
}
|
2020-08-12 16:30:13 +08:00
|
|
|
|
return <VcTree {...vcTreeProps} __propsSymbol__={Symbol()} />;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
|
};
|