I am trying to implement responsive and dynamic grid design into my calculator. I know of a simple way of achieving this(explicitly create buttons and add classes), but I am trying to find out if there is more concise and “trendy” way of setting classes to the buttons(see below).
Here is what I got:
import style from "./calculator.module.scss";
// interface CalcGridProps {
// addition: () => void;
// subtraction: () => void;
// division: () => void;
// multiplication: () => void;
// }
const CalcGrid: React.FC = (
{
// addition,
// subtraction,
// division,
// multiplication,
},
) => {
const buttons = [
["AC", "+/-", "%", "/"],
["7", "8", "9", "*"],
["4", "5", "6", "-"],
["1", "2", "3", "+"],
["0", ",", "="],
];
return (
<div className={style.container}>
<section className={style.screen}>
<div>0</div>
</section>
<section className={style.grid}>
{buttons.flat().map((label, i) => (
<button
key={i}
// className={style[areaMap[label]]}
style={{ gridArea: areaMap[label] }}
>
{label}
</button>
))}
</section>
</div>
);
};
export default CalcGrid;
const areaMap: Record<string, string> = {
AC: "ac",
"+/-": "plusminus",
"%": "percent",
"/": "divide",
"7": "seven",
"8": "eight",
"9": "nine",
"*": "multiply",
"4": "four",
"5": "five",
"6": "six",
"-": "subtract",
"1": "one",
"2": "two",
"3": "three",
"+": "add",
funcs: "funcs",
"0": "zero",
",": "comma",
"=": "equals",
};
With some assistance from AI, i tried a way it offered. It works, yes, but I would like to ask if the suggested approach is viable? And what needs to be changed for consistent result.