From 27b824c2355055711738771cd2d0c4c4cd3ae4b6 Mon Sep 17 00:00:00 2001 From: Artur Tamborski Date: Sat, 28 Nov 2020 09:55:59 +0100 Subject: [PATCH] components: add move validation, draw lines on mouse move --- src/components/App/App.tsx | 2 +- src/components/Board/Board.tsx | 38 ++++++++++++++++++---------------- src/components/Line/Line.tsx | 2 ++ 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/components/App/App.tsx b/src/components/App/App.tsx index d26f9bf..3791fc4 100644 --- a/src/components/App/App.tsx +++ b/src/components/App/App.tsx @@ -14,7 +14,7 @@ export default function App(): JSX.Element {
diff --git a/src/components/Board/Board.tsx b/src/components/Board/Board.tsx index dfdd7e7..f24d733 100644 --- a/src/components/Board/Board.tsx +++ b/src/components/Board/Board.tsx @@ -17,7 +17,7 @@ interface IBoardProps { interface IBoardState { cells: string[][]; - lines: string[]; + lines: boolean[]; isSelecting: boolean; startPos: Point; endPos: Point; @@ -29,33 +29,34 @@ export default class Board extends React.Component { this.state = { cells: Array(props.numCols).fill(Array(props.numRows).fill('A')), - lines: Array(props.numLines).fill(''), + lines: Array(props.numLines).fill(false), isSelecting: false, startPos: {x: 0, y: 0}, endPos: {x: 0, y: 0}, } } + moveIsValid(pos: Point): boolean { + const x = Math.abs(pos.x - this.state.startPos.x); + const y = Math.abs(pos.y - this.state.startPos.y); + const lineIsValid = x === 0 || y === 0 || x === y; + + return this.state.isSelecting && lineIsValid; + } + handleMouseDown(startPos: Point) { - this.setState({...this.state, startPos, isSelecting: true}); + const endPos = startPos; + this.setState({...this.state, startPos, endPos, isSelecting: true}); } handleMouseUp(endPos: Point) { - const x = Math.abs(endPos.x - this.state.startPos.x); - const y = Math.abs(endPos.y - this.state.startPos.y); - - if (!(x === 0 || y === 0 || x === y)) - return; - - this.setState({...this.state, endPos, isSelecting: false}); + if (this.moveIsValid(endPos)) + this.setState({...this.state, endPos, isSelecting: false}); } - handleMouseOver(currentPos: Point) { - if (!this.state.isSelecting) { - this.setState({...this.state}); - } else { - - } + handleMouseOver(endPos: Point) { + if (this.moveIsValid(endPos)) + this.setState({...this.state, endPos}); } renderCell(s: string, pos: Point): JSX.Element { @@ -76,9 +77,10 @@ export default class Board extends React.Component { return ( ); } diff --git a/src/components/Line/Line.tsx b/src/components/Line/Line.tsx index a190e1b..ac40d38 100644 --- a/src/components/Line/Line.tsx +++ b/src/components/Line/Line.tsx @@ -8,6 +8,7 @@ interface ILineProps { startPos: Point; endPos: Point; cellSize: number; + isVisible: boolean; } export default function Line(props: ILineProps): JSX.Element { @@ -16,6 +17,7 @@ export default function Line(props: ILineProps): JSX.Element { return (