-
Notifications
You must be signed in to change notification settings - Fork 4
/
Slider.tsx
143 lines (126 loc) · 3.54 KB
/
Slider.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
import React, { ReactElement } from "react";
import * as RX from "reactxp";
import { LayoutInfo } from "reactxp/dist/common/Types";
import { usePrevious } from "../../lib/utils/hooks";
import Pagination from "../Pagination";
type SliderProps = {
children: ReactElement[];
loop?: boolean;
index?: number;
onIndexChanged?: (index: number) => void;
style?: RX.Types.StyleRuleSetOrArray<RX.Types.ViewStyle>;
};
const Slider = (props: SliderProps) => {
const { children } = props;
const len = children.length;
const [selected, setSelected] = React.useState(props.index || 0);
const prevSelected = usePrevious(selected);
const [width, setWidth] = React.useState(
RX.UserInterface.measureWindow().width
);
const scrollView = React.useRef<RX.ScrollView>(null);
const scrollTimeout = React.useRef<NodeJS.Timeout>();
const [offset, setOffset] = React.useState(0);
const scrollBy = (index: number, animated = true) => {
if (scrollView.current) {
const x = index * width;
scrollView.current.setScrollLeft(x, animated);
setOffset(x);
}
};
const updateIndex = (currentOffset: number) => {
const step = width;
const diff = currentOffset - offset;
if (!diff) {
return;
}
// @ts-ignore Integer value from maybe float
let index = parseInt(selected + Math.round(diff / step));
if (props.loop) {
if (index <= -1) {
index = len - 1;
} else if (index >= len) {
index = 0;
}
}
setOffset(currentOffset);
setSelected(index);
scrollBy(index);
};
React.useEffect(() => {
if (width) {
scrollBy(selected, typeof prevSelected !== "undefined");
}
if (props.onIndexChanged) {
props.onIndexChanged(selected);
}
}, [selected, width]);
const handleLayout = (layout: LayoutInfo) => {
setWidth(layout.width);
};
const handleScroll = (_: number, scrollLeft: number) => {
if (scrollTimeout.current) {
clearTimeout(scrollTimeout.current);
}
scrollTimeout.current = setTimeout(() => {
updateIndex(scrollLeft);
}, 100);
};
const handlePanHorizontal = (gestureState: RX.Types.PanGestureState) => {
if (gestureState.isComplete) {
const diff = gestureState.initialClientX - gestureState.clientX;
if (diff < 0) {
setSelected(selected - 1 >= 0 ? selected - 1 : len - 1);
}
if (diff > 0) {
setSelected(selected + 1 < len ? selected + 1 : 0);
}
}
};
const handlePaginationItemPress = (index: number) => {
setSelected(index);
};
return (
<RX.GestureView onPanHorizontal={handlePanHorizontal} style={props.style}>
<RX.ScrollView
ref={scrollView}
horizontal
onScroll={handleScroll}
style={{
flex: 1,
flexDirection: "row",
...RX.Platform.select({
web: {
display: "flex",
// Hide scrollbar on web
marginBottom: -30,
paddingBottom: 30
}
})
}}
onLayout={handleLayout}
showsHorizontalScrollIndicator={false}
>
{children}
</RX.ScrollView>
<Pagination
number={len}
value={selected}
onItemPress={handlePaginationItemPress}
style={{
position: "absolute",
bottom: 20,
left: 0,
right: 0,
justifyContent: "center",
...RX.Platform.select({
web: {
zIndex: 2
}
})
}}
/>
</RX.GestureView>
);
};
export default Slider;