-
Notifications
You must be signed in to change notification settings - Fork 34
/
app.js
100 lines (77 loc) · 1.76 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
import React, {
View, Text, StyleSheet,
} from 'react-native';
import Keyboard from 'react-native-keyboard';
let model = {
_keys: [],
_listeners: [],
addKey(key) {
this._keys.push(key);
this._notify();
},
delKey() {
this._keys.pop();
this._notify();
},
clearAll() {
this._keys = [];
this._notify();
},
getKeys() {
return this._keys;
},
onChange(listener) {
if (typeof listener === 'function') {
this._listeners.push(listener);
}
},
_notify() {
this._listeners.forEach((listner) => {
listner(this);
});
}
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
text: ''
};
}
componentDidMount() {
model.onChange((model) => {
this.setState({text: model.getKeys().join('')});
});
}
_handleClear() {
model.clearAll();
}
_handleDelete() {
model.delKey();
}
_handleKeyPress(key) {
model.addKey(key);
}
render() {
return (
<View style={{flex: 1}}>
<Text style={styles.text}>{this.state.text}</Text>
<Keyboard
isRenderDot={true}
onClear={this._handleClear.bind(this)}
onDelete={this._handleDelete.bind(this)}
onKeyPress={this._handleKeyPress.bind(this)}
/>
</View>
);
}
}
let styles = StyleSheet.create({
text: {marginTop: 100, textAlign: 'center'}
});
export default App;