bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/checkbox/component.jsx

101 lines
2.6 KiB
React
Raw Normal View History

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
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';
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
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);
this.handleKeyDown = this.handleKeyDown.bind(this);
}
2019-05-08 03:38:29 +08:00
componentDidMount() {
const checkbox = findDOMNode(this.checkbox);
2019-05-08 03:51:14 +08:00
if (checkbox) checkbox.addEventListener('keydown', this.handleKeyDown);
}
componentWillUnmount() {
2019-05-08 03:51:14 +08:00
const checkbox = findDOMNode(this.checkbox);
if (checkbox) checkbox.removeEventListener('keydown', this.handleKeyDown);
}
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();
}
2017-02-16 02:49:40 +08:00
}
handleChange() {
const { disabled, keyValue } = this.props;
if (disabled) return;
this.onChange(keyValue);
2017-01-27 23:41:11 +08:00
}
render() {
const {
2021-05-07 19:54:01 +08:00
ariaLabel, ariaDesc, ariaDescribedBy,
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)}
tabIndex={0}
2019-05-08 03:38:29 +08:00
ref={(node) => { this.checkbox = node; }}
>
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}
checked={checked}
2017-04-25 10:08:18 +08:00
className={styles.input}
aria-label={ariaLabel}
2017-05-04 04:51:17 +08:00
aria-describedby={ariaDescribedBy}
disabled={disabled}
2019-05-08 03:38:29 +08:00
ref={(node) => { this.input = node; }}
2017-06-03 03:25:02 +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>
);
}
}
Checkbox.propTypes = propTypes;
Checkbox.defaultProps = defaultProps;