fix(select-tree): adjust tree node indentation and alignment logic
- Add level prop to track node hierarchy - Fix indentation calculation for leaves and nodes - Simplify alignment logic based on node level
This commit is contained in:
No files matched your search
@@ -21,7 +21,7 @@ function handleSelect(value: string) {
|
|||||||
<CommandItem
|
<CommandItem
|
||||||
:value="item.value"
|
:value="item.value"
|
||||||
class="flex items-center justify-between p-2 w-full text-sm font-normal hover:text-primary cursor-pointer rounded-md"
|
class="flex items-center justify-between p-2 w-full text-sm font-normal hover:text-primary cursor-pointer rounded-md"
|
||||||
:class="{ 'pl-12': shouldAlign }"
|
:class="{ 'pl-8': shouldAlign }"
|
||||||
@select="() => handleSelect(item.value)"
|
@select="() => handleSelect(item.value)"
|
||||||
>
|
>
|
||||||
<span class="text-sm font-normal">{{ item.label }}</span>
|
<span class="text-sm font-normal">{{ item.label }}</span>
|
||||||
|
|||||||
@@ -7,14 +7,13 @@ const props = defineProps<{
|
|||||||
item: TreeItem
|
item: TreeItem
|
||||||
selectedValue?: string
|
selectedValue?: string
|
||||||
onFetchChildren: (parentId: string) => Promise<void>
|
onFetchChildren: (parentId: string) => Promise<void>
|
||||||
|
level?: number
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const emit = defineEmits(['select'])
|
const emit = defineEmits(['select'])
|
||||||
|
|
||||||
// Computed untuk memastikan reactivity pada children
|
|
||||||
const hasChildren = computed(() => props.item.children && props.item.children.length > 0)
|
const hasChildren = computed(() => props.item.children && props.item.children.length > 0)
|
||||||
|
|
||||||
// State terpisah untuk chevron animation dan loading
|
|
||||||
const isOpen = ref(false)
|
const isOpen = ref(false)
|
||||||
const isLoading = ref(false)
|
const isLoading = ref(false)
|
||||||
const isChevronRotated = ref(false)
|
const isChevronRotated = ref(false)
|
||||||
@@ -27,14 +26,11 @@ function handleLabelClick() {
|
|||||||
handleSelect(props.item.value)
|
handleSelect(props.item.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Watch untuk handle fetch data ketika collapsible dibuka
|
|
||||||
watch(isOpen, async (newValue) => {
|
watch(isOpen, async (newValue) => {
|
||||||
console.log(`[TreeNode] ${props.item.label} - isOpen changed to:`, newValue)
|
console.log(`[TreeNode] ${props.item.label} - isOpen changed to:`, newValue)
|
||||||
|
|
||||||
// Update chevron rotation berdasarkan open state
|
|
||||||
isChevronRotated.value = newValue
|
isChevronRotated.value = newValue
|
||||||
|
|
||||||
// Jika membuka dan belum ada children, fetch data
|
|
||||||
if (newValue && props.item.hasChildren && !props.item.children && !isLoading.value) {
|
if (newValue && props.item.hasChildren && !props.item.children && !isLoading.value) {
|
||||||
console.log(`[TreeNode] Fetching children for: ${props.item.label}`)
|
console.log(`[TreeNode] Fetching children for: ${props.item.label}`)
|
||||||
isLoading.value = true
|
isLoading.value = true
|
||||||
@@ -65,9 +61,7 @@ watch(isOpen, async (newValue) => {
|
|||||||
variant="ghost"
|
variant="ghost"
|
||||||
class="h-4 w-4 p-0 flex items-center justify-center"
|
class="h-4 w-4 p-0 flex items-center justify-center"
|
||||||
>
|
>
|
||||||
<!-- Loading State -->
|
|
||||||
<Loader2 v-if="isLoading" class="w-4 h-4 animate-spin text-muted-foreground" />
|
<Loader2 v-if="isLoading" class="w-4 h-4 animate-spin text-muted-foreground" />
|
||||||
<!-- Chevron dengan animasi terpisah -->
|
|
||||||
<ChevronRight
|
<ChevronRight
|
||||||
v-else
|
v-else
|
||||||
class="w-4 h-4 transition-transform duration-200 ease-in-out text-muted-foreground"
|
class="w-4 h-4 transition-transform duration-200 ease-in-out text-muted-foreground"
|
||||||
@@ -93,7 +87,7 @@ watch(isOpen, async (newValue) => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Children Container -->
|
<!-- Children Container -->
|
||||||
<CollapsibleContent class="pl-8">
|
<CollapsibleContent class="pl-6">
|
||||||
<div v-if="!hasChildren" class="text-sm text-muted-foreground p-2">
|
<div v-if="!hasChildren" class="text-sm text-muted-foreground p-2">
|
||||||
{{ isLoading ? 'Memuat...' : 'Tidak ada data' }}
|
{{ isLoading ? 'Memuat...' : 'Tidak ada data' }}
|
||||||
</div>
|
</div>
|
||||||
@@ -102,6 +96,7 @@ watch(isOpen, async (newValue) => {
|
|||||||
:data="item.children!"
|
:data="item.children!"
|
||||||
:selected-value="selectedValue"
|
:selected-value="selectedValue"
|
||||||
:on-fetch-children="onFetchChildren"
|
:on-fetch-children="onFetchChildren"
|
||||||
|
:level="(level || 0) + 1"
|
||||||
@select="handleSelect"
|
@select="handleSelect"
|
||||||
/>
|
/>
|
||||||
</CollapsibleContent>
|
</CollapsibleContent>
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ const selectedLabel = computed(() => {
|
|||||||
:data="data"
|
:data="data"
|
||||||
:selected-value="modelValue"
|
:selected-value="modelValue"
|
||||||
:on-fetch-children="onFetchChildren"
|
:on-fetch-children="onFetchChildren"
|
||||||
|
:level="0"
|
||||||
@select="handleSelect"
|
@select="handleSelect"
|
||||||
/>
|
/>
|
||||||
</CommandGroup>
|
</CommandGroup>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ const props = defineProps<{
|
|||||||
data: TreeItem[]
|
data: TreeItem[]
|
||||||
selectedValue?: string
|
selectedValue?: string
|
||||||
onFetchChildren: (parentId: string) => Promise<void>
|
onFetchChildren: (parentId: string) => Promise<void>
|
||||||
|
level?: number
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const emit = defineEmits(['select'])
|
const emit = defineEmits(['select'])
|
||||||
@@ -19,25 +20,34 @@ function handleSelect(value: string) {
|
|||||||
const hasAnyChildrenInLevel = computed(() => {
|
const hasAnyChildrenInLevel = computed(() => {
|
||||||
return props.data.some(item => item.hasChildren)
|
return props.data.some(item => item.hasChildren)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Computed untuk menentukan apakah perlu alignment berdasarkan level
|
||||||
|
const shouldAlignLeaves = computed(() => {
|
||||||
|
// Di root level (level 0), selalu align leaf dengan tree nodes jika ada mixed content
|
||||||
|
// Di level lain, hanya align jika ada mixed content
|
||||||
|
const isRootLevel = (props.level || 0) === 0
|
||||||
|
const hasMixedContent = hasAnyChildrenInLevel.value && props.data.some(item => !item.hasChildren)
|
||||||
|
|
||||||
|
return isRootLevel ? hasAnyChildrenInLevel.value : hasMixedContent
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="tree-view min-w-max">
|
<div class="tree-view min-w-max">
|
||||||
<template v-for="item in data" :key="item.value">
|
<template v-for="item in data" :key="item.value">
|
||||||
<!-- Jika item memiliki children, gunakan TreeNode -->
|
|
||||||
<TreeNode
|
<TreeNode
|
||||||
v-if="item.hasChildren"
|
v-if="item.hasChildren"
|
||||||
:item="item"
|
:item="item"
|
||||||
:selected-value="selectedValue"
|
:selected-value="selectedValue"
|
||||||
:on-fetch-children="onFetchChildren"
|
:on-fetch-children="onFetchChildren"
|
||||||
|
:level="level || 0"
|
||||||
@select="handleSelect"
|
@select="handleSelect"
|
||||||
/>
|
/>
|
||||||
<!-- Jika item tidak memiliki children, gunakan Leaf -->
|
|
||||||
<Leaf
|
<Leaf
|
||||||
v-else
|
v-else
|
||||||
:item="item"
|
:item="item"
|
||||||
:selected-value="selectedValue"
|
:selected-value="selectedValue"
|
||||||
:should-align="hasAnyChildrenInLevel"
|
:should-align="shouldAlignLeaves"
|
||||||
@select="handleSelect"
|
@select="handleSelect"
|
||||||
/>
|
/>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
Reference in New Issue
Block a user