|
1 | | -import React, { useState } from 'react'; |
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
2 | 2 | import styles from './Tag.module.css'; |
3 | 3 | import { getTagColors, getIsClickable, tagList } from '../../utils/buttonContentList'; |
4 | 4 | import DropBoxButton from '../buttons/dropBoxButton/DropBoxButton'; |
5 | 5 |
|
6 | | -export default function Tag({ title, onTagChange }) { |
| 6 | +export default function Tag({ title }) { |
7 | 7 | const tagColors = getTagColors(title); |
8 | 8 | const isClickable = getIsClickable(title); |
9 | 9 |
|
10 | 10 | // TODO : 관리자 페이지의 학습자 관리 tag에만 사용 |
11 | 11 | const [isOpen, setIsOpen] = useState(false); |
| 12 | + const containerRef = useRef(null); |
12 | 13 |
|
13 | | - const handleOpenDropBox = () => { |
14 | | - setIsOpen(true); |
| 14 | + const handleToggleDropBox = () => { |
| 15 | + setIsOpen((prev) => !prev); |
15 | 16 | }; |
16 | 17 |
|
17 | 18 | const handleCloseDropBox = () => { |
18 | 19 | setIsOpen(false); |
19 | 20 | }; |
20 | 21 |
|
21 | 22 | const handleTagClick = (newTagTitle) => { |
22 | | - onTagChange(newTagTitle); |
| 23 | + // TODO : patch 요청 |
23 | 24 | setIsOpen(false); |
24 | 25 | }; |
25 | 26 |
|
26 | 27 | const dropBoxButtonList = tagList.map((item, index) => ( |
27 | 28 | <DropBoxButton key={index} title={item} handleClick={() => handleTagClick(item)} /> |
28 | 29 | )); |
29 | 30 |
|
| 31 | + useEffect(() => { |
| 32 | + const handleClickOutside = (event) => { |
| 33 | + if (containerRef.current && !containerRef.current.contains(event.target)) { |
| 34 | + setIsOpen(false); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + document.addEventListener('mousedown', handleClickOutside); |
| 39 | + return () => { |
| 40 | + document.removeEventListener('mousedown', handleClickOutside); |
| 41 | + }; |
| 42 | + }, []); |
| 43 | + |
30 | 44 | return ( |
31 | | - <div className={styles.tagBox}> |
| 45 | + <div ref={containerRef} className={styles.tagBox}> |
32 | 46 | <button |
33 | 47 | disabled={!isClickable} |
34 | | - onClick={handleOpenDropBox} |
35 | | - // onBlur={handleCloseDropBox} |
| 48 | + onClick={handleToggleDropBox} |
36 | 49 | className={`${styles.tag} ${tagColors ? styles[tagColors] : ''}`} |
37 | 50 | > |
38 | 51 | {title} |
|
0 commit comments