mirror of
https://github.com/tenrok/vue-select.git
synced 2026-06-22 10:30:34 +03:00
V3/update list items slot (#799)
* add tests for slots, add normalized function for passing options to slots * update active class * update active class
This commit is contained in:
@@ -9,12 +9,12 @@
|
|||||||
<div class="vs__selected-options" ref="selectedOptions">
|
<div class="vs__selected-options" ref="selectedOptions">
|
||||||
<slot v-for="option in selectedValue"
|
<slot v-for="option in selectedValue"
|
||||||
name="selected-option-container"
|
name="selected-option-container"
|
||||||
:option="(typeof option === 'object')?option:{[label]: option}"
|
:option="normalizeOptionForSlot(option)"
|
||||||
:deselect="deselect"
|
:deselect="deselect"
|
||||||
:multiple="multiple"
|
:multiple="multiple"
|
||||||
:disabled="disabled">
|
:disabled="disabled">
|
||||||
<span class="vs__selected" v-bind:key="option.index">
|
<span class="vs__selected" v-bind:key="option.index">
|
||||||
<slot name="selected-option" v-bind="(typeof option === 'object')?option:{[label]: option}">
|
<slot name="selected-option" v-bind="normalizeOptionForSlot(option)">
|
||||||
{{ getOptionLabel(option) }}
|
{{ getOptionLabel(option) }}
|
||||||
</slot>
|
</slot>
|
||||||
<button v-if="multiple" :disabled="disabled" @click="deselect(option)" type="button" class="vs__deselect" aria-label="Deselect option">
|
<button v-if="multiple" :disabled="disabled" @click="deselect(option)" type="button" class="vs__deselect" aria-label="Deselect option">
|
||||||
@@ -55,11 +55,11 @@
|
|||||||
v-for="(option, index) in filteredOptions"
|
v-for="(option, index) in filteredOptions"
|
||||||
:key="index"
|
:key="index"
|
||||||
class="vs__dropdown-option"
|
class="vs__dropdown-option"
|
||||||
:class="{ active: isOptionSelected(option), 'vs__dropdown-option--highlight': index === typeAheadPointer }"
|
:class="{ 'vs__dropdown-option--selected': isOptionSelected(option), 'vs__dropdown-option--highlight': index === typeAheadPointer }"
|
||||||
@mouseover="typeAheadPointer = index"
|
@mouseover="typeAheadPointer = index"
|
||||||
@mousedown.prevent.stop="select(option)"
|
@mousedown.prevent.stop="select(option)"
|
||||||
>
|
>
|
||||||
<slot name="option" v-bind="(typeof option === 'object')?option:{[label]: option}">
|
<slot name="option" v-bind="normalizeOptionForSlot(option)">
|
||||||
{{ getOptionLabel(option) }}
|
{{ getOptionLabel(option) }}
|
||||||
</slot>
|
</slot>
|
||||||
</li>
|
</li>
|
||||||
@@ -700,6 +700,16 @@
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensures that options are always
|
||||||
|
* passed as objects to scoped slots.
|
||||||
|
* @param option
|
||||||
|
* @return {*}
|
||||||
|
*/
|
||||||
|
normalizeOptionForSlot (option) {
|
||||||
|
return (typeof option === 'object') ? option : {[this.label]: option};
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If push-tags is true, push the
|
* If push-tags is true, push the
|
||||||
* given option to `this.pushedTags`.
|
* given option to `this.pushedTags`.
|
||||||
|
|||||||
+10
-5
@@ -28,15 +28,20 @@ export const selectWithProps = (propsData = {}) => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a Wrapper with a v-select component.
|
* Returns a Wrapper with a v-select component.
|
||||||
|
* @param props
|
||||||
* @param options
|
* @param options
|
||||||
* @return {Wrapper<Vue>}
|
* @return {Wrapper<Vue>}
|
||||||
*/
|
*/
|
||||||
export const mountDefault = (options = {}) =>
|
export const mountDefault = (props = {}, options = {}) => {
|
||||||
shallowMount(VueSelect, {
|
return shallowMount(VueSelect, {
|
||||||
propsData: { options: ["one", "two", "three"],
|
propsData: {
|
||||||
...options
|
options: ['one', 'two', 'three'],
|
||||||
}
|
...props,
|
||||||
|
},
|
||||||
|
...options,
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a v-select component directly.
|
* Returns a v-select component directly.
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import { mountDefault } from '../helpers';
|
||||||
|
|
||||||
|
describe('Scoped Slots', () => {
|
||||||
|
it('receives an option object to the selected-option-container slot', () => {
|
||||||
|
const Select = mountDefault(
|
||||||
|
{value: 'one'},
|
||||||
|
{
|
||||||
|
scopedSlots: {
|
||||||
|
'selected-option-container': `<span slot="selected-option-container" slot-scope="{option}">{{ option.label }}</span>`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(Select.find({ ref: 'selectedOptions' }).text()).toEqual('one')
|
||||||
|
});
|
||||||
|
|
||||||
|
it('receives an option object to the selected-option slot', () => {
|
||||||
|
const Select = mountDefault(
|
||||||
|
{value: 'one'},
|
||||||
|
{
|
||||||
|
scopedSlots: {
|
||||||
|
'selected-option': `<span slot="selected-option" slot-scope="option">{{ option.label }}</span>`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(Select.find('.vs__selected').text()).toEqual('one')
|
||||||
|
});
|
||||||
|
|
||||||
|
it('receives an option object to the option slot in the dropdown menu', () => {
|
||||||
|
const Select = mountDefault(
|
||||||
|
{value: 'one'},
|
||||||
|
{
|
||||||
|
scopedSlots: {
|
||||||
|
'option': `<span slot="option" slot-scope="option">{{ option.label }}</span>`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
Select.vm.open = true;
|
||||||
|
|
||||||
|
expect(Select.find({ref: 'dropdownMenu'}).text()).toEqual('onetwothree')
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user