-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTile.tsx
68 lines (63 loc) · 1.44 KB
/
Tile.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { twMerge } from "tailwind-merge";
import {
END_TILE_STYLE,
MAX_ROWS,
PATH_TILE_STYLE,
START_TILE_STYLE,
TILE_STYLE,
TRAVERSED_TILE_STYLE,
WALL_TILE_STYLE,
} from "../utils/constants";
interface MouseFunction {
(row: number, col: number): void;
}
export function Tile({
row,
col,
isStart,
isEnd,
isTraversed,
isWall,
isPath,
handleMouseDown,
handleMouseUp,
handleMouseEnter,
}: {
row: number;
col: number;
isStart: boolean;
isEnd: boolean;
isTraversed: boolean;
isWall: boolean;
isPath: boolean;
handleMouseDown: MouseFunction;
handleMouseUp: MouseFunction;
handleMouseEnter: MouseFunction;
}) {
let tileTyleStyle;
if (isStart) {
tileTyleStyle = START_TILE_STYLE;
} else if (isEnd) {
tileTyleStyle = END_TILE_STYLE;
} else if (isWall) {
tileTyleStyle = WALL_TILE_STYLE;
} else if (isPath) {
tileTyleStyle = PATH_TILE_STYLE;
} else if (isTraversed) {
tileTyleStyle = TRAVERSED_TILE_STYLE;
} else {
tileTyleStyle = TILE_STYLE;
}
const borderStyle =
row === MAX_ROWS - 1 ? "border-b" : col === 0 ? "border-l" : "";
const edgeStyle = row === MAX_ROWS - 1 && col === 0 ? "border-l" : "";
return (
<div
className={twMerge(tileTyleStyle, borderStyle, edgeStyle)}
id={`${row}-${col}`}
onMouseDown={() => handleMouseDown(row, col)}
onMouseUp={() => handleMouseUp(row, col)}
onMouseEnter={() => handleMouseEnter(row, col)}
/>
);
}