Skip to content

Commit 4ba3821

Browse files
IcetCodejd-solanki
andauthored
fix(select): rendering card slots (#181)
Co-authored-by: JD Solanki <jdsolanki0001@gmail.com>
1 parent 7368593 commit 4ba3821

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

packages/anu-vue/src/components/select/ASelect.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { AFloating, sameWidthFloatingUIMiddleware } from '@/components/floating'
88
import type { AListPropItems } from '@/components/list'
99
import { useDefaults } from '@/composables/useDefaults'
1010
import { extractItemValueFromItemOption } from '@/composables/useSelection'
11+
import { filterUsedSlots } from '@/utils/reactivity'
1112
1213
// SECTION Meta
1314
export interface ObjectOption { label: string; value: string | number }
@@ -135,7 +136,7 @@ function middleware() {
135136
>
136137
<!-- ℹ️ Recursively pass down slots to child -->
137138
<template
138-
v-for="(_, name) in aSelectCardSlots"
139+
v-for="name in filterUsedSlots(aSelectCardSlots)"
139140
#[name]="slotProps"
140141
>
141142
<slot
@@ -144,7 +145,7 @@ function middleware() {
144145
/>
145146
</template>
146147
<AList
147-
:items="options"
148+
:items="props.options"
148149
:model-value="props.modelValue"
149150
:emit-object="props.emitObject"
150151
class="a-select-options-list"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { objectKeys } from '@antfu/utils'
2+
3+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4+
export const filterUsedSlots = computed(() => <const T extends Record<string, any>>(slotsToUse: T): (keyof T)[] => {
5+
const slots = useSlots()
6+
7+
return objectKeys(slotsToUse).filter(key => slots[key as keyof typeof slots]) as (keyof T)[]
8+
})
Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,56 @@
11
import { mount } from '@vue/test-utils'
2-
import { describe, expect, it } from 'vitest'
2+
import { afterEach, describe, expect, it } from 'vitest'
33
import { nextTick, ref } from 'vue'
44
import { ASelect } from '../src'
55

66
describe('Testing ASelect', async () => {
7+
let wrapper: ReturnType<typeof mount<ASelect>>
8+
9+
afterEach(() => {
10+
document.body.innerHTML = ''
11+
})
12+
713
it('should render', () => {
814
const options = ref(['banana', 'apple', 'watermelon'])
9-
const wrapper = mount(() =>
15+
wrapper = mount(() =>
1016
<ASelect options={options.value}></ASelect>,
17+
{ attachTo: 'body' },
1118
)
1219
expect(wrapper).toBeDefined()
1320
})
1421

1522
it('should select default value', async () => {
1623
const options = ref(['banana', 'apple', 'watermelon'])
1724
const modelValue = ref('apple')
18-
const wrapper = mount(() =>
25+
wrapper = mount(() =>
1926
<ASelect
2027
v-model={modelValue.value}
2128
options={options.value}
2229
></ASelect>,
30+
{ attachTo: 'body' },
2331
)
2432
await nextTick()
2533
const inputElement = wrapper.find('.a-select-input').element as HTMLInputElement
2634
expect(inputElement.value).toContain('apple')
2735
})
36+
37+
it('should render card slots', async () => {
38+
const options = ref(['banana', 'apple', 'watermelon'])
39+
wrapper = mount(ASelect, {
40+
props: {
41+
options: options.value,
42+
},
43+
slots: {
44+
title: '<div class="title-slot">title</div>',
45+
subtitle: '<div class="subtitle-slot">subtitle</div>',
46+
},
47+
attachTo: 'body',
48+
})
49+
await wrapper.find('.a-select-input').trigger('click')
50+
await nextTick()
51+
const titleText = document.querySelector('.title-slot')?.textContent
52+
const subTitleText = document.querySelector('.subtitle-slot')?.textContent
53+
expect(titleText).toBe('title')
54+
expect(subTitleText).toBe('subtitle')
55+
})
2856
})

0 commit comments

Comments
 (0)