use-tree.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { computed, ref } from 'vue'
  2. export const useTree = (props, { emit }, tree, filterText) => {
  3. const config = computed(() => {
  4. return props.config ?? {}
  5. })
  6. // 头部按钮
  7. const appendTree = () => {
  8. emit('node-append', {})
  9. }
  10. const reload = () => {
  11. filterText.value = ''
  12. emit('tree-reload')
  13. }
  14. const defaultProps = computed(() => {
  15. return Object.assign({}, {
  16. children: 'children',
  17. label: 'label'
  18. }, config.value.defaultProps, config.value.optionProps)
  19. })
  20. const isTreeAppend = computed(() => {
  21. return config.value.isTreeAppend ?? false
  22. })
  23. // 节点唯一标识
  24. const nodeKey = computed(() => {
  25. return config.value.nodeKey ?? 'id'
  26. })
  27. // 是否手风琴模式
  28. const accordion = computed(() => {
  29. return config.value.accordion ?? false
  30. })
  31. // 懒加载函数
  32. const lazyLoad = computed(() => {
  33. return config.value.lazyLoad ?? null
  34. })
  35. // 是否进入展开,使用懒加载时默认不展开,不使用时默认展开
  36. const expandOnClickNode = computed(() => {
  37. return config.value.expandOnClickNode
  38. })
  39. // 且不是手风琴模式才可以默认展开
  40. const defaultExpandAll = computed(() => {
  41. return config.value.defaultExpandAll ?? !lazyLoad.value
  42. })
  43. // 显示checkbox
  44. const showCheckbox = computed(() => {
  45. return config.value.showCheckbox ?? false
  46. })
  47. // 树形组件搜索
  48. const filterNode = (value, data) => {
  49. if (!value) return true
  50. return data[defaultProps.value.label].indexOf(value) !== -1
  51. }
  52. // 定义渲染层级
  53. const level = computed(() => {
  54. return config.value.treeLevel ?? 0
  55. })
  56. // 定义树按钮渲染层级
  57. const buttonLevel = computed(() => {
  58. return config.value.buttonLevel
  59. })
  60. // 显示按钮类型列表
  61. const buttonList = computed(() => {
  62. return config.value.buttonList ?? []
  63. })
  64. // 点击树节点
  65. const nodeClick = (data, node, comp) => {
  66. // 清除高亮后手动开启,配置中不开启高亮则跳过
  67. if (config.value.highlightCurrent && !highlightCurrent.value) {
  68. highlightCurrent.value = true
  69. }
  70. emit('node-click', { data, node, comp })
  71. }
  72. // 是否高亮当前节点
  73. const highlightCurrent = ref(!!config.value.highlightCurrent)
  74. // 一些重置操作需要清除高亮,只能手动清除
  75. const clearHighlight = () => {
  76. highlightCurrent.value = false
  77. }
  78. // 默认展示高亮节点的key值
  79. const currentNodeKey = computed(() => {
  80. return config.value.currentNodeKey
  81. })
  82. return {
  83. config,
  84. appendTree,
  85. reload,
  86. defaultProps,
  87. isTreeAppend,
  88. nodeKey,
  89. accordion,
  90. lazyLoad,
  91. expandOnClickNode,
  92. defaultExpandAll,
  93. showCheckbox,
  94. filterNode,
  95. level,
  96. buttonLevel,
  97. buttonList,
  98. nodeClick,
  99. highlightCurrent,
  100. currentNodeKey,
  101. clearHighlight
  102. }
  103. }