forked from MetaLabs-inc/rn-range-slider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.js
142 lines (127 loc) · 5.49 KB
/
hooks.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
import React, { useCallback, useState, useRef, useMemo } from 'react';
import { Animated, I18nManager } from 'react-native';
import { clamp } from './helpers';
import styles from './styles';
import FollowerContainer from './LabelContainer';
/**
* low and high state variables are fallbacks for props (props are not required).
* This hook ensures that current low and high are not out of [min, max] range.
* It returns an object which contains:
* - ref containing correct low, high, min, max and step to work with.
* - setLow and setHigh setters
* @param lowProp
* @param highProp
* @param min
* @param max
* @param step
* @returns {{inPropsRef: React.MutableRefObject<{high: (*|number), low: (*|number)}>, setLow: (function(number): undefined), setHigh: (function(number): undefined)}}
*/
export const useLowHigh = (lowProp, highProp, min, max, step) => {
const validLowProp = lowProp === undefined ? min : clamp(lowProp, min, max);
const validHighProp = highProp === undefined ? max : clamp(highProp, min, max);
const inPropsRef = useRef({ low: validLowProp, high: validHighProp });
const { low: lowState, high: highState } = inPropsRef.current;
const inPropsRefPrev = { lowPrev: lowState, highPrev: highState };
// Props have higher priority.
// If no props are passed, use internal state variables.
const low = clamp(lowProp === undefined ? lowState : lowProp, min, max);
const high = clamp(highProp === undefined ? highState : highProp, min, max);
// Always update values of refs so pan responder will have updated values
Object.assign(inPropsRef.current, { low, high, min, max, step });
const setLow = value => inPropsRef.current.low = value;
const setHigh = value => inPropsRef.current.high = value;
return { inPropsRef, inPropsRefPrev, setLow, setHigh };
};
/**
* Sets the current value of widthRef and calls the callback with new width parameter.
* @param widthRef
* @param callback
* @returns {function({nativeEvent: *}): void}
*/
export const useWidthLayout = (widthRef, callback) => {
return useCallback(({ nativeEvent }) => {
const { layout: {width}} = nativeEvent;
const { current: w } = widthRef;
if (w !== width) {
widthRef.current = width;
if (callback) {
callback(width);
}
}
}, [callback, widthRef]);
};
/**
* This hook creates a component which follows the thumb.
* Content renderer is passed to FollowerContainer which re-renders only it's content with setValue method.
* This allows to re-render only follower, instead of the whole slider with all children (thumb, rail, etc.).
* Returned update function should be called every time follower should be updated.
* @param containerWidthRef
* @param gestureStateRef
* @param renderContent
* @param isPressed
* @param allowOverflow
* @returns {[JSX.Element, function(*, *=): void]|*[]}
*/
export const useThumbFollower = (containerWidthRef, gestureStateRef, renderContent, isPressed, allowOverflow) => {
const xRef = useRef(new Animated.Value(0));
const widthRef = useRef(0);
const contentContainerRef = useRef(null);
const { current: x } = xRef;
const update = useCallback((thumbPositionInView, value) => {
const { current: width } = widthRef;
const { current: containerWidth } = containerWidthRef;
const position = thumbPositionInView - width / 2;
xRef.current.setValue(allowOverflow ? position : clamp(position, 0, containerWidth - width));
contentContainerRef.current.setValue(value);
}, [widthRef, containerWidthRef, allowOverflow]);
const handleLayout = useWidthLayout(widthRef, () => {
update(gestureStateRef.current.lastPosition, gestureStateRef.current.lastValue);
});
if (!renderContent) {
return [];
}
const transform = { transform: [{ translateX: x }]};
const follower = (
<Animated.View style={[transform, { opacity: isPressed ? 1 : 0 }]}>
<FollowerContainer
onLayout={handleLayout}
ref={contentContainerRef}
renderContent={renderContent}
/>
</Animated.View>
);
return [follower, update];
};
export const useSelectedRail = (inPropsRef, containerWidthRef, thumbWidth, disableRange) => {
const { current: left } = useRef(new Animated.Value(0));
const { current: right } = useRef(new Animated.Value(0));
const update = useCallback(() => {
const { low, high, min, max } = inPropsRef.current;
const { current: containerWidth } = containerWidthRef;
const fullScale = (max - min) / (containerWidth - thumbWidth);
const leftValue = (low - min) / fullScale;
const rightValue = (max - high) / fullScale;
left.setValue(disableRange ? 0 : leftValue);
right.setValue(disableRange ? (containerWidth - thumbWidth) - leftValue : rightValue);
}, [inPropsRef, containerWidthRef, disableRange, thumbWidth, left, right]);
const styles = useMemo(() => ({
position: 'absolute',
left: I18nManager.isRTL ? right : left,
right: I18nManager.isRTL ? left : right,
}), [left, right]);
return [styles, update];
};
/**
* @param floating
* @returns {{onLayout: ((function({nativeEvent: *}): void)|undefined), style: [*, {top}]}}
*/
export const useLabelContainerProps = floating => {
const [labelContainerHeight, setLabelContainerHeight] = useState(0);
const onLayout = useCallback(({ nativeEvent }) => {
const { layout: {height}} = nativeEvent;
setLabelContainerHeight(height);
}, []);
const top = floating ? -labelContainerHeight : 0;
const style = [floating ? styles.labelFloatingContainer : styles.labelFixedContainer, { top }];
return { style, onLayout: onLayout };
};