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

Add suggestion scoped slot #9

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/components/VueBootstrapTypeahead.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@
:text-variant="textVariant"
:minMatchingChars="minMatchingChars"
@hit="handleHit"
/>
>
<!-- pass down all scoped slots -->
<template v-for="(slot, slotName) in $scopedSlots" :slot="slotName" slot-scope="{ data, htmlText }">
<slot :name="slotName" v-bind="{ data, htmlText }"></slot>
</template>
<!-- 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
<template v-if="$scopedSlots.suggestion" slot="suggestion" slot-scope="{ data, htmlText }">
<slot name="suggestion" v-bind="{ data, htmlText }" />
</template>-->
</vue-bootstrap-typeahead-list>
</div>
</template>

Expand Down
9 changes: 7 additions & 2 deletions src/components/VueBootstrapTypeaheadList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
<div class="list-group shadow">
<vue-bootstrap-typeahead-list-item
v-for="(item, id) in matchedItems" :key="id"
v-html="highlight(item.text)"
:data="item.data"
:html-text="highlight(item.text)"
:background-variant="backgroundVariant"
:text-variant="textVariant"
@click.native="handleHit(item, $event)"
/>
>
<template v-if="$scopedSlots.suggestion" slot="suggestion" slot-scope="{ data, htmlText }">
<slot name="suggestion" v-bind="{ data, htmlText }" />
</template>
</vue-bootstrap-typeahead-list-item>
</div>
</template>

Expand Down
8 changes: 7 additions & 1 deletion src/components/VueBootstrapTypeaheadListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
@mouseover="active = true"
@mouseout="active = false"
>
<slot></slot>
<slot name="suggestion" v-bind="{ data: data, htmlText: htmlText }">
<span v-html="htmlText"></span>
</slot>
</a>
</template>

Expand All @@ -14,6 +16,10 @@ export default {
name: 'VueBootstrapTypeaheadListItem',

props: {
data: {},
htmlText: {
type: String
},
backgroundVariant: {
type: String
},
Expand Down
10 changes: 7 additions & 3 deletions src/views/Home.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div>
<div class="row">
<div class="row mb-5">
<div class="col-md-6">
<h2>A simple typeahead for Vue 2 using Bootstrap 4</h2>
<ul>
Expand All @@ -21,7 +21,11 @@
:serializer="s => s.name"
placeholder="Canada, United States, etc..."
@hit="selectedCountry = $event"
/>
>
<template slot="suggestion" slot-scope="{ data, htmlText }">
<span v-html="htmlText"></span>&nbsp;<small>{{ data.code }}</small>
</template>
</vue-bootstrap-typeahead>
</b-card>
</div>
</div>
Expand All @@ -46,7 +50,7 @@
</vue-bootstrap-typeahead>
</div>
</div>
<div class="row">
<div class="row mb-5">
<div class="col">
<b-card title="Selected Address">
<p class="card-text">
Expand Down