From ce49f42eb908f5100667627041915c62aea179f6 Mon Sep 17 00:00:00 2001 From: Antoine Matyja Date: Sat, 1 Sep 2018 20:27:27 +0200 Subject: [PATCH 1/6] Add keyboard navigation --- src/components/VueBootstrapTypeahead.vue | 3 ++ src/components/VueBootstrapTypeaheadList.vue | 39 +++++++++++++++++++ .../VueBootstrapTypeaheadListItem.vue | 21 ++++------ src/views/Home.vue | 1 - 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/components/VueBootstrapTypeahead.vue b/src/components/VueBootstrapTypeahead.vue index 8bf1c85..2b64f67 100644 --- a/src/components/VueBootstrapTypeahead.vue +++ b/src/components/VueBootstrapTypeahead.vue @@ -16,6 +16,9 @@ @focus="isFocused = true" @blur="handleBlur" @input="$emit('input', $event.target.value)" + @keyup.down="$emit('keyup.down', $event.target.value)" + @keyup.up="$emit('keyup.up', $event.target.value)" + @keyup.enter="$emit('keyup.enter', $event.target.value)" autocomplete="off" />
diff --git a/src/components/VueBootstrapTypeaheadList.vue b/src/components/VueBootstrapTypeaheadList.vue index 8b72d28..05f76a8 100644 --- a/src/components/VueBootstrapTypeaheadList.vue +++ b/src/components/VueBootstrapTypeaheadList.vue @@ -5,6 +5,7 @@ v-html="highlight(item.text)" :background-variant="backgroundVariant" :text-variant="textVariant" + :active="isListItemActive(id)" @click.native="handleHit(item, $event)" />
@@ -54,6 +55,12 @@ export default { } }, + data() { + return { + activeListItem: -1 + } + }, + computed: { highlight() { return (text) => { @@ -96,7 +103,39 @@ export default { handleHit(item, evt) { this.$emit('hit', item) evt.preventDefault() + }, + isListItemActive(id) { + return this.activeListItem === id + }, + resetActiveListItem() { + this.activeListItem = -1 + }, + selectNextListItem() { + if (this.activeListItem < this.matchedItems.length - 1) { + this.activeListItem++ + } else { + this.resetActiveListItem() + } + }, + selectPreviousListItem() { + if (this.activeListItem < 0) { + this.activeListItem = this.matchedItems.length - 1 + } else { + this.activeListItem-- + } + }, + hitActiveListItem() { + if (this.activeListItem >= 0) { + this.$emit('hit', this.matchedItems[this.activeListItem]) + } } + }, + + created() { + this.$parent.$on('input', this.resetActiveListItem) + this.$parent.$on('keyup.down', this.selectNextListItem) + this.$parent.$on('keyup.up', this.selectPreviousListItem) + this.$parent.$on('keyup.enter', this.hitActiveListItem) } } diff --git a/src/components/VueBootstrapTypeaheadListItem.vue b/src/components/VueBootstrapTypeaheadListItem.vue index 74c8ad3..f3ae546 100644 --- a/src/components/VueBootstrapTypeaheadListItem.vue +++ b/src/components/VueBootstrapTypeaheadListItem.vue @@ -2,8 +2,6 @@ @@ -14,6 +12,9 @@ export default { name: 'VueBootstrapTypeaheadListItem', props: { + active: { + type: Boolean + }, backgroundVariant: { type: String }, @@ -22,19 +23,13 @@ export default { } }, - data() { - return { - active: false - } - }, - computed: { textClasses() { - let classes = '' - classes += this.active ? 'active' : '' - classes += this.backgroundVariant ? ` bg-${this.backgroundVariant}` : '' - classes += this.textVariant ? ` text-${this.textVariant}` : '' - return `vbst-item list-group-item list-group-item-action ${classes}` + const classes = ['vbst-item', 'list-group-item', 'list-group-item-action'] + if (this.active) classes.push('active') + if (this.backgroundVariant) classes.push(`bg-${this.backgroundVariant}`) + if (this.textVariant) classes.push(`text-${this.textVariant}`) + return classes.join(' ') } } } diff --git a/src/views/Home.vue b/src/views/Home.vue index 564edd9..58656a7 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -17,7 +17,6 @@ Date: Fri, 7 Sep 2018 23:40:56 +0200 Subject: [PATCH 2/6] Remove tests related to list item active class on mouse over --- tests/unit/VueBootstrapTypeaheadListItem.spec.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/tests/unit/VueBootstrapTypeaheadListItem.spec.js b/tests/unit/VueBootstrapTypeaheadListItem.spec.js index 5450164..26236e9 100644 --- a/tests/unit/VueBootstrapTypeaheadListItem.spec.js +++ b/tests/unit/VueBootstrapTypeaheadListItem.spec.js @@ -21,19 +21,4 @@ describe('VueBootstrapTypeaheadListItem.vue', () => { wrapper.setProps({backgroundVariant: 'light'}) expect(wrapper.classes()).toEqual(expect.arrayContaining(['bg-light'])) }) - - it('Applies the active class on mouseOver', () => { - expect(wrapper.vm.active).toBe(false) - wrapper.trigger('mouseover') - expect(wrapper.vm.active).toBe(true) - expect(wrapper.classes()).toEqual(expect.arrayContaining(['active'])) - }) - - it('Removes the active class on mouse out', () => { - wrapper.trigger('mouseover') - expect(wrapper.vm.active).toBe(true) - wrapper.trigger('mouseout') - expect(wrapper.vm.active).toBe(false) - expect(wrapper.classes()).toEqual(expect.not.arrayContaining(['active'])) - }) }) From 46027c2b219d444ccc401d41c695d8030cdcfec7 Mon Sep 17 00:00:00 2001 From: Antoine Matyja Date: Fri, 7 Sep 2018 23:44:17 +0200 Subject: [PATCH 3/6] Add list item active prop/css class test --- tests/unit/VueBootstrapTypeaheadListItem.spec.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/unit/VueBootstrapTypeaheadListItem.spec.js b/tests/unit/VueBootstrapTypeaheadListItem.spec.js index 26236e9..8000915 100644 --- a/tests/unit/VueBootstrapTypeaheadListItem.spec.js +++ b/tests/unit/VueBootstrapTypeaheadListItem.spec.js @@ -21,4 +21,10 @@ describe('VueBootstrapTypeaheadListItem.vue', () => { wrapper.setProps({backgroundVariant: 'light'}) expect(wrapper.classes()).toEqual(expect.arrayContaining(['bg-light'])) }) + + it('Renders active class properly', () => { + wrapper.setProps({active: true}) + expect(wrapper.vm.active).toBe(true) + expect(wrapper.classes()).toEqual(expect.arrayContaining(['active'])) + }) }) From 303f51d0fb220b4ec91d23046e1f445f643cd184 Mon Sep 17 00:00:00 2001 From: Raymond Muller Date: Sun, 7 Oct 2018 18:52:32 +0200 Subject: [PATCH 4/6] feat: keyboard navigation fix: lint issues fix: formatting fix: ref --- src/components/VueBootstrapTypeaheadList.vue | 118 +++++++++++------- .../VueBootstrapTypeaheadListItem.vue | 6 +- 2 files changed, 78 insertions(+), 46 deletions(-) diff --git a/src/components/VueBootstrapTypeaheadList.vue b/src/components/VueBootstrapTypeaheadList.vue index 19f2471..8191653 100644 --- a/src/components/VueBootstrapTypeaheadList.vue +++ b/src/components/VueBootstrapTypeaheadList.vue @@ -1,12 +1,12 @@