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

94 lines
2.3 KiB
React
Raw Normal View History

2017-01-27 23:41:11 +08:00
import React from 'react';
import Toggle from 'react-toggle';
2019-04-10 22:59:25 +08:00
import cx from 'classnames';
2018-03-15 00:09:52 +08:00
import { defineMessages, injectIntl } from 'react-intl';
2019-04-10 22:59:25 +08:00
import { styles } from './styles';
2017-01-27 23:41:11 +08:00
2018-03-15 00:09:52 +08:00
const intlMessages = defineMessages({
on: {
id: 'app.switch.onLabel',
description: 'label for toggle switch on state',
},
off: {
id: 'app.switch.offLabel',
description: 'label for toggle switch off state',
},
});
const defaultProps = {
showToggleLabel: true,
invertColors: false,
};
2018-03-15 00:09:52 +08:00
class Switch extends Toggle {
2017-01-27 23:41:11 +08:00
render() {
2017-03-09 22:34:33 +08:00
const {
2018-03-15 00:09:52 +08:00
intl,
2017-03-09 22:34:33 +08:00
className,
icons: _icons,
ariaLabelledBy,
ariaDescribedBy,
ariaLabel,
ariaDesc,
showToggleLabel,
invertColors,
disabled,
2018-03-15 00:09:52 +08:00
...inputProps
2017-03-09 22:34:33 +08:00
} = this.props;
const {
checked,
hasFocus,
} = this.state;
const classes = cx('react-toggle', {
'react-toggle--checked': checked,
'react-toggle--focus': hasFocus,
'react-toggle--disabled': disabled,
2017-03-01 20:22:11 +08:00
}, className);
2017-01-27 23:41:11 +08:00
return (
2018-03-15 00:09:52 +08:00
<div
2019-04-10 22:59:25 +08:00
className={cx(classes, styles.switch)}
2017-01-27 23:41:11 +08:00
onClick={this.handleClick}
onTouchStart={this.handleTouchStart}
onTouchMove={this.handleTouchMove}
2018-03-15 00:09:52 +08:00
onTouchEnd={this.handleTouchEnd}
>
<div
className={cx('react-toggle-track',
invertColors && styles.invertBackground,
checked && styles.checked)}
aria-hidden="true"
>
2018-03-15 00:09:52 +08:00
<div className="react-toggle-track-check">
{showToggleLabel ? intl.formatMessage(intlMessages.on) : null}
2017-01-27 23:41:11 +08:00
</div>
2018-03-15 00:09:52 +08:00
<div className="react-toggle-track-x">
{showToggleLabel ? intl.formatMessage(intlMessages.off) : null}
2017-01-27 23:41:11 +08:00
</div>
</div>
2018-03-15 00:09:52 +08:00
<div className="react-toggle-thumb" />
2017-01-27 23:41:11 +08:00
<input
{...inputProps}
2018-03-15 00:09:52 +08:00
ref={(ref) => { this.input = ref; }}
2017-01-27 23:41:11 +08:00
onFocus={this.handleFocus}
onBlur={this.handleBlur}
2018-03-15 00:09:52 +08:00
className="react-toggle-screenreader-only"
type="checkbox"
tabIndex="0"
disabled={disabled}
aria-label={ariaLabel}
2018-03-15 00:09:52 +08:00
aria-describedby={ariaDescribedBy}
/>
<div id={ariaDescribedBy} hidden>{ariaDesc}</div>
2017-01-27 23:41:11 +08:00
</div>
);
}
2018-03-15 00:09:52 +08:00
}
Switch.defaultProps = defaultProps;
2018-03-15 00:09:52 +08:00
export default injectIntl(Switch);