SideSubject.js
2.51 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { useState } from "react";
const SideSubject = ({ subject, dispatch }) => {
const defaultColor = "#EFEFEF";
const [state, setState] = useState({
nickname: subject.nickname || subject.name,
color: subject.color,
});
const handleChangeState = (e) => {
setState({
...state,
[e.target.name]: e.target.value,
});
};
const check = (e) => {
dispatch({ type: "CHECKED", subjectID: subject.subjectID });
if (subject.status) e.target.style["background-color"] = defaultColor;
else e.target.style["background-color"] = subject.color;
};
const labelClick = (e) => {
e.target.nextSibling.style.display = "flex";
};
const popupClose = (e) => {
setState({
nickname: subject.nickname || subject.name,
color: subject.color,
});
e.target.offsetParent.style.display = "none";
};
const popupApply = (e) => {
if (state.nickname.length < 1) alert("이름을 입력해 주세요");
else if (state.color.length !== 6) alert("색상은 6자리 16진수 값 입니다");
else {
let check = true;
for (const c of state.color) if (isNaN(parseInt(c, 16))) check = false;
if (!check) alert("색상은 16진수 값 입니다");
else {
dispatch({
type: "MODIFY",
subjectID: subject.subjectID,
nickname: state.nickname,
color: state.color,
});
e.target.offsetParent.style.display = "none";
}
}
};
return (
<div className="SideSubject">
<div
className="ssc"
onClick={check}
style={{
backgroundColor: subject.status ? "#" + subject.color : defaultColor,
}}
></div>
<span className="ssl" onClick={labelClick}>
{subject.nickname || subject.name}
</span>
<div className="ss_popup">
<div className="sspd">
<div className="sspd_1">
<span>이름</span>
<input
name="nickname"
value={state.nickname}
onChange={handleChangeState}
/>
</div>
<div className="sspd_2">
<span>색상</span>
<input
name="color"
value={state.color}
onChange={handleChangeState}
/>
</div>
</div>
<div className="ssp_btns">
<button onClick={popupApply}>적용 </button>
<button onClick={popupClose}>취소</button>
</div>
</div>
</div>
);
};
export default SideSubject;