Skip to content

Commit f6d06ef

Browse files
committed
Add keyboard navigator widget example. Resolve #112
1 parent 1b86bae commit f6d06ef

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ All attributes are optional.
3030
- `widgets` {Array of ReactClass} Indicator and switcher could be various,
3131
so it's not builtin. Here's some example custom widgets
3232
([dots indicator](src/indicator-dots.js),
33-
[prev/next buttons](src/buttons.js)):
33+
[prev/next buttons](src/buttons.js), [keyboard navigation](src/keyboard-navigator)):
3434

3535
```javascript
3636
import Carousel from 're-carousel'

src/keyboard-navigator.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
3+
export default class KeyboardNavigator extends React.Component {
4+
onKeydown = event => {
5+
const { prevHandler, nextHandler } = this.props
6+
switch (event.key) {
7+
case 'j':
8+
prevHandler()
9+
break;
10+
case 'k':
11+
nextHandler()
12+
break
13+
}
14+
}
15+
16+
componentDidMount () {
17+
const { index, total, loop, prevHandler, nextHandler } = props
18+
window.addEventListener('keydown', this.onKeydown)
19+
}
20+
21+
componentWillUnmount () {
22+
window.removeEventListener('keydown', this.onKeydown)
23+
}
24+
25+
render () { return null }
26+
}

0 commit comments

Comments
 (0)