forked from githuboftigran/rn-range-slider
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
239 lines (212 loc) · 8.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import React, { memo, useState, useEffect, useCallback, useMemo, useRef } from 'react';
import { Animated, PanResponder, View, ViewPropTypes } from 'react-native';
import PropTypes from 'prop-types';
import styles from './styles';
import {useThumbFollower, useLowHigh, useWidthLayout, useLabelContainerProps, useSelectedRail} from './hooks';
import {clamp, getValueForPosition, isLowCloser} from './helpers';
const trueFunc = () => true;
const Slider = (
{
min,
max,
minRange,
step,
low: lowProp,
high: highProp,
floatingLabel,
allowLabelOverflow,
disableRange,
disabled,
onValueChanged,
onTouchStart,
onTouchEnd,
renderThumb,
renderLabel,
renderNotch,
renderRail,
renderRailSelected,
value,
...restProps
}
) => {
const { inPropsRef, inPropsRefPrev, setLow, setHigh } = useLowHigh(value ?? lowProp, disableRange ? max : highProp, min, max, step);
const lowThumbXRef = useRef(new Animated.Value(0));
const highThumbXRef = useRef(new Animated.Value(0));
const pointerX = useRef(new Animated.Value(0)).current;
const { current: lowThumbX } = lowThumbXRef;
const { current: highThumbX } = highThumbXRef;
const gestureStateRef = useRef({ isLow: true, lastValue: 0, lastPosition: 0 });
const [isPressed, setPressed] = useState(false);
const containerWidthRef = useRef(0);
const [thumbWidth, setThumbWidth] = useState(0);
const [selectedRailStyle, updateSelectedRail] = useSelectedRail(inPropsRef, containerWidthRef, thumbWidth, disableRange);
const updateThumbs = useCallback(() => {
const { lowPrev, highPrev } = inPropsRefPrev;
const { current: containerWidth } = containerWidthRef;
if (!thumbWidth || !containerWidth) {
return;
}
const { low, high } = inPropsRef.current;
if (!disableRange) {
const { current: highThumbX } = highThumbXRef;
const highPosition = (high - min) / (max - min) * (containerWidth - thumbWidth);
highThumbX.setValue(highPosition);
}
const { current: lowThumbX } = lowThumbXRef;
const lowPosition = (low - min) / (max - min) * (containerWidth - thumbWidth);
lowThumbX.setValue(lowPosition);
updateSelectedRail();
if ((lowProp !== undefined && lowProp !== lowPrev) || (highProp !== undefined && highProp !== highPrev)) {
onValueChanged?.(low, high, false);
}
}, [disableRange, inPropsRef, max, min, onValueChanged, thumbWidth, updateSelectedRail,highProp, inPropsRefPrev.lowPrev, inPropsRefPrev.highPrev, lowProp]);
useEffect(() => {
const { lowPrev, highPrev } = inPropsRefPrev;
if ((lowProp !== undefined && lowProp !== lowPrev) || (highProp !== undefined && highProp !== highPrev)) {
updateThumbs();
}
}, [highProp, inPropsRefPrev.lowPrev, inPropsRefPrev.highPrev, lowProp]);
useEffect(() => {
updateThumbs();
}, [updateThumbs]);
const handleContainerLayout = useWidthLayout(containerWidthRef, updateThumbs);
const handleThumbLayout = useCallback(({ nativeEvent }) => {
const { layout: {width}} = nativeEvent;
if (thumbWidth !== width) {
setThumbWidth(width);
}
}, [thumbWidth]);
const lowStyles = useMemo(() => {
return {transform: [{translateX: lowThumbX}]};
}, [lowThumbX]);
const highStyles = useMemo(() => {
return disableRange ? null : [
styles.highThumbContainer,
{transform: [{translateX: highThumbX}]},
];
}, [disableRange, highThumbX]);
const railContainerStyles = useMemo(() => {
return [styles.railsContainer, { marginHorizontal: thumbWidth / 2 }];
}, [thumbWidth]);
const [labelView, labelUpdate] = useThumbFollower(containerWidthRef, gestureStateRef, renderLabel, isPressed, allowLabelOverflow);
const [notchView, notchUpdate] = useThumbFollower(containerWidthRef, gestureStateRef, renderNotch, isPressed, allowLabelOverflow);
const lowThumb = renderThumb();
const highThumb = renderThumb();
const labelContainerProps = useLabelContainerProps(floatingLabel);
const { panHandlers } = useMemo(() => PanResponder.create({
onStartShouldSetPanResponder: trueFunc,
onStartShouldSetPanResponderCapture: trueFunc,
onMoveShouldSetPanResponder: trueFunc,
onMoveShouldSetPanResponderCapture: trueFunc,
onPanResponderTerminationRequest: trueFunc,
onPanResponderTerminate: trueFunc,
onShouldBlockNativeResponder: trueFunc,
onPanResponderGrant: ({ nativeEvent }, gestureState) => {
if (disabled) {
return;
}
const { numberActiveTouches } = gestureState;
if (numberActiveTouches > 1) {
return;
}
setPressed(true);
const { current: lowThumbX } = lowThumbXRef;
const { current: highThumbX } = highThumbXRef;
const { locationX: downX, pageX } = nativeEvent;
const containerX = pageX - downX;
const { low, high, min, max } = inPropsRef.current;
onTouchStart?.(low, high);
const containerWidth = containerWidthRef.current;
const lowPosition = thumbWidth / 2 + (low - min) / (max - min) * (containerWidth - thumbWidth);
const highPosition = thumbWidth / 2 + (high - min) / (max - min) * (containerWidth - thumbWidth);
const isLow = disableRange || isLowCloser(downX, lowPosition, highPosition);
gestureStateRef.current.isLow = isLow;
const handlePositionChange = positionInView => {
const { low, high, min, max, step } = inPropsRef.current;
const minValue = isLow ? min : low + minRange;
const maxValue = isLow ? high - minRange : max;
const value = clamp(getValueForPosition(positionInView, containerWidth, thumbWidth, min, max, step), minValue, maxValue);
if (gestureStateRef.current.lastValue === value) {
return;
}
const availableSpace = containerWidth - thumbWidth;
const absolutePosition = (value - min) / (max - min) * availableSpace;
gestureStateRef.current.lastValue = value;
gestureStateRef.current.lastPosition = absolutePosition + thumbWidth / 2;
(isLow ? lowThumbX : highThumbX).setValue(absolutePosition);
onValueChanged?.(isLow ? value : low, isLow ? high : value, true);
(isLow ? setLow : setHigh)(value);
labelUpdate && labelUpdate(gestureStateRef.current.lastPosition, value);
notchUpdate && notchUpdate(gestureStateRef.current.lastPosition, value);
updateSelectedRail();
};
handlePositionChange(downX);
pointerX.removeAllListeners();
pointerX.addListener(({ value: pointerPosition }) => {
const positionInView = pointerPosition - containerX;
handlePositionChange(positionInView);
});
},
onPanResponderMove: disabled ? undefined : Animated.event([null, { moveX: pointerX }], { useNativeDriver: false }),
onPanResponderRelease: () => {
setPressed(false);
const { low, high } = inPropsRef.current;
onTouchEnd?.(low, high);
},
}), [pointerX, inPropsRef, thumbWidth, disableRange, disabled, onValueChanged, setLow, setHigh, labelUpdate, notchUpdate, updateSelectedRail]);
return (
<View {...restProps}>
<View {...labelContainerProps}>
{labelView}
{notchView}
</View>
<View onLayout={handleContainerLayout} style={styles.controlsContainer}>
<View style={railContainerStyles}>
{renderRail()}
<Animated.View style={selectedRailStyle}>
{renderRailSelected()}
</Animated.View>
</View>
<Animated.View style={lowStyles} onLayout={handleThumbLayout}>
{lowThumb}
</Animated.View>
{
!disableRange && <Animated.View style={highStyles}>
{highThumb}
</Animated.View>
}
<View { ...panHandlers } style={styles.touchableArea} collapsable={false}/>
</View>
</View>
);
};
Slider.propTypes = {
...ViewPropTypes,
min: PropTypes.number.isRequired,
max: PropTypes.number.isRequired,
minRange: PropTypes.number,
step: PropTypes.number.isRequired,
renderThumb: PropTypes.func.isRequired,
low: PropTypes.number,
high: PropTypes.number,
allowLabelOverflow: PropTypes.bool,
disableRange: PropTypes.bool,
disabled: PropTypes.bool,
floatingLabel: PropTypes.bool,
renderLabel: PropTypes.func,
renderNotch: PropTypes.func,
renderRail: PropTypes.func.isRequired,
renderRailSelected: PropTypes.func.isRequired,
onValueChanged: PropTypes.func,
onTouchStart: PropTypes.func,
onTouchEnd: PropTypes.func,
value:PropTypes.number
};
Slider.defaultProps = {
minRange: 0,
allowLabelOverflow: false,
disableRange: false,
disabled: false,
floatingLabel: false,
};
export default memo(Slider);