Skip to content

Commit bc2fab8

Browse files
committed
added state in playground when 'Add a Row' button is clicked
1 parent f95998b commit bc2fab8

File tree

8 files changed

+128
-4
lines changed

8 files changed

+128
-4
lines changed

package-lock.json

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@mui/material": "^5.10.12",
1111
"@mui/styled-engine-sc": "^5.10.6",
1212
"@mui/x-data-grid": "^5.17.10",
13+
"@react-hook/resize-observer": "^1.2.6",
1314
"@testing-library/jest-dom": "^5.16.5",
1415
"@testing-library/react": "^13.4.0",
1516
"@testing-library/user-event": "^13.5.0",

src/components/Editor.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import TransitionTable from "../features/TransitionTable";
1313
import { promptNewStateName } from "../utils/PromptNewStateName";
1414
import { PossibleTransitionValues } from "../consts/PossibleTransitionValues";
1515
import { StateNameMaxLength } from "../consts/StateNameMaxLength";
16+
import { PlaygroundSize } from "./interfaces/playgroundSize";
1617

1718
const Editor = () => {
1819
console.log("re rendering Editor");
@@ -93,10 +94,23 @@ const Editor = () => {
9394

9495
const [selected, setSelected] = useState<SelectedElementType | null>(null);
9596
const [actionState, setActionState] = useState("Normal");
97+
const [size, setSize] = useState<PlaygroundSize>({ width: 0, height: 0 });
9698

9799
const handleAddRow = (row: RowModel) => {
98100
setGridData((prev) => [...prev, row]);
99101
setGridRowId((prev) => prev + 1);
102+
console.log(
103+
"added row, now adding new state at: ",
104+
size.width,
105+
size.height
106+
);
107+
let newBox = {
108+
id: row.node,
109+
x: Math.floor(Math.random() * size.width),
110+
y: Math.floor(Math.random() * size.height),
111+
shape: "state",
112+
};
113+
setBoxes((prev) => [...prev, newBox]);
100114
};
101115

102116
const handleDeleteRow = (row: RowModel) => {
@@ -218,7 +232,7 @@ const Editor = () => {
218232
return prev;
219233
}
220234

221-
return [...prev].map((r) =>
235+
return prev.map((r) =>
222236
r.id === row.id
223237
? {
224238
...r,
@@ -373,7 +387,11 @@ const Editor = () => {
373387
}
374388
console.log("boxes", boxes);
375389

376-
handleAddRow(new RowModel(gridRowId, stateName, "", "", "", false, false));
390+
setGridData((prev) => [
391+
...prev,
392+
new RowModel(gridRowId, stateName, "", "", "", false, false),
393+
]);
394+
setGridRowId((prev) => prev + 1);
377395
};
378396

379397
const transitionTableProps: TransitionTableProps = {
@@ -402,6 +420,7 @@ const Editor = () => {
402420
handleDeleteRow,
403421
toggleInitialState,
404422
toggleFinalState,
423+
setSize,
405424
};
406425

407426
return (
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface PlaygroundSize {
2+
width: number;
3+
height: number;
4+
}

src/features/Playground.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ import TopBar from "./components/playground/TopBar";
77
import { PlaygroundProps } from "./props/PlaygroundProps";
88
import Box from "./components/playground/Box";
99
import Xarrow from "./components/playground/Xarrow";
10+
import useElementSize from "./hooks/useElementSize";
11+
import { useEffect } from "react";
1012

1113
const Playground = (props: PlaygroundProps) => {
1214
console.log("re rendering Playground: props", props);
1315

16+
const [boxRef, { width, height }] = useElementSize();
17+
useEffect(() => {
18+
console.log("useEffect of playground due to width & height", width, height);
19+
props.setSize({ width, height });
20+
}, [width, height]);
21+
1422
const topBarprops: TopBarProps = {
1523
boxes: props.boxes,
1624
setBoxes: props.setBoxes,
@@ -73,6 +81,7 @@ const Playground = (props: PlaygroundProps) => {
7381
className="boxesContainer"
7482
onDragOver={(e) => e.preventDefault()}
7583
onDrop={props.handleDropDynamic}
84+
ref={boxRef}
7685
>
7786
<TopBar {...topBarprops} />
7887

src/features/css/Playground.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@
6565
align-items: center;
6666
height: calc(100% - 68px);
6767
}
68-
.state,
69-
.interfaceBox {
68+
.state {
7069
border: 1px #999 solid;
7170
border-radius: 50px;
7271
text-align: center;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { MutableRefObject, useLayoutEffect, useRef, useState } from "react";
2+
import useResizeObserver from "@react-hook/resize-observer";
3+
import { PlaygroundSize } from "../../components/interfaces/playgroundSize";
4+
5+
export default function useElementSize<
6+
T extends HTMLElement = HTMLDivElement
7+
>(): [MutableRefObject<T | null>, PlaygroundSize] {
8+
const target = useRef<T | null>(null);
9+
const [size, setSize] = useState<PlaygroundSize>({
10+
width: 0,
11+
height: 0,
12+
});
13+
14+
const setRoundedSize = ({ width, height }: PlaygroundSize) => {
15+
setSize({ width: Math.round(width) - 51, height: Math.round(height) - 51 }); // -51 because state is 50px in size so we need to remove that from the width and height plus 1 for the border
16+
};
17+
18+
useLayoutEffect(() => {
19+
target.current && setRoundedSize(target.current.getBoundingClientRect());
20+
}, [target]);
21+
22+
useResizeObserver(target, (entry) => {
23+
const { inlineSize: width, blockSize: height } = entry.contentBoxSize[0];
24+
setRoundedSize({ width, height });
25+
});
26+
27+
return [target, size];
28+
}

src/features/props/PlaygroundProps.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PlaygroundSize } from "../../components/interfaces/playgroundSize";
12
import { DraggableStateModel, RowModel, TransitionModel } from "../../models";
23
import { SelectedElementType } from "./SelectedElementType";
34

@@ -18,4 +19,5 @@ export type PlaygroundProps = {
1819
handleDeleteRow: (row: RowModel) => void;
1920
toggleInitialState: (row: RowModel) => void;
2021
toggleFinalState: (row: RowModel) => void;
22+
setSize: React.Dispatch<React.SetStateAction<PlaygroundSize>>;
2123
};

0 commit comments

Comments
 (0)