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',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-09-10 21:43:06 +08:00
|
|
|
const defaultProps = {
|
|
|
|
showToggleLabel: true,
|
2019-10-05 05:45:09 +08:00
|
|
|
invertColors: false,
|
2019-09-10 21:43:06 +08:00
|
|
|
};
|
|
|
|
|
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,
|
2019-09-10 21:43:06 +08:00
|
|
|
showToggleLabel,
|
2019-10-05 05:45:09 +08:00
|
|
|
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;
|
|
|
|
|
2019-10-05 05:45:09 +08:00
|
|
|
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
|
2019-10-05 05:45:09 +08:00
|
|
|
aria-hidden="true"
|
2021-10-27 20:51:44 +08:00
|
|
|
checked={checked}
|
|
|
|
invertColors={invertColors}
|
|
|
|
animations={animations}
|
2019-10-05 05:45:09 +08:00
|
|
|
>
|
2021-10-27 20:51:44 +08:00
|
|
|
<Styled.ToggleTrackCheck checked={checked} animations={animations}>
|
2019-09-10 21:43:06 +08:00
|
|
|
{showToggleLabel ? intl.formatMessage(intlMessages.on) : null}
|
2021-10-27 20:51:44 +08:00
|
|
|
</Styled.ToggleTrackCheck>
|
|
|
|
<Styled.ToggleTrackX checked={checked} animations={animations}>
|
2019-09-10 21:43:06 +08:00
|
|
|
{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}
|
|
|
|
/>
|
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"
|
2019-10-05 05:45:09 +08:00
|
|
|
disabled={disabled}
|
2018-03-02 05:15:18 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-10 21:43:06 +08:00
|
|
|
Switch.defaultProps = defaultProps;
|
|
|
|
|
2018-03-15 00:09:52 +08:00
|
|
|
export default injectIntl(Switch);
|