Skip to content

Commit 776c292

Browse files
committed
Merge pull request glittershark#248 from apprennet/sort-on-enter-keydown
Adds on onKeyDown handler to Th to sort on enter
2 parents 8652a6a + 2302f31 commit 776c292

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

build/reactable.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,13 @@ window.ReactDOM["default"] = window.ReactDOM;
659659
value: function handleClickTh(column) {
660660
this.props.onSort(column.key);
661661
}
662+
}, {
663+
key: 'handleKeyDownTh',
664+
value: function handleKeyDownTh(column, event) {
665+
if (event.keyCode === 13) {
666+
this.props.onSort(column.key);
667+
}
668+
}
662669
}, {
663670
key: 'render',
664671
value: function render() {
@@ -696,6 +703,7 @@ window.ReactDOM["default"] = window.ReactDOM;
696703
className: thClass,
697704
key: index,
698705
onClick: this.handleClickTh.bind(this, column),
706+
onKeyDown: this.handleKeyDownTh.bind(this, column),
699707
role: 'button',
700708
tabIndex: '0' }),
701709
column.label

build/tests/reactable_test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,30 @@
12231223
// Make sure the headers have the right classes
12241224
expect($(nameHeader)).to.have['class']('reactable-header-sort-desc');
12251225
});
1226+
1227+
it('sorts by last name in ascending order by enter keydown', function () {
1228+
var nameHeader = $('#table thead tr.reactable-column-header th')[0];
1229+
ReactTestUtils.Simulate.keyDown(nameHeader, { keyCode: 13 });
1230+
1231+
ReactableTestUtils.expectRowText(0, ['Lee Salminen', '23', 'Programmer']);
1232+
ReactableTestUtils.expectRowText(1, ['Griffin Smith', '18', 'Engineer']);
1233+
ReactableTestUtils.expectRowText(2, ['Ian Zhang', '28', 'Developer']);
1234+
1235+
// Make sure the headers have the right classes
1236+
expect($(nameHeader)).to.have['class']('reactable-header-sort-asc');
1237+
});
1238+
1239+
it('does not sort on non-enter keydown', function () {
1240+
var nameHeader = $('#table thead tr.reactable-column-header th')[0];
1241+
ReactTestUtils.Simulate.keyDown(nameHeader, { keyCode: 10 });
1242+
1243+
ReactableTestUtils.expectRowText(0, ['Lee Salminen', '23', 'Programmer']);
1244+
ReactableTestUtils.expectRowText(1, ['Griffin Smith', '18', 'Engineer']);
1245+
ReactableTestUtils.expectRowText(2, ['Ian Zhang', '28', 'Developer']);
1246+
1247+
// Make sure the headers have the right classes
1248+
expect($(nameHeader)).to.have['class']('reactable-header-sort-asc');
1249+
});
12261250
});
12271251

12281252
describe('passing `true` to sortable', function () {

lib/reactable/thead.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ var Thead = (function (_React$Component) {
4040
value: function handleClickTh(column) {
4141
this.props.onSort(column.key);
4242
}
43+
}, {
44+
key: 'handleKeyDownTh',
45+
value: function handleKeyDownTh(column, event) {
46+
if (event.keyCode === 13) {
47+
this.props.onSort(column.key);
48+
}
49+
}
4350
}, {
4451
key: 'render',
4552
value: function render() {
@@ -77,6 +84,7 @@ var Thead = (function (_React$Component) {
7784
className: thClass,
7885
key: index,
7986
onClick: this.handleClickTh.bind(this, column),
87+
onKeyDown: this.handleKeyDownTh.bind(this, column),
8088
role: 'button',
8189
tabIndex: '0' }),
8290
column.label

src/reactable/thead.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export class Thead extends React.Component {
4545
this.props.onSort(column.key);
4646
}
4747

48+
handleKeyDownTh(column, event) {
49+
if (event.keyCode === 13) {
50+
this.props.onSort(column.key);
51+
}
52+
}
53+
4854
render() {
4955
// Declare the list of Ths
5056
var Ths = [];
@@ -83,6 +89,7 @@ export class Thead extends React.Component {
8389
className={thClass}
8490
key={index}
8591
onClick={this.handleClickTh.bind(this, column)}
92+
onKeyDown={this.handleKeyDownTh.bind(this, column)}
8693
role="button"
8794
tabIndex="0">
8895
{column.label}

tests/reactable_test.jsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,30 @@ describe('Reactable', function() {
11231123
// Make sure the headers have the right classes
11241124
expect($(nameHeader)).to.have.class('reactable-header-sort-desc');
11251125
});
1126+
1127+
it('sorts by last name in ascending order by enter keydown', function(){
1128+
var nameHeader = $('#table thead tr.reactable-column-header th')[0];
1129+
ReactTestUtils.Simulate.keyDown(nameHeader, {keyCode: 13});
1130+
1131+
ReactableTestUtils.expectRowText(0, ['Lee Salminen', '23', 'Programmer']);
1132+
ReactableTestUtils.expectRowText(1, ['Griffin Smith', '18', 'Engineer']);
1133+
ReactableTestUtils.expectRowText(2, ['Ian Zhang', '28', 'Developer']);
1134+
1135+
// Make sure the headers have the right classes
1136+
expect($(nameHeader)).to.have.class('reactable-header-sort-asc');
1137+
});
1138+
1139+
it('does not sort on non-enter keydown', function(){
1140+
var nameHeader = $('#table thead tr.reactable-column-header th')[0];
1141+
ReactTestUtils.Simulate.keyDown(nameHeader, {keyCode: 10});
1142+
1143+
ReactableTestUtils.expectRowText(0, ['Lee Salminen', '23', 'Programmer']);
1144+
ReactableTestUtils.expectRowText(1, ['Griffin Smith', '18', 'Engineer']);
1145+
ReactableTestUtils.expectRowText(2, ['Ian Zhang', '28', 'Developer']);
1146+
1147+
// Make sure the headers have the right classes
1148+
expect($(nameHeader)).to.have.class('reactable-header-sort-asc');
1149+
});
11261150
});
11271151

11281152
describe('passing `true` to sortable', function() {
@@ -1658,7 +1682,7 @@ describe('Reactable', function() {
16581682
ReactableTestUtils.expectRowText(2, ['Third']);
16591683
});
16601684
});
1661-
1685+
16621686
describe('sorts and calls onSort callback via props', function(){
16631687
var sortColumn = null;
16641688

@@ -1678,7 +1702,7 @@ describe('Reactable', function() {
16781702
// sort based on classname
16791703
return a.props.className.localeCompare(b.props.className);
16801704
}
1681-
}]}
1705+
}]}
16821706
onSort={ callback }/>,
16831707
ReactableTestUtils.testNode()
16841708
);

0 commit comments

Comments
 (0)