-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filters.js
146 lines (132 loc) · 3.8 KB
/
Filters.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
// @flow
import React, { Component, type ComponentType } from "react";
import {
Animated,
Dimensions,
StyleSheet,
View,
ScrollView
} from "react-native";
import IconSelectArrowDownBlue from "./components/IconSelectArrowDownBlue";
import IconSelectArrowDownWhite from "./components/IconSelectArrowDownWhite";
import ListItemButton from "./components/ListItemButton";
import StickyItemButton from "./components/StickyItemButton";
type Props = {|
filters: Filter[]
|};
const FILTERS_ICON_WIDTH = 44;
const FILTERS_BUTTON_WIDTH = 100;
const SCREEN_WIDTH = Dimensions.get("screen").width;
export default class Filters extends Component<Props> {
animatedWidth = new Animated.Value(FILTERS_BUTTON_WIDTH);
scrollViewRef = React.createRef();
onFiltersScroll = (event: *) => {
const eventX = event.nativeEvent.contentOffset.x;
const direction = eventX > 0 ? 1 : -1;
const offsetX = Math.min(
Math.abs(eventX),
FILTERS_BUTTON_WIDTH - FILTERS_ICON_WIDTH
);
this.animatedWidth.setValue(FILTERS_BUTTON_WIDTH - offsetX * direction);
};
onScrollEndSnapToEdge = (event: *) => {
const offsetX = event.nativeEvent.contentOffset.x;
const maxOffset = FILTERS_BUTTON_WIDTH - FILTERS_ICON_WIDTH;
const velocityFactor = Math.abs(event.nativeEvent.velocity.x * 30);
if (offsetX > 0 && offsetX < maxOffset / 2 - velocityFactor) {
this.scrollViewRef.scrollTo({ x: 0 });
} else if (
maxOffset / 2 + velocityFactor <= offsetX &&
offsetX < maxOffset
) {
this.scrollViewRef.scrollTo({
x: FILTERS_BUTTON_WIDTH
});
}
};
render() {
const { filters, activeFiltersCount, activeFiltersMap } = this.props;
const scrollViewPaddingLeft = FILTERS_BUTTON_WIDTH - 18;
return (
<View style={styles.container}>
<View style={styles.stickyItem}>
<Animated.View
style={[
styles.stickyItemMask,
{
width: this.animatedWidth,
maxWidth: FILTERS_BUTTON_WIDTH
}
]}
>
<StickyItemButton activeFiltersCount={activeFiltersCount} />
</Animated.View>
</View>
<ScrollView
horizontal
style={styles.scrollView}
contentContainerStyle={[
styles.scrollViewContent,
{ paddingLeft: scrollViewPaddingLeft }
]}
showsHorizontalScrollIndicator={false}
onScroll={this.onFiltersScroll}
onScrollEndDrag={this.onScrollEndSnapToEdge}
scrollEventThrottle={16}
ref={this.scrollViewRef}
>
{filters.map(filter => (
<ListItemButton
key={filter.name}
active={activeFiltersMap[filter.name]}
text={filter.label}
icon={
filter.type === "MULTI_CHOICE" && (
<DropDownIcon active={!!activeFiltersMap[filter.name]} />
)
}
/>
))}
</ScrollView>
</View>
);
}
}
const DropDownIcon = ({ active }: { active: boolean }) =>
active ? (
<IconSelectArrowDownBlue style={styles.dropDownIcon} />
) : (
<IconSelectArrowDownWhite style={styles.dropDownIcon} />
);
const styles = StyleSheet.create({
container: {
width: SCREEN_WIDTH,
flexDirection: "row",
paddingLeft: 10,
backgroundColor: "#2252C7"
},
stickyItem: {
position: "absolute",
zIndex: 1,
left: 10,
paddingRight: 8,
backgroundColor: "#2252C7"
},
stickyItemMask: {
minWidth: FILTERS_ICON_WIDTH,
marginLeft: -8,
borderRadius: 8,
overflow: "hidden"
},
scrollView: {
marginLeft: 10
},
scrollViewContent: {
paddingLeft: 100,
paddingRight: 10,
paddingBottom: 13
},
dropDownIcon: {
marginRight: 6
}
});