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

95 lines
2.4 KiB
React
Raw Normal View History

2017-01-27 23:41:11 +08:00
import React from 'react';
import Toggle from 'react-toggle';
2018-03-15 00:09:52 +08:00
import { defineMessages, injectIntl } from 'react-intl';
2021-10-27 20:51:44 +08:00
import Settings from '/imports/ui/services/settings';
import Styled 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
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;
2021-10-27 20:51:44 +08:00
const { animations } = Settings.application;
const {
checked,
hasFocus,
} = this.state;
2017-01-27 23:41:11 +08:00
return (
2021-10-27 20:51:44 +08:00
<Styled.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}
2021-10-27 20:51:44 +08:00
disabled={disabled}
animations={animations}
2018-03-15 00:09:52 +08:00
>
2021-10-27 20:51:44 +08:00
<Styled.ToggleTrack
aria-hidden="true"
2021-10-27 20:51:44 +08:00
checked={checked}
invertColors={invertColors}
animations={animations}
>
2021-10-27 20:51:44 +08:00
<Styled.ToggleTrackCheck checked={checked} animations={animations}>
{showToggleLabel ? intl.formatMessage(intlMessages.on) : null}
2021-10-27 20:51:44 +08:00
</Styled.ToggleTrackCheck>
<Styled.ToggleTrackX checked={checked} animations={animations}>
{showToggleLabel ? intl.formatMessage(intlMessages.off) : null}
2021-10-27 20:51:44 +08:00
</Styled.ToggleTrackX>
</Styled.ToggleTrack>
<Styled.ToggleThumb
checked={checked}
hasFocus={hasFocus}
disabled={disabled}
animations={animations}
isRTL={document.getElementsByTagName('html')[0].dir === 'rtl'}
2021-10-27 20:51:44 +08:00
/>
2017-01-27 23:41:11 +08:00
2021-10-27 20:51:44 +08:00
<Styled.ScreenreaderInput
2017-01-27 23:41:11 +08:00
{...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
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>
2021-10-27 20:51:44 +08:00
</Styled.Switch>
2017-01-27 23:41:11 +08:00
);
}
2018-03-15 00:09:52 +08:00
}
Switch.defaultProps = defaultProps;
2018-03-15 00:09:52 +08:00
export default injectIntl(Switch);