-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
118 lines (109 loc) · 2.69 KB
/
App.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
import React, {PureComponent} from 'react';
import {StyleSheet, Text, View, TextInput, FlatList} from 'react-native';
const getSuggestions = require('./trie-service.js');
const idiomsOfInput = require('./trie-from-idiom.js');
const pinyinDict = require('./cedict.json');
export default class App extends PureComponent {
state = {
suggestions: [],
};
onChangeText = (text) => {
text = text.trim().toLowerCase();
if (text) {
let suggestions = getSuggestions(text);
if (text.length >= 3 && pinyinDict[text]) {
suggestions = pinyinDict[text]
.map((item) => ({word: item}))
.concat(suggestions);
}
this.setState({
suggestions: suggestions,
});
} else {
this.setState({
suggestions: [],
});
}
};
onChangeText2 = (text) => {
text = text.trim().toLowerCase();
if (text) {
const suggestions = idiomsOfInput(text).map((item) => {
return {word: item};
});
this.setState({
suggestions: suggestions,
});
} else {
this.setState({
suggestions: [],
});
}
};
keyExtractor = (item) => item.word;
render() {
const suggestions = this.state.suggestions;
return (
<View style={styles.container}>
<Text>Tiny english dictionary</Text>
<TextInput
style={styles.input}
onChangeText={this.onChangeText}
autoFocus={true}
autoCorrect={false}
autoCapitalize="none"
placeholder="Input the word..."
/>
<TextInput
style={styles.input}
onChangeText={this.onChangeText2}
autoFocus={false}
autoCorrect={false}
autoCapitalize="none"
placeholder="成语接龙..."
/>
<FlatList
style={styles.list}
data={suggestions}
keyExtractor={this.keyExtractor}
renderItem={({item}) => (
<View style={styles.item}>
<Text>
{item.word} {item.ipa ? ' [ ' + item.ipa + ' ]' : ' '}
{item.translation && ' ' + item.translation.join(' ')}
</Text>
</View>
)}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
marginTop: 30,
},
input: {
width: '80%',
borderWidth: 1,
borderColor: '#eee',
borderRadius: 5,
margin: 10,
},
list: {
marginTop: 5,
marginRight: 5,
marginBottom: 30,
marginLeft: 5,
},
item: {
borderBottomWidth: 1,
borderColor: '#ddd',
paddingBottom: 3,
marginBottom: 5,
},
});