forked from yoonzm/react-native-alphabet-flat-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlphabetListView.js
88 lines (80 loc) · 2.46 KB
/
AlphabetListView.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
/**
* Created by JetBrains WebStorm.
* Author: yoon
* Date: 19-4-12
* Time: 下午3:03
* Desc: 字母列表
*/
import React from 'react';
import PropTypes from 'prop-types';
import { PanResponder, View } from 'react-native';
import Toast from 'react-native-root-toast';
let toast = null;
export function AlphabetListView({ onLayout, container, contentHeight, pageY, titles, onSelect, selectAlphabet, item }) {
const itemHeight = contentHeight / titles.length;
const onTouchChange = (evt, type) => {
const event = evt.nativeEvent || {};
const index = Math.floor((event.pageY - pageY) / itemHeight);
// console.log('AlphabetListView.onTouchChange()', event.pageY, index, type);
if (index >= 0 && titles[index]) {
if (toast) {
setTimeout(() => {
toast && Toast.hide(toast);
toast = null;
}, 100);
}
if (type === 'Move') {
toast = Toast.show(`${titles[index]}`, {
position: Toast.positions.CENTER,
duration: 1000,
shadow: false,
opacity: 1,
containerStyle: {
height: 70,
width: 70,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'black'
},
textStyle: {
color: 'white',
fontSize: 35,
fontWeight: 'bold'
}
});
}
onSelect && onSelect(index);
}
};
const responder = PanResponder.create({
// 要求成为响应者:
onStartShouldSetPanResponderCapture: () => true,
onMoveShouldSetPanResponderCapture: () => { },
onStartShouldSetPanResponder: () => true,
onMoveShouldSetPanResponder: () => { },
onPanResponderTerminationRequest: () => true,
onPanResponderGrant: evt => onTouchChange(evt, 'Grant'),
// onPanResponderStart: evt => onTouchChange(evt, 'Start'),
onPanResponderMove: evt => onTouchChange(evt, 'Move')
// onPanResponderEnd: evt => onTouchChange(evt, 'End'),
// onPanResponderRelease: evt => onTouchChange(evt, 'Release')
});
return (
<View
ref={ref => container(ref)}
onLayout={onLayout}
style={{
justifyContent: 'center',
alignItems: 'center',
position: 'absolute',
right: 5,
zIndex: 10
}}
{...responder.panHandlers}>
{titles.map((title, index) => item({ title, active: selectAlphabet === title }))}
</View>
);
}
AlphabetListView.propTypes = {
prop: PropTypes.any
};