From 5c09d6ffbae008a5d441bc2e7e9831e8deb063ee Mon Sep 17 00:00:00 2001 From: Mehdi ghasemi Date: Thu, 17 Feb 2022 23:32:24 +0330 Subject: [PATCH] feat: set pointer to most recent selected option on open (#1574) Co-authored-by: Jeff Sagal --- src/mixins/typeAheadPointer.js | 22 +++++++++++++++++++++ tests/unit/Accessibility.spec.js | 6 +++--- tests/unit/TypeAhead.spec.js | 33 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/mixins/typeAheadPointer.js b/src/mixins/typeAheadPointer.js index 849cfda..af35f89 100644 --- a/src/mixins/typeAheadPointer.js +++ b/src/mixins/typeAheadPointer.js @@ -14,6 +14,16 @@ export default { } } }, + open(open) { + if (open) { + this.typeAheadToLastSelected() + } + }, + selectedValue() { + if (this.open) { + this.typeAheadToLastSelected() + } + }, }, methods: { @@ -61,5 +71,17 @@ export default { this.select(typeAheadOption) } }, + + /** + * Moves the pointer to the last selected option. + */ + typeAheadToLastSelected() { + this.typeAheadPointer = + this.selectedValue.length !== 0 + ? this.filteredOptions.indexOf( + this.selectedValue[this.selectedValue.length - 1] + ) + : -1 + }, }, } diff --git a/tests/unit/Accessibility.spec.js b/tests/unit/Accessibility.spec.js index a235b21..7170a2a 100644 --- a/tests/unit/Accessibility.spec.js +++ b/tests/unit/Accessibility.spec.js @@ -4,7 +4,7 @@ describe('Search Slot Scope', () => { /** * @see https://www.w3.org/WAI/PF/aria/states_and_properties#aria-activedescendant */ - describe('aria-activedescendant', () => { + fdescribe('aria-activedescendant', () => { it('adds the active descendant attribute only when the dropdown is open and there is a typeAheadPointer value', async () => { const Select = mountDefault() @@ -21,7 +21,7 @@ describe('Search Slot Scope', () => { }) it("adds the active descendant attribute when there's a typeahead value and an open dropdown", async () => { - const Select = mountDefault() + const Select = mountDefault({ value: 'three' }, ['one', 'two', 'three']) Select.vm.open = true Select.vm.typeAheadPointer = 1 @@ -29,7 +29,7 @@ describe('Search Slot Scope', () => { expect( Select.vm.scope.search.attributes['aria-activedescendant'] - ).toEqual(`vs${Select.vm.uid}__option-1`) + ).toEqual(`vs${Select.vm.uid}__option-2`) }) }) }) diff --git a/tests/unit/TypeAhead.spec.js b/tests/unit/TypeAhead.spec.js index 3d091d1..3a4f832 100755 --- a/tests/unit/TypeAhead.spec.js +++ b/tests/unit/TypeAhead.spec.js @@ -44,4 +44,37 @@ describe('Moving the Typeahead Pointer', () => { Select.vm.typeAheadDown() expect(Select.vm.typeAheadPointer).toEqual(2) }) + + it('will set the pointer to the selected option when opening', async () => { + const Select = shallowMount(VueSelect, { + propsData: { + value: 'three', + options: ['one', 'two', 'three'], + }, + }) + + Select.findComponent({ ref: 'search' }).trigger('focus') + await Select.vm.$nextTick() + + expect(Select.vm.typeAheadPointer).toEqual(2) + }) + + it('will set the pointer to the reduced selected option when opening', async () => { + const Select = shallowMount(VueSelect, { + propsData: { + value: 3, + reduce: ({ value }) => value, + options: [ + { label: 'one', value: 1 }, + { label: 'two', value: 2 }, + { label: 'three', value: 3 }, + ], + }, + }) + + Select.findComponent({ ref: 'search' }).trigger('focus') + await Select.vm.$nextTick() + + expect(Select.vm.typeAheadPointer).toEqual(2) + }) })