Skip to content

Commit

Permalink
add click handling to change to slider value
Browse files Browse the repository at this point in the history
  • Loading branch information
lipp committed Sep 22, 2016
1 parent b3d3ccc commit cb71634
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions components/slider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,28 @@ export default class Slider extends React.Component {
value: 0
}

update = (value, func) => {
let newValue
getSteppedValue = (value) => {
if (this.props.step) {
const steps = (value - this.state.prevValue) / this.props.step
if (Math.round(steps)) {
newValue = this.state.prevValue + Math.round(steps) * this.props.step
return this.state.prevValue + Math.round(steps) * this.props.step
}
} else {
newValue = value
return value
}
}

updateMoveValue = (value) => {
const newValue = this.getSteppedValue(value)
if (newValue !== undefined && newValue !== this.state.prevValue) {
this.setState({value: newValue, prevValue: newValue})
if (func) {
func(newValue)
if (this.props.onMove) {
this.props.onMove(newValue)
}
}
}

onChange = (event) => {
const value = parseFloat(event.target.value, 10)
setValue = function (value) {
this.setState({value, prevValue: value})
if (this.props.onChange) {
this.props.onChange(value)
Expand All @@ -59,6 +61,11 @@ export default class Slider extends React.Component {
}
}

onChange = (event) => {
const value = parseFloat(event.target.value, 10)
this.setValue(value)
}

constructor (props) {
super(props)
this.state = {
Expand All @@ -67,7 +74,7 @@ export default class Slider extends React.Component {
value: props.value || 0,
prevValue: props.value || 0,
step: 1,
// ratio between actual dom element width and range value (0 -> 100)
// ratio between actual dom element width and range value (min -> max)
ratio: 1
}
}
Expand Down Expand Up @@ -121,7 +128,15 @@ export default class Slider extends React.Component {
onMouseMove = (event) => {
if (this.state.mouseDown) {
const value = this.getValueFromSlider(event)
this.update(value, this.props.onMove)
this.updateMoveValue(value)
}
}

onClick = (event) => {
const value = this.getValueFromSlider(event)
const steppedValue = this.getSteppedValue(value)
if (steppedValue !== undefined) {
this.setValue(steppedValue)
}
}

Expand All @@ -145,6 +160,7 @@ export default class Slider extends React.Component {
onTouchStart={this.onMouseDown}
onMouseMove={this.onMouseMove}
onTouchMove={this.onMouseMove}
onClick={this.onClick}
ref='slider'>
<div className='Slider-track-off'>
<div className='Slider-track-on' style={{width: `${position}px`}} />
Expand Down

0 comments on commit cb71634

Please sign in to comment.