Skip to content

Commit

Permalink
fix(select): rendering card slots (#181)
Browse files Browse the repository at this point in the history
Co-authored-by: JD Solanki <jdsolanki0001@gmail.com>
  • Loading branch information
IcetCode and jd-solanki committed Jun 6, 2023
1 parent 7368593 commit 4ba3821
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/anu-vue/src/components/select/ASelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AFloating, sameWidthFloatingUIMiddleware } from '@/components/floating'
import type { AListPropItems } from '@/components/list'
import { useDefaults } from '@/composables/useDefaults'
import { extractItemValueFromItemOption } from '@/composables/useSelection'
import { filterUsedSlots } from '@/utils/reactivity'
// SECTION Meta
export interface ObjectOption { label: string; value: string | number }
Expand Down Expand Up @@ -135,7 +136,7 @@ function middleware() {
>
<!-- 鈩癸笍 Recursively pass down slots to child -->
<template
v-for="(_, name) in aSelectCardSlots"
v-for="name in filterUsedSlots(aSelectCardSlots)"
#[name]="slotProps"
>
<slot
Expand All @@ -144,7 +145,7 @@ function middleware() {
/>
</template>
<AList
:items="options"
:items="props.options"
:model-value="props.modelValue"
:emit-object="props.emitObject"
class="a-select-options-list"
Expand Down
8 changes: 8 additions & 0 deletions packages/anu-vue/src/utils/reactivity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { objectKeys } from '@antfu/utils'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const filterUsedSlots = computed(() => <const T extends Record<string, any>>(slotsToUse: T): (keyof T)[] => {
const slots = useSlots()

return objectKeys(slotsToUse).filter(key => slots[key as keyof typeof slots]) as (keyof T)[]
})
34 changes: 31 additions & 3 deletions packages/anu-vue/test/ASelect.test.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import { afterEach, describe, expect, it } from 'vitest'
import { nextTick, ref } from 'vue'
import { ASelect } from '../src'

describe('Testing ASelect', async () => {
let wrapper: ReturnType<typeof mount<ASelect>>

afterEach(() => {
document.body.innerHTML = ''
})

it('should render', () => {
const options = ref(['banana', 'apple', 'watermelon'])
const wrapper = mount(() =>
wrapper = mount(() =>
<ASelect options={options.value}></ASelect>,
{ attachTo: 'body' },
)
expect(wrapper).toBeDefined()
})

it('should select default value', async () => {
const options = ref(['banana', 'apple', 'watermelon'])
const modelValue = ref('apple')
const wrapper = mount(() =>
wrapper = mount(() =>
<ASelect
v-model={modelValue.value}
options={options.value}
></ASelect>,
{ attachTo: 'body' },
)
await nextTick()
const inputElement = wrapper.find('.a-select-input').element as HTMLInputElement
expect(inputElement.value).toContain('apple')
})

it('should render card slots', async () => {
const options = ref(['banana', 'apple', 'watermelon'])
wrapper = mount(ASelect, {
props: {
options: options.value,
},
slots: {
title: '<div class="title-slot">title</div>',
subtitle: '<div class="subtitle-slot">subtitle</div>',
},
attachTo: 'body',
})
await wrapper.find('.a-select-input').trigger('click')
await nextTick()
const titleText = document.querySelector('.title-slot')?.textContent
const subTitleText = document.querySelector('.subtitle-slot')?.textContent
expect(titleText).toBe('title')
expect(subTitleText).toBe('subtitle')
})
})

0 comments on commit 4ba3821

Please sign in to comment.