Files
cdTestPlant3/cdTMP/src/components/ma-treeSlider/index.vue

115 lines
2.6 KiB
Vue
Raw Normal View History

2024-09-06 10:48:22 +08:00
<!--
- @Author XXX
- @Link XXX
-->
2023-06-08 21:09:28 +08:00
<template>
2024-09-06 10:48:22 +08:00
<div class="flex flex-col w-full h-full">
<a-input-group class="mb-2 w-full" size="mini">
<a-input
:placeholder="props?.searchPlaceholder"
allow-clear
@input="changeKeyword"
@clear="resetData"
/>
<a-button
@click="() => {
isExpand ? maTree.expandAll(false) : maTree.expandAll(true)
isExpand = ! isExpand
}"
>{{ isExpand ? '折叠' : '展开' }}</a-button>
</a-input-group>
<a-tree
blockNode
ref="maTree"
:data="treeData"
class="h-full w-full"
@select="handlerSelect"
:field-names="props.fieldNames"
v-model:selected-keys="modelValue"
v-bind="$attrs"
>
<template #icon v-if="props.icon"><component :is="props.icon" /></template>
</a-tree>
</div>
2023-06-08 21:09:28 +08:00
</template>
<script setup>
2024-09-06 10:48:22 +08:00
import { ref, watch, computed } from 'vue'
2023-06-08 21:09:28 +08:00
2024-09-06 10:48:22 +08:00
const treeData = ref([])
const maTree = ref()
const isExpand = ref(false)
2023-06-08 21:09:28 +08:00
2024-09-06 10:48:22 +08:00
const emit = defineEmits(['update:modelValue', 'click'])
2023-06-08 21:09:28 +08:00
2024-09-06 10:48:22 +08:00
const props = defineProps({
2023-06-08 21:09:28 +08:00
modelValue: { type: Array },
2024-09-06 10:48:22 +08:00
data: { type: Array },
2023-06-08 21:09:28 +08:00
searchPlaceholder: { type: String },
2024-09-06 10:48:22 +08:00
fieldNames: { type: Object, default: () => { return { title: 'label', key: 'value' } } },
icon: { type: String, default: undefined },
})
2023-06-08 21:09:28 +08:00
2024-09-06 10:48:22 +08:00
const modelValue = computed({
get() {
return props.modelValue
},
set(newVal) {
emit('update:modelValue', newVal)
2023-06-08 21:09:28 +08:00
}
2024-09-06 10:48:22 +08:00
})
2023-06-08 21:09:28 +08:00
2024-09-06 10:48:22 +08:00
watch(
() => props.data,
val => {
treeData.value = val
},
{ immediate: true, deep: true }
)
2023-06-08 21:09:28 +08:00
2024-09-06 10:48:22 +08:00
const handlerSelect = (item, data) => {
modelValue.value = [ item ]
emit('click', ...[item, data])
}
2023-06-08 21:09:28 +08:00
2024-09-06 10:48:22 +08:00
const resetData = () => treeData.value = props.data
const changeKeyword = (keyword) => {
if (!keyword || keyword === '') {
treeData.value = Object.assign(props.data, [])
return false
2023-06-08 21:09:28 +08:00
}
treeData.value = searchNode(keyword)
2024-09-06 10:48:22 +08:00
}
2023-06-08 21:09:28 +08:00
2024-09-06 10:48:22 +08:00
const searchNode = (keyword) => {
2023-06-08 21:09:28 +08:00
const loop = (data) => {
2024-09-06 10:48:22 +08:00
let tree = []
data.map(item => {
if (item.children && item.children.length > 0) {
const temp = loop(item.children)
tree.push(...temp)
} else if (item[props.fieldNames['title']].indexOf(keyword) !== -1) {
tree.push(item)
}
2023-06-08 21:09:28 +08:00
return tree
2024-09-06 10:48:22 +08:00
})
return tree
2023-06-08 21:09:28 +08:00
}
2024-09-06 10:48:22 +08:00
return loop(treeData.value)
}
2023-06-08 21:09:28 +08:00
2024-09-06 10:48:22 +08:00
defineExpose({ maTree })
2023-06-08 21:09:28 +08:00
</script>
<style scoped lang="less">
:deep(.arco-tree-node:hover) {
2024-09-06 10:48:22 +08:00
background-color: var(--color-fill-2);
border-radius: 3px;
2023-06-08 21:09:28 +08:00
}
:deep(.arco-tree-node-switcher) {
2024-09-06 10:48:22 +08:00
margin-left: 2px;
2023-06-08 21:09:28 +08:00
}
</style>