-
Notifications
You must be signed in to change notification settings - Fork 101
/
AlphabetPicker.js
98 lines (85 loc) · 2.97 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
89
90
91
92
93
94
95
96
97
98
import React, { Component } from 'react';
import { View, Text, PanResponder } from 'react-native';
import PropTypes from 'prop-types';
class LetterPicker extends Component {
render() {
return (
<Text style={{ fontSize: 11, fontWeight: 'bold' }}>
{this.props.letter}
</Text>
);
}
}
let Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
export default class AlphabetPicker extends Component {
constructor(props, context) {
super(props, context);
if(props.alphabet){
Alphabet = props.alphabet;
}
this.state = {
alphabet: 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),
});
}
componentWillReceiveProps(nextProps) {
if(this.props.alphabet !== nextProps.alphabet){
this.setState({alphabet:nextProps.alphabet})
}
}
_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);
const {alphabet} = this.state
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() {
const {alphabet} = this.state
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>
);
}
}