-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculadoraSideEffect.html
204 lines (180 loc) · 5.87 KB
/
CalculadoraSideEffect.html
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
margin: 0;
background-color: #121212;
font-family: sans-serif;
}
.header {
background-color: #333;
text-align: center;
color: #fff;
padding: 16px;
border-radius: 4px;
}
.calculator {
background-color: #fff;
margin: 40px auto;
max-width: 400px;
padding: 16px;
border-radius: 4px;
}
.display {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1.2em;
}
.keyboard {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-gap: 16px;
font-size: 1.2em;
margin-top: 16px;
}
button {
border: 0;
border-radius: 4px;
background-color: #efefef;
padding: 8px;
font-weight: bold;
cursor: pointer;
}
button.span2 {
grid-column: span 2;
}
button.primary {
background-color: #ffeec2;
color: #c49023;
}
.error {
color: red;
}
</style>
<title>Calculadora - React Profissional</title>
</head>
<body>
<div id="root"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
const calcRegex = /^([0-9]|-|\+|\*|\/|\.)*$/;
const Header = () => <div className="header">Calculadora</div>;
const Button = ({ value, onClick, className }) => (
<button
onClick={() => {
onClick(value);
}}
className={className}
>
{value}
</button>
);
const Calculator = () => {
const [display, setDisplay] = React.useState("");
const [error, setError] = React.useState();
const keyboard = [
7, 8, 9, "/", "C",
4, 5, 6, "*", "Del",
1, 2, 3, "-", "",
0, ".", "+", "="
];
//-- React.useEffect só é executado após renderizar a tela
// -- o segundo parâmetro onde está [display] significa que só será executado quando o display for modificado
//-- no segundo parâmetro se passamos o valor vazio [] significa que só será executado uma única vez
React.useEffect(() => {
if (error) {
setError();
}
}, [display]);
const doTheMath = () => {
try {
setDisplay("" + eval(display));
} catch (err) {
setError(`Invalid expression: ${err.message}`);
}
}
const handleClick = (value) => {
switch (value) {
case "=":
doTheMath();
break;
case "C":
setDisplay("");
break;
case "Del":
setDisplay(
display.substring(0, display.length - 1)
);
break;
default:
setDisplay(`${display}${value}`);
}
}
return (
<div className="calculator">
<input
type="text"
className="display"
value={display}
onChange={event => {
const { value } = event.target;
if (calcRegex.test(value)) {
setDisplay(event.target.value);
}
}}
onKeyPress={event => {
if (event.key === 'Enter') {
doTheMath();
} else if (event.key.toLowerCase() === 'c') {
setDisplay('');
}
}}
/>
{error && (
<p className="error">{error}</p>
)}
<div className="keyboard">
{keyboard.map(value => {
const span2Class = value === 0 ? "span2" : "";
const primaryClass = isNaN(value) ? "primary" : "";
return (
<Button
key={value}
value={value}
onClick={handleClick}
className={`${span2Class} ${primaryClass}`}
/>
);
})}
</div>
</div>
);
}
const App = () => (
<div>
<Header />
<Calculator />
</div>
);
ReactDOM.render(
<App />,
document.getElementById("root")
);
</script>
</body>
</html>