Skip to content

Prevent selection of current selected item #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -267,6 +267,10 @@
},
handleClick(item, i, event) {
event.stopPropagation();

const {optionIdentifier, selectedValue} = this.get();
if(selectedValue && selectedValue[optionIdentifier] === item[optionIdentifier]) return;

this.set({activeItemIndex: i, hoverItemIndex: i});
this.handleSelect(item);
},
@@ -287,7 +291,7 @@
this.scrollToActiveItem('hover');
},
handleKeyDown(e) {
const {items, hoverItemIndex} = this.get();
const {items, hoverItemIndex, optionIdentifier, selectedValue} = this.get();

switch (e.key) {
case 'ArrowDown':
@@ -300,11 +304,17 @@
break;
case 'Enter':
e.preventDefault();

if(selectedValue && selectedValue[optionIdentifier] === items[hoverItemIndex][optionIdentifier]) return;

this.set({activeItemIndex: hoverItemIndex});
this.handleSelect(items[hoverItemIndex]);
break;
case 'Tab':
e.preventDefault();

if(selectedValue && selectedValue[optionIdentifier] === items[hoverItemIndex][optionIdentifier]) return;

this.set({activeItemIndex: hoverItemIndex});
this.handleSelect(items[hoverItemIndex]);
break;
12 changes: 11 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
@@ -261,6 +261,10 @@ var methods = {
},
handleClick(item, i, event) {
event.stopPropagation();

const {optionIdentifier, selectedValue} = this.get();
if(selectedValue && selectedValue[optionIdentifier] === item[optionIdentifier]) return;

this.set({activeItemIndex: i, hoverItemIndex: i});
this.handleSelect(item);
},
@@ -281,7 +285,7 @@ var methods = {
this.scrollToActiveItem('hover');
},
handleKeyDown(e) {
const {items, hoverItemIndex} = this.get();
const {items, hoverItemIndex, optionIdentifier, selectedValue} = this.get();

switch (e.key) {
case 'ArrowDown':
@@ -294,11 +298,17 @@ var methods = {
break;
case 'Enter':
e.preventDefault();

if(selectedValue && selectedValue[optionIdentifier] === items[hoverItemIndex][optionIdentifier]) return;

this.set({activeItemIndex: hoverItemIndex});
this.handleSelect(items[hoverItemIndex]);
break;
case 'Tab':
e.preventDefault();

if(selectedValue && selectedValue[optionIdentifier] === items[hoverItemIndex][optionIdentifier]) return;

this.set({activeItemIndex: hoverItemIndex});
this.handleSelect(items[hoverItemIndex]);
break;
12 changes: 11 additions & 1 deletion src/List.svelte
Original file line number Diff line number Diff line change
@@ -151,6 +151,10 @@
},
handleClick(item, i, event) {
event.stopPropagation();

const {optionIdentifier, selectedValue} = this.get();
if(selectedValue && selectedValue[optionIdentifier] === item[optionIdentifier]) return;

this.set({activeItemIndex: i, hoverItemIndex: i});
this.handleSelect(item);
},
@@ -171,7 +175,7 @@
this.scrollToActiveItem('hover');
},
handleKeyDown(e) {
const {items, hoverItemIndex} = this.get();
const {items, hoverItemIndex, optionIdentifier, selectedValue} = this.get();

switch (e.key) {
case 'ArrowDown':
@@ -184,11 +188,17 @@
break;
case 'Enter':
e.preventDefault();

if(selectedValue && selectedValue[optionIdentifier] === items[hoverItemIndex][optionIdentifier]) return;

this.set({activeItemIndex: hoverItemIndex});
this.handleSelect(items[hoverItemIndex]);
break;
case 'Tab':
e.preventDefault();

if(selectedValue && selectedValue[optionIdentifier] === items[hoverItemIndex][optionIdentifier]) return;

this.set({activeItemIndex: hoverItemIndex});
this.handleSelect(items[hoverItemIndex]);
break;
37 changes: 34 additions & 3 deletions test/public/bundle.js
Original file line number Diff line number Diff line change
@@ -24180,7 +24180,7 @@
Object.defineProperty(exports, '__esModule', { value: true });

})));
//# sourceMappingURL=svelte.js.map

});

unwrapExports(svelte);
@@ -24702,6 +24702,10 @@
},
handleClick(item, i, event) {
event.stopPropagation();

const {optionIdentifier, selectedValue} = this.get();
if(selectedValue && selectedValue[optionIdentifier] === item[optionIdentifier]) return;

this.set({activeItemIndex: i, hoverItemIndex: i});
this.handleSelect(item);
},
@@ -24722,7 +24726,7 @@
this.scrollToActiveItem('hover');
},
handleKeyDown(e) {
const {items, hoverItemIndex} = this.get();
const {items, hoverItemIndex, optionIdentifier, selectedValue} = this.get();

switch (e.key) {
case 'ArrowDown':
@@ -24735,11 +24739,17 @@
break;
case 'Enter':
e.preventDefault();

if(selectedValue && selectedValue[optionIdentifier] === items[hoverItemIndex][optionIdentifier]) return;

this.set({activeItemIndex: hoverItemIndex});
this.handleSelect(items[hoverItemIndex]);
break;
case 'Tab':
e.preventDefault();

if(selectedValue && selectedValue[optionIdentifier] === items[hoverItemIndex][optionIdentifier]) return;

this.set({activeItemIndex: hoverItemIndex});
this.handleSelect(items[hoverItemIndex]);
break;
@@ -27223,7 +27233,6 @@
});
});
}
//# sourceMappingURL=tape-modern.esm.js.map

// setup
const target = document.querySelector('main');
@@ -27506,6 +27515,28 @@
list.destroy();
});

test('on selected of current active item does not fire a itemSelected event', async (t) => {
const list = new List({
target,
data: {
items: itemsWithIndex,
selectedValue: { value: 'chocolate', label: 'Chocolate', index: 0 },
isFocused: true
}
});

let itemSelectedFired = false;

list.on('itemSelected', () => {
itemSelectedFired = true;
});

window.dispatchEvent(new KeyboardEvent('keydown', {'key': 'Enter'}));

t.equal(itemSelectedFired, false);
list.destroy();
});

test('selected item\'s default view', async (t) => {
const testTemplate = new Select_itemSelected({
target: testTarget
22 changes: 22 additions & 0 deletions test/src/index.js
Original file line number Diff line number Diff line change
@@ -299,6 +299,28 @@ test('on tab active item fires a itemSelected event', async (t) => {
list.destroy();
});

test('on selected of current active item does not fire a itemSelected event', async (t) => {
const list = new List({
target,
data: {
items: itemsWithIndex,
selectedValue: { value: 'chocolate', label: 'Chocolate', index: 0 },
isFocused: true
}
});

let itemSelectedFired = false;

list.on('itemSelected', () => {
itemSelectedFired = true;
});

window.dispatchEvent(new KeyboardEvent('keydown', {'key': 'Enter'}));

t.equal(itemSelectedFired, false);
list.destroy();
});

test('selected item\'s default view', async (t) => {
const testTemplate = new SelectItemSelected({
target: testTarget