|
| 1 | +/** |
| 2 | + * @jsx React.DOM |
| 3 | + */ |
| 4 | + |
| 5 | +var React = require('react'), |
| 6 | + Paper = require('./paper.jsx'), |
| 7 | + Classable = require('./mixins/classable.js'), |
| 8 | + Draggable = require('react-draggable'); |
| 9 | + |
| 10 | +var Slider = React.createClass({ |
| 11 | + |
| 12 | + propTypes: { |
| 13 | + required: React.PropTypes.bool, |
| 14 | + disabled: React.PropTypes.bool, |
| 15 | + min: React.PropTypes.number, |
| 16 | + max: React.PropTypes.number, |
| 17 | + step: React.PropTypes.number, |
| 18 | + error: React.PropTypes.string, |
| 19 | + description: React.PropTypes.string, |
| 20 | + name: React.PropTypes.string.isRequired, |
| 21 | + onChange: React.PropTypes.func |
| 22 | + }, |
| 23 | + |
| 24 | + mixins: [Classable], |
| 25 | + |
| 26 | + getDefaultProps: function() { |
| 27 | + return { |
| 28 | + required: true, |
| 29 | + disabled: false, |
| 30 | + defaultValue: 0, |
| 31 | + min: 0, |
| 32 | + max: 1, |
| 33 | + dragging: false |
| 34 | + }; |
| 35 | + }, |
| 36 | + |
| 37 | + getInitialState: function() { |
| 38 | + var value = this.props.value; |
| 39 | + if (value == null) value = this.props.defaultValue; |
| 40 | + var percent = value / this.props.max; |
| 41 | + if (isNaN(percent)) percent = 0; |
| 42 | + return { |
| 43 | + value: value, |
| 44 | + percent: percent |
| 45 | + } |
| 46 | + }, |
| 47 | + |
| 48 | + componentWillReceiveProps: function(nextProps) { |
| 49 | + if (nextProps.value != null) { |
| 50 | + this.setValue(nextProps.value); |
| 51 | + } |
| 52 | + }, |
| 53 | + |
| 54 | + render: function() { |
| 55 | + var classes = this.getClasses('mui-input', { |
| 56 | + 'mui-error': this.props.error != null |
| 57 | + }); |
| 58 | + |
| 59 | + var sliderClasses = this.getClasses('mui-slider', { |
| 60 | + 'mui-slider-zero': this.state.percent == 0, |
| 61 | + 'mui-disabled': this.props.disabled |
| 62 | + }); |
| 63 | + |
| 64 | + var percent = this.state.percent; |
| 65 | + if (percent > 1) percent = 1; else if (percent < 0) percent = 0; |
| 66 | + |
| 67 | + return ( |
| 68 | + <div className={classes}> |
| 69 | + <span className="mui-input-highlight"></span> |
| 70 | + <span className="mui-input-bar"></span> |
| 71 | + <span className="mui-input-description">{this.props.description}</span> |
| 72 | + <span className="mui-input-error">{this.props.error}</span> |
| 73 | + <div className={sliderClasses} onClick={this._onClick}> |
| 74 | + <div ref="track" className="mui-slider-track"> |
| 75 | + <Draggable axis="x" bound="point" |
| 76 | + cancel={this.props.disabled ? '*' : null} |
| 77 | + start={{x: (percent * 100) + '%'}} |
| 78 | + onStart={this._onDragStart} |
| 79 | + onStop={this._onDragStop} |
| 80 | + onDrag={this._onDragUpdate}> |
| 81 | + <div className="mui-slider-handle" tabIndex={0}></div> |
| 82 | + </Draggable> |
| 83 | + <div className="mui-slider-selection mui-slider-selection-low" |
| 84 | + style={{width: (percent * 100) + '%'}}> |
| 85 | + <div className="mui-slider-selection-fill"></div> |
| 86 | + </div> |
| 87 | + <div className="mui-slider-selection mui-slider-selection-high" |
| 88 | + style={{width: ((1 - percent) * 100) + '%'}}> |
| 89 | + <div className="mui-slider-selection-fill"></div> |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + <input ref="input" type="hidden" |
| 94 | + name={this.props.name} |
| 95 | + value={this.state.value} |
| 96 | + required={this.props.required} |
| 97 | + min={this.props.min} |
| 98 | + max={this.props.max} |
| 99 | + step={this.props.step} /> |
| 100 | + </div> |
| 101 | + ); |
| 102 | + }, |
| 103 | + |
| 104 | + getValue: function() { |
| 105 | + return this.state.value; |
| 106 | + }, |
| 107 | + |
| 108 | + setValue: function(i) { |
| 109 | + // calculate percentage |
| 110 | + var percent = (i - this.props.min) / (this.props.max - this.props.min); |
| 111 | + if (isNaN(percent)) percent = 0; |
| 112 | + // update state |
| 113 | + this.setState({ |
| 114 | + value: i, |
| 115 | + percent: percent |
| 116 | + }); |
| 117 | + }, |
| 118 | + |
| 119 | + getPercent: function() { |
| 120 | + return this.state.percent; |
| 121 | + }, |
| 122 | + |
| 123 | + setPercent: function (percent) { |
| 124 | + var value = percent * (this.props.max - this.props.min) + this.props.min; |
| 125 | + this.setState({value: value, percent: percent}); |
| 126 | + }, |
| 127 | + |
| 128 | + clearValue: function() { |
| 129 | + this.setValue(0); |
| 130 | + }, |
| 131 | + |
| 132 | + _onClick: function (e) { |
| 133 | + // let draggable handle the slider |
| 134 | + if (this.state.dragging || this.props.disabled) return; |
| 135 | + var node = this.refs.track.getDOMNode(); |
| 136 | + var boundingClientRect = node.getBoundingClientRect(); |
| 137 | + var offset = e.clientX - boundingClientRect.left; |
| 138 | + var percent = offset / node.clientWidth; |
| 139 | + this.setPercent(percent); |
| 140 | + }, |
| 141 | + |
| 142 | + _onDragStart: function(e, ui) { |
| 143 | + this.setState({ |
| 144 | + dragging: true |
| 145 | + }); |
| 146 | + }, |
| 147 | + |
| 148 | + _onDragStop: function(e, ui) { |
| 149 | + this.setState({ |
| 150 | + dragging: false |
| 151 | + }); |
| 152 | + }, |
| 153 | + |
| 154 | + _onDragUpdate: function(e, ui) { |
| 155 | + if (!this.state.dragging) return; |
| 156 | + var value = this.state.value; |
| 157 | + if (!this.props.disabled) this._dragX(ui.position.left); |
| 158 | + if (this.state.value != value && this.props.onChange) { |
| 159 | + this.props.onChange(e, this.state.value); |
| 160 | + } |
| 161 | + }, |
| 162 | + |
| 163 | + _dragX: function(pos) { |
| 164 | + var max = this.refs.track.getDOMNode().clientWidth; |
| 165 | + if (pos < 0) pos = 0; else if (pos > max) pos = max; |
| 166 | + this.setPercent(pos / max); |
| 167 | + } |
| 168 | + |
| 169 | +}); |
| 170 | + |
| 171 | +module.exports = Slider; |
0 commit comments