-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionInputAndList.js
More file actions
98 lines (92 loc) · 2.8 KB
/
QuestionInputAndList.js
File metadata and controls
98 lines (92 loc) · 2.8 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
95
96
97
98
import styled from "styled-components";
import { TextInput } from "../style/styledComponents";
import { useEffect, useState } from "react";
import useSolutionListValue from "../hooks/solutionList/useSolutionListValue";
import { createFuzzyMatcher } from "./utils/createFuzzyMatcher";
const QuestionInputAndList = ({ onQuestionNameChange = () => {} }) => {
const [questionName, setQuestionName] = useState("");
const [questionList, setQuestionList] = useState([]);
const [isQuestionListVisible, setIsQuestionListVisible] = useState(false);
const solutionList = useSolutionListValue();
useEffect(() => {
setQuestionList(solutionList);
}, []);
useEffect(() => {
onQuestionNameChange(questionName);
}, [questionName]);
function handleQuestionNameInput(e) {
const inputValue = e.target.value;
setQuestionName(inputValue);
const findMatchedNameRegex = createFuzzyMatcher(inputValue);
setQuestionList(
solutionList.filter((solution) =>
findMatchedNameRegex.test(solution.name)
)
);
if (!isQuestionListVisible) setIsQuestionListVisible(true);
}
function handleQuestionNameBlur(e) {
setIsQuestionListVisible(false);
}
function handleQuestionNameFocus(e) {
setIsQuestionListVisible(true);
}
function handleQuestionClick(e) {
setIsQuestionListVisible(false);
setQuestionName(e.target.dataset.value);
}
return (
<>
<TextInput
id="questionNameInput"
placeholder="문제 이름을 검색하세요."
defaultValue={questionName}
value={questionName}
onFocus={handleQuestionNameFocus}
onInput={handleQuestionNameInput}
/>
{isQuestionListVisible && (
<QuestionList id="questionsList">
{questionList.map((value, index) => (
<QuestionItem key={value.name + index}>
<QuestionBtn
onClick={handleQuestionClick}
data-value={value.name}
>
{value.name} / Level {value.level}
</QuestionBtn>
</QuestionItem>
))}
</QuestionList>
)}
</>
);
};
export default QuestionInputAndList;
export const QuestionList = styled.ul`
//display: none;
position: absolute;
top: 20rem;
left: 0;
width: 100%;
height: 33.2rem;
background-color: ${(props) => props.theme.searchBg};
overflow: scroll;
z-index: 10;
`;
export const QuestionItem = styled.li``;
export const QuestionBtn = styled.div`
width: 100%;
height: 9rem;
text-align: left;
line-height: 9rem;
text-indent: 2rem;
background-color: transparent;
font-size: 3.1rem;
color: ${(props) => props.theme.basicWhite};
border-bottom: 1px solid ${(props) => props.theme.notSelectedTab};
cursor: pointer;
&:hover {
background-color: ${(props) => props.theme.programmersBlue};
}
`;