forked from JamBrain/JamBrain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.js
50 lines (43 loc) · 1.16 KB
/
base.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
import {h, Component} from 'preact/preact';
export default class ButtonBase extends Component {
constructor( props ) {
super(props);
}
render( props, {} ) {
if ( !props.hasOwnProperty('tabIndex') )
props.tabIndex = "0";
if ( props.class )
props.class = "button-base " + props.class;
else
props.class = "button-base";
if ( props.disabled )
props.class += " -disabled";
if ( props.onclick ) {
// As long as you don't set the "keep focus" property //
if ( !props.keepFocus ) {
// Wrap onClick with a function that deselects current element //
let func = props.onclick;
props.onclick = (e) => {
if (props.disabled)
return;
func(e);
if ( typeof document.activeElement.blur !== "undefined" ) {
document.activeElement.blur();
}
// SVG Elements on Internet Explorer have no blur() method, so call the parent's blur //
else if ( document.activeElement.parentNode.blur ) {
document.activeElement.parentNode.blur();
}
};
}
props.onkeydown = (e) => {
if ( e.keyCode === 13 && !props.disabled ) {
props.onclick();
}
};
}
return (
<div {...props} />
);
}
}