Skip to content

Commit

Permalink
Working version
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-grace committed Nov 14, 2024
1 parent e47ed4e commit 7e96116
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/components/BluetoothPatternInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const BluetoothPatternInput = ({
generateMatrix(matrixDim, false)
);
const matrixColumns = transformMatrixToColumns(pattern, matrixDim);
const [inputValues, setInputValues] = useState<string[]>(
matrixColumns.map((cols) => cols.filter((c) => c).length.toString())
);

const clearHighlighted = useCallback(() => {
setHighlighted(generateMatrix(matrixDim, false));
Expand All @@ -53,10 +56,18 @@ const BluetoothPatternInput = ({
const columnInputOnChange = useCallback(
(colIdx: number): ((value: string) => void) => {
return (v) => {
updateMatrix(colIdx, matrixDim - parseInt(v));
const inputValsCopy = [...inputValues];
const cleanedValue = v.replace("-", "").replace("+", "");
inputValsCopy[colIdx] = cleanedValue;
const colValue = parseInt(v) || 0;
if (colValue > 5 || colValue < 0) {
return;
}
setInputValues(inputValsCopy);
updateMatrix(colIdx, matrixDim - colValue);
};
},
[updateMatrix]
[inputValues, updateMatrix]
);

return (
Expand Down Expand Up @@ -94,7 +105,7 @@ const BluetoothPatternInput = ({
isInvalid={invalid}
onChange={columnInputOnChange(colIdx)}
colIdx={colIdx}
value={cells.filter((c) => c).length}
value={inputValues[colIdx]}
/>
</GridItem>
</React.Fragment>
Expand Down Expand Up @@ -138,7 +149,7 @@ const PatternBox = ({

interface PatternColumnInputProps {
colIdx: number;
value: number;
value: string;
isInvalid: boolean;
onChange: (value: string) => void;
}
Expand Down

0 comments on commit 7e96116

Please sign in to comment.