Skip to content

Commit

Permalink
Web-Specific Fixes for onValueChange and Deprecated React Methods (#563)
Browse files Browse the repository at this point in the history
* Fix web-specific onValueChange behavior and deprecate React methods

- Modify onValueChange in RNCSliderNativeComponent.web.tsx for web platform to ensure it's called only during user interaction.
- Replace deprecated StyleSheet.compose with array syntax in Slider.tsx and RNCSliderNativeComponent.web.tsx.

* Fix linting issues and improve code formatting in slider component

- Adjust onResponderMove and closing tag syntax for better readability and consistency
- Ensure adherence to project's linting rules for cleaner code structure

* Fix snapshot test issues by refining style definition in Slider

- Adjust the style array in Slider.tsx to conditionally include props.style
- Resolve snapshot test failures by preventing inclusion of undefined in style array
- Eliminate the need for updating snapshots as component rendering remains consistent
  • Loading branch information
landabaso authored Dec 22, 2023
1 parent 6f8ee41 commit 41e8d70
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions package/src/RNCSliderNativeComponent.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import React, {RefObject, useCallback} from 'react';
import {
Animated,
View,
StyleSheet,
ColorValue,
ViewStyle,
GestureResponderEvent,
Expand Down Expand Up @@ -118,18 +117,22 @@ const RCTSliderWebComponent = React.forwardRef(

const onSlidingStart = useCallback(
(value: number) => {
isUserInteracting.current = true;
onRNCSliderSlidingStart && onRNCSliderSlidingStart(valueToEvent(value));
},
[onRNCSliderSlidingStart],
);

const onSlidingComplete = useCallback(
(value: number) => {
isUserInteracting.current = false;
onRNCSliderSlidingComplete &&
onRNCSliderSlidingComplete(valueToEvent(value));
},
[onRNCSliderSlidingComplete],
);
// Add a ref to track user interaction
const isUserInteracting = React.useRef(false);
const updateValue = useCallback(
(newValue: number) => {
// Ensure that the value is correctly rounded
Expand All @@ -145,7 +148,9 @@ const RCTSliderWebComponent = React.forwardRef(
);
if (value !== withinBounds) {
setValue(withinBounds);
onValueChange(withinBounds);
if (isUserInteracting.current) {
onValueChange(withinBounds);
}
return withinBounds;
}
return hardRounded;
Expand Down Expand Up @@ -197,7 +202,7 @@ const RCTSliderWebComponent = React.forwardRef(
};
}, [containerRef]);

const containerStyle = StyleSheet.compose(
const containerStyle = [
{
flexGrow: 1,
flexShrink: 1,
Expand All @@ -206,7 +211,7 @@ const RCTSliderWebComponent = React.forwardRef(
alignItems: 'center',
},
style,
);
] as ViewStyle[];

const trackStyle = {
height: trackHeight,
Expand All @@ -226,7 +231,7 @@ const RCTSliderWebComponent = React.forwardRef(
flexGrow: maxPercent,
};

const thumbViewStyle = StyleSheet.compose(
const thumbViewStyle = [
{
width: thumbSize,
height: thumbSize,
Expand All @@ -236,7 +241,7 @@ const RCTSliderWebComponent = React.forwardRef(
overflow: 'hidden',
},
thumbStyle,
);
] as ViewStyle[];

const decimalPrecision = React.useRef(
calculatePrecision(minimumValue, maximumValue, step),
Expand Down
2 changes: 1 addition & 1 deletion package/src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const SliderComponent = (
props: Props,
forwardedRef?: Ref<typeof RCTSliderNativeComponent>,
) => {
const style = StyleSheet.compose(props.style, styles.slider);
const style = props.style ? [props.style, styles.slider] : styles.slider;

const {
onValueChange,
Expand Down

0 comments on commit 41e8d70

Please sign in to comment.