-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCostTable.js
113 lines (106 loc) · 3 KB
/
CostTable.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
113
import React, { useState } from "react";
import "./CostTable.css";
import EssayForm from './Calc';
const _defaultCosts = [
{
name: "maggie",
qty:2,
price: 12,
},
{
name: "mushroom",
qty:1,
price: 85,
},
{
name: "paneer",
qty:2,
price: 70,
}
];
const CostTable = () => {
const [costs, setCosts] = useState(_defaultCosts);
const handleCostsChange = event => {
const _tempCosts = [...costs];
_tempCosts[event.target.dataset.id][event.target.name] = event.target.value;
setCosts(_tempCosts);
};
const addNewCost = () => {
setCosts(prevCosts => [...prevCosts, { name: "", price: 0 }]);
};
const getTotalCosts = () => {
return costs.reduce((total, item) => {
return total + Number(item.price);
}, 0);
};
return (
<div className="table">
<div className="table-title"><h1>FOOD COSTS</h1></div>
<div className="table-content">
<div className="table-header">
<div className="table-row">
<div className="table-data">
<div><h3>ITEM</h3></div>
</div>
<div className="table-data">
<div><h3>NO OF QTY</h3></div>
</div>
<div className="table-data">
<div><h3>PRICE</h3></div>
</div>
</div>
</div>
<div className="table-body">
{costs.map((item, index) => (
<div className="table-row" key={index}>
<div className="table-data">
<input
name="name"
data-id={index}
type="text"
value={item.name}
onChange={handleCostsChange}
/>
</div>
<div className="table-data">
<input
name="qty"
data-id={index}
type="number"
value={item.qty}
onChange={handleCostsChange}
/>
</div>
<div className="table-data">
<input
name="price"
data-id={index}
type="number"
value={item.price}
onChange={handleCostsChange}
/>
</div>
</div>
))}
<div className="table-row">
<div className="table-data">
<button onClick={addNewCost}><b>ADD ANOTHER PRODUCT</b></button>
</div>
</div>
</div>
<div className="table-footer">
<div className="table-row">
<div className="table-data">
<div>Total</div>
</div>
<div className="table-data">
<div>{getTotalCosts()}</div>
</div>
</div>
</div>
<EssayForm />
</div>
</div>
);
};
export default CostTable;