Skip to content

Commit 0a6ee2f

Browse files
* Version changed to 2.0.1
* Rest props are passed to root component now
1 parent 3be6676 commit 0a6ee2f

4 files changed

Lines changed: 69 additions & 65 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ You can find the version 1 [here](https://github.com/githuboftigran/rn-range-sli
2525

2626
RangeSlider uses react hooks, so this component doesn't work with React Native versions below 0.59.0
2727

28+
You can find basic implementation of needed components (Thumb, Rail, RailSelected, Label, Notch) [here](https://github.com/githuboftigran/rn-widgets-demo).
29+
30+
2831
```
2932
...
3033
@@ -63,7 +66,6 @@ const handleValueChange = useCallback((low, high) => {
6366

6467
### Properties
6568

66-
6769
| Name | Description | Type | Default Value |
6870
| --- | --- | --- | :-------------: |
6971
| `min` | Minimum value of slider | number | _**required**_ |
@@ -81,6 +83,8 @@ const handleValueChange = useCallback((low, high) => {
8183
| `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` |
8284
| `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` |
8385

86+
All the other props (e.g. style) will be passed to root container component.
87+
8488
## A special section about permanent labels.
8589

8690
The label of active thumb is a hint for a user. It's not showing selected values permanently.

hooks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useCallback, useState, useRef, useMemo } from 'react';
2-
import {Animated} from 'react-native';
3-
import {clamp} from './helpers';
2+
import { Animated } from 'react-native';
3+
import { clamp } from './helpers';
44
import styles from './styles';
55
import FollowerContainer from './LabelContainer';
66

index.js

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const noop = () => {};
1111

1212
const Slider = (
1313
{
14-
style,
1514
min,
1615
max,
1716
step,
@@ -26,6 +25,7 @@ const Slider = (
2625
renderNotch,
2726
renderRail,
2827
renderRailSelected,
28+
...restProps
2929
}
3030
) => {
3131
const { inPropsRef, setLow, setHigh } = useLowHigh(lowProp, disableRange ? max : highProp, min, max, step);
@@ -103,70 +103,70 @@ const Slider = (
103103
const labelContainerProps = useLabelContainerProps(floatingLabel);
104104

105105
const { panHandlers } = useMemo(() => PanResponder.create({
106-
onStartShouldSetPanResponder: trueFunc,
107-
onStartShouldSetPanResponderCapture: trueFunc,
108-
onMoveShouldSetPanResponder: trueFunc,
109-
onMoveShouldSetPanResponderCapture: trueFunc,
110-
onPanResponderTerminationRequest: trueFunc,
111-
onPanResponderTerminate: trueFunc,
112-
onShouldBlockNativeResponder: trueFunc,
113-
114-
onPanResponderGrant: ({ nativeEvent }, gestureState) => {
115-
const { numberActiveTouches } = gestureState;
116-
if (numberActiveTouches > 1) {
106+
onStartShouldSetPanResponder: trueFunc,
107+
onStartShouldSetPanResponderCapture: trueFunc,
108+
onMoveShouldSetPanResponder: trueFunc,
109+
onMoveShouldSetPanResponderCapture: trueFunc,
110+
onPanResponderTerminationRequest: trueFunc,
111+
onPanResponderTerminate: trueFunc,
112+
onShouldBlockNativeResponder: trueFunc,
113+
114+
onPanResponderGrant: ({ nativeEvent }, gestureState) => {
115+
const { numberActiveTouches } = gestureState;
116+
if (numberActiveTouches > 1) {
117+
return;
118+
}
119+
setPressed(true);
120+
const { current: lowThumbX } = lowThumbXRef;
121+
const { current: highThumbX } = highThumbXRef;
122+
const { locationX: downX, pageX } = nativeEvent;
123+
const containerX = pageX - downX;
124+
125+
const { low, high, min, max } = inPropsRef.current;
126+
const containerWidth = containerWidthRef.current;
127+
128+
const lowPosition = thumbWidth / 2 + (low - min) / (max - min) * (containerWidth - thumbWidth);
129+
const highPosition = thumbWidth / 2 + (high - min) / (max - min) * (containerWidth - thumbWidth);
130+
131+
const isLow = disableRange || isLowCloser(downX, lowPosition, highPosition);
132+
gestureStateRef.current.isLow = isLow;
133+
134+
const handlePositionChange = positionInView => {
135+
const { low, high, min, max, step } = inPropsRef.current;
136+
const minValue = isLow ? min : low;
137+
const maxValue = isLow ? high : max;
138+
const value = clamp(getValueForPosition(positionInView, containerWidth, thumbWidth, min, max, step), minValue, maxValue);
139+
if (gestureStateRef.current.lastValue === value) {
117140
return;
118141
}
119-
setPressed(true);
120-
const { current: lowThumbX } = lowThumbXRef;
121-
const { current: highThumbX } = highThumbXRef;
122-
const { locationX: downX, pageX } = nativeEvent;
123-
const containerX = pageX - downX;
124-
125-
const { low, high, min, max } = inPropsRef.current;
126-
const containerWidth = containerWidthRef.current;
127-
128-
const lowPosition = thumbWidth / 2 + (low - min) / (max - min) * (containerWidth - thumbWidth);
129-
const highPosition = thumbWidth / 2 + (high - min) / (max - min) * (containerWidth - thumbWidth);
130-
131-
const isLow = disableRange || isLowCloser(downX, lowPosition, highPosition);
132-
gestureStateRef.current.isLow = isLow;
133-
134-
const handlePositionChange = positionInView => {
135-
const { low, high, min, max, step } = inPropsRef.current;
136-
const minValue = isLow ? min : low;
137-
const maxValue = isLow ? high : max;
138-
const value = clamp(getValueForPosition(positionInView, containerWidth, thumbWidth, min, max, step), minValue, maxValue);
139-
if (gestureStateRef.current.lastValue === value) {
140-
return;
141-
}
142-
const availableSpace = containerWidth - thumbWidth;
143-
const absolutePosition = (value - min) / (max - min) * availableSpace;
144-
gestureStateRef.current.lastValue = value;
145-
gestureStateRef.current.lastPosition = absolutePosition + thumbWidth / 2;
146-
(isLow ? lowThumbX : highThumbX).setValue(absolutePosition);
147-
onValueChanged(isLow ? value : low, isLow ? high : value);
148-
(isLow ? setLow : setHigh)(value);
149-
labelUpdate && labelUpdate(gestureStateRef.current.lastPosition, value);
150-
notchUpdate && notchUpdate(gestureStateRef.current.lastPosition, value);
151-
updateSelectedRail();
152-
};
153-
handlePositionChange(downX);
154-
pointerX.removeAllListeners();
155-
pointerX.addListener(({ value: pointerPosition }) => {
156-
const positionInView = pointerPosition - containerX;
157-
handlePositionChange(positionInView);
158-
});
159-
},
160-
161-
onPanResponderMove: Animated.event([null, { moveX: pointerX }], { useNativeDriver: false }),
162-
163-
onPanResponderRelease: () => {
164-
setPressed(false);
165-
},
166-
}), [pointerX, inPropsRef, thumbWidth, disableRange, onValueChanged, setLow, setHigh, labelUpdate, notchUpdate, updateSelectedRail]);
142+
const availableSpace = containerWidth - thumbWidth;
143+
const absolutePosition = (value - min) / (max - min) * availableSpace;
144+
gestureStateRef.current.lastValue = value;
145+
gestureStateRef.current.lastPosition = absolutePosition + thumbWidth / 2;
146+
(isLow ? lowThumbX : highThumbX).setValue(absolutePosition);
147+
onValueChanged(isLow ? value : low, isLow ? high : value);
148+
(isLow ? setLow : setHigh)(value);
149+
labelUpdate && labelUpdate(gestureStateRef.current.lastPosition, value);
150+
notchUpdate && notchUpdate(gestureStateRef.current.lastPosition, value);
151+
updateSelectedRail();
152+
};
153+
handlePositionChange(downX);
154+
pointerX.removeAllListeners();
155+
pointerX.addListener(({ value: pointerPosition }) => {
156+
const positionInView = pointerPosition - containerX;
157+
handlePositionChange(positionInView);
158+
});
159+
},
160+
161+
onPanResponderMove: Animated.event([null, { moveX: pointerX }], { useNativeDriver: false }),
162+
163+
onPanResponderRelease: () => {
164+
setPressed(false);
165+
},
166+
}), [pointerX, inPropsRef, thumbWidth, disableRange, onValueChanged, setLow, setHigh, labelUpdate, notchUpdate, updateSelectedRail]);
167167

168168
return (
169-
<View style={style}>
169+
<View {...restProps}>
170170
<View {...labelContainerProps}>
171171
{labelView}
172172
{notchView}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rn-range-slider",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"author": "Tigran Sahakyan <mail.of.tigran@gmail.com>",
55
"description": "A highly optimized pure JS implementation of Range Slider for React Native",
66
"homepage": "https://github.com/githuboftigran/rn-range-slider",

0 commit comments

Comments
 (0)