Skip to content

Commit 41e8d70

Browse files
authored
Web-Specific Fixes for onValueChange and Deprecated React Methods (#563)
* 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
1 parent 6f8ee41 commit 41e8d70

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

package/src/RNCSliderNativeComponent.web.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import React, {RefObject, useCallback} from 'react';
44
import {
55
Animated,
66
View,
7-
StyleSheet,
87
ColorValue,
98
ViewStyle,
109
GestureResponderEvent,
@@ -118,18 +117,22 @@ const RCTSliderWebComponent = React.forwardRef(
118117

119118
const onSlidingStart = useCallback(
120119
(value: number) => {
120+
isUserInteracting.current = true;
121121
onRNCSliderSlidingStart && onRNCSliderSlidingStart(valueToEvent(value));
122122
},
123123
[onRNCSliderSlidingStart],
124124
);
125125

126126
const onSlidingComplete = useCallback(
127127
(value: number) => {
128+
isUserInteracting.current = false;
128129
onRNCSliderSlidingComplete &&
129130
onRNCSliderSlidingComplete(valueToEvent(value));
130131
},
131132
[onRNCSliderSlidingComplete],
132133
);
134+
// Add a ref to track user interaction
135+
const isUserInteracting = React.useRef(false);
133136
const updateValue = useCallback(
134137
(newValue: number) => {
135138
// Ensure that the value is correctly rounded
@@ -145,7 +148,9 @@ const RCTSliderWebComponent = React.forwardRef(
145148
);
146149
if (value !== withinBounds) {
147150
setValue(withinBounds);
148-
onValueChange(withinBounds);
151+
if (isUserInteracting.current) {
152+
onValueChange(withinBounds);
153+
}
149154
return withinBounds;
150155
}
151156
return hardRounded;
@@ -197,7 +202,7 @@ const RCTSliderWebComponent = React.forwardRef(
197202
};
198203
}, [containerRef]);
199204

200-
const containerStyle = StyleSheet.compose(
205+
const containerStyle = [
201206
{
202207
flexGrow: 1,
203208
flexShrink: 1,
@@ -206,7 +211,7 @@ const RCTSliderWebComponent = React.forwardRef(
206211
alignItems: 'center',
207212
},
208213
style,
209-
);
214+
] as ViewStyle[];
210215

211216
const trackStyle = {
212217
height: trackHeight,
@@ -226,7 +231,7 @@ const RCTSliderWebComponent = React.forwardRef(
226231
flexGrow: maxPercent,
227232
};
228233

229-
const thumbViewStyle = StyleSheet.compose(
234+
const thumbViewStyle = [
230235
{
231236
width: thumbSize,
232237
height: thumbSize,
@@ -236,7 +241,7 @@ const RCTSliderWebComponent = React.forwardRef(
236241
overflow: 'hidden',
237242
},
238243
thumbStyle,
239-
);
244+
] as ViewStyle[];
240245

241246
const decimalPrecision = React.useRef(
242247
calculatePrecision(minimumValue, maximumValue, step),

package/src/Slider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ const SliderComponent = (
192192
props: Props,
193193
forwardedRef?: Ref<typeof RCTSliderNativeComponent>,
194194
) => {
195-
const style = StyleSheet.compose(props.style, styles.slider);
195+
const style = props.style ? [props.style, styles.slider] : styles.slider;
196196

197197
const {
198198
onValueChange,

0 commit comments

Comments
 (0)