import React, { Component } from 'react'
import PropTypes from 'prop-types'
import cx from 'classnames'
import ToggleStyled from './toggle.styled'
const IconOn = ({ color = '#6197C6' }) => (
<span>
<svg xmlns="http://www.w3.org/2000/svg" width="9" height="6" viewBox="0 0 9 6">
<path fill={color} fillRule="evenodd" d="M2.293 5.707l-2-2a.999.999 0 1 1 1.414-1.414L3 3.586 6.293.293a.999.999 0 1 1 1.414 1.414l-4 4a.997.997 0 0 1-1.414 0z" />
</svg>
</span>
)
const IconOff = ({ color = '#95A3B7' }) => (
<span>
<svg xmlns="http://www.w3.org/2000/svg" width="6" height="6" viewBox="0 0 6 6">
<path fill={color} fillRule="evenodd" d="M1.586 3L.293 1.707A.999.999 0 1 1 1.707.293L3 1.586 4.293.293a.999.999 0 1 1 1.414 1.414L4.414 3l1.293 1.293a.999.999 0 1 1-1.414 1.414L3 4.414 1.707 5.707A.999.999 0 1 1 .293 4.293L1.586 3z" />
</svg>
</span>
)
class Toggle extends Component {
constructor(props) {
super(props)
this.state = {
on: props.on || false
}
}
onClick = () => {
const { onChange, onCheck, onUnCheck, disabled } = this.props
if (disabled)
return
this.setState({
on: !this.state.on
}, () => {
if (onChange) {
onChange(this.state.on)
}
if (this.state.on && onCheck) {
onCheck()
}
else if (!this.state.on && onUnCheck) {
onUnCheck()
}
})
}
render () {
const { on } = this.state
const { position, label, disabled } = this.props
return (
<ToggleStyled onClick={this.onClick} disabled={disabled}>
{label !== null && <div className='widget-toggle-label'>{label}</div>}
<div className={cx({
[position]: true
}, 'element-position')}>
<div className={cx({
'on': on
}, 'widget-toggle-container')}>
<div className='toggle-labels'>
<IconOn color={disabled ? '#aaaaaa' : '#6197C6'} />
<IconOff color={disabled ? '#aaaaaa' : '#95A3B7'} />
</div>
<div className='toggle-button' />
</div>
</div>
</ToggleStyled>
)
}
}
Toggle.propTypes = {
position: PropTypes.oneOf(['left', 'center', 'right']),
label: PropTypes.string,
on: PropTypes.bool,
onChange: PropTypes.func,
onCheck: PropTypes.func,
onUnCheck: PropTypes.func
}
Toggle.defaultProps = {
position: 'left',
label: null,
on: false,
onChange: () => { },
onCheck: () => { },
onUnCheck: () => { }
}
export { Toggle }
|