Skip to content

Commit

Permalink
Open list when arrow pressed, close list when escape pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
dahliacreative committed Aug 2, 2016
1 parent 6f4f8f9 commit 377387d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 14 deletions.
26 changes: 22 additions & 4 deletions dist/selectron.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// |___/\___|_|\___|\___|\__|_| \___/|_| |_|
//
// --------------------------------------------------------------------------
// Version: 2.0.0
// Version: 2.0.1
// Author: Simon Sturgess
// Docs: dahliacreative.github.io/selectron
// Repo: github.com/dahliacreative/selectron
Expand Down Expand Up @@ -193,14 +193,24 @@ Selectron.prototype.handleKeyStrokes = function(e) {
alphaNumbericKeyPressed = (e.which >= 48 && e.which <= 57) || (e.which >= 65 && e.which <= 90) || e.which === 8,
self = this;

if(!this.isOpen && (upArrowKeyPressed || downArrowKeyPressed || enterKeyPressed)) {


if(!this.isOpen && enterKeyPressed) {
return false;
}

if(!this.isOpen && (upArrowKeyPressed || downArrowKeyPressed)) {
this.openOptions();
return false;
}

if(escapeKeyPressed || enterKeyPressed) {
if(enterKeyPressed) {
this.updateSelection(hovered);
}
if(escapeKeyPressed && this.isOpen) {
this.closeOptions();
}
}

if(spaceKeyPressed) {
Expand Down Expand Up @@ -326,9 +336,11 @@ Selectron.prototype.populateOptions = function() {
self.options.append(self.createOption(child, false));
}
});

var firstOption = this.options.find('.selectron__option:first-child');
this.placeholderExists = firstOption.data('value') === '';
if(this.opts.search) {
firstOption.addClass('selectron__option--is-hovered');
}
if(!this.isOpen) {
this.updateTrigger();
}
Expand Down Expand Up @@ -401,7 +413,13 @@ Selectron.prototype.registerEvents = function() {
downArrowKeyPressed = e.which === 40,
leftArrowKeyPress = e.which === 37,
rightArrowKeyPress = e.which === 39,
enterKeyPressed = e.which === 13;
enterKeyPressed = e.which === 13,
escapeKeyPressed = e.which === 27;

if(escapeKeyPressed && self.isOpen) {
self.closeOptions();
return false;
}

if(downArrowKeyPressed || upArrowKeyPressed || leftArrowKeyPress || rightArrowKeyPress || enterKeyPressed) {
self.handleKeyStrokes(e);
Expand Down
2 changes: 1 addition & 1 deletion dist/selectron.sass
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// |___/\___|_|\___|\___|\__|_| \___/|_| |_|
//
// --------------------------------------------------------------------------
// Version: 2.0.0
// Version: 2.0.1
// Author: Simon Sturgess
// Docs: dahliacreative.github.io/selectron
// Repo: github.com/dahliacreative/selectron
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/_selectron.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// |___/\___|_|\___|\___|\__|_| \___/|_| |_|
//
// --------------------------------------------------------------------------
// Version: 2.0.0
// Version: 2.0.1
// Author: Simon Sturgess
// Docs: dahliacreative.github.io/selectron
// Repo: github.com/dahliacreative/selectron
Expand Down
12 changes: 11 additions & 1 deletion src/javascripts/selectron.handleKeyStrokes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ Selectron.prototype.handleKeyStrokes = function(e) {
alphaNumbericKeyPressed = (e.which >= 48 && e.which <= 57) || (e.which >= 65 && e.which <= 90) || e.which === 8,
self = this;

if(!this.isOpen && (upArrowKeyPressed || downArrowKeyPressed || enterKeyPressed)) {


if(!this.isOpen && enterKeyPressed) {
return false;
}

if(!this.isOpen && (upArrowKeyPressed || downArrowKeyPressed)) {
this.openOptions();
return false;
}

if(escapeKeyPressed || enterKeyPressed) {
if(enterKeyPressed) {
this.updateSelection(hovered);
}
if(escapeKeyPressed && this.isOpen) {
this.closeOptions();
}
}

if(spaceKeyPressed) {
Expand Down
4 changes: 3 additions & 1 deletion src/javascripts/selectron.populateOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ Selectron.prototype.populateOptions = function() {
self.options.append(self.createOption(child, false));
}
});

var firstOption = this.options.find('.selectron__option:first-child');
this.placeholderExists = firstOption.data('value') === '';
if(this.opts.search) {
firstOption.addClass('selectron__option--is-hovered');
}
if(!this.isOpen) {
this.updateTrigger();
}
Expand Down
8 changes: 7 additions & 1 deletion src/javascripts/selectron.registerEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ Selectron.prototype.registerEvents = function() {
downArrowKeyPressed = e.which === 40,
leftArrowKeyPress = e.which === 37,
rightArrowKeyPress = e.which === 39,
enterKeyPressed = e.which === 13;
enterKeyPressed = e.which === 13,
escapeKeyPressed = e.which === 27;

if(escapeKeyPressed && self.isOpen) {
self.closeOptions();
return false;
}

if(downArrowKeyPressed || upArrowKeyPressed || leftArrowKeyPress || rightArrowKeyPress || enterKeyPressed) {
self.handleKeyStrokes(e);
Expand Down
2 changes: 1 addition & 1 deletion src/stylesheets/selectron.sass
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// |___/\___|_|\___|\___|\__|_| \___/|_| |_|
//
// --------------------------------------------------------------------------
// Version: 2.0.0
// Version: 2.0.1
// Author: Simon Sturgess
// Docs: dahliacreative.github.io/selectron
// Repo: github.com/dahliacreative/selectron
Expand Down
26 changes: 22 additions & 4 deletions tests/selectron.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// |___/\___|_|\___|\___|\__|_| \___/|_| |_|
//
// --------------------------------------------------------------------------
// Version: 2.0.0
// Version: 2.0.1
// Author: Simon Sturgess
// Docs: dahliacreative.github.io/selectron
// Repo: github.com/dahliacreative/selectron
Expand Down Expand Up @@ -193,14 +193,24 @@ Selectron.prototype.handleKeyStrokes = function(e) {
alphaNumbericKeyPressed = (e.which >= 48 && e.which <= 57) || (e.which >= 65 && e.which <= 90) || e.which === 8,
self = this;

if(!this.isOpen && (upArrowKeyPressed || downArrowKeyPressed || enterKeyPressed)) {


if(!this.isOpen && enterKeyPressed) {
return false;
}

if(!this.isOpen && (upArrowKeyPressed || downArrowKeyPressed)) {
this.openOptions();
return false;
}

if(escapeKeyPressed || enterKeyPressed) {
if(enterKeyPressed) {
this.updateSelection(hovered);
}
if(escapeKeyPressed && this.isOpen) {
this.closeOptions();
}
}

if(spaceKeyPressed) {
Expand Down Expand Up @@ -326,9 +336,11 @@ Selectron.prototype.populateOptions = function() {
self.options.append(self.createOption(child, false));
}
});

var firstOption = this.options.find('.selectron__option:first-child');
this.placeholderExists = firstOption.data('value') === '';
if(this.opts.search) {
firstOption.addClass('selectron__option--is-hovered');
}
if(!this.isOpen) {
this.updateTrigger();
}
Expand Down Expand Up @@ -401,7 +413,13 @@ Selectron.prototype.registerEvents = function() {
downArrowKeyPressed = e.which === 40,
leftArrowKeyPress = e.which === 37,
rightArrowKeyPress = e.which === 39,
enterKeyPressed = e.which === 13;
enterKeyPressed = e.which === 13,
escapeKeyPressed = e.which === 27;

if(escapeKeyPressed && self.isOpen) {
self.closeOptions();
return false;
}

if(downArrowKeyPressed || upArrowKeyPressed || leftArrowKeyPress || rightArrowKeyPress || enterKeyPressed) {
self.handleKeyStrokes(e);
Expand Down

0 comments on commit 377387d

Please sign in to comment.