react-toolbox/components/slider/index.jsx

258 lines
6.4 KiB
React
Raw Normal View History

import React from 'react';
import ReactDOM from 'react-dom';
2015-10-10 12:25:47 +03:00
import style from './style';
2015-09-09 11:23:19 +03:00
import utils from '../utils';
import ProgressBar from '../progress_bar';
import Input from '../input';
2015-09-09 03:16:05 +03:00
export default React.createClass({
2015-09-09 03:16:05 +03:00
displayName: 'Slider',
propTypes: {
className: React.PropTypes.string,
editable: React.PropTypes.bool,
max: React.PropTypes.number,
min: React.PropTypes.number,
onChange: React.PropTypes.func,
pinned: React.PropTypes.bool,
snaps: React.PropTypes.bool,
step: React.PropTypes.number,
value: React.PropTypes.number
},
getDefaultProps () {
return {
className: '',
editable: false,
max: 100,
min: 0,
pinned: false,
snaps: false,
step: 0.01,
value: 0
};
},
getInitialState () {
return {
sliderStart: 0,
sliderLength: 0,
value: this.props.value
};
},
componentDidMount () {
window.addEventListener('resize', this.onResize);
this.onResize();
},
componentWillUnmount () {
window.removeEventListener('resize', this.onResize);
},
componentDidUpdate (prevProps, prevState) {
2015-09-09 03:57:53 +03:00
if (prevState.value !== this.state.value) {
if (this.props.onChange) this.props.onChange(this);
if (this.refs.input) this.refs.input.setValue(this.valueForInput(this.state.value));
2015-09-09 03:16:05 +03:00
}
},
onResize () {
const {left, right} = ReactDOM.findDOMNode(this.refs.progressbar).getBoundingClientRect();
2015-09-09 03:57:53 +03:00
this.setState({sliderStart: left, sliderLength: right - left});
2015-09-09 03:16:05 +03:00
},
onSliderFocus () {
utils.events.addEventsToDocument(this.getKeyboardEvents());
},
onSliderBlur () {
utils.events.removeEventsFromDocument(this.getKeyboardEvents());
},
2015-10-10 12:25:47 +03:00
onInputChange () {
this.setState({value: this.trimValue(this.refs.input.getValue()) });
2015-09-09 03:16:05 +03:00
},
onKeyDown (event) {
if ([13, 27].indexOf(event.keyCode) !== -1) ReactDOM.findDOMNode(this).blur();
2015-09-09 03:16:05 +03:00
if (event.keyCode === 38) this.addToValue(this.props.step);
if (event.keyCode === 40) this.addToValue(-this.props.step);
if (event.keyCode !== 9) utils.events.pauseEvent(event);
2015-09-09 03:16:05 +03:00
},
onMouseDown (event) {
utils.events.addEventsToDocument(this.getMouseEventMap());
2015-09-09 03:57:53 +03:00
this.start(utils.events.getMousePosition(event));
utils.events.pauseEvent(event);
2015-09-09 03:16:05 +03:00
},
onTouchStart (event) {
this.start(utils.events.getTouchPosition(event));
utils.events.addEventsToDocument(this.getTouchEventMap());
2015-09-09 03:57:53 +03:00
utils.events.pauseEvent(event);
2015-09-09 03:16:05 +03:00
},
onMouseMove (event) {
utils.events.pauseEvent(event);
this.move(utils.events.getMousePosition(event));
},
onTouchMove (event) {
this.move(utils.events.getTouchPosition(event));
},
onMouseUp () {
this.end(this.getMouseEventMap());
},
onTouchEnd () {
this.end(this.getTouchEventMap());
},
getMouseEventMap () {
return {
mousemove: this.onMouseMove,
mouseup: this.onMouseUp
};
},
getTouchEventMap () {
return {
touchmove: this.onTouchMove,
touchend: this.onTouchEnd
};
},
getKeyboardEvents () {
return {
keydown: this.onKeyDown
};
},
start (position) {
2015-09-09 03:57:53 +03:00
this.setState({pressed: true, value: this.positionToValue(position)});
2015-09-09 03:16:05 +03:00
},
move (position) {
2015-09-09 03:57:53 +03:00
this.setState({value: this.positionToValue(position)});
2015-09-09 03:16:05 +03:00
},
end (revents) {
utils.events.removeEventsFromDocument(revents);
this.setState({pressed: false});
},
2015-09-09 03:57:53 +03:00
positionToValue (position) {
let { sliderStart: start, sliderLength: length } = this.state;
let { max, min } = this.props;
return this.trimValue((position.x - start) / length * (max - min) + min);
2015-09-09 03:16:05 +03:00
},
trimValue (value) {
if (value < this.props.min) return this.props.min;
if (value > this.props.max) return this.props.max;
return utils.round(value, this.stepDecimals());
},
stepDecimals () {
return (this.props.step.toString().split('.')[1] || []).length;
},
addToValue (value) {
2015-09-09 03:57:53 +03:00
this.setState({value: this.trimValue(this.state.value + value)});
2015-09-09 03:16:05 +03:00
},
valueForInput (value) {
const decimals = this.stepDecimals();
return decimals > 0 ? value.toFixed(decimals) : value.toString();
},
2015-09-09 03:57:53 +03:00
knobOffset () {
let { max, min } = this.props;
return this.state.sliderLength * (this.state.value - min) / (max - min);
2015-09-09 03:16:05 +03:00
},
renderSnaps () {
if (this.props.snaps) {
return (
2015-10-10 12:25:47 +03:00
<div ref='snaps' className={style.snaps}>
2015-09-09 03:57:53 +03:00
{
utils.range(0, (this.props.max - this.props.min) / this.props.step).map(i => {
2015-10-10 12:25:47 +03:00
return (<div key={`span-${i}`} className={style.snap}></div>);
2015-09-09 03:57:53 +03:00
})
}
2015-09-09 03:16:05 +03:00
</div>
);
}
},
renderInput () {
if (this.props.editable) {
return (
<Input
ref='input'
2015-10-10 12:25:47 +03:00
className={style.input}
2015-09-09 03:16:05 +03:00
onChange={this.onInputChange}
value={this.valueForInput(this.state.value)} />
);
}
},
render () {
2015-09-09 03:57:53 +03:00
let knobStyles = utils.prefixer({transform: `translateX(${this.knobOffset()}px)`});
2015-09-09 03:16:05 +03:00
let className = this.props.className;
2015-10-10 12:25:47 +03:00
if (this.props.editable) className += ` ${style.editable}`;
if (this.props.pinned) className += ` ${style.pinned}`;
if (this.state.pressed) className += ` ${style.pressed}`;
if (this.state.value === this.props.min) className += ` ${style.ring}`;
2015-09-09 03:16:05 +03:00
return (
<div
2015-10-06 05:42:56 +03:00
data-react-toolbox='slider'
2015-10-10 12:25:47 +03:00
className={style.root + className}
2015-09-09 03:16:05 +03:00
tabIndex='0'
onFocus={this.onSliderFocus}
onBlur={this.onSliderBlur} >
<div
ref='slider'
2015-10-10 12:25:47 +03:00
className={style.container}
2015-09-09 03:57:53 +03:00
onTouchStart={this.onTouchStart}
onMouseDown={this.onMouseDown} >
2015-09-09 03:16:05 +03:00
<div
ref='knob'
2015-10-10 12:25:47 +03:00
className={style.knob}
2015-09-09 03:16:05 +03:00
style={knobStyles}
onMouseDown={this.onMouseDown}
onTouchStart={this.onTouchStart} >
2015-10-10 12:25:47 +03:00
<div className={style.innerknob} data-value={parseInt(this.state.value)}></div>
2015-09-09 03:16:05 +03:00
</div>
2015-10-10 12:25:47 +03:00
<div className={style.progress}>
2015-09-09 03:16:05 +03:00
<ProgressBar
ref='progressbar'
mode='determinate'
2015-10-10 12:25:47 +03:00
className={style.innerprogress}
2015-09-09 03:16:05 +03:00
value={this.state.value}
max={this.props.max}
min={this.props.min}/>
{ this.renderSnaps() }
</div>
</div>
{ this.renderInput() }
</div>
);
},
getValue () {
return this.state.value;
},
setValue (value) {
this.setState({value: value});
}
});