Tree.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _extends from "@babel/runtime/helpers/esm/extends";
  3. import { resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";
  4. import { watchEffect, ref, defineComponent, computed } from 'vue';
  5. import classNames from '../_util/classNames';
  6. import VcTree from '../vc-tree';
  7. import PropTypes from '../_util/vue-types';
  8. import { filterEmpty } from '../_util/props-util';
  9. import initDefaultProps from '../_util/props-util/initDefaultProps';
  10. import { treeProps as vcTreeProps } from '../vc-tree/props';
  11. import useConfigInject from '../config-provider/hooks/useConfigInject';
  12. import renderSwitcherIcon from './utils/iconUtil';
  13. import dropIndicatorRender from './utils/dropIndicator';
  14. import devWarning from '../vc-util/devWarning';
  15. import { warning } from '../vc-util/warning';
  16. import omit from '../_util/omit';
  17. import { booleanType, someType, arrayType, functionType, objectType } from '../_util/type';
  18. // CSSINJS
  19. import useStyle from './style';
  20. export const treeProps = () => {
  21. const baseTreeProps = vcTreeProps();
  22. return _extends(_extends({}, baseTreeProps), {
  23. showLine: someType([Boolean, Object]),
  24. /** 是否支持多选 */
  25. multiple: booleanType(),
  26. /** 是否自动展开父节点 */
  27. autoExpandParent: booleanType(),
  28. /** checkable状态下节点选择完全受控(父子节点选中状态不再关联)*/
  29. checkStrictly: booleanType(),
  30. /** 是否支持选中 */
  31. checkable: booleanType(),
  32. /** 是否禁用树 */
  33. disabled: booleanType(),
  34. /** 默认展开所有树节点 */
  35. defaultExpandAll: booleanType(),
  36. /** 默认展开对应树节点 */
  37. defaultExpandParent: booleanType(),
  38. /** 默认展开指定的树节点 */
  39. defaultExpandedKeys: arrayType(),
  40. /** (受控)展开指定的树节点 */
  41. expandedKeys: arrayType(),
  42. /** (受控)选中复选框的树节点 */
  43. checkedKeys: someType([Array, Object]),
  44. /** 默认选中复选框的树节点 */
  45. defaultCheckedKeys: arrayType(),
  46. /** (受控)设置选中的树节点 */
  47. selectedKeys: arrayType(),
  48. /** 默认选中的树节点 */
  49. defaultSelectedKeys: arrayType(),
  50. selectable: booleanType(),
  51. loadedKeys: arrayType(),
  52. draggable: booleanType(),
  53. showIcon: booleanType(),
  54. icon: functionType(),
  55. switcherIcon: PropTypes.any,
  56. prefixCls: String,
  57. /**
  58. * @default{title,key,children}
  59. * deprecated, please use `fieldNames` instead
  60. * 替换treeNode中 title,key,children字段为treeData中对应的字段
  61. */
  62. replaceFields: objectType(),
  63. blockNode: booleanType(),
  64. openAnimation: PropTypes.any,
  65. onDoubleclick: baseTreeProps.onDblclick,
  66. 'onUpdate:selectedKeys': functionType(),
  67. 'onUpdate:checkedKeys': functionType(),
  68. 'onUpdate:expandedKeys': functionType()
  69. });
  70. };
  71. export default defineComponent({
  72. compatConfig: {
  73. MODE: 3
  74. },
  75. name: 'ATree',
  76. inheritAttrs: false,
  77. props: initDefaultProps(treeProps(), {
  78. checkable: false,
  79. selectable: true,
  80. showIcon: false,
  81. blockNode: false
  82. }),
  83. slots: Object,
  84. setup(props, _ref) {
  85. let {
  86. attrs,
  87. expose,
  88. emit,
  89. slots
  90. } = _ref;
  91. warning(!(props.treeData === undefined && slots.default), '`children` of Tree is deprecated. Please use `treeData` instead.');
  92. const {
  93. prefixCls,
  94. direction,
  95. virtual
  96. } = useConfigInject('tree', props);
  97. // style
  98. const [wrapSSR, hashId] = useStyle(prefixCls);
  99. const treeRef = ref();
  100. const scrollTo = scroll => {
  101. var _a;
  102. (_a = treeRef.value) === null || _a === void 0 ? void 0 : _a.scrollTo(scroll);
  103. };
  104. expose({
  105. treeRef,
  106. onNodeExpand: function () {
  107. var _a;
  108. (_a = treeRef.value) === null || _a === void 0 ? void 0 : _a.onNodeExpand(...arguments);
  109. },
  110. scrollTo,
  111. selectedKeys: computed(() => {
  112. var _a;
  113. return (_a = treeRef.value) === null || _a === void 0 ? void 0 : _a.selectedKeys;
  114. }),
  115. checkedKeys: computed(() => {
  116. var _a;
  117. return (_a = treeRef.value) === null || _a === void 0 ? void 0 : _a.checkedKeys;
  118. }),
  119. halfCheckedKeys: computed(() => {
  120. var _a;
  121. return (_a = treeRef.value) === null || _a === void 0 ? void 0 : _a.halfCheckedKeys;
  122. }),
  123. loadedKeys: computed(() => {
  124. var _a;
  125. return (_a = treeRef.value) === null || _a === void 0 ? void 0 : _a.loadedKeys;
  126. }),
  127. loadingKeys: computed(() => {
  128. var _a;
  129. return (_a = treeRef.value) === null || _a === void 0 ? void 0 : _a.loadingKeys;
  130. }),
  131. expandedKeys: computed(() => {
  132. var _a;
  133. return (_a = treeRef.value) === null || _a === void 0 ? void 0 : _a.expandedKeys;
  134. })
  135. });
  136. watchEffect(() => {
  137. devWarning(props.replaceFields === undefined, 'Tree', '`replaceFields` is deprecated, please use fieldNames instead');
  138. });
  139. const handleCheck = (checkedObjOrKeys, eventObj) => {
  140. emit('update:checkedKeys', checkedObjOrKeys);
  141. emit('check', checkedObjOrKeys, eventObj);
  142. };
  143. const handleExpand = (expandedKeys, eventObj) => {
  144. emit('update:expandedKeys', expandedKeys);
  145. emit('expand', expandedKeys, eventObj);
  146. };
  147. const handleSelect = (selectedKeys, eventObj) => {
  148. emit('update:selectedKeys', selectedKeys);
  149. emit('select', selectedKeys, eventObj);
  150. };
  151. return () => {
  152. const {
  153. showIcon,
  154. showLine,
  155. switcherIcon = slots.switcherIcon,
  156. icon = slots.icon,
  157. blockNode,
  158. checkable,
  159. selectable,
  160. fieldNames = props.replaceFields,
  161. motion = props.openAnimation,
  162. itemHeight = 28,
  163. onDoubleclick,
  164. onDblclick
  165. } = props;
  166. const newProps = _extends(_extends(_extends({}, attrs), omit(props, ['onUpdate:checkedKeys', 'onUpdate:expandedKeys', 'onUpdate:selectedKeys', 'onDoubleclick'])), {
  167. showLine: Boolean(showLine),
  168. dropIndicatorRender,
  169. fieldNames,
  170. icon,
  171. itemHeight
  172. });
  173. const children = slots.default ? filterEmpty(slots.default()) : undefined;
  174. return wrapSSR(_createVNode(VcTree, _objectSpread(_objectSpread({}, newProps), {}, {
  175. "virtual": virtual.value,
  176. "motion": motion,
  177. "ref": treeRef,
  178. "prefixCls": prefixCls.value,
  179. "class": classNames({
  180. [`${prefixCls.value}-icon-hide`]: !showIcon,
  181. [`${prefixCls.value}-block-node`]: blockNode,
  182. [`${prefixCls.value}-unselectable`]: !selectable,
  183. [`${prefixCls.value}-rtl`]: direction.value === 'rtl'
  184. }, attrs.class, hashId.value),
  185. "direction": direction.value,
  186. "checkable": checkable,
  187. "selectable": selectable,
  188. "switcherIcon": nodeProps => renderSwitcherIcon(prefixCls.value, switcherIcon, nodeProps, slots.leafIcon, showLine),
  189. "onCheck": handleCheck,
  190. "onExpand": handleExpand,
  191. "onSelect": handleSelect,
  192. "onDblclick": onDblclick || onDoubleclick,
  193. "children": children
  194. }), _extends(_extends({}, slots), {
  195. checkable: () => _createVNode("span", {
  196. "class": `${prefixCls.value}-checkbox-inner`
  197. }, null)
  198. })));
  199. };
  200. }
  201. });