2019-03-19 20:58:21 +08:00
|
|
|
import React, { PureComponent } from 'react';
|
2017-06-04 10:40:14 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2017-10-02 22:12:58 +08:00
|
|
|
import cx from 'classnames';
|
2019-05-07 06:20:14 +08:00
|
|
|
import { findDOMNode } from 'react-dom';
|
|
|
|
import KEY_CODES from '/imports/utils/keyCodes';
|
2017-01-27 23:41:11 +08:00
|
|
|
import Icon from '../icon/component';
|
2018-01-08 14:17:18 +08:00
|
|
|
import { styles } from './styles';
|
2017-10-02 22:12:58 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
checked: PropTypes.bool,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
className: PropTypes.string,
|
|
|
|
ariaLabelledBy: PropTypes.string,
|
|
|
|
ariaLabel: PropTypes.string,
|
|
|
|
ariaDescribedBy: PropTypes.string,
|
|
|
|
ariaDesc: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
disabled: false,
|
|
|
|
checked: false,
|
|
|
|
className: null,
|
|
|
|
ariaLabelledBy: null,
|
|
|
|
ariaLabel: null,
|
|
|
|
ariaDescribedBy: null,
|
|
|
|
ariaDesc: null,
|
|
|
|
};
|
2017-01-27 23:41:11 +08:00
|
|
|
|
2019-03-19 20:58:21 +08:00
|
|
|
export default class Checkbox extends PureComponent {
|
2017-01-27 23:41:11 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2017-02-16 02:49:40 +08:00
|
|
|
this.onChange = props.onChange;
|
|
|
|
this.handleChange = this.handleChange.bind(this);
|
2019-05-07 06:20:14 +08:00
|
|
|
this.handleKeyDown = this.handleKeyDown.bind(this);
|
|
|
|
}
|
|
|
|
|
2019-05-08 03:38:29 +08:00
|
|
|
componentDidMount() {
|
2019-05-07 06:20:14 +08:00
|
|
|
const checkbox = findDOMNode(this.checkbox);
|
2019-05-08 03:51:14 +08:00
|
|
|
if (checkbox) checkbox.addEventListener('keydown', this.handleKeyDown);
|
2019-05-07 06:20:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2019-05-08 03:51:14 +08:00
|
|
|
const checkbox = findDOMNode(this.checkbox);
|
|
|
|
if (checkbox) checkbox.removeEventListener('keydown', this.handleKeyDown);
|
2019-05-07 06:20:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown(event) {
|
|
|
|
const { which } = event;
|
|
|
|
const input = findDOMNode(this.input);
|
|
|
|
if ([KEY_CODES.ENTER].includes(which)) {
|
2019-05-08 03:51:14 +08:00
|
|
|
if (input) input.click();
|
2019-05-07 06:20:14 +08:00
|
|
|
}
|
2017-02-16 02:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleChange() {
|
2019-03-19 20:58:21 +08:00
|
|
|
const { disabled, keyValue } = this.props;
|
|
|
|
if (disabled) return;
|
|
|
|
this.onChange(keyValue);
|
2017-01-27 23:41:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-10-02 22:12:58 +08:00
|
|
|
const {
|
2021-05-07 19:54:01 +08:00
|
|
|
ariaLabel, ariaDesc, ariaDescribedBy,
|
2017-10-02 22:12:58 +08:00
|
|
|
className, checked, disabled,
|
|
|
|
} = this.props;
|
2017-04-25 10:08:18 +08:00
|
|
|
|
2017-01-27 23:41:11 +08:00
|
|
|
return (
|
2019-05-08 03:38:29 +08:00
|
|
|
<div
|
|
|
|
className={cx({
|
|
|
|
[styles.disabled]: !!disabled,
|
|
|
|
}, className)}
|
2019-05-07 06:20:14 +08:00
|
|
|
tabIndex={0}
|
2019-05-08 03:38:29 +08:00
|
|
|
ref={(node) => { this.checkbox = node; }}
|
2017-10-02 22:12:58 +08:00
|
|
|
>
|
2017-01-27 23:41:11 +08:00
|
|
|
<input
|
2017-06-03 03:25:02 +08:00
|
|
|
type="checkbox"
|
2017-01-27 23:41:11 +08:00
|
|
|
onChange={this.handleChange}
|
2017-10-02 22:12:58 +08:00
|
|
|
checked={checked}
|
2017-04-25 10:08:18 +08:00
|
|
|
className={styles.input}
|
2019-05-09 05:47:00 +08:00
|
|
|
aria-label={ariaLabel}
|
2017-05-04 04:51:17 +08:00
|
|
|
aria-describedby={ariaDescribedBy}
|
2017-10-02 22:12:58 +08:00
|
|
|
disabled={disabled}
|
2019-05-08 03:38:29 +08:00
|
|
|
ref={(node) => { this.input = node; }}
|
2017-06-03 03:25:02 +08:00
|
|
|
/>
|
2017-10-02 22:12:58 +08:00
|
|
|
<div role="presentation" onClick={this.handleChange}>
|
2018-12-06 01:42:31 +08:00
|
|
|
{ checked
|
|
|
|
? <Icon iconName="check" className={cx(styles.icon, styles.checked)} />
|
|
|
|
: <Icon iconName="circle" className={styles.icon} />
|
2017-02-16 02:49:40 +08:00
|
|
|
}
|
2017-01-27 23:41:11 +08:00
|
|
|
</div>
|
2017-04-25 10:08:18 +08:00
|
|
|
<div id={ariaDescribedBy} hidden>{ariaDesc}</div>
|
2017-01-27 23:41:11 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-10-02 22:12:58 +08:00
|
|
|
|
|
|
|
Checkbox.propTypes = propTypes;
|
|
|
|
Checkbox.defaultProps = defaultProps;
|