-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (48 loc) · 1.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var React = require('react');
module.exports = React.createClass({
getDefaultProps: function() {
return {
tag: 'div',
tabIndex: 0,
};
},
handleKeyDown: function(e) {
// Trigger a click event on 'enter'
if (e.key === 'Enter') {
e.preventDefault();
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent('click', true, true);
this.refs.button.dispatchEvent(clickEvent);
}
if (this.props.onKeyDown && typeof this.props.onKeyDown === 'function') {
this.props.onKeyDown(e);
}
},
handleKeyUp: function(e) {
// Trigger a click event on 'space'
if (e.key === ' ') {
e.preventDefault();
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent('click', true, true);
this.refs.button.dispatchEvent(clickEvent);
}
if (this.props.onKeyUp && typeof this.props.onKeyUp === 'function') {
this.props.onKeyUp(e);
}
},
render: function() {
var props = {};
Object.keys(this.props).forEach(function(key){
props[key] = this.props[key];
}.bind(this));
delete props.tag;
props.ref = 'button';
props.role = 'button';
props.onKeyUp = this.handleKeyUp;
props.onKeyDown = this.handleKeyDown;
if (props.disabled) {
delete props.tabIndex;
}
return React.createElement(this.props.tag, props);
},
});