react-toolbox/components/switch/index.jsx

77 lines
2.0 KiB
React
Raw Normal View History

import React from 'react';
2015-10-07 23:24:02 +03:00
import Ripple from '../ripple';
2015-09-19 19:56:06 +03:00
import style from './style';
2015-10-07 23:24:02 +03:00
import events from '../utils/events';
2015-09-19 19:56:06 +03:00
2015-10-22 02:31:17 +03:00
class Switch extends React.Component {
static propTypes = {
2015-10-07 23:24:02 +03:00
checked: React.PropTypes.bool,
2015-09-19 19:56:06 +03:00
className: React.PropTypes.string,
disabled: React.PropTypes.bool,
label: React.PropTypes.string,
2015-10-07 23:24:02 +03:00
name: React.PropTypes.string,
onBlur: React.PropTypes.func,
onChange: React.PropTypes.func,
onFocus: React.PropTypes.func
};
2015-09-19 19:56:06 +03:00
static defaultProps = {
checked: false,
className: '',
disabled: false
};
2015-09-19 19:56:06 +03:00
2015-10-22 02:31:17 +03:00
handleChange = (event) => {
2015-10-07 23:24:02 +03:00
events.pauseEvent(event);
2015-11-12 07:52:05 +03:00
if (this.props.onChange && !this.props.disabled) this.props.onChange(event);
2015-10-22 02:31:17 +03:00
};
2015-09-19 19:56:06 +03:00
2015-10-22 02:31:17 +03:00
handleInputClick = (event) => {
2015-10-07 23:24:02 +03:00
events.pauseEvent(event);
2015-10-22 02:31:17 +03:00
};
2015-09-19 19:56:06 +03:00
2015-10-22 02:31:17 +03:00
handleMouseDown = (event) => {
2015-10-07 23:24:02 +03:00
if (!this.props.disabled) this.refs.ripple.start(event);
2015-10-22 02:31:17 +03:00
};
2015-09-19 19:56:06 +03:00
render () {
2015-10-07 23:24:02 +03:00
let labelClassName = style[this.props.disabled ? 'disabled' : 'field'];
2015-11-12 07:52:05 +03:00
const switchClassName = style[this.props.checked ? 'on' : 'off'];
2015-10-07 23:24:02 +03:00
if (this.props.className) labelClassName += ` ${this.props.className}`;
2015-09-19 19:56:06 +03:00
return (
2015-10-07 23:24:02 +03:00
<label
data-react-toolbox='checkbox'
className={labelClassName}
2015-11-12 07:52:05 +03:00
onClick={this.handleChange}
2015-09-19 19:56:06 +03:00
>
2015-10-07 23:24:02 +03:00
<input
{...this.props}
ref='input'
2015-11-12 07:52:05 +03:00
checked={this.props.checked}
2015-10-07 23:24:02 +03:00
className={style.input}
onChange={this.handleChange}
onClick={this.handleInputClick}
type='checkbox'
2015-10-07 23:24:02 +03:00
/>
<span role='switch' className={switchClassName}>
<span role='thumb' className={style.thumb} onMouseDown={this.handleMouseDown}>
<Ripple ref='ripple' role='ripple' className={style.ripple} spread={2.4} centered />
</span>
</span>
{ this.props.label ? <span className={style.text}>{this.props.label}</span> : null }
</label>
2015-09-19 19:56:06 +03:00
);
}
2015-09-19 19:56:06 +03:00
2015-10-07 23:24:02 +03:00
blur () {
this.refs.input.blur();
}
2015-10-07 23:24:02 +03:00
focus () {
this.refs.input.focus();
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Switch;