Skip to content

Commit

Permalink
fix: onChange now returns the correct value when used as a controlled…
Browse files Browse the repository at this point in the history
… component

Fixes #197
  • Loading branch information
Brian Stone committed Nov 3, 2020
1 parent 5e26409 commit 19c218a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/components/ReactSlider/ReactSlider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ class ReactSlider extends React.Component {
sliderLength: 0,
value,
zIndices,
tempArray: value.slice(),
};
}

Expand All @@ -384,10 +383,13 @@ class ReactSlider extends React.Component {
return null;
}

// Do not allow controlled upates to happen while we have pending updates
if (state.pending) {
return null;
}

return {
...state,
value: value.map(item => trimAlignValue(item, props)),
tempArray: value.slice(),
};
}

Expand Down Expand Up @@ -423,6 +425,9 @@ class ReactSlider extends React.Component {
};

onEnd(eventMap) {
// Allow controlled updates to continue
this.setState({ pending: false });

if (eventMap) {
removeHandlers(eventMap);
}
Expand All @@ -433,6 +438,9 @@ class ReactSlider extends React.Component {
}

onMouseMove = e => {
// Prevent controlled updates from happening while mouse is moving
this.setState({ pending: true });

const position = this.getMousePosition(e);
const diffPosition = this.getDiffPosition(position[0]);
const newValue = this.getValueFromPosition(diffPosition);
Expand All @@ -444,6 +452,9 @@ class ReactSlider extends React.Component {
return;
}

// Prevent controlled updates from happending while touch is moving
this.setState({ pending: true });

const position = this.getTouchPosition(e);

if (typeof this.isScrolling === 'undefined') {
Expand All @@ -467,6 +478,10 @@ class ReactSlider extends React.Component {
if (e.ctrlKey || e.shiftKey || e.altKey) {
return;
}

// Prevent controlled updates from happening while a key is pressed
this.setState({ pending: true });

switch (e.key) {
case 'ArrowLeft':
case 'ArrowDown':
Expand Down Expand Up @@ -507,6 +522,10 @@ class ReactSlider extends React.Component {
if (this.props.disabled || e.button === 2) {
return;
}

// Prevent controlled updates from happening while mouse is moving
this.setState({ pending: true });

if (!this.props.snapDragDisabled) {
const position = this.getMousePosition(e);
this.forceValueFromPosition(position[0], i => {
Expand Down Expand Up @@ -618,6 +637,10 @@ class ReactSlider extends React.Component {
if (this.props.disabled || e.button === 2) {
return;
}

// Prevent controlled updates from happending while mouse is moving
this.setState({ pending: true });

const position = this.getMousePosition(e);
this.start(i, position[0]);
addHandlers(this.getMouseEventMap());
Expand All @@ -629,6 +652,10 @@ class ReactSlider extends React.Component {
if (this.props.disabled || e.touches.length > 1) {
return;
}

// Prevent controlled updates from happending while touch is moving
this.setState({ pending: true });

const position = this.getTouchPosition(e);
this.startPosition = position;
// don't know yet if the user is trying to scroll
Expand Down Expand Up @@ -981,7 +1008,7 @@ class ReactSlider extends React.Component {
renderThumbs(offset) {
const { length } = offset;

const styles = this.state.tempArray;
const styles = [];
for (let i = 0; i < length; i += 1) {
styles[i] = this.buildThumbStyle(offset[i], i);
}
Expand Down Expand Up @@ -1055,7 +1082,7 @@ class ReactSlider extends React.Component {
}

render() {
const offset = this.state.tempArray;
const offset = [];
const { value } = this.state;
const l = value.length;
for (let i = 0; i < l; i += 1) {
Expand Down
17 changes: 17 additions & 0 deletions src/components/ReactSlider/ReactSlider.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ Track changes with `onBeforeChange`, `onChange`, and `onAfterChange` event handl
/>
```

Using the `onChange` event handler, you can use the slider as a controlled component

```jsx
const [value, setValue] = React.useState([25, 50]);

<ReactSlider
value={value}
onBeforeChange={val => console.log('onBeforeChange value:', val)}
onChange={val => { console.log('onChange value:', val); setValue(val); }}
onAfterChange={val => console.log('onAfterChange value:', val)}
className="horizontal-slider"
thumbClassName="example-thumb"
trackClassName="example-track"
renderThumb={(props, state) => <div {...props}>{state.valueNow}</div>}
/>
```

Custom styling using [styled-components](https://www.styled-components.com/)

```jsx
Expand Down

0 comments on commit 19c218a

Please sign in to comment.