Skip to content

Commit 4995a16

Browse files
author
Damian Dulisz
committed
Migrate custom option templates to scoped slots
1 parent 4e993e4 commit 4995a16

File tree

8 files changed

+30
-63
lines changed

8 files changed

+30
-63
lines changed

docs/MultiselectExample.vue

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ div
99
:multiple="true",
1010
:show-labels="false",
1111
:limit="3",
12-
:option-function="customRender",
1312
@tag="onTagging"
1413
)
14+
template(slot="option", scope="props")
15+
span.badge__name {{ props.option.name }}
16+
img.badge__img(:src="props.option.img", :alt="props.option.name")
1517
span(slot="noResult").
1618
Badge not found. Suggest a badge <a class="typo__link" href="https://github.com/monterail/vue-multiselect/issues" target="_blank">here</a>.
1719
</template>
@@ -39,12 +41,6 @@ export default {
3941
onTagging (newTag) {
4042
this.source.push({ name: newTag, language: newTag })
4143
this.value.push({ name: newTag, language: newTag })
42-
},
43-
customRender (h, option) {
44-
return <div>
45-
<span class="badge__name">{ option.name }</span>
46-
<img class="badge__img" src={ option.img } alt={ option.name }/>
47-
</div>
4844
}
4945
}
5046
}

docs/docs.scss

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ section {
129129
margin-bottom: rem(20px)
130130
}
131131

132-
#examples {
132+
#examples {
133133
.grid__column {
134-
& > a {
134+
& > p > a {
135+
font-weight: $global-font-weight-bold;
135136
color: $primary-color;
136137
text-decoration: none;
137138
}

docs/main.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ new Vue({
8181
for (let i = sections.length - 1; i >= 0; i--) {
8282
if (window.scrollY > sections[i].offset) {
8383
this.currentPosition = sections[i].id
84-
console.log(i)
8584
break
8685
}
8786
}

docs/partials/api/_slots.pug

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,28 @@ h2.typo__h2#sub-slots(data-section) Slots
88
th.table__th Description
99
tbody
1010
tr.table__tr
11-
td.table__td: strong MaxElements
11+
td.table__td: strong option
12+
td.table__td
13+
| Slot for custom option template. See <a class="typo__link" href="#sub-custom-option-template">example.</a>
14+
br
15+
strong Default: Shows option label
16+
tr.table__tr
17+
td.table__td: strong maxElements
1218
td.table__td
1319
| Shows when the maximum options have been selected. Defaults to string:
14-
strong Maximum of &lt;max&gt; options selected. First remove a selected option to select another.
20+
br
21+
strong Default: Maximum of &lt;max&gt; options selected. First remove a selected option to select another.
1522
tr.table__tr
16-
td.table__td: strong NoResult
23+
td.table__td: strong noResult
1724
td.table__td
1825
| Shows when no elements match the search query. Defaults to string:
19-
strong No elements found. Consider changing the search query.
26+
br
27+
strong Default: No elements found. Consider changing the search query.
2028
tr.table__tr
21-
td.table__td: strong BeforeList
29+
td.table__td: strong beforeList
2230
td.table__td
2331
| Shows before the list, when dropdown is open.
2432
tr.table__tr
25-
td.table__td: strong AfterList
33+
td.table__td: strong afterList
2634
td.table__td
2735
| Shows after the list, when dropdown is open.

docs/partials/examples/CustomOption.vue

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ div
99
:options="options",
1010
:option-height="104",
1111
:custom-label="customLabel",
12-
:show-labels="false",
13-
:option-function="customRender"
12+
:show-labels="false"
1413
)
15-
span(slot="noResult").
16-
Oops! No elements found. Consider changing the search query.
14+
template(slot="option", scope="props")
15+
img.option__image(:src="props.option.img", alt="No Man’s Sky")
16+
.option__desc
17+
span.option__title {{ props.option.title }}
18+
span.option__small {{ props.option.desc }}
1719
pre.language-json
1820
code.
1921
{{ value }}
@@ -40,15 +42,6 @@ export default {
4042
methods: {
4143
customLabel ({ title, desc }) {
4244
return `${title}${desc}`
43-
},
44-
customRender (h, option) {
45-
return <div>
46-
<img class="option__image" src={ option.img } alt="No Man’s Sky" />
47-
<div class="option__desc">
48-
<span class="option__title">{ option.title }</span>
49-
<span class="option__small">{ option.desc }</span>
50-
</div>
51-
</div>
5245
}
5346
}
5447
}

docs/partials/examples/_examples.pug

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@
6666
+example('Tagging')
6767
+subsection('Custom option template')
6868
:markdown-it
69-
Vue-Multiselect can use custom render functions for option elements. This makes it possible to render different kind of data inside like images or even other components! For this to happen, pass a custom render function to the `:option-function` prop.
70-
71-
The `option-function` always receives the following `h`, `option`, `label` params. Please look at the `customRender` method for an example with JSX.
69+
You can use `option` [scoped slot](https://vuejs.org/v2/guide/components.html#Scoped-Slots) to provide a custom option template. The available `props` include `props.option` and `props.search`. Look at the provided example for more details.
7270

7371
To ensure the keyboard navigation works properly, remember to set the `:option-height` so it equals the height of the option template. By default, the component assumes an option height of 40px.
7472

src/Multiselect.vue

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@
7777
:data-selected="selectedLabelText"
7878
:data-deselect="deselectLabelText"
7979
class="multiselect__option">
80-
<multiselect-option
81-
:option-function="optionFunction"
82-
:label="getOptionLabel(option)"
83-
:option="option">
84-
</multiselect-option>
80+
<slot name="option" :option="option" :search="search">
81+
<span>{{ getOptionLabel(option) }}</span>
82+
</slot>
8583
</span>
8684
<span
8785
v-if="option.$isLabel"
@@ -105,13 +103,9 @@
105103
<script>
106104
import multiselectMixin from './multiselectMixin'
107105
import pointerMixin from './pointerMixin'
108-
import MultiselectOption from './MultiselectOption'
109106
110107
export default {
111108
name: 'vue-multiselect',
112-
components: {
113-
MultiselectOption
114-
},
115109
mixins: [multiselectMixin, pointerMixin],
116110
props: {
117111
/**
@@ -196,12 +190,6 @@
196190
disabled: {
197191
type: Boolean,
198192
default: false
199-
},
200-
optionFunction: {
201-
type: Function,
202-
default (h, option, label) {
203-
return h('span', {}, label)
204-
}
205193
}
206194
},
207195
computed: {

src/MultiselectOption.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)