Skip to content

Commit 9076ca2

Browse files
authored
fix(components): fall back to md for custom sizes in virtualizer (#6917)
1 parent 5fd94e1 commit 9076ca2

4 files changed

Lines changed: 58 additions & 19 deletions

File tree

src/runtime/components/Tree.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ const flattenedPaddingFormula = computed(() => {
190190
lg: { base: 3, perLevel: 7 }, // px-3, ms-5.5 + ps-1.5
191191
xl: { base: 3, perLevel: 7.5 } // px-3, ms-6 + ps-1.5
192192
}
193-
const config = sizeConfig[props.size || 'md']
193+
const config = sizeConfig[props.size as keyof typeof sizeConfig] ?? sizeConfig.md
194194
return (level: number) => `calc(var(--spacing) * ${(level - 1) * config.perLevel + config.base})`
195195
})
196196

src/runtime/utils/virtualizer.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ function itemHasDescription(item: any, descriptionKey: string): boolean {
88
return value !== undefined && value !== null && value !== ''
99
}
1010

11-
function getSize(size: 'xs' | 'sm' | 'md' | 'lg' | 'xl', hasDescription: boolean): number {
12-
if (hasDescription) {
13-
return ({
14-
xs: 44,
15-
sm: 48,
16-
md: 52,
17-
lg: 56,
18-
xl: 60
19-
})[size]
20-
}
11+
function getSize(size: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | (string & {}), hasDescription: boolean): number {
12+
const sizes = hasDescription
13+
? {
14+
xs: 44,
15+
sm: 48,
16+
md: 52,
17+
lg: 56,
18+
xl: 60
19+
}
20+
: {
21+
xs: 24,
22+
sm: 28,
23+
md: 32,
24+
lg: 36,
25+
xl: 40
26+
}
2127

22-
return ({
23-
xs: 24,
24-
sm: 28,
25-
md: 32,
26-
lg: 36,
27-
xl: 40
28-
})[size]
28+
return sizes[size as keyof typeof sizes] ?? sizes.md
2929
}
3030

3131
/**
3232
* Get estimate size for virtualizers that checks each item individually
3333
*/
34-
export function getEstimateSize(items: any[], size: 'xs' | 'sm' | 'md' | 'lg' | 'xl', descriptionKey?: string, hasDescriptionSlot?: boolean): (index: number) => number {
34+
export function getEstimateSize(items: any[], size: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | (string & {}), descriptionKey?: string, hasDescriptionSlot?: boolean): (index: number) => number {
3535
const sizeWithDescription = getSize(size, true)
3636
const sizeWithoutDescription = getSize(size, false)
3737

test/components/Tree.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ describe('Tree', () => {
7373
['with dynamic slot', { props, slots: { app: () => 'dynamic slot' } }]
7474
])
7575

76+
it('does not throw when flattened with a custom theme size', async () => {
77+
const wrapper = await mountSuspended(Tree, {
78+
props: {
79+
items: [{ label: 'app', defaultExpanded: true, children: items }],
80+
nested: false,
81+
size: 'xxs' as any
82+
}
83+
})
84+
expect(wrapper.exists()).toBe(true)
85+
})
86+
7687
it('passes accessibility tests', async () => {
7788
const wrapper = await mountSuspended(Tree, {
7889
props: {

test/utils/virtualizer.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { getEstimateSize } from '../../src/runtime/utils/virtualizer'
3+
4+
describe('getEstimateSize', () => {
5+
it('returns the height for each built-in size', () => {
6+
expect(getEstimateSize([{ label: 'foo' }], 'xs')(0)).toBe(24)
7+
expect(getEstimateSize([{ label: 'foo' }], 'xl')(0)).toBe(40)
8+
})
9+
10+
it('falls back to the `md` size for a custom theme size', () => {
11+
expect(getEstimateSize([{ label: 'foo' }], 'xxs')(0)).toBe(32)
12+
})
13+
14+
it('falls back to the `md` size for a custom theme size with a description', () => {
15+
const items = [{ label: 'foo', description: 'bar' }]
16+
17+
expect(getEstimateSize(items, 'xxs', 'description')(0)).toBe(52)
18+
expect(getEstimateSize(items, 'xxs', undefined, true)(0)).toBe(52)
19+
})
20+
21+
it('uses the larger size only for items that have a description', () => {
22+
const items = [{ label: 'foo', description: 'bar' }, { label: 'baz' }]
23+
const estimateSize = getEstimateSize(items, 'md', 'description')
24+
25+
expect(estimateSize(0)).toBe(52)
26+
expect(estimateSize(1)).toBe(32)
27+
})
28+
})

0 commit comments

Comments
 (0)