CountButton.js 1.12 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import {useState} from 'react'
Jiwon Yoon's avatar
Jiwon Yoon committed
2
3
const CountButton = ({count, setCount}) => {
    const name = name
Jiwon Yoon's avatar
Jiwon Yoon committed
4
5
    function handleCount(event) {
        if (event.target.name === "backbutton") {
Jiwon Yoon's avatar
Jiwon Yoon committed
6
            setCount({...count, [name] :count[name] - 1})
Jiwon Yoon's avatar
Jiwon Yoon committed
7
        } else if (event.target.name === "forwardbutton") {
Jiwon Yoon's avatar
Jiwon Yoon committed
8
            setCount({...count, [name] :count[name] + 1})
Jiwon Yoon's avatar
Jiwon Yoon committed
9
        } else {
Jiwon Yoon's avatar
Jiwon Yoon committed
10
            setCount({...count, [name] :event.target.value})
Jiwon Yoon's avatar
Jiwon Yoon committed
11
12
13
14

        }
    }
    return (
Jiwon Yoon's avatar
Jiwon Yoon committed
15
        <div className="">
Jiwon Yoon's avatar
Jiwon Yoon committed
16
17
18
            <button type="button" name="backbutton" style={{ backgroundColor: "black", border: "0" }} onClick={handleCount}>
                <img src="/images/icons8-back-24.png" name="backbutton" alt="<" />
            </button>
Jiwon Yoon's avatar
Jiwon Yoon committed
19
            <input type='number' onChange={handleCount} value={count[name]} style={{ width: '2rem' }} className="text-center" />
Jiwon Yoon's avatar
Jiwon Yoon committed
20
21
22
            <button type="button" name="forwardbutton" min="0" style={{ backgroundColor: "black", border: "0" }} onClick={handleCount}>
                <img src="/images/icons8-forward-24.png" name="forwardbutton" alt=">" />
            </button>
Jiwon Yoon's avatar
Jiwon Yoon committed
23
        </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
24
25
26
27
    )
}

export default CountButton