Skip to content

Commit 8ecce16

Browse files
committed
[EC-220] fix: 버튼 수정
1 parent 02b8591 commit 8ecce16

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

client/src/components/tag/Tag.jsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useRef, useState } from 'react';
22
import styles from './Tag.module.css';
33
import { getTagColors, getIsClickable, tagList } from '../../utils/buttonContentList';
44
import DropBoxButton from '../buttons/dropBoxButton/DropBoxButton';
55

6-
export default function Tag({ title, onTagChange }) {
6+
export default function Tag({ title }) {
77
const tagColors = getTagColors(title);
88
const isClickable = getIsClickable(title);
99

1010
// TODO : 관리자 페이지의 학습자 관리 tag에만 사용
1111
const [isOpen, setIsOpen] = useState(false);
12+
const containerRef = useRef(null);
1213

13-
const handleOpenDropBox = () => {
14-
setIsOpen(true);
14+
const handleToggleDropBox = () => {
15+
setIsOpen((prev) => !prev);
1516
};
1617

1718
const handleCloseDropBox = () => {
1819
setIsOpen(false);
1920
};
2021

2122
const handleTagClick = (newTagTitle) => {
22-
onTagChange(newTagTitle);
23+
// TODO : patch 요청
2324
setIsOpen(false);
2425
};
2526

2627
const dropBoxButtonList = tagList.map((item, index) => (
2728
<DropBoxButton key={index} title={item} handleClick={() => handleTagClick(item)} />
2829
));
2930

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+
3044
return (
31-
<div className={styles.tagBox}>
45+
<div ref={containerRef} className={styles.tagBox}>
3246
<button
3347
disabled={!isClickable}
34-
onClick={handleOpenDropBox}
35-
// onBlur={handleCloseDropBox}
48+
onClick={handleToggleDropBox}
3649
className={`${styles.tag} ${tagColors ? styles[tagColors] : ''}`}
3750
>
3851
{title}

0 commit comments

Comments
 (0)