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

91 lines
2.4 KiB
React
Raw Normal View History

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { findDOMNode } from 'react-dom';
import KEY_CODES from '/imports/utils/keyCodes';
2021-11-06 01:21:35 +08:00
import Styled from './styles';
const propTypes = {
disabled: PropTypes.bool,
checked: PropTypes.bool,
onChange: PropTypes.func.isRequired,
ariaLabelledBy: PropTypes.string,
ariaLabel: PropTypes.string,
ariaDescribedBy: PropTypes.string,
ariaDesc: PropTypes.string,
};
const defaultProps = {
disabled: false,
checked: false,
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() {
2021-11-06 01:21:35 +08:00
const { ariaLabel, ariaDesc, ariaDescribedBy, checked, disabled } = this.props;
2017-04-25 10:08:18 +08:00
2017-01-27 23:41:11 +08:00
return (
2021-11-06 01:21:35 +08:00
<Styled.CheckboxWrapper
disabled={!!disabled}
tabIndex={0}
2019-05-08 03:38:29 +08:00
ref={(node) => { this.checkbox = node; }}
>
2021-11-06 01:21:35 +08:00
<Styled.CheckboxInput
2017-06-03 03:25:02 +08:00
type="checkbox"
2017-01-27 23:41:11 +08:00
onChange={this.handleChange}
checked={checked}
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
2021-11-06 01:21:35 +08:00
? <Styled.CheckboxIconChecked iconName="check" />
: <Styled.CheckboxIcon iconName="circle" />
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>
2021-11-06 01:21:35 +08:00
</Styled.CheckboxWrapper>
2017-01-27 23:41:11 +08:00
);
}
}
Checkbox.propTypes = propTypes;
Checkbox.defaultProps = defaultProps;