forked from rgovindji/react-native-atoz-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlphabetPicker.js
88 lines (74 loc) · 2.66 KB
/
AlphabetPicker.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
import React, { Component, PropTypes } from 'react';
import { View, Text, PanResponder } from 'react-native';
class LetterPicker extends Component {
render() {
return (
<Text style={{ fontSize: 11, fontWeight: 'bold' }}>
{this.props.letter}
</Text>
);
}
}
const Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
export default class AlphabetPicker extends Component {
constructor(props, context) {
super(props, context);
if(props.alphabet){
Alphabet = props.alphabet;
}
}
componentWillMount() {
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onMoveShouldSetPanResponder: () => true,
onPanResponderGrant: (e, gestureState) => {
this.props.onTouchStart && this.props.onTouchStart();
this.tapTimeout = setTimeout(() => {
this._onTouchLetter(this._findTouchedLetter(gestureState.y0));
}, 100);
},
onPanResponderMove: (evt, gestureState) => {
clearTimeout(this.tapTimeout);
this._onTouchLetter(this._findTouchedLetter(gestureState.moveY));
},
onPanResponderTerminate: this._onPanResponderEnd.bind(this),
onPanResponderRelease: this._onPanResponderEnd.bind(this),
});
}
_onTouchLetter(letter) {
letter && this.props.onTouchLetter && this.props.onTouchLetter(letter);
}
_onPanResponderEnd() {
requestAnimationFrame(() => {
this.props.onTouchEnd && this.props.onTouchEnd();
});
}
_findTouchedLetter(y) {
let top = y - (this.absContainerTop || 0);
if (top >= 1 && top <= this.containerHeight) {
return Alphabet[Math.round((top / this.containerHeight) * Alphabet.length)]
}
}
_onLayout(event) {
this.refs.alphabetContainer.measure((x1, y1, width, height, px, py) => {
this.absContainerTop = py;
this.containerHeight = height;
});
}
render() {
this._letters = this._letters || (
Alphabet.map((letter) => <LetterPicker letter={letter} key={letter} />)
);
return (
<View
ref='alphabetContainer'
{...this._panResponder.panHandlers}
onLayout={this._onLayout.bind(this)}
style={{ paddingHorizontal: 5, backgroundColor: '#fff', borderRadius: 1, justifyContent: 'center' }}>
<View>
{this._letters}
</View>
</View>
);
}
}