2017-01-27 23:41:11 +08:00
|
|
|
import React from 'react';
|
|
|
|
import Toggle from 'react-toggle';
|
2017-03-01 20:22:11 +08:00
|
|
|
import classNames from 'classnames';
|
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',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
2018-03-15 00:09:52 +08:00
|
|
|
...inputProps
|
2017-03-09 22:34:33 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2017-01-27 23:41:11 +08:00
|
|
|
const classes = classNames('react-toggle', {
|
|
|
|
'react-toggle--checked': this.state.checked,
|
|
|
|
'react-toggle--focus': this.state.hasFocus,
|
|
|
|
'react-toggle--disabled': this.props.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="react-toggle-track" aria-hidden="true">
|
|
|
|
<div className="react-toggle-track-check">
|
|
|
|
{intl.formatMessage(intlMessages.on)}
|
2017-01-27 23:41:11 +08:00
|
|
|
</div>
|
2018-03-15 00:09:52 +08:00
|
|
|
<div className="react-toggle-track-x">
|
|
|
|
{intl.formatMessage(intlMessages.off)}
|
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"
|
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>
|
2017-01-27 23:41:11 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-03-15 00:09:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default injectIntl(Switch);
|