import clsx from "clsx"; import { PrimaryButton, SuccessButton } from "mattrixwv-components"; import { useCallback } from "react"; import { BsDashLg, BsPlusLg } from "react-icons/bs"; export default function SquareNumberArray({ value, onChange }:{ value: number[][]; onChange: (value: number[][]) => void; }){ //TODO: Check for a square array const decreaseArraySize = useCallback(() => { if(value.length <= 2){ throw new Error("Cannot decrease grid size below 2"); } const newValue = value.slice(0, -1); newValue.forEach((row) => { row.pop(); }); onChange(newValue); }, [ onChange, value ]); const increaseArraySize = useCallback(() => { if(value.length >= 10){ throw new Error("Cannot increase grid size above 10"); } const newValue = [...value]; newValue.forEach((row) => { row.push(0); }); newValue.push(new Array(value.length + 1).fill(0) as number[]); onChange(newValue); }, [ onChange, value ]); const updateValue = useCallback((rowIndex: number, cellIndex: number, newCellValue: number) => { const newValue = [...value]; newValue[rowIndex][cellIndex] = newCellValue; onChange(newValue); }, [ onChange, value ]); return (
= 10}>
{ value.map((row, rowIndex) => ( { row.map((cell, cellIndex) => ( )) } )) }
updateValue(rowIndex, cellIndex, parseInt(e.target.value))} />
); }