1- import {
1+ import React , {
22 forwardRef ,
3+ useCallback ,
34 useEffect ,
45 useImperativeHandle ,
56 useRef ,
@@ -28,29 +29,38 @@ const Calculator = forwardRef<CalculatorHandles, CalculatorProps>(
2829 const elementRef = useRef < HTMLDivElement > ( null ) ;
2930 const [ input , setInput ] = useState < string > ( "" ) ;
3031 const [ output , setOutput ] = useState < string > ( "" ) ;
31- const [ scrollbarColor , buttonColor , fontColor , primaryButtonColor ] =
32- useSystemSettings ( ( s ) => [
33- s . iconColor ,
34- s . secondaryColor ,
35- s . fontColor ,
36- s . primaryColor ,
37- ] ) ;
32+ const [
33+ scrollbarColor ,
34+ secondaryColor ,
35+ fontColor ,
36+ primaryColor ,
37+ errorColor ,
38+ ] = useSystemSettings ( ( s ) => [
39+ s . iconColor ,
40+ s . secondaryColor ,
41+ s . fontColor ,
42+ s . primaryColor ,
43+ s . errorColor ,
44+ ] ) ;
3845 const appendToInput = ( value : string ) =>
3946 setInput ( ( current ) => current + value ) ;
4047 const removeFromEnd = ( count : number = 1 ) =>
41- setInput ( ( current ) => current . slice ( 0 , - count ) ) ;
48+ setInput ( ( current ) => {
49+ console . log ( "removingFromEnd" , "current" , current ) ;
50+ return current . slice ( 0 , - count ) ;
51+ } ) ;
4252 const clear = ( ) => setInput ( "" ) ;
43- const evaluate = ( ) => {
53+ const evaluate = useCallback ( ( ) => {
4454 try {
45- setInput ( eval ( input ) ) ;
55+ setInput ( String ( eval ( input ) ) ) ;
4656 setOutput ( "" ) ;
4757 } catch {
4858 setOutput ( "Invalid" ) ;
4959 }
50- } ;
60+ } , [ input ] ) ;
5161
52- useImperativeHandle ( ref , ( ) => ( {
53- onBorderedAppKeyDown ( e ) {
62+ const handleKeyDown = useCallback (
63+ ( e : React . KeyboardEvent ) => {
5464 const { key } = e ;
5565
5666 if ( isDigit ( key ) || operators . has ( key ) ) {
@@ -79,6 +89,11 @@ const Calculator = forwardRef<CalculatorHandles, CalculatorProps>(
7989 break ;
8090 }
8191 } ,
92+ [ evaluate ] ,
93+ ) ;
94+
95+ useImperativeHandle ( ref , ( ) => ( {
96+ onBorderedAppKeyDown : handleKeyDown ,
8297 element : elementRef . current ,
8398 } ) ) ;
8499
@@ -94,18 +109,18 @@ const Calculator = forwardRef<CalculatorHandles, CalculatorProps>(
94109 } , [ input ] ) ;
95110
96111 function Button ( {
97- displayChar,
98112 mathematicalChar,
113+ displayChar = mathematicalChar . toString ( ) ,
99114 action,
115+ buttonColor,
116+ disabled,
100117 } : {
101118 mathematicalChar : string | number ;
102119 displayChar ?: string ;
103120 action : "append-to-end" | "remove-from-end" | "evaluate" | "clear" ;
121+ buttonColor : string ;
122+ disabled ?: boolean ;
104123 } ) {
105- if ( displayChar === undefined ) {
106- displayChar = mathematicalChar . toString ( ) ;
107- }
108-
109124 function handleClick ( ) {
110125 switch ( action ) {
111126 case "append-to-end" :
@@ -127,10 +142,11 @@ const Calculator = forwardRef<CalculatorHandles, CalculatorProps>(
127142 width = { "100%" }
128143 height = { "100%" }
129144 borderRadius = { 12 }
130- backgroundColor = {
131- displayChar === "=" ? primaryButtonColor : buttonColor
132- }
145+ backgroundColor = { buttonColor }
133146 color = { fontColor }
147+ disabled = { disabled }
148+ onKeyDown = { handleKeyDown }
149+ className = { `calculator-button--${ displayChar } ` }
134150 >
135151 { displayChar }
136152 </ CommonButton >
@@ -150,27 +166,102 @@ const Calculator = forwardRef<CalculatorHandles, CalculatorProps>(
150166 < StyledInputOutputContents > { output } </ StyledInputOutputContents >
151167 </ StyledInputOutput >
152168 < StyledButtons >
153- < Button mathematicalChar = { "AC" } action = "clear" />
169+ < Button
170+ mathematicalChar = { "AC" }
171+ action = "clear"
172+ buttonColor = { secondaryColor }
173+ />
154174 < div />
155175 < div />
156- < Button mathematicalChar = "/" displayChar = "÷" action = "append-to-end" />
157-
158- < Button mathematicalChar = { 7 } action = "append-to-end" />
159- < Button mathematicalChar = { 8 } action = "append-to-end" />
160- < Button mathematicalChar = { 9 } action = "append-to-end" />
161- < Button mathematicalChar = "*" displayChar = "x" action = "append-to-end" />
162- < Button mathematicalChar = { 4 } action = "append-to-end" />
163- < Button mathematicalChar = { 5 } action = "append-to-end" />
164- < Button mathematicalChar = { 6 } action = "append-to-end" />
165- < Button mathematicalChar = "-" action = "append-to-end" />
166- < Button mathematicalChar = { 1 } action = "append-to-end" />
167- < Button mathematicalChar = { 2 } action = "append-to-end" />
168- < Button mathematicalChar = { 3 } action = "append-to-end" />
169- < Button mathematicalChar = "+" action = "append-to-end" />
170- < Button mathematicalChar = { 0 } action = "append-to-end" />
171- < Button mathematicalChar = "." action = "append-to-end" />
172- < Button mathematicalChar = "←" action = "remove-from-end" />
173- < Button mathematicalChar = "=" action = "evaluate" />
176+ < Button
177+ mathematicalChar = "/"
178+ displayChar = "÷"
179+ action = "append-to-end"
180+ buttonColor = { secondaryColor }
181+ />
182+
183+ < Button
184+ mathematicalChar = { 7 }
185+ action = "append-to-end"
186+ buttonColor = { secondaryColor }
187+ />
188+ < Button
189+ mathematicalChar = { 8 }
190+ action = "append-to-end"
191+ buttonColor = { secondaryColor }
192+ />
193+ < Button
194+ mathematicalChar = { 9 }
195+ action = "append-to-end"
196+ buttonColor = { secondaryColor }
197+ />
198+ < Button
199+ mathematicalChar = "*"
200+ displayChar = "x"
201+ action = "append-to-end"
202+ buttonColor = { secondaryColor }
203+ />
204+ < Button
205+ mathematicalChar = { 4 }
206+ action = "append-to-end"
207+ buttonColor = { secondaryColor }
208+ />
209+ < Button
210+ mathematicalChar = { 5 }
211+ action = "append-to-end"
212+ buttonColor = { secondaryColor }
213+ />
214+ < Button
215+ mathematicalChar = { 6 }
216+ action = "append-to-end"
217+ buttonColor = { secondaryColor }
218+ />
219+ < Button
220+ mathematicalChar = "-"
221+ action = "append-to-end"
222+ buttonColor = { secondaryColor }
223+ />
224+ < Button
225+ mathematicalChar = { 1 }
226+ action = "append-to-end"
227+ buttonColor = { secondaryColor }
228+ />
229+ < Button
230+ mathematicalChar = { 2 }
231+ action = "append-to-end"
232+ buttonColor = { secondaryColor }
233+ />
234+ < Button
235+ mathematicalChar = { 3 }
236+ action = "append-to-end"
237+ buttonColor = { secondaryColor }
238+ />
239+ < Button
240+ mathematicalChar = "+"
241+ action = "append-to-end"
242+ buttonColor = { secondaryColor }
243+ />
244+ < Button
245+ mathematicalChar = { 0 }
246+ action = "append-to-end"
247+ buttonColor = { secondaryColor }
248+ />
249+ < Button
250+ mathematicalChar = "."
251+ action = "append-to-end"
252+ buttonColor = { secondaryColor }
253+ />
254+ < Button
255+ mathematicalChar = "←"
256+ action = "remove-from-end"
257+ buttonColor = { errorColor }
258+ />
259+ < Button
260+ mathematicalChar = "="
261+ action = "evaluate"
262+ buttonColor = { primaryColor }
263+ disabled = { ! input }
264+ />
174265 </ StyledButtons >
175266 </ StyledCalc >
176267 ) ;
0 commit comments