-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathExampleCalculator.js
97 lines (86 loc) · 2.12 KB
/
ExampleCalculator.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
// @flow
import React, { Component } from 'react';
import { StyleSheet, Text, TextInput, View } from 'react-native';
type Props = {};
type State = {
value1: number,
value2: number,
};
export default class ExampleCalculator extends Component<Props, State> {
props: Props;
state: State = {
value1: 0,
value2: 0,
};
static navigationOptions = {
title: 'Calculator',
};
_calculate(value1: number, value2: number): number {
return value1 + value2;
}
_onChangeTextValue1(text: string) {
let value = parseFloat(text);
if (isNaN(value)) {
value = 0;
}
this.setState({ value1: value });
}
_onChangeTextValue2(text: string) {
let value = parseFloat(text);
if (isNaN(value)) {
value = 0;
}
this.setState({ value2: value });
}
render() {
let value1 = this.state.value1;
let value2 = this.state.value1;
let result = this._calculate(value1, value2);
// console.log('value1', value1);
// console.log('value2', value2);
// console.log('result', result);
return (
<View style={styles.container}>
<View style={styles.inputRow}>
<TextInput
style={[styles.textInput, styles.text]}
multiline={false}
keyboardType={'numeric'}
value={this.state.value1.toString()}
onChangeText={this._onChangeTextValue1.bind(this)}
/>
<Text style={styles.text}>+</Text>
<TextInput
style={[styles.textInput, styles.text]}
multiline={false}
keyboardType={'numeric'}
value={this.state.value2.toString()}
onChangeText={this._onChangeTextValue2.bind(this)}
/>
</View>
<View>
<Text style={styles.text}>= {result}</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
backgroundColor: '#F5FCFF',
paddingTop: 80,
},
inputRow: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
textInput: {
width: 100,
},
text: {
fontSize: 40,
},
});