Skip to content

Commit

Permalink
Fix #182 (#186)
Browse files Browse the repository at this point in the history
* Fix value not being updated on change

* Update src/js/RNCSliderNativeComponent.web.js

* Fix lint warnings

Co-authored-by: François Billioud <darksharcoux@hotmail.com>
  • Loading branch information
brentvatne and Sharcoux committed May 25, 2020
1 parent 2d9a498 commit 6ce386e
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions src/js/RNCSliderNativeComponent.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import React from 'react';
import React, {useCallback} from 'react';
import {View, StyleSheet} from 'react-native';

// import type {ViewStyleProp} from 'react-native/Libraries/StyleSheet/StyleSheet';
Expand Down Expand Up @@ -154,19 +154,37 @@ const RCTSliderWebComponent = React.forwardRef(
},
forwardedRef,
) => {
const onValueChange = value =>
onRNCSliderValueChange &&
onRNCSliderValueChange({nativeEvent: {fromUser: true, value}});
const onSlidingStart = value =>
onRNCSliderSlidingStart &&
onRNCSliderSlidingStart({nativeEvent: {fromUser: true, value}});
const onSlidingComplete = value =>
onRNCSliderSlidingComplete &&
onRNCSliderSlidingComplete({nativeEvent: {fromUser: true, value}});
const onValueChange = useCallback(
value => {
onRNCSliderValueChange &&
onRNCSliderValueChange({nativeEvent: {fromUser: true, value}});
},
[onRNCSliderValueChange],
);

const onSlidingStart = useCallback(
value => {
onRNCSliderSlidingStart &&
onRNCSliderSlidingStart({nativeEvent: {fromUser: true, value}});
},
[onRNCSliderSlidingStart],
);

const onSlidingComplete = useCallback(
value => {
onRNCSliderSlidingComplete &&
onRNCSliderSlidingComplete({nativeEvent: {fromUser: true, value}});
},
[onRNCSliderSlidingComplete],
);

const containerSize = React.useRef({width: 0, height: 0});
const containerRef = forwardedRef || React.createRef();
const [value, setValue] = React.useState(initialValue || minimumValue);
React.useLayoutEffect(() => updateValue(initialValue), [
initialValue,
updateValue,
]);

const percentageValue =
(value - minimumValue) / (maximumValue - minimumValue);
Expand Down Expand Up @@ -223,17 +241,20 @@ const RCTSliderWebComponent = React.forwardRef(
thumbStyle,
);

const updateValue = newValue => {
// Ensure that the new value is still between the bounds
const withinBounds = Math.max(
minimumValue,
Math.min(newValue, maximumValue),
);
if (value !== withinBounds) {
setValue(withinBounds);
onValueChange(withinBounds);
}
};
const updateValue = useCallback(
newValue => {
// Ensure that the new value is still between the bounds
const withinBounds = Math.max(
minimumValue,
Math.min(newValue, maximumValue),
);
if (value !== withinBounds) {
setValue(withinBounds);
onValueChange(value);
}
},
[minimumValue, maximumValue, value, onValueChange],
);

const onTouchEnd = () => {
onSlidingComplete(value);
Expand Down

0 comments on commit 6ce386e

Please sign in to comment.