UITreeSelect
UITreeSelect
UITreeSelect is a component that combines a dropdown select with a tree structure, allowing users to select options from hierarchical data. It’s ideal for selecting items from categories, folders, or any nested structure.
<template> <UITreeSelect v-model="selectedValue" :options="treeData" placeholder="Select an option" /></template>
<script setup>import { ref } from 'vue';
const selectedValue = ref(null);const treeData = [ { label: 'Parent 1', value: 'parent1', children: [ { label: 'Child 1.1', value: 'child1.1' }, { label: 'Child 1.2', value: 'child1.2' }, ], }, { label: 'Parent 2', value: 'parent2', children: [ { label: 'Child 2.1', value: 'child2.1' }, ], },];</script>Props
modelValue(any): The selected value(s), to be used with v-modeloptions(Array): Tree data optionsplaceholder(string, default: ‘Please select’): Input placeholder textdisabled(boolean, default: false): Whether the component is disabledclearable(boolean, default: false): Whether to show a clear buttonmultiple(boolean, default: false): Whether multiple selection is allowedcheckable(boolean, default: false): Whether to show checkboxes for selectionfilterable(boolean, default: false): Whether the options can be filtered by searchshowPath(boolean, default: false): Whether to show the full path in the selectionexpandAll(boolean, default: false): Whether to expand all nodes by defaultsize(‘small’ | ‘medium’ | ‘large’, default: ‘medium’): Size of the componentloading(boolean, default: false): Whether the tree select is in loading stateloadingText(string, default: ‘Loading…’): Text to display during loadingemptyText(string, default: ‘No data’): Text to display when there are no optionsvalueField(string, default: ‘value’): Field name for option valueslabelField(string, default: ‘label’): Field name for option labelschildrenField(string, default: ‘children’): Field name for children optionsvirtualScroll(boolean, default: false): Whether to use virtual scrolling for large datasets
Events
update:modelValue(value: any): Emitted when the selected value changesfocus(event: FocusEvent): Emitted when the component gains focusblur(event: FocusEvent): Emitted when the component loses focusclear(): Emitted when the clear button is clickedsearch(searchValue: string): Emitted when the user types in the search inputnode-expand(node: TreeNode): Emitted when a tree node is expandednode-collapse(node: TreeNode): Emitted when a tree node is collapsed
Slots
prefix: Custom prefix content in the inputsuffix: Custom suffix content in the inputempty: Custom empty content when no options are availablelabel(scoped slot with{ node, selected }): Custom node label renderingloading: Custom loading content
Usage Examples
- Basic Usage:
<template> <UITreeSelect v-model="value" :options="options" placeholder="Select category" /></template>
<script setup>import { ref } from 'vue';
const value = ref(null);const options = [ { label: 'Electronics', value: 'electronics', children: [ { label: 'Phones', value: 'phones' }, { label: 'Computers', value: 'computers' }, ], }, { label: 'Clothing', value: 'clothing', children: [ { label: 'Men', value: 'men' }, { label: 'Women', value: 'women' }, ], },];</script>- Multiple Selection:
<template> <UITreeSelect v-model="values" :options="options" placeholder="Select categories" multiple checkable /></template>
<script setup>import { ref } from 'vue';
const values = ref([]);const options = [ // Tree options data...];</script>- With Search Filtering:
<template> <UITreeSelect v-model="value" :options="options" placeholder="Search and select" filterable /></template>- Showing Full Path:
<template> <UITreeSelect v-model="value" :options="options" placeholder="Select with path" showPath /></template>- Custom Node Rendering:
<template> <UITreeSelect v-model="value" :options="options" placeholder="Custom nodes" > <template #label="{ node, selected }"> <div :style="{ fontWeight: selected ? 'bold' : 'normal' }"> <UIIcon :name="node.icon" v-if="node.icon" /> {{ node.label }} <span v-if="node.count" class="count">({{ node.count }})</span> </div> </template> </UITreeSelect></template>
<script setup>const options = [ { label: 'Projects', value: 'projects', icon: 'folder', count: 5, children: [ { label: 'Project A', value: 'project-a', icon: 'file', count: 3 }, { label: 'Project B', value: 'project-b', icon: 'file', count: 2 }, ], }, // More options...];</script>Best Practices
-
Data Organization:
- Keep hierarchy depth reasonable (ideally 2-4 levels)
- Use consistent naming conventions
- Consider alphabetical ordering when appropriate
- Include counts or metadata when helpful
-
User Experience:
- Use clear, descriptive labels
- Consider expandAll for small trees
- Enable filtering for large datasets
- Show path when context is important
- Use checkable mode for multiple selections
-
Performance:
- Enable virtual scrolling for large datasets
- Implement lazy loading for very large trees
- Optimize option rendering with custom slots
- Consider collapsed state for initial rendering
-
Accessibility:
- Include aria labels for screen readers
- Ensure keyboard navigation works properly
- Provide sufficient color contrast
- Use appropriate focus indicators