-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
SelectButtonItem.js
79 lines (67 loc) · 2.18 KB
/
SelectButtonItem.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
export class SelectButtonItem extends Component {
static defaultProps = {
option: null,
label: null,
selected: null,
tabIndex: null,
onClick: null
};
static propTypes = {
option: PropTypes.object,
label: PropTypes.string,
selected: PropTypes.bool,
tabIndex: PropTypes.number,
onClick: PropTypes.func
};
constructor(props) {
super(props);
this.state = {};
this.onClick = this.onClick.bind(this);
this.onFocus = this.onFocus.bind(this);
this.onBlur = this.onBlur.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
}
onClick(event) {
if (this.props.onClick) {
this.props.onClick({
originalEvent: event,
option: this.props.option
});
this.input.focus();
}
}
onFocus() {
this.setState({focused: true});
}
onBlur() {
this.setState({focused: false});
}
onKeyDown(event) {
if (event.key === 'Enter') {
this.onClick(event);
event.preventDefault();
}
}
componentDidUpdate() {
this.input.checked = this.props.selected;
}
render() {
let className = classNames('p-button p-component p-button-text-only', {
'p-highlight': this.props.selected,
'p-disabled': this.props.disabled,
'p-focus': this.state.focused
});
return (
<div ref={(el) => this.el = el} className={className} onClick={this.onClick}>
<span className="p-button-text p-c">{this.props.label}</span>
<div className="p-hidden-accessible">
<input ref={(el) => this.input = el} type="checkbox" defaultChecked={this.props.selected} onFocus={this.onFocus} onBlur={this.onBlur} onKeyDown={this.onKeyDown}
tabIndex={this.props.tabIndex} disabled={this.props.disabled} value={this.props.label}/>
</div>
</div>
);
}
}