Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(group/layout/helpers): remove findChildrenWithProvide reliance #21118

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions packages/vuetify/src/composables/group.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { useProxiedModel } from './proxiedModel'

// Utilities
import { computed, inject, onBeforeUnmount, onMounted, onUpdated, provide, reactive, toRef, unref, useId, watch } from 'vue'
import { consoleWarn, deepEqual, findChildrenWithProvide, getCurrentInstance, propsFactory, wrapInArray } from '@/util'
import { consoleWarn, deepEqual, getCurrentInstance, propsFactory, wrapInArray } from '@/util'

// Types
import type { ComponentInternalInstance, ComputedRef, ExtractPropTypes, InjectionKey, PropType, Ref, UnwrapRef } from 'vue'
@@ -180,26 +180,16 @@ export function useGroup (
}
)

const groupVm = getCurrentInstance('useGroup')

function register (item: GroupItem, vm: ComponentInternalInstance) {
function register (item: GroupItem) {
// Is there a better way to fix this typing?
const unwrapped = item as unknown as UnwrapRef<GroupItem>

const key = Symbol.for(`${injectKey.description}:id`)
const children = findChildrenWithProvide(key, groupVm?.vnode)
const index = children.indexOf(vm)

if (unref(unwrapped.value) == null) {
unwrapped.value = index
unwrapped.value = items.length
unwrapped.useIndexAsValue = true
}

if (index > -1) {
items.splice(index, 0, unwrapped)
} else {
items.push(unwrapped)
}
items.push(unwrapped)
}

function unregister (id: string) {
18 changes: 4 additions & 14 deletions packages/vuetify/src/composables/layout.ts
Original file line number Diff line number Diff line change
@@ -15,10 +15,10 @@ import {
shallowRef,
useId,
} from 'vue'
import { convertToUnit, findChildrenWithProvide, getCurrentInstance, propsFactory } from '@/util'
import { convertToUnit, propsFactory } from '@/util'

// Types
import type { ComponentInternalInstance, CSSProperties, InjectionKey, Prop, Ref } from 'vue'
import type { CSSProperties, InjectionKey, Prop, Ref } from 'vue'

export type Position = 'top' | 'left' | 'right' | 'bottom'

@@ -37,7 +37,6 @@ interface LayoutItem extends Layer {

interface LayoutProvide {
register: (
vm: ComponentInternalInstance,
options: {
id: string
order: Ref<number>
@@ -115,8 +114,6 @@ export function useLayoutItem (options: {

const id = options.id ?? `layout-item-${useId()}`

const vm = getCurrentInstance('useLayoutItem')

provide(VuetifyLayoutItemKey, { id })

const isKeptAlive = shallowRef(false)
@@ -126,7 +123,7 @@ export function useLayoutItem (options: {
const {
layoutItemStyles,
layoutItemScrimStyles,
} = layout.register(vm, {
} = layout.register({
...options,
active: computed(() => isKeptAlive.value ? false : options.active.value),
id,
@@ -246,16 +243,13 @@ export function createLayout (props: { overlaps?: string[], fullHeight?: boolean
return items.value.find(item => item.id === id)
}

const rootVm = getCurrentInstance('createLayout')

const isMounted = shallowRef(false)
onMounted(() => {
isMounted.value = true
})

provide(VuetifyLayoutKey, {
register: (
vm: ComponentInternalInstance,
{
id,
order,
@@ -273,11 +267,7 @@ export function createLayout (props: { overlaps?: string[], fullHeight?: boolean
activeItems.set(id, active)
disableTransitions && disabledTransitions.set(id, disableTransitions)

const instances = findChildrenWithProvide(VuetifyLayoutItemKey, rootVm?.vnode)
const instanceIndex = instances.indexOf(vm)

if (instanceIndex > -1) registered.value.splice(instanceIndex, 0, id)
else registered.value.push(id)
registered.value.push(id)

const index = computed(() => items.value.findIndex(i => i.id === id))
const zIndex = computed(() => rootZIndex.value + (layers.value.length * 2) - (index.value * 2))
26 changes: 0 additions & 26 deletions packages/vuetify/src/util/helpers.ts
Original file line number Diff line number Diff line change
@@ -4,16 +4,13 @@ import { IN_BROWSER } from '@/util/globals'

// Types
import type {
ComponentInternalInstance,
ComponentPublicInstance,
ComputedGetter,
InjectionKey,
PropType,
Ref,
ToRefs,
VNode,
VNodeArrayChildren,
VNodeChild,
WatchOptions,
} from 'vue'

@@ -527,29 +524,6 @@ toKebabCase.cache = new Map<string, string>()

export type MaybeRef<T> = T | Ref<T>

export function findChildrenWithProvide (
key: InjectionKey<any> | symbol,
vnode?: VNodeChild,
): ComponentInternalInstance[] {
if (!vnode || typeof vnode !== 'object') return []

if (Array.isArray(vnode)) {
return vnode.map(child => findChildrenWithProvide(key, child)).flat(1)
} else if (vnode.suspense) {
return findChildrenWithProvide(key, vnode.ssContent!)
} else if (Array.isArray(vnode.children)) {
return vnode.children.map(child => findChildrenWithProvide(key, child)).flat(1)
} else if (vnode.component) {
if (Object.getOwnPropertySymbols(vnode.component.provides).includes(key as symbol)) {
return [vnode.component]
} else if (vnode.component.subTree) {
return findChildrenWithProvide(key, vnode.component.subTree).flat(1)
}
}

return []
}

export class CircularBuffer<T = never> {
readonly #arr: Array<T> = []
#pointer = 0

Unchanged files with check annotations Beta

wrapper.vm.next()
await nextTick()
expect(wrapper.emitted()['update:modelValue']).toEqual([

Check failure on line 240 in packages/vuetify/src/composables/__tests__/group.spec.ts

GitHub Actions / Test (Unit)

composables/__tests__/group.spec.ts > group > with explicit values > should select newly inserted value

AssertionError: expected [ [ 'two' ] ] to deeply equal [ [ 'three' ] ] - Expected + Received [ [ - "three", + "two", ], ] ❯ composables/__tests__/group.spec.ts:240:54

Check failure on line 240 in packages/vuetify/src/composables/__tests__/group.spec.ts

GitHub Actions / Test (Unit)

composables/__tests__/group.spec.ts > group > with explicit values > should select newly inserted value

AssertionError: expected [ [ 'two' ] ] to deeply equal [ [ 'three' ] ] - Expected + Received [ [ - "three", + "two", ], ] ❯ composables/__tests__/group.spec.ts:240:54
['three'],
])
})
await items[3].trigger('click')
await items[4].trigger('click')
expect(wrapper.emitted()['update:modelValue']).toEqual([[1], [2], [3], [4]])

Check failure on line 404 in packages/vuetify/src/composables/__tests__/group.spec.ts

GitHub Actions / Test (Unit)

composables/__tests__/group.spec.ts > group > with implicit values > should update the items that use index as the value when delete

AssertionError: expected [ [ 1 ], [ 2 ], [ undefined ], [ 3 ] ] to deeply equal [ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ] - Expected + Received [ [ 1, ], [ 2, ], [ - 3, + undefined, ], [ - 4, + 3, ], ] ❯ composables/__tests__/group.spec.ts:404:54

Check failure on line 404 in packages/vuetify/src/composables/__tests__/group.spec.ts

GitHub Actions / Test (Unit)

composables/__tests__/group.spec.ts > group > with implicit values > should update the items that use index as the value when delete

AssertionError: expected [ [ 1 ], [ 2 ], [ undefined ], [ 3 ] ] to deeply equal [ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ] - Expected + Received [ [ 1, ], [ 2, ], [ - 3, + undefined, ], [ - 4, + 3, ], ] ❯ composables/__tests__/group.spec.ts:404:54
})
})
})