Skip to content

Commit

Permalink
Added ability to add select key to grid
Browse files Browse the repository at this point in the history
  • Loading branch information
crisward committed Mar 25, 2016
1 parent e834213 commit 2d38493
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ Note: the keyboard interaction doesn't seem to work in codepen
| dblclick | Callback fired when a row is double clicked, this returns the row selected (useful for edit events)
| onchange | Callback fires whenever row selection changes. This returns and array of active rows. This fires on both mouse and keyboard events
| active | Setting an array of rows will select them in the grid
| selectkey | optional keycode to select the row, ie 13 = enter


## Keyboard

* Clicking on the grid gives it keyboard focus, allowing multiple grids to work on the page at once.
* Arrow up selects the previous row in the grid
* Arrow down selects the next row
* Provider optional selectkey (see attributes above)
* Shift + Arrow adds more to a selection
* Shift + Click selects all rows between the first click, and the new click
* CMD/CTRL + Click toggles row selection
Expand Down
10 changes: 9 additions & 1 deletion lib/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ this.downKey = 40;

this.upKey = 38;

this.selectKey = null;

this.on('mount', function() {
var ref;
this.rowheight = ((ref = this.parent.opts) != null ? ref.rowheight : void 0) || 30;
if (this.parent.opts.active != null) {
this.active = this.parent.opts.active;
}
if (this.parent.opts.selectkey != null) {
this.selectKey = this.parent.opts.selectkey;
}
document.addEventListener('keydown', this.keydown);
['click', 'focus', 'blur'].forEach((function(_this) {
return function(ev) {
Expand Down Expand Up @@ -97,9 +102,12 @@ this.keydown = (function(_this) {
hasFocus: false
});
}
if (e.keyCode !== _this.downKey && e.keyCode !== _this.upKey) {
if (e.keyCode !== _this.downKey && e.keyCode !== _this.upKey && e.keyCode !== _this.selectKey) {
return;
}
if (e.keyCode === _this.selectKey && _this.active && (_this.parent.opts.dblclick != null) && typeof _this.parent.opts.dblclick === "function") {
return _this.parent.opts.dblclick(_this.active[0]);
}
_this.hasFocus = true;
index = _this.parent.opts.data.indexOf(_this.active[_this.active.length - 1]);
if (e.keyCode === _this.downKey) {
Expand Down
5 changes: 4 additions & 1 deletion src/grid.tag
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ gridbody
@prevScrollTop = -1
@downKey = 40
@upKey = 38
@selectKey = null

# @on 'error',(err)-> console.error err.message

@on 'mount',->
@rowheight = @parent.opts?.rowheight || 30
@active = @parent.opts.active if @parent.opts.active?
@selectKey = @parent.opts.selectkey if @parent.opts.selectkey?
document.addEventListener 'keydown',@keydown
['click','focus','blur'].forEach (ev)=> @root.addEventListener ev,@focused
@update()
Expand Down Expand Up @@ -95,7 +97,8 @@ gridbody

@keydown = (e)=>
return @update(hasFocus:false) if @parent.root != document.activeElement
return if e.keyCode != @downKey && e.keyCode != @upKey
return if e.keyCode != @downKey && e.keyCode != @upKey && e.keyCode != @selectKey
return @parent.opts.dblclick(@active[0]) if e.keyCode == @selectKey && @active && @parent.opts.dblclick? && typeof @parent.opts.dblclick == "function"
@hasFocus = true
index = @parent.opts.data.indexOf(@active[@active.length-1])
index++ if e.keyCode == @downKey
Expand Down

0 comments on commit 2d38493

Please sign in to comment.