-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuForm.js
112 lines (101 loc) · 3 KB
/
MenuForm.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { useState, useEffect } from "react";
import {
FormControl,
FormLabel,
Select,
Input,
Container,
Button,
Modal,
ModalOverlay,
ModalContent,
ModalHeader,
ModalFooter,
ModalBody,
ModalCloseButton,
} from "@chakra-ui/react";
function MenuForm({ handleItem, values, handleClose, buttonText = "Add" }) {
const [inputValues, setInputValues] = useState(values);
useEffect(() => {
setInputValues(values);
}, [values]);
const handleChange = (event) => {
console.log(event.target.value);
setInputValues({
...inputValues,
[event.target.name]: event.target.value,
});
};
function handleSubmit(event) {
event.preventDefault();
// const input = event.target;
// console.log("name:", input.name);
handleItem(inputValues);
}
const options = [
{ label: "Early Breakfast", value: "1" },
{ label: "Breakfast", value: "2" },
{ label: "Supper", value: "3" },
{ label: "Beverages", value: "4" },
];
const selectedCategory = options.find(
(op) => inputValues && op.label === inputValues.categoryname
);
return (
<Modal isOpen={true} onClose={handleClose}>
<ModalOverlay />
<ModalContent>
<form onSubmit={handleSubmit}>
<ModalHeader>{buttonText} Menu Item</ModalHeader>
<ModalCloseButton />
<ModalBody>
<Container>
<FormControl onSubmit={handleSubmit} isRequired>
<FormLabel htmlFor="item-name">Item name</FormLabel>
<Input
id="item-name"
data-testid="item-name"
name="itemname"
value={inputValues.itemname}
onChange={handleChange}
/>
<FormLabel htmlFor="price">Price</FormLabel>
<Input
id="price"
data-testid="price"
name="price"
value={inputValues.price}
onChange={handleChange}
/>
<FormLabel htmlFor="categoryId">Select Category</FormLabel>
<Select
id="categoryId"
placeholder="Select option"
data-testid="select-category"
name="categoryid"
value={(selectedCategory || {}).value}
onChange={handleChange}
>
{options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</Select>
</FormControl>
</Container>
</ModalBody>
<ModalFooter>
<Button colorScheme="blue" type="submit">
{buttonText}
</Button>
<Button variant="ghost" onClick={handleClose}>
Cancel
</Button>
</ModalFooter>
</form>
</ModalContent>
</Modal>
);
}
export default MenuForm;