Skip to content
This repository was archived by the owner on Jul 26, 2022. It is now read-only.

Commit e9fd33b

Browse files
committed
Add suggestion scoped slot
1 parent 555f38f commit e9fd33b

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

src/components/VueBootstrapTypeahead.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@
3434
:text-variant="textVariant"
3535
:minMatchingChars="minMatchingChars"
3636
@hit="handleHit"
37-
/>
37+
>
38+
<!-- pass down all scoped slots -->
39+
<template v-for="(slot, slotName) in $scopedSlots" :slot="slotName" slot-scope="{ data, htmlText }">
40+
<slot :name="slotName" v-bind="{ data, htmlText }"></slot>
41+
</template>
42+
<!-- below is the right solution, however if the user does not provide a scoped slot, vue will still set $scopedSlots.suggestion to a blank scope
43+
<template v-if="$scopedSlots.suggestion" slot="suggestion" slot-scope="{ data, htmlText }">
44+
<slot name="suggestion" v-bind="{ data, htmlText }" />
45+
</template>-->
46+
</vue-bootstrap-typeahead-list>
3847
</div>
3948
</template>
4049

src/components/VueBootstrapTypeaheadList.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
<div class="list-group shadow">
33
<vue-bootstrap-typeahead-list-item
44
v-for="(item, id) in matchedItems" :key="id"
5-
v-html="highlight(item.text)"
5+
:data="item.data"
6+
:html-text="highlight(item.text)"
67
:background-variant="backgroundVariant"
78
:text-variant="textVariant"
89
@click.native="handleHit(item, $event)"
9-
/>
10+
>
11+
<template v-if="$scopedSlots.suggestion" slot="suggestion" slot-scope="{ data, htmlText }">
12+
<slot name="suggestion" v-bind="{ data, htmlText }" />
13+
</template>
14+
</vue-bootstrap-typeahead-list-item>
1015
</div>
1116
</template>
1217

src/components/VueBootstrapTypeaheadListItem.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
@mouseover="active = true"
66
@mouseout="active = false"
77
>
8-
<slot></slot>
8+
<slot name="suggestion" v-bind="{ data: data, htmlText: htmlText }">
9+
<span v-html="htmlText"></span>
10+
</slot>
911
</a>
1012
</template>
1113

@@ -14,6 +16,10 @@ export default {
1416
name: 'VueBootstrapTypeaheadListItem',
1517
1618
props: {
19+
data: {},
20+
htmlText: {
21+
type: String
22+
},
1723
backgroundVariant: {
1824
type: String
1925
},

src/views/Home.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div>
3-
<div class="row">
3+
<div class="row mb-5">
44
<div class="col-md-6">
55
<h2>A simple typeahead for Vue 2 using Bootstrap 4</h2>
66
<ul>
@@ -21,7 +21,11 @@
2121
:serializer="s => s.name"
2222
placeholder="Canada, United States, etc..."
2323
@hit="selectedCountry = $event"
24-
/>
24+
>
25+
<template slot="suggestion" slot-scope="{ data, htmlText }">
26+
<span v-html="htmlText"></span>&nbsp;<small>{{ data.code }}</small>
27+
</template>
28+
</vue-bootstrap-typeahead>
2529
</b-card>
2630
</div>
2731
</div>
@@ -46,7 +50,7 @@
4650
</vue-bootstrap-typeahead>
4751
</div>
4852
</div>
49-
<div class="row">
53+
<div class="row mb-5">
5054
<div class="col">
5155
<b-card title="Selected Address">
5256
<p class="card-text">

0 commit comments

Comments
 (0)