-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.tsx
311 lines (287 loc) · 9.48 KB
/
index.tsx
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import React, { useState, useEffect, useRef } from "react";
import { withAzir, getColorByName } from "azir-theme";
import { Animated, StyleSheet, PanResponder, View, Easing, I18nManager } from "react-native";
function Rect(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
Rect.prototype.containsPoint = function(x, y) {
return x >= this.x && y >= this.y && x <= this.x + this.width && y <= this.y + this.height;
};
const DEFAULT_ANIMATION_CONFIGS = {
spring: {
friction: 7,
tension: 100
},
timing: {
duration: 150,
easing: Easing.inOut(Easing.ease),
delay: 0
}
};
const AzirSlider: React.FC<Props> = props => {
const {
value: _value,
minimumValue,
maximumValue,
thumbTouchSize: _thumbTouchSize,
disabled,
step,
theme,
children,
styles,
style,
trackStyle,
thumbStyle,
animationType,
progressTrackColor,
trackColor,
thumbColor,
trackSize: _trackSize,
thumbSize: _thumbSize,
icon,
onStart,
onChange,
onComplete,
...rest
} = props;
const __thumbSize = _thumbSize ? _thumbSize : theme.SIZES.THUMB_SIZE;
const thumbTouchSize = _thumbTouchSize ? _thumbTouchSize : { width: 20 + __thumbSize, height: 20 + __thumbSize };
const [value, setValue] = useState(new Animated.Value(_value));
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 });
const [thumbSize, setThumbSize] = useState({ width: 0, height: 0 });
const [panResponder, setPanResponder] = useState(null);
const valueRef = useRef(value);
valueRef.current = value;
useEffect(() => {
if (containerSize.width != 0 && thumbSize.width != 0) {
setPanResponder(
PanResponder.create({
onStartShouldSetPanResponder: _handleStartShouldSetPanResponder,
onMoveShouldSetPanResponder: _handleMoveShouldSetPanResponder,
onPanResponderGrant: _handlePanResponderGrant,
onPanResponderMove: _handlePanResponderMove,
onPanResponderRelease: _handlePanResponderEnd,
onPanResponderTerminationRequest: _handlePanResponderRequestEnd,
onPanResponderTerminate: _handlePanResponderEnd
})
);
}
}, [containerSize, thumbSize]);
const _getValue = (gestureState: any) => {
const length = containerSize.width - thumbSize.width;
const thumbLeft = this._previousLeft + gestureState.dx;
const nonRtlRatio = thumbLeft / length;
const ratio = I18nManager.isRTL ? 1 - nonRtlRatio : nonRtlRatio;
if (step) {
return Math.max(minimumValue, Math.min(maximumValue, minimumValue + Math.round((ratio * (maximumValue - minimumValue)) / step) * step));
}
return Math.max(minimumValue, Math.min(maximumValue, ratio * (maximumValue - minimumValue) + minimumValue));
};
const _setCurrentValueAnimated = (value: number) => {
const animationConfig = Object.assign({}, DEFAULT_ANIMATION_CONFIGS[animationType], animationConfig, {
toValue: value
});
Animated[animationType](value, animationConfig).start();
};
const _setCurrentValue = _value => {
setValue(new Animated.Value(_value));
};
const _getCurrentValue = () => valueRef.current.__getValue();
const _getTouchOverflowSize = () => {
var size = {};
size.width = Math.max(0, thumbTouchSize.width - thumbSize.width);
size.height = Math.max(0, thumbTouchSize.height - containerSize.height);
return size;
};
const _getRatio = (__value: number) => (__value - minimumValue) / (maximumValue - minimumValue);
const _getThumbLeft = (__value: number) => {
const nonRtlRatio = _getRatio(__value);
const ratio = I18nManager.isRTL ? 1 - nonRtlRatio : nonRtlRatio;
return ratio * (containerSize.width - thumbSize.width);
};
const _getThumbTouchRect = () => {
const touchOverflowSize = _getTouchOverflowSize();
return new Rect(
-20 + touchOverflowSize.width / 2 + _getThumbLeft(_getCurrentValue()) + (thumbSize.width - thumbTouchSize.width) / 2,
touchOverflowSize.height / 2 + (containerSize.height - thumbTouchSize.height) / 2,
thumbTouchSize.width,
thumbTouchSize.height
);
};
const _thumbHitTest = (e: Object) => {
const nativeEvent = e.nativeEvent;
const thumbTouchRect = _getThumbTouchRect();
return thumbTouchRect.containsPoint(nativeEvent.locationX, nativeEvent.locationY);
};
const _handleStartShouldSetPanResponder = (e: Object /* gestureState: Object */): boolean => {
return _thumbHitTest(e);
};
const _handlePanResponderRequestEnd = (e: Object, gestureState: Object) => {
// Should we allow another component to take over this pan?
return false;
};
const _handleMoveShouldSetPanResponder = (/* e: Object, gestureState: Object */): boolean => {
// Should we become active when the user moves a touch over the thumb?
return false;
};
///end
const renderIcon = () => {
return icon;
};
const _handlePanResponderGrant = (/* e: Object, gestureState: Object */) => {
this._previousLeft = _getThumbLeft(_getCurrentValue());
onStart && onStart(_getCurrentValue());
//this._fireChangeEvent("onSlidingStart");
};
const _handlePanResponderMove = (e: Object, gestureState: Object) => {
if (disabled) {
return;
}
_setCurrentValue(_getValue(gestureState));
onChange && onChange(_getCurrentValue());
//this._fireChangeEvent("onValueChange");
};
const _handlePanResponderEnd = (e: Object, gestureState: Object) => {
if (disabled) {
return;
}
onComplete && onComplete(_getCurrentValue());
};
const _measureContainer = (event: Object) => {
const { width, height } = event.nativeEvent.layout;
setContainerSize({ width: width, height: height });
};
const _measureThumb = (event: Object) => {
const { width, height } = event.nativeEvent.layout;
setThumbSize({ width: width, height: height });
};
const _renderDebugThumbTouchRect = thumbLeft => {
const thumbTouchRect = _getThumbTouchRect();
const positionStyle = {
left: thumbTouchRect.x,
top: thumbTouchRect.y,
width: thumbTouchRect.width,
height: thumbTouchRect.height
};
return <Animated.View style={[styles.debugThumbTouchArea, positionStyle]} pointerEvents="none" />;
};
const thumbLeft = value.interpolate({
inputRange: [minimumValue, maximumValue],
outputRange: I18nManager.isRTL ? [0, -(containerSize.width - thumbSize.width)] : [0, containerSize.width - thumbSize.width]
// extrapolate: 'clamp',
});
const minimumTrackWidth = value.interpolate({
inputRange: [minimumValue, maximumValue],
outputRange: [0, containerSize.width - thumbSize.width]
// extrapolate: 'clamp',
});
const minimumTrackStyle = {
position: "absolute",
width: Animated.add(minimumTrackWidth, thumbSize.width / 2),
backgroundColor: getColorByName(progressTrackColor, theme.COLORS)
};
//end
return (
<View {...rest} style={[{ height: __thumbSize }, styles.container, style]} onLayout={_measureContainer}>
<View
style={[
{ backgroundColor: getColorByName(trackColor, theme.COLORS), height: _trackSize ? _trackSize : theme.SIZES.TRACK_SIZE, borderRadius: theme.SIZES.TRACK_SIZE / 2 },
trackStyle
]}
renderToHardwareTextureAndroid
/>
<Animated.View
renderToHardwareTextureAndroid
style={[{ height: _trackSize ? _trackSize : theme.SIZES.TRACK_SIZE, borderRadius: theme.SIZES.TRACK_SIZE / 2 }, trackStyle, minimumTrackStyle]}
/>
<Animated.View
onLayout={_measureThumb}
renderToHardwareTextureAndroid
style={[
{
justifyContent: "center",
alignItems: "center",
width: __thumbSize ? __thumbSize : theme.SIZES.THUMB_SIZE,
height: __thumbSize ? __thumbSize : theme.SIZES.THUMB_SIZE,
borderRadius: __thumbSize ? __thumbSize : theme.SIZES.THUMB_SIZE / 2
},
{ backgroundColor: getColorByName(thumbColor, theme.COLORS) },
styles.thumb,
thumbStyle,
{
transform: [{ translateX: thumbLeft }, { translateY: 0 }]
}
]}
>
{icon && renderIcon()}
</Animated.View>
{panResponder && <View renderToHardwareTextureAndroid style={[styles.touchArea]} {...panResponder.panHandlers}></View>}
</View>
);
};
//Props Type ? for optional props
type Props = {
styles: any;
style: any;
trackStyle: any;
thumbStyle: any;
value: number;
progressTrackColor: string;
trackColor: string;
thumbColor: string;
minimumValue: number;
maximumValue: number;
thumbTouchSize: any;
icon: boolean;
animationType: string;
disabled: boolean;
step: number;
trackSize: number;
thumbSize: number;
onStart: any;
onChange: any;
onComplete: any;
};
//default props 4
AzirSlider.defaultProps = {
progressTrackColor: "theme",
trackColor: "#b3b3b3",
thumbColor: "theme",
value: 0,
trackSize: 3,
thumbSize: null,
minimumValue: 0,
maximumValue: 1,
thumbTouchSize: null,
icon: null,
animationType: "timing",
disabled: false,
step: 0
};
const styles = theme =>
StyleSheet.create({
container: {
//height: 40,
justifyContent: "center"
},
thumb: {
position: "absolute"
},
touchArea: {
position: "absolute",
backgroundColor: "transparent",
top: 0,
left: 0,
right: 0,
bottom: 0
},
debugThumbTouchArea: {
position: "absolute",
backgroundColor: "green",
opacity: 0.5
}
});
export default withAzir(AzirSlider, styles);