-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter.jsx
More file actions
56 lines (51 loc) · 1.36 KB
/
Counter.jsx
File metadata and controls
56 lines (51 loc) · 1.36 KB
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
import React, { useState } from 'react';
import { Button } from './Button';
export const Counter = () => {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(1);
// query max from stock
const max=31
const add = () => {
// setCount(count + 1)
if (count+1 <= max) {
setCount(count + 1)
};
};
const add10 = () => {
if (count+10 <= max) {
setCount(count + 10)
}
else{
setCount(max)
};
};
const sub = () => {
if (count > 1) {
setCount(count - 1)
};
};
const sub10 = () => {
if (count > 10) {
setCount(count - 10)
}
else{
setCount(1)
};
};
return (
<>
<div className="">
<div className="grid grid-cols-5 gap-2 justify-items-center w-full px-4">
<Button type="count" logo="/minus.svg" text="10" onClick={sub10} />
<Button type="count" logo="/minus.svg" onClick={sub} />
<div className="text-center w-10">
<p className="align-center text-xl font-semibold">{count}</p>
</div>
<Button type="count" logo="/add.svg" onClick={add} />
<Button type="count" logo="/add.svg" text="10" onClick={add10} />
</div>
<p className="text-center text-sm text-red-600 font-medium pt-1">คงเหลือ {max} ชิ้น</p>
</div>
</>
);
}