|
| 1 | +import { BiX } from '@react-icons/all-files/bi/BiX'; |
| 2 | +import classNames from 'classnames/bind'; |
| 3 | +import React, { useState, useRef } from 'react'; |
| 4 | + |
| 5 | +import Button from '../common/Button'; |
| 6 | +import style from './style.module.scss'; |
| 7 | + |
| 8 | +const cx = classNames.bind(style); |
| 9 | + |
| 10 | +interface Option { |
| 11 | + id: number; |
| 12 | + option: string; |
| 13 | +} |
| 14 | + |
| 15 | +const initialOption: Option[] = [ |
| 16 | + { id: 1, option: '์ง์ฅ๋ฉด' }, |
| 17 | + { |
| 18 | + id: 2, |
| 19 | + option: '์งฌ๋ฝ', |
| 20 | + }, |
| 21 | +]; |
| 22 | + |
| 23 | +function VoteBlock() { |
| 24 | + const [options, setOptions] = useState<Option[]>(initialOption); |
| 25 | + const [isUnRegisterd, setIsUnRegisterd] = useState<boolean>(true); |
| 26 | + const inputRef = useRef<HTMLInputElement>(null); |
| 27 | + |
| 28 | + const onRegister = () => { |
| 29 | + if (!options.length) throw new Error('ํฌํ ํญ๋ชฉ์ ์ต์ 1๊ฐ์์ ^^'); |
| 30 | + |
| 31 | + if (!isUnRegisterd) return; |
| 32 | + |
| 33 | + setIsUnRegisterd(false); |
| 34 | + }; |
| 35 | + |
| 36 | + const onAdd = () => { |
| 37 | + if (!inputRef.current || !inputRef.current.value) return; |
| 38 | + |
| 39 | + const { value: option } = inputRef.current; |
| 40 | + |
| 41 | + const lastId = options.at(-1)?.id; |
| 42 | + const nextId = lastId !== undefined ? lastId + 1 : 0; |
| 43 | + const newOption: Option = { id: nextId, option }; |
| 44 | + |
| 45 | + setOptions([...options, newOption]); |
| 46 | + |
| 47 | + inputRef.current.value = ''; |
| 48 | + }; |
| 49 | + |
| 50 | + const onDelete = (targetId: number) => { |
| 51 | + const filteredOptions = options.filter( |
| 52 | + (option) => option.id !== Number(targetId), |
| 53 | + ); |
| 54 | + |
| 55 | + setOptions(filteredOptions); |
| 56 | + }; |
| 57 | + |
| 58 | + const onClose = () => { |
| 59 | + alert('ํฌํ ์ข
๋ฃ..!'); |
| 60 | + /**ํฌํ ๊ฒฐ๊ณผ ๋ณด์ฌ์ค์ผํจ */ |
| 61 | + setIsUnRegisterd(true); |
| 62 | + }; |
| 63 | + |
| 64 | + return ( |
| 65 | + <div className={style['vote-container']}> |
| 66 | + <h3 className={style.title}>ํฌํ</h3> |
| 67 | + <ul> |
| 68 | + {options.map(({ id, option }, index) => ( |
| 69 | + <li className={style['option-item']} key={id} id={id.toString()}> |
| 70 | + <div className={style['box-fill']}>{index + 1}</div> |
| 71 | + <p className={style.option}>{option}</p> |
| 72 | + {isUnRegisterd && ( |
| 73 | + <div |
| 74 | + className={cx('box-fill', { 'position-right': true })} |
| 75 | + onClick={() => onDelete(id)} |
| 76 | + > |
| 77 | + <BiX size="20" /> |
| 78 | + </div> |
| 79 | + )} |
| 80 | + </li> |
| 81 | + ))} |
| 82 | + </ul> |
| 83 | + {isUnRegisterd && ( |
| 84 | + <div className={style['option-item']}> |
| 85 | + <div className={style['box-fill']}></div> |
| 86 | + <input |
| 87 | + ref={inputRef} |
| 88 | + className={style['option-input']} |
| 89 | + placeholder="ํญ๋ชฉ์ ์
๋ ฅํด์ฃผ์ธ์" |
| 90 | + ></input> |
| 91 | + </div> |
| 92 | + )} |
| 93 | + <div className={style['vote-buttons']}> |
| 94 | + {isUnRegisterd ? ( |
| 95 | + <> |
| 96 | + <Button onClick={onAdd} text="ํญ๋ชฉ ์ถ๊ฐ" /> |
| 97 | + <Button onClick={onRegister} text="ํฌํ ๋ฑ๋ก" /> |
| 98 | + </> |
| 99 | + ) : ( |
| 100 | + <Button onClick={onClose} text="ํฌํ ์ข
๋ฃ" /> |
| 101 | + )} |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +export default VoteBlock; |
0 commit comments