Skip to content

Commit cebcc14

Browse files
authored
Merge pull request #732 from Zoey1988/main
feat(BFormTags): Add tag scoped slot
2 parents 2e92a66 + 80c6ff6 commit cebcc14

File tree

2 files changed

+83
-10
lines changed

2 files changed

+83
-10
lines changed

packages/bootstrap-vue-3/src/components/BFormTags/BFormTags.vue

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@
3333
:id="`${computedId}tag_list__`"
3434
class="b-form-tags-list list-unstyled mb-0 d-flex flex-wrap align-items-center"
3535
>
36-
<b-form-tag
37-
v-for="tag in tags"
38-
:key="tag"
39-
:class="tagClass"
40-
tag="li"
41-
:variant="tagVariant"
42-
:pill="tagPillsBoolean"
43-
@remove="removeTag"
44-
>{{ tag }}</b-form-tag
45-
>
36+
<template v-for="tag in tags">
37+
<slot name="tag" v-bind="{tag, tagClass, tagVariant, tagPillsBoolean, removeTag}">
38+
<b-form-tag
39+
:key="tag"
40+
:class="tagClass"
41+
tag="li"
42+
:variant="tagVariant"
43+
:pill="tagPillsBoolean"
44+
@remove="removeTag"
45+
>{{ tag }}</b-form-tag
46+
>
47+
</slot>
48+
</template>
4649
<li
4750
role="none"
4851
aria-live="off"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {enableAutoUnmount, mount} from '@vue/test-utils'
2+
import BFormTags from './BFormTags.vue'
3+
import {afterEach, describe, expect, it} from 'vitest'
4+
5+
describe('form-tags', () => {
6+
enableAutoUnmount(afterEach)
7+
8+
it('has proper root element', () => {
9+
const wrapper = mount(BFormTags)
10+
11+
expect(wrapper.element.tagName).toBe('DIV')
12+
expect(wrapper.classes()).toContain('b-form-tags')
13+
expect(wrapper.classes()).toContain('form-control')
14+
})
15+
16+
it('has proper input element', () => {
17+
const wrapper = mount(BFormTags)
18+
19+
const $input = wrapper.find('input')
20+
21+
expect($input.exists()).toBe(true)
22+
expect($input.classes()).toContain('b-form-tags-input')
23+
})
24+
25+
it('adds new tag', async () => {
26+
const wrapper = mount(BFormTags)
27+
28+
const $input = wrapper.get('input')
29+
$input.element.value = 'test'
30+
await $input.trigger('input')
31+
32+
const $tagStateEmitted = wrapper.emitted('tag-state') as unknown[][]
33+
34+
expect($tagStateEmitted).toBeDefined()
35+
expect($tagStateEmitted[0][0]).toEqual(['test'])
36+
37+
await $input.trigger('keydown', {key: 'Enter'})
38+
39+
expect(wrapper.emitted('update:modelValue')).toBeDefined()
40+
})
41+
42+
it('renders tag slot', async () => {
43+
const wrapper = mount(BFormTags, {
44+
props: {
45+
modelValue: ['foo', 'bar'],
46+
},
47+
slots: {
48+
tag: `
49+
<template #tag="params">
50+
<button class="custom-tag" @click="params.removeTag">{{ params.tag }}</button>
51+
</template>
52+
`,
53+
},
54+
})
55+
56+
const $tags = wrapper.findAll('.custom-tag')
57+
expect($tags.length).toBe(2)
58+
59+
expect($tags[0].text()).toBe('foo')
60+
expect($tags[1].text()).toBe('bar')
61+
62+
await $tags[1].trigger('click')
63+
64+
const $emitted = wrapper.emitted('update:modelValue') as unknown[][]
65+
66+
expect($emitted.length).toBe(1)
67+
expect($emitted).toBeDefined()
68+
expect($emitted[0][0]).toEqual(['foo'])
69+
})
70+
})

0 commit comments

Comments
 (0)