Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
206 lines (199 loc) · 5.52 KB
/
Copy pathApp.js
File metadata and controls
206 lines (199 loc) · 5.52 KB
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
import * as React from 'react';
import {
Text,
View,
StyleSheet,
SectionList,
SafeAreaView,
} from 'react-native';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import countries from './countryCode.json';
const sectionMapArr = [
['A', 0],
['B', 1],
['C', 2],
['D', 3],
['E', 4],
['F', 5],
['G', 6],
['H', 7],
['I', 8],
['J', 9],
['K', 10],
['L', 11],
['M', 12],
['N', 13],
['O', 14],
['P', 15],
['Q', 16],
['R', 17],
['S', 18],
['T', 19],
['U', 20],
['V', 21],
['W', 22],
['X', 23],
['Y', 24],
['Z', 25],
];
export default class App extends React.Component {
state = {
select: 'no',
};
constructor(props) {
super(props);
this.ps = [];
this.offsetY = 0;
this.sectionlist = React.createRef();
}
render() {
const { select, key, show, showX, showY = -1000 } = this.state;
return (
<SafeAreaView style={styles.container}>
<SectionList
ref={this.sectionlist}
onLayout={({ nativeEvent: { layout: { y }, }, }) => {
this.offsetY = y;
}}
onScrollToIndexFailed={() => {
}}
containerStyle={{ flex: 1, justifyContent: 'center' }}
ItemSeparatorComponent={() => (<View style={{ borderBottomColor: '#F8F8F8', borderBottomWidth: 1, }} />)}
renderItem={({ item, index, section }) => (
<View style={styles.itemContainer}>
<Text style={styles.itemText} key={index}>
{item.countryName}
</Text>
</View>
)}
renderSectionHeader={({ section: { key } }) => (
<View style={styles.headerContainer}>
<Text style={styles.headerText}>{key}</Text>
</View>
)}
sections={countries}
keyExtractor={(item, index) => item + index}
/>
<View
style={{ width: 16, justifyContent: 'center', alignItems: 'center' }}
onStartShouldSetResponder={() => true}
onMoveShouldSetResponder={() => true}
onResponderGrant={evt => {
this.setState({
show: true,
});
}}
onResponderRelease={e => {
this.setState({
show: false,
});
}}
onResponderReject={() => {
console.log('onResponderReject');
}}
onResponderMove={({ nativeEvent: { pageX, pageY } }) => {
const offsetY = pageY - this.offsetY;
const find = this.ps.find(
i => i.min < offsetY && i.max > offsetY
);
if (find) {
const { key } = this.state;
if (find.key !== key) {
this.setState({
key: find.key,
showX: pageX,
showY: find.min,
});
this.sectionlist.current.scrollToLocation({
sectionIndex: find.index,
itemIndex: 0,
animated: false,
});
ReactNativeHapticFeedback.trigger('impactLight', {
enableVibrateFallback: true,
ignoreAndroidSystemSettings: false,
});
}
}
}}
>
{sectionMapArr.map((item, index) => {
return (
<Text
key={index}
onLayout={({
nativeEvent: {
layout: { x, y, width, height },
},
}) => {
this.ps = this.ps.filter(i => i.key !== item[0]);
this.ps.push({
key: item[0], // 对应的字母 A-Z
min: y, // 字母顶部Y坐标
max: y + height, // 字母底部Y坐标
index: item[1], // 字母对应SectionList的index
});
}}
>
{item[0]}
</Text>
);
})}
{show && (
<View style={{
position: 'absolute', left: -60, top: showY - 10,borderRadius: 20,width: 60, height: 40,
flexDirection: 'row',
}}>
<View style={{
width: 40,
borderRadius: 20,
backgroundColor: '#999999',
alignItems: 'center',
justifyContent: 'center',
}}>
<Text style={{ fontSize: 30, color: '#FFFFFF', }}>
{key}
</Text>
</View>
<View style={{
position: 'absolute',
top: 3,
bottom: 3,
right: 0,
width: 30,
borderLeftColor: '#999999',
borderLeftWidth: 22,
borderTopColor: 'transparent',
borderTopWidth: 16,
borderBottomColor: 'transparent',
borderBottomWidth: 16,
}}>
</View>
</View>
)}
</View>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#FFFFFF',
flexDirection: 'row',
},
headerContainer: {
padding: 5,
backgroundColor: '#F8F8F8',
},
headerText: {
fontWeight: 'bold',
},
itemContainer: {
paddingHorizontal: 5,
paddingVertical: 10,
},
itemText: {
fontSize: 16,
},
});