Advertisement
Guest User

Untitled

a guest
Aug 24th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export default class RangeStepInput extends React.Component {
  2.     constructor(props) {
  3.         super(props);
  4.         this.state = {
  5.             isMouseDown: false,
  6.             isDragging: false
  7.         }
  8.         this.onInput = this.onInput.bind(this);
  9.         this.onMouseDown = this.onMouseDown.bind(this);
  10.         this.onMouseUp = this.onMouseUp.bind(this);
  11.         this.onMouseMove = this.onMouseMove.bind(this);
  12.     }
  13.     render() {
  14.         return <input
  15.                    type="range"
  16.                    className={this.props.className}
  17.                    min={this.props.min}
  18.                    max={this.props.max}
  19.                    step={this.props.step}
  20.                    value={this.props.value}
  21.                    name={this.props.name}
  22.                    id={this.props.id}
  23.                    onChange={this.props.onChange}
  24.                    onMouseDown={this.onMouseDown}
  25.                    onMouseUp={this.onMouseUp}
  26.                    onMouseMove={this.onMouseMove}
  27.                    onClick={this.onClick}
  28.                    onInput={this.onInput} />;
  29.     }
  30.     onMouseDown() {
  31.         this.setState({isMouseDown: true});
  32.     }
  33.     onMouseUp() {
  34.         this.setState({
  35.             isMouseDown: false,
  36.             isDragging: false
  37.         });
  38.     }
  39.     onMouseMove() {
  40.         if (this.state.isMouseDown) {
  41.             this.setState({isDragging: true});
  42.         }
  43.     }
  44.     onInput(e) {
  45.         const step = forceFloat(e.target.step);
  46.         const newVal = forceFloat(e.target.value);
  47.         const oldVal = this.props.value;
  48.  
  49.         if (
  50.             // Disable the oninput filter with the user is dragging
  51.             // the slider's knob.
  52.             !(this.state.isMouseDown && this.state.isDragging) &&
  53.             oldVal
  54.         ) {
  55.             e.target.value = (newVal > oldVal) ?
  56.                              oldVal + step : oldVal - step;
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement