Skip to content

Commit b3e8f28

Browse files
* disabled prop added
* fromUser param added to onValueChanged callback * Additional onValueChanged event trigger removed
1 parent a9fcd1e commit b3e8f28

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ const handleValueChange = useCallback((low, high) => {
7575
| `high` | High value of slider | number | Initially `max` value will be set if not provided |
7676
| `floatingLabel` | If set to `true`, labels will not take space in component tree. Instead they will be rendered over the content above the slider (like a small popup). | boolean | `false` |
7777
| `disableRange` | Slider works as an ordinary slider with 1 control if `true` | boolean | `false` |
78+
| `disabled` | Any user interactions will be ignored if `true` | boolean | `false` |
7879
| `allowLabelOverflow` | If set to `true`, labels are allowed to be drawn outside of slider component's bounds.<br/>Otherwise label's edges will never get out of component's edges. | boolean | `false` |
7980
| `renderThumb` | Should render the thumb. | `() => Node` | _**required**_ |
8081
| `renderRail` | Should render the "rail" for thumbs.<br/>Rendered component **should** have `flex: 1` style so it won't fill up the whole space. | `() => Node` | _**required**_ |
8182
| `renderRailSelected` | Should render the selected part of "rail" for thumbs.<br/>Rendered component **should not** have `flex: 1` style so it fills up the whole space. | `() => Node` | _**required**_ |
8283
| `renderLabel` | Should render label above thumbs.<br/>If no function is passed, no label will be drawn. | `(value: number) => Node` | `undefined` |
8384
| `renderNotch` | Should render the notch below the label (above the thumbs).<br/>Classic notch is a small triangle below the label.<br/>If `allowLabelOverflow` is not set to true, the notch will continue moving with thumb even if the label has already reached the edge of the component and can't move further.| `() => Node` | `undefined` |
84-
| `onValueChanged` | Will be called when a value was changed.<br/>If `disableRange` is set to true, the second argument should be ignored. | `(low: number, high: number) => void` | `undefined` |
85+
| `onValueChanged` | Will be called when a value was changed.<br/>If `disableRange` is set to true, the second argument should be ignored.<br/>`fromUser` will be true if the value was changed by user's interaction. | `(low: number, high: number, fromUser: boolean) => void` | `undefined` |
8586

8687
All the other props (e.g. style) will be passed to root container component.
8788

hooks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ export const useThumbFollower = (containerWidthRef, gestureStateRef, renderConte
107107
export const useSelectedRail = (inPropsRef, containerWidthRef, thumbWidth, disableRange) => {
108108
const { current: left } = useRef(new Animated.Value(0));
109109
const { current: right } = useRef(new Animated.Value(0));
110-
const update = () => {
110+
const update = useCallback(() => {
111111
const { low, high, min, max } = inPropsRef.current;
112112
const { current: containerWidth } = containerWidthRef;
113113
const fullScale = (max - min) / (containerWidth - thumbWidth);
114114
const leftValue = (low - min) / fullScale;
115115
const rightValue = (max - high) / fullScale;
116116
left.setValue(disableRange ? 0 : leftValue);
117117
right.setValue(disableRange ? (containerWidth - thumbWidth) - leftValue : rightValue);
118-
};
118+
}, [inPropsRef, containerWidthRef, disableRange, thumbWidth, left, right]);
119119
const styles = useMemo(() => ({
120120
position: 'absolute',
121121
left,

index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Slider = (
1919
floatingLabel,
2020
allowLabelOverflow,
2121
disableRange,
22+
disabled,
2223
onValueChanged,
2324
renderThumb,
2425
renderLabel,
@@ -58,7 +59,7 @@ const Slider = (
5859
const lowPosition = (low - min) / (max - min) * (containerWidth - thumbWidth);
5960
lowThumbX.setValue(lowPosition);
6061
updateSelectedRail();
61-
onValueChanged(low, high);
62+
onValueChanged(low, high, false);
6263
}, [disableRange, inPropsRef, max, min, onValueChanged, thumbWidth, updateSelectedRail]);
6364

6465
useEffect(() => {
@@ -112,6 +113,9 @@ const Slider = (
112113
onShouldBlockNativeResponder: trueFunc,
113114

114115
onPanResponderGrant: ({ nativeEvent }, gestureState) => {
116+
if (disabled) {
117+
return;
118+
}
115119
const { numberActiveTouches } = gestureState;
116120
if (numberActiveTouches > 1) {
117121
return;
@@ -144,7 +148,7 @@ const Slider = (
144148
gestureStateRef.current.lastValue = value;
145149
gestureStateRef.current.lastPosition = absolutePosition + thumbWidth / 2;
146150
(isLow ? lowThumbX : highThumbX).setValue(absolutePosition);
147-
onValueChanged(isLow ? value : low, isLow ? high : value);
151+
onValueChanged(isLow ? value : low, isLow ? high : value, true);
148152
(isLow ? setLow : setHigh)(value);
149153
labelUpdate && labelUpdate(gestureStateRef.current.lastPosition, value);
150154
notchUpdate && notchUpdate(gestureStateRef.current.lastPosition, value);
@@ -158,12 +162,12 @@ const Slider = (
158162
});
159163
},
160164

161-
onPanResponderMove: Animated.event([null, { moveX: pointerX }], { useNativeDriver: false }),
165+
onPanResponderMove: disabled ? undefined : Animated.event([null, { moveX: pointerX }], { useNativeDriver: false }),
162166

163167
onPanResponderRelease: () => {
164168
setPressed(false);
165169
},
166-
}), [pointerX, inPropsRef, thumbWidth, disableRange, onValueChanged, setLow, setHigh, labelUpdate, notchUpdate, updateSelectedRail]);
170+
}), [pointerX, inPropsRef, thumbWidth, disableRange, disabled, onValueChanged, setLow, setHigh, labelUpdate, notchUpdate, updateSelectedRail]);
167171

168172
return (
169173
<View {...restProps}>
@@ -202,6 +206,7 @@ Slider.propTypes = {
202206
high: PropTypes.number,
203207
allowLabelOverflow: PropTypes.bool,
204208
disableRange: PropTypes.bool,
209+
disabled: PropTypes.bool,
205210
floatingLabel: PropTypes.bool,
206211
renderLabel: PropTypes.func,
207212
renderNotch: PropTypes.func,
@@ -213,6 +218,7 @@ Slider.propTypes = {
213218
Slider.defaultProps = {
214219
allowLabelOverflow: false,
215220
disableRange: false,
221+
disabled: false,
216222
floatingLabel: false,
217223
onValueChanged: noop,
218224
};

0 commit comments

Comments
 (0)