|
1 | | -import React, { useState } from 'react'; |
2 | | -import Toggle from './toggle'; |
3 | | -import EdgeDisplay from './edgeDisplay'; |
| 1 | +import React, { useState } from "react"; |
| 2 | +import Toggle from "./toggle"; |
| 3 | +import EdgeDisplay from "./edgeDisplay"; |
4 | 4 |
|
5 | 5 | interface EdgeCollectorProps { |
6 | | - title: string; |
7 | | - cols?: number; |
8 | | - allVertices: string[]; |
9 | | - onChange?: (edges: string[][]) => void; |
| 6 | + title: string; |
| 7 | + cols?: number; |
| 8 | + allVertices: string[]; |
| 9 | + onChange?: (edges: string[][]) => void; |
10 | 10 | } |
11 | 11 |
|
12 | | -const EdgeCollector: React.FC<EdgeCollectorProps> = ({ title, cols = 4, allVertices, onChange}) => { |
13 | | - // Component logic goes here |
14 | | - const [items, setItems] = useState<string[][]>([]); |
| 12 | +const EdgeCollector: React.FC<EdgeCollectorProps> = ({ |
| 13 | + title, |
| 14 | + cols = 4, |
| 15 | + allVertices, |
| 16 | + onChange, |
| 17 | +}) => { |
| 18 | + // Component logic goes here |
| 19 | + const [items, setItems] = useState<string[][]>([]); |
15 | 20 |
|
16 | | - const itemAdded = (fromVertex: string, toVertex: string) => { |
17 | | - if(fromVertex === "" || toVertex === "") |
18 | | - return; |
19 | | - if(items.some((item) => item[0] == fromVertex && item[1] == toVertex)) |
20 | | - return; |
21 | | - const newItems = [...items]; |
22 | | - newItems.push([fromVertex, toVertex]); |
23 | | - setItems(newItems); |
24 | | - onChange?.(newItems); |
25 | | - } |
| 21 | + const itemAdded = (fromVertex: string, toVertex: string) => { |
| 22 | + if (fromVertex === "" || toVertex === "") return; |
| 23 | + if (items.some((item) => item[0] == fromVertex && item[1] == toVertex)) |
| 24 | + return; |
| 25 | + const newItems = [...items]; |
| 26 | + newItems.push([fromVertex, toVertex]); |
| 27 | + setItems(newItems); |
| 28 | + onChange?.(newItems); |
| 29 | + }; |
26 | 30 |
|
27 | | - const itemRemoved = (fromVertex: string, toVertex: string) => { |
28 | | - const newItems = [...items]; |
29 | | - newItems.splice(newItems.findIndex((item) => item[0] == fromVertex && item[1] == toVertex), 1); |
30 | | - setItems(newItems); |
31 | | - onChange?.(newItems); |
32 | | - } |
| 31 | + const itemRemoved = (fromVertex: string, toVertex: string) => { |
| 32 | + const newItems = [...items]; |
| 33 | + newItems.splice( |
| 34 | + newItems.findIndex( |
| 35 | + (item) => item[0] == fromVertex && item[1] == toVertex, |
| 36 | + ), |
| 37 | + 1, |
| 38 | + ); |
| 39 | + setItems(newItems); |
| 40 | + onChange?.(newItems); |
| 41 | + }; |
33 | 42 |
|
34 | | - const fromRef = React.createRef<HTMLSelectElement>(); |
35 | | - const toRef = React.createRef<HTMLSelectElement>(); |
| 43 | + const fromRef = React.createRef<HTMLSelectElement>(); |
| 44 | + const toRef = React.createRef<HTMLSelectElement>(); |
36 | 45 |
|
37 | | - return ( |
38 | | - // JSX markup goes here |
39 | | - <div> |
40 | | - <h1>{title}</h1> |
41 | | - <div className='flex flex-row gap-4 mb-2'> |
42 | | - <select ref={fromRef} className="border-2 rounded-sm p-1"> |
43 | | - { |
44 | | - allVertices.map((vertex, index) => ( |
45 | | - <option key={index}>{vertex}</option> |
46 | | - )) |
47 | | - } |
48 | | - </select> |
49 | | - <span className="pb-1 pt-1">➔</span> |
50 | | - <select ref={toRef} className="border-2 rounded-sm p-1"> |
51 | | - { |
52 | | - allVertices.map((vertex, index) => ( |
53 | | - <option key={index}>{vertex}</option> |
54 | | - )) |
55 | | - } |
56 | | - </select> |
57 | | - <button className='border-2 rounded-sm bg-slate-100 p-1 hover:bg-slate-200 active:bg-slate-300' |
58 | | - onClick={() => itemAdded(fromRef.current?.value ?? "", toRef.current?.value ?? "")}>Add</button> |
59 | | - </div> |
60 | | - <div className={`grid grid-cols-${cols} gap-4`}> |
61 | | - { |
62 | | - items.map((item, index) => ( |
63 | | - <EdgeDisplay |
64 | | - onClick={(fromVertex, toVertex) => itemRemoved?.(fromVertex, toVertex)} |
65 | | - fromVertex={item[0]} |
66 | | - toVertex={item[1]} |
67 | | - key={index}> |
68 | | - </EdgeDisplay> |
69 | | - )) |
70 | | - } |
71 | | - </div> |
72 | | - </div> |
73 | | - ); |
| 46 | + return ( |
| 47 | + // JSX markup goes here |
| 48 | + <div> |
| 49 | + <h1>{title}</h1> |
| 50 | + <div className="flex flex-row gap-4 mb-2"> |
| 51 | + <select ref={fromRef} className="border-2 rounded-sm p-1"> |
| 52 | + {allVertices.map((vertex, index) => ( |
| 53 | + <option key={index}>{vertex}</option> |
| 54 | + ))} |
| 55 | + </select> |
| 56 | + <span className="pb-1 pt-1">➔</span> |
| 57 | + <select ref={toRef} className="border-2 rounded-sm p-1"> |
| 58 | + {allVertices.map((vertex, index) => ( |
| 59 | + <option key={index}>{vertex}</option> |
| 60 | + ))} |
| 61 | + </select> |
| 62 | + <button |
| 63 | + className="border-2 rounded-sm bg-slate-100 p-1 hover:bg-slate-200 active:bg-slate-300" |
| 64 | + onClick={() => |
| 65 | + itemAdded(fromRef.current?.value ?? "", toRef.current?.value ?? "") |
| 66 | + } |
| 67 | + > |
| 68 | + Add |
| 69 | + </button> |
| 70 | + </div> |
| 71 | + <div className={`grid grid-cols-${cols} gap-4`}> |
| 72 | + {items.map((item, index) => ( |
| 73 | + <EdgeDisplay |
| 74 | + onClick={(fromVertex, toVertex) => |
| 75 | + itemRemoved?.(fromVertex, toVertex) |
| 76 | + } |
| 77 | + fromVertex={item[0]} |
| 78 | + toVertex={item[1]} |
| 79 | + key={index} |
| 80 | + ></EdgeDisplay> |
| 81 | + ))} |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + ); |
74 | 85 | }; |
75 | 86 |
|
76 | 87 | export default EdgeCollector; |
0 commit comments