Skip to content

Commit fd3fefd

Browse files
committed
Calculator button fixes
1 parent b98732f commit fd3fefd

4 files changed

Lines changed: 145 additions & 47 deletions

File tree

src/components/Button/Button.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
import { forwardRef, PropsWithChildren, useCallback } from "react";
1+
import {
2+
forwardRef,
3+
HTMLAttributes,
4+
PropsWithChildren,
5+
useCallback,
6+
} from "react";
27
import { StyledButton, StyledButtonProps } from "./styles";
38
import { setRef } from "../../common/utils/htmlHelpers";
49
import { adjustLuminance } from "../../common/utils/colorUtils";
510

6-
interface ButtonProps extends StyledButtonProps {
11+
interface ButtonProps
12+
extends StyledButtonProps, HTMLAttributes<HTMLDivElement> {
713
onClick?: () => void;
814
disabled?: boolean;
915
}
1016

11-
const Button = forwardRef<HTMLButtonElement, PropsWithChildren<ButtonProps>>(
17+
const Button = forwardRef<HTMLDivElement, PropsWithChildren<ButtonProps>>(
1218
({ onClick, children, ...rest }, ref) => {
1319
const onRef = useCallback(
14-
(el: HTMLButtonElement | null) => setRef(ref, el),
20+
(el: HTMLDivElement | null) => setRef(ref, el),
1521
[ref],
1622
);
1723
const separatorColor =
@@ -26,6 +32,7 @@ const Button = forwardRef<HTMLButtonElement, PropsWithChildren<ButtonProps>>(
2632
onClick={onClick}
2733
ref={onRef}
2834
separatorColor={separatorColor}
35+
role="button"
2936
>
3037
{children}
3138
</StyledButton>

src/components/Button/styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface StyledButtonProps {
2222
separatorColor?: string;
2323
}
2424

25-
export const StyledButton = styled.button<StyledButtonProps>`
25+
export const StyledButton = styled.div<StyledButtonProps>`
2626
${buildStyledButton}
2727
`;
2828

src/programs/Calculator/Calculator.tsx

Lines changed: 132 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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
);

src/programs/Calculator/styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const StyledButtons = styled.div`
5050
height: 100%;
5151
justify-self: center;
5252
display: grid;
53-
gap: 4px;
53+
gap: 6px;
5454
grid-template-columns: 1fr 1fr 1fr 1fr;
5555
grid-template-rows: 1fr 1fr 1fr 1fr 1fr;
5656
box-sizing: border-box;

0 commit comments

Comments
 (0)