@@ -5,19 +5,47 @@ import styles from './src/styles/_calculator'
5
5
class App extends React . Component {
6
6
constructor ( ) {
7
7
super ( )
8
+ this . state = {
9
+ calculationText : ""
10
+ }
11
+ }
12
+
13
+ calculateResult ( ) {
14
+ const text = this . state . calculationText
15
+ // now parse this text
16
+ }
8
17
18
+ buttonbPressed ( text ) {
19
+ //console.log(text)
20
+
21
+ if ( text == '=' ) {
22
+ return this . calculateResult ( )
23
+ }
24
+ this . setState ( {
25
+ calculationText : this . state . calculationText + text
26
+ } )
9
27
}
10
28
11
-
29
+ operate ( operation ) {
30
+ switch ( operation ) {
31
+ case 'Del' :
32
+ let text = this . state . calculationText . split ( '' )
33
+ text . pop ( )
34
+
35
+ this . setState ( {
36
+ calculationText : text . join ( '' )
37
+ } )
38
+ }
39
+ }
12
40
13
41
render ( ) {
14
42
let rows = [ ] ;
15
- let numb = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] , [ 0 , 0 , '=' ] ] ;
43
+ let numb = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] , [ "." , 0 , '=' ] ] ;
16
44
for ( let i = 0 ; i < 4 ; i ++ ) {
17
45
let row = [ ]
18
46
for ( let j = 0 ; j < 3 ; j ++ ) {
19
47
row . push (
20
- < TouchableOpacity style = { styles . btn } >
48
+ < TouchableOpacity onPress = { ( ) => this . buttonbPressed ( numb [ i ] [ j ] ) } >
21
49
< Text style = { styles . btnText } > { numb [ i ] [ j ] } </ Text >
22
50
</ TouchableOpacity >
23
51
)
@@ -26,18 +54,18 @@ class App extends React.Component {
26
54
}
27
55
28
56
let oper = [ ] ;
29
- let operation = [ '+' , '-' , '*' , '/' ] ;
57
+ let operation = [ 'Del' , ' +', '-' , '*' , '/' ] ;
30
58
for ( let i = 0 ; i < 4 ; i ++ ) {
31
59
oper . push (
32
- < TouchableOpacity style = { styles . btn } >
60
+ < TouchableOpacity style = { styles . btn } onPress = { ( ) => this . operate ( operation [ i ] ) } >
33
61
< Text style = { [ styles . btnText , { color : 'white' } ] } > { operation [ i ] } </ Text >
34
62
</ TouchableOpacity >
35
63
)
36
64
}
37
65
return (
38
66
< View style = { styles . container } >
39
67
< View style = { styles . calculation } >
40
- < Text style = { styles . calculationText } > 11*11 </ Text >
68
+ < Text style = { styles . calculationText } > { this . state . calculationText } </ Text >
41
69
</ View >
42
70
< View style = { styles . result } >
43
71
< Text style = { styles . resultText } > 121</ Text >
0 commit comments