|
1
|
import { |
|
2
|
Button, |
|
3
|
FocusModal, |
|
4
|
Heading, |
|
5
|
Table, |
|
6
|
useToggleState, |
|
7
|
} from "@medusajs/ui" |
|
8
|
import { useState } from "react" |
|
9
|
|
|
10
|
export default function useToggleStateDemo() { |
|
11
|
const [editOpen, showEdit, closeEdit] = useToggleState() |
|
12
|
const [entityToEdit, setEntityToEdit] = useState<string>() |
|
13
|
|
|
14
|
const entities = ["foo", "bar", "baz"] |
|
15
|
|
|
16
|
const editEntity = (entity: string) => { |
|
17
|
setEntityToEdit(entity) |
|
18
|
showEdit() |
|
19
|
} |
|
20
|
|
|
21
|
const onSave = () => { |
|
22
|
// do entity update, etc. |
|
23
|
closeEdit() |
|
24
|
} |
|
25
|
|
|
26
|
return ( |
|
27
|
<> |
|
28
|
<Table> |
|
29
|
<Table.Header> |
|
30
|
<Table.Row> |
|
31
|
<Table.HeaderCell>Entity Name</Table.HeaderCell> |
|
32
|
<Table.HeaderCell className="text-right">Actions</Table.HeaderCell> |
|
33
|
</Table.Row> |
|
34
|
</Table.Header> |
|
35
|
<Table.Body> |
|
36
|
{entities.map((entity, index) => ( |
|
37
|
<Table.Row key={index}> |
|
38
|
<Table.Cell>{entity}</Table.Cell> |
|
39
|
<Table.Cell className="text-right"> |
|
40
|
<Button variant="secondary" onClick={() => editEntity(entity)}> |
|
41
|
Edit |
|
42
|
</Button> |
|
43
|
</Table.Cell> |
|
44
|
</Table.Row> |
|
45
|
))} |
|
46
|
</Table.Body> |
|
47
|
</Table> |
|
48
|
<FocusModal |
|
49
|
open={editOpen} |
|
50
|
onOpenChange={(modalOpened) => { |
|
51
|
if (!modalOpened) { |
|
52
|
closeEdit() |
|
53
|
} |
|
54
|
}} |
|
55
|
> |
|
56
|
<FocusModal.Content> |
|
57
|
<FocusModal.Header> |
|
58
|
<FocusModal.Title>Edit Entity</FocusModal.Title> |
|
59
|
</FocusModal.Header> |
|
60
|
<FocusModal.Body> |
|
61
|
<div className="p-10"> |
|
62
|
<Heading>Edit {entityToEdit}</Heading> |
|
63
|
</div> |
|
64
|
</FocusModal.Body> |
|
65
|
<FocusModal.Footer> |
|
66
|
<Button onClick={() => onSave()}>Save</Button> |
|
67
|
</FocusModal.Footer> |
|
68
|
</FocusModal.Content> |
|
69
|
</FocusModal> |
|
70
|
</> |
|
71
|
) |
|
72
|
} |