Skip to content

Commit bfb2592

Browse files
authored
Merge pull request #1245 from Michael-Hanley/feature/KeyUpSort
Adds onKeyUp event to header for sort
2 parents b8c9712 + 3d4c28a commit bfb2592

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

packages/react-bootstrap-table2/src/header-cell.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ class HeaderCell extends eventDelegater(React.Component) {
7777

7878
if (sort) {
7979
const customClick = cellAttrs.onClick;
80+
cellAttrs.onKeyUp = (e) => {
81+
if (e.key === 'Enter') {
82+
onSort(column);
83+
if (_.isFunction(customClick)) customClick(e);
84+
}
85+
};
8086
cellAttrs.onClick = (e) => {
8187
onSort(column);
8288
if (_.isFunction(customClick)) customClick(e);

packages/react-bootstrap-table2/test/header-cell.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,25 @@ describe('HeaderCell', () => {
394394
expect(wrapper.find('th').prop('onClick')).toBeDefined();
395395
});
396396

397+
it('should have onKeyUp event on header cell', () => {
398+
expect(wrapper.find('th').prop('onKeyUp')).toBeDefined();
399+
});
400+
397401
it('should trigger onSort callback when click on header cell', () => {
398402
wrapper.find('th').simulate('click');
399403
expect(onSortCallBack.callCount).toBe(1);
400404
});
401405

406+
it('should trigger onSort callback when keyup Enter on header cell', () => {
407+
wrapper.find('th').simulate('keyup', { key: 'Enter' });
408+
expect(onSortCallBack.callCount).toBe(1);
409+
});
410+
411+
it('should not trigger onSort callback when keyup key is not Enter on header cell', () => {
412+
wrapper.find('th').simulate('keyup', { key: 'test-key' });
413+
expect(onSortCallBack.callCount).toBe(0);
414+
});
415+
402416
describe('and sorting prop is false', () => {
403417
it('header should render SortSymbol as default', () => {
404418
expect(wrapper.find(SortSymbol).length).toBe(1);

0 commit comments

Comments
 (0)