2
0
mirror of https://github.com/tenrok/vue-select.git synced 2026-06-22 10:30:34 +03:00

refactor toggleDropdown click handler (#992)

This commit is contained in:
Jeff Sagal
2019-11-14 11:05:05 -08:00
committed by GitHub
parent a85181b5fb
commit 1d024d6ad1
2 changed files with 53 additions and 45 deletions
+15 -24
View File
@@ -17,7 +17,7 @@
<slot name="selected-option" v-bind="normalizeOptionForSlot(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" ref="deselectButtons">
<component :is="childComponents.Deselect" /> <component :is="childComponents.Deselect" />
</button> </button>
</span> </span>
@@ -36,6 +36,7 @@
type="button" type="button"
class="vs__clear" class="vs__clear"
title="Clear selection" title="Clear selection"
ref="clearButton"
> >
<component :is="childComponents.Deselect" /> <component :is="childComponents.Deselect" />
</button> </button>
@@ -652,33 +653,23 @@
* @param {Event} e * @param {Event} e
* @return {void} * @return {void}
*/ */
toggleDropdown (e) { toggleDropdown ({target}) {
const target = e.target; // don't react to click on deselect/clear buttons,
const toggleTargets = [ // they dropdown state will be set in their click handlers
this.$el, const ignoredButtons = [
this.searchEl, ...(this.$refs['deselectButtons'] || []),
this.$refs.toggle, ...([this.$refs['clearButton']] || [])
this.$refs.actions,
this.$refs.selectedOptions,
]; ];
if (typeof this.$refs.openIndicator !== 'undefined') { if (ignoredButtons.some(ref => ref.contains(target) || ref === target)) {
toggleTargets.push( return;
this.$refs.openIndicator.$el,
// the line below is a bit gross, but required to support IE11 without adding polyfills
...Array.prototype.slice.call(this.$refs.openIndicator.$el.childNodes),
);
} }
if (toggleTargets.indexOf(target) > -1 || target.classList.contains('vs__selected')) { if (this.open) {
if (this.open) { this.searchEl.blur();
this.searchEl.blur(); // dropdown will close on blur } else if (!this.disabled) {
} else { this.open = true;
if (!this.disabled) { this.searchEl.focus();
this.open = true;
this.searchEl.focus();
}
}
} }
}, },
+38 -21
View File
@@ -10,32 +10,49 @@ describe('Scoped Slots', () => {
}, },
}); });
expect(Select.find({ ref: 'selectedOptions' }).text()).toEqual('one') expect(Select.find({ref: 'selectedOptions'}).text()).toEqual('one');
}); });
it('receives an option object to the selected-option slot', () => { describe('Slot: selected-option', () => {
const Select = mountDefault( it('receives an option object to the selected-option slot', () => {
{value: 'one'}, const Select = mountDefault(
{ {value: 'one'},
scopedSlots: { {
'selected-option': `<span slot="selected-option" slot-scope="option">{{ option.label }}</span>`, scopedSlots: {
}, 'selected-option': `<span slot="selected-option" slot-scope="option">{{ option.label }}</span>`,
},
});
expect(Select.find('.vs__selected').text()).toEqual('one');
});
it('opens the dropdown when clicking an option in selected-option slot',
() => {
const Select = mountDefault(
{value: 'one'},
{
scopedSlots: {
'selected-option': `<span class="my-option" slot-scope="option">{{ option.label }}</span>`,
},
});
Select.find('.my-option').trigger('mousedown');
expect(Select.vm.open).toEqual(true);
}); });
expect(Select.find('.vs__selected').text()).toEqual('one')
}); });
it('receives an option object to the option slot in the dropdown menu', () => { it('receives an option object to the option slot in the dropdown menu',
const Select = mountDefault( () => {
{value: 'one'}, const Select = mountDefault(
{ {value: 'one'},
scopedSlots: { {
'option': `<span slot="option" slot-scope="option">{{ option.label }}</span>`, scopedSlots: {
}, 'option': `<span slot="option" slot-scope="option">{{ option.label }}</span>`,
}); },
});
Select.vm.open = true; Select.vm.open = true;
expect(Select.find({ref: 'dropdownMenu'}).text()).toEqual('onetwothree') expect(Select.find({ref: 'dropdownMenu'}).text()).toEqual('onetwothree');
}); });
}); });