-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from codestates-seb/feat/tag-haeun
[구현]Feat/tag기능 구현 완료했습니다.
- Loading branch information
Showing
7 changed files
with
141 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,40 @@ | ||
const TagDropdown = ({ tags }: { tags: string }) => { | ||
import styled from "styled-components"; | ||
|
||
const TagDropdown = ({ | ||
defaultTags, | ||
tags, | ||
setTags, | ||
}: { | ||
defaultTags: string[]; | ||
tags: string[]; | ||
setTags: React.Dispatch<React.SetStateAction<string[]>>; | ||
}) => { | ||
const handleTagClick = (item: string) => { | ||
if (tags.includes(item)) { | ||
alert("이미 존재하는 태그입니다."); | ||
} else { | ||
setTags([...tags, item]); | ||
} | ||
}; | ||
return ( | ||
<> | ||
<li>{tags}</li> | ||
</> | ||
<TagLiDiv> | ||
{defaultTags.map((defaultTag) => { | ||
return ( | ||
<li onClick={() => handleTagClick(defaultTag)} key={defaultTag}> | ||
{defaultTag} | ||
</li> | ||
); | ||
})} | ||
</TagLiDiv> | ||
); | ||
}; | ||
const TagLiDiv = styled.div` | ||
list-style-type: none; | ||
background-color: white; | ||
border-radius: 5px; | ||
padding: 5px 15px; | ||
cursor: pointer; | ||
margin-top: 5px; | ||
font-size: 0.7rem; | ||
`; | ||
export default TagDropdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,106 @@ | ||
import { useEffect, useState } from "react"; | ||
import styled from "styled-components"; | ||
import TagDropdown from "./TagDropdown"; | ||
import { StudyInfoDto } from "../apis/StudyGroupApi"; | ||
import { eduApi } from "../apis/EduApi"; | ||
|
||
const TagInput = ({ selectedCategory }: { selectedCategory: string }) => { | ||
const TagInput = ({ | ||
selectedCategory, | ||
tags, | ||
setTags, | ||
}: { | ||
selectedCategory: string; | ||
tags: string[]; | ||
setTags: React.Dispatch<React.SetStateAction<string[]>>; | ||
}) => { | ||
const [view, setView] = useState(false); | ||
|
||
const [tag, setTag] = useState<{ [key: string]: string }>({}); | ||
const [defaultTag, setDefaultTag] = useState<{ [key: string]: string }>({}); | ||
const [createdTag, setCreatedTag] = useState<string>(""); | ||
|
||
const handleTag = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setCreatedTag(e.target.value); | ||
}; | ||
|
||
const handleTagPost = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === "Enter") { | ||
e.preventDefault(); | ||
if (tags.includes(createdTag)) { | ||
alert("이미 존재하는 태그입니다."); | ||
} else if (createdTag) { | ||
setTags([...tags, createdTag]); | ||
setCreatedTag(""); | ||
} | ||
} | ||
}; | ||
|
||
const handleDelete = (tag: string) => { | ||
const updatedTags = tags.filter((clickedTag) => clickedTag !== tag); | ||
setTags(updatedTags); | ||
}; | ||
|
||
useEffect(() => { | ||
setView(false); | ||
setTags([]); | ||
setCreatedTag(""); | ||
const fetchData = async () => { | ||
try { | ||
const response = await eduApi.get<StudyInfoDto>( | ||
`/search?key=${selectedCategory}` | ||
); | ||
const result = response.data.tags; | ||
setTag(result); | ||
setDefaultTag(result); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
fetchData(); | ||
}, []); | ||
}, [selectedCategory]); | ||
|
||
return ( | ||
<> | ||
<input type="text" /*value={tag} onChange={}*/ /> | ||
<input | ||
type="text" | ||
value={createdTag} | ||
onChange={handleTag} | ||
onKeyDown={handleTagPost} | ||
/> | ||
|
||
<ul | ||
onClick={() => { | ||
setView(!view); | ||
}} | ||
> | ||
{selectedCategory} {view ? "⌃" : "⌄"} | ||
{view && tag && <TagDropdown tags={tag[selectedCategory]} />} | ||
{view && defaultTag && ( | ||
<TagDropdown | ||
defaultTags={[defaultTag[selectedCategory]]} | ||
setTags={setTags} | ||
tags={tags} | ||
/> | ||
)} | ||
</ul> | ||
<div> | ||
{tags.map((tag) => { | ||
return ( | ||
<StudyTag onClick={() => handleDelete(tag)} key={tag}> | ||
{tag} | ||
</StudyTag> | ||
); | ||
})} | ||
</div> | ||
</> | ||
); | ||
}; | ||
const StudyTag = styled.div` | ||
height: 24px; | ||
color: #39739d; | ||
font-size: 0.8125rem; | ||
border-radius: 4px; | ||
background-color: #e1ecf4; | ||
padding: 2px 6px 5px 6px; | ||
margin-right: 7px; | ||
cursor: pointer; | ||
`; | ||
export default TagInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters