ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<script lang="ts" setup>
import type { FieldNames } from './props';
 
import { computed, ref, unref, watch, watchEffect } from 'vue';
 
import { useAttrs } from '@jnpf/hooks';
import { isArray, isBoolean } from '@jnpf/utils';
 
import { TreeSelect } from 'ant-design-vue';
import { cloneDeep } from 'lodash-es';
 
import { useTree } from '../../../core/tree/src/hooks/useTree';
import { treeSelectProps } from './props';
 
defineOptions({ inheritAttrs: false, name: 'JnpfTreeSelect' });
const props = defineProps(treeSelectProps);
const emit = defineEmits(['update:modelValue', 'update:value', 'change']);
const attrs: any = useAttrs({ excludeDefaultKeys: false });
const innerValue = ref('');
const treeDataRef = ref<any[]>([]);
 
const getFieldNames = computed((): Required<FieldNames> => {
  const { fieldNames } = props;
  return {
    label: 'fullName',
    value: 'id',
    children: 'children',
    ...fieldNames,
    key: fieldNames.value || 'id',
  };
});
const getTreeData = computed(() => {
  const treeData = cloneDeep(props.options);
  if (props.multiple) return treeData;
  const loop = (list) => {
    for (const item of list) {
      if (
        props.lastLevel &&
        ((isBoolean(props.lastLevelValue) && !!item[props.lastLevelKey] != props.lastLevelValue) ||
          (!isBoolean(props.lastLevelValue) && item[props.lastLevelKey] != props.lastLevelValue))
      ) {
        item.disabled = true;
      }
      if (item[unref(getFieldNames).children] && isArray(item[unref(getFieldNames).children])) loop(item[unref(getFieldNames).children]);
    }
  };
  loop(treeData);
  return treeData;
});
const getBindValue = computed(() => ({
  ...unref(attrs),
  ...props,
  fieldNames: unref(getFieldNames),
  showArrow: Reflect.has(unref(attrs), 'showArrow') ? unref(attrs).showArrow : true,
  showCheckedStrategy: Reflect.has(unref(attrs), 'showCheckedStrategy') ? unref(attrs).showCheckedStrategy : TreeSelect.SHOW_ALL,
  treeCheckable: props.multiple,
  treeData: unref(getTreeData),
  treeNodeFilterProp: unref(getFieldNames).label,
}));
 
const { getSelectedNode } = useTree(treeDataRef, getFieldNames);
 
watch(
  () => unref(props.modelValue),
  (val) => {
    setValue(val);
  },
  { immediate: true },
);
 
watch(
  () => unref(props.value),
  (val) => {
    setValue(val);
  },
  { immediate: true },
);
 
watchEffect(() => {
  treeDataRef.value = props.options;
});
 
function setValue(value) {
  innerValue.value = !!value || value === 0 ? value : props.multiple ? [] : undefined;
}
function onChange(value, _label, extra) {
  if (!props.multiple && value && extra.preValue.some((o) => o.value === value)) return;
  const keys = props.multiple ? value : [value];
  const nodeList: any[] = [];
  for (const key of keys) {
    const node = getSelectedNode(key);
    if (node) nodeList.push(node);
  }
  const data = props.multiple ? nodeList : nodeList.length > 0 ? nodeList[0] : {};
  emit('update:modelValue', value);
  emit('update:value', value);
  emit('change', value, data);
}
</script>
 
<template>
  <TreeSelect class="jnpf-tree-select" v-bind="getBindValue" v-model:value="innerValue" @change="onChange">
    <template #title="data">
      <slot name="title" v-bind="data || {}">
        <div :key="data.id" class="custom-tree-node">
          <i v-if="data.icon && showIcon" :class="data.icon" class="custom-tree-node-icon"></i>
          <span class="custom-tree-node-icon-name">{{ data[getFieldNames.label] || innerValue }}</span>
        </div>
      </slot>
    </template>
    <template v-for="item in Object.keys($slots)" #[item]="data">
      <slot :name="item" v-bind="data || {}"></slot>
    </template>
  </TreeSelect>
</template>
<style lang="scss" scoped>
.jnpf-tree-select {
  :deep(.ant-select-selection-item) {
    .custom-tree-node-icon {
      display: none;
      margin-right: 6px;
      font-size: 14px;
      line-height: 30px;
      vertical-align: top;
    }
  }
}
</style>
<style lang="scss">
.ant-select-tree-treenode {
  .custom-tree-node-icon {
    margin-right: 6px;
    font-size: 14px;
    line-height: 24px;
    vertical-align: top;
  }
}
 
.ant-tree-select-dropdown {
  .ant-select-tree {
    .ant-select-tree-switcher {
      .ant-select-tree-switcher-icon {
        vertical-align: 0.25em;
      }
    }
  }
}
</style>