Skip to content

Commit c734b6c

Browse files
committed
refactor: move watch to nested
1 parent edb46f3 commit c734b6c

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

packages/vuetify/src/components/VList/VList.tsx

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { makeThemeProps, provideTheme } from '@/composables/theme'
2222
import { makeVariantProps } from '@/composables/variant'
2323

2424
// Utilities
25-
import { computed, ref, shallowRef, toRef, watch } from 'vue'
25+
import { computed, ref, shallowRef, toRef } from 'vue'
2626
import {
2727
EventProp,
2828
focusChild,
@@ -169,34 +169,7 @@ export const VList = genericComponent<new <
169169
const { dimensionStyles } = useDimension(props)
170170
const { elevationClasses } = useElevation(props)
171171
const { roundedClasses } = useRounded(props)
172-
const { children, disabled, open, parents, select, getPath } = useNested(props)
173-
174-
function flatten (items: InternalListItem<any>[]): InternalListItem<any>[] {
175-
return [
176-
...items,
177-
...items.length ? flatten(items.flatMap(x => x.children ?? [])) : [],
178-
]
179-
}
180-
181-
watch(items, val => {
182-
if (props.itemsRegistration === 'render') return
183-
const allNodes = flatten(val)
184-
children.value = new Map(
185-
allNodes
186-
.filter(item => item.children)
187-
.map(item => [item.value, item.children!.map(x => x.value)])
188-
)
189-
parents.value = new Map(
190-
allNodes
191-
.filter(item => !val.includes(item))
192-
.map(item => [item.value, allNodes.find(x => x.children?.includes(item))?.value])
193-
)
194-
disabled.value = new Set(
195-
allNodes
196-
.filter(item => (item as any).disabled)
197-
.map(item => item.value)
198-
)
199-
}, { immediate: true })
172+
const { children, open, parents, select, getPath } = useNested(props, items)
200173

201174
const lineClasses = toRef(() => props.lines ? `v-list--${props.lines}-line` : undefined)
202175
const activeColor = toRef(() => props.activeColor)

packages/vuetify/src/composables/nested/nested.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import type { InjectionKey, MaybeRefOrGetter, PropType, Ref } from 'vue'
3838
import type { ActiveStrategy } from './activeStrategies'
3939
import type { OpenStrategy } from './openStrategies'
4040
import type { SelectStrategy } from './selectStrategies'
41+
import type { ListItem } from '@/composables/list-items'
4142
import type { EventProp } from '@/util'
4243

4344
export type ActiveStrategyProp =
@@ -140,7 +141,7 @@ export const makeNestedProps = propsFactory({
140141
},
141142
}, 'nested')
142143

143-
export const useNested = (props: NestedProps) => {
144+
export const useNested = (props: NestedProps, items: Ref<ListItem[]>) => {
144145
let isUnmounted = false
145146
const children = shallowRef(new Map<unknown, unknown[]>())
146147
const parents = shallowRef(new Map<unknown, unknown>())
@@ -233,6 +234,34 @@ export const useNested = (props: NestedProps) => {
233234
parents.value = new Map(parents.value)
234235
}, 100)
235236

237+
function flatten (items: ListItem[]): ListItem[] {
238+
return [
239+
...items,
240+
...items.length ? flatten(items.flatMap(x => x.children ?? [])) : [],
241+
]
242+
}
243+
244+
watch(items, val => {
245+
if (props.itemsRegistration === 'render') return
246+
247+
const allNodes = flatten(val)
248+
children.value = new Map(
249+
allNodes
250+
.filter(item => item.children)
251+
.map(item => [item.value, item.children!.map(x => x.value)])
252+
)
253+
parents.value = new Map(
254+
allNodes
255+
.filter(item => !val.includes(item))
256+
.map(item => [item.value, allNodes.find(x => x.children?.includes(item))?.value])
257+
)
258+
disabled.value = new Set(
259+
allNodes
260+
.filter(item => (item as any).disabled)
261+
.map(item => item.value)
262+
)
263+
}, { immediate: true })
264+
236265
const nested: NestedProvide = {
237266
id: shallowRef(),
238267
root: {

0 commit comments

Comments
 (0)