|
| 1 | +import { useRef, useState } from "react"; |
| 2 | + |
| 3 | +import { useGetTags } from "@/lib/tanstack/query/tag"; |
| 4 | +import type { ContentSchemaType } from "@/schemas/content"; |
| 5 | +import { Button, infoToast, Input } from "@linkyboard/components"; |
| 6 | +import { useOutsideClick } from "@linkyboard/hooks"; |
| 7 | + |
| 8 | +import { Plus, X } from "lucide-react"; |
| 9 | +import type { UseFormReturn } from "react-hook-form"; |
| 10 | + |
| 11 | +interface SelectTagProps { |
| 12 | + form: UseFormReturn<ContentSchemaType>; |
| 13 | +} |
| 14 | + |
| 15 | +export default function SelectTag({ form }: SelectTagProps) { |
| 16 | + const [isTagDropdownOpen, setIsTagDropdownOpen] = useState(false); |
| 17 | + const [newTag, setNewTag] = useState(""); |
| 18 | + |
| 19 | + const tagInputRef = useRef<HTMLInputElement>(null); |
| 20 | + |
| 21 | + const [tagFormRef] = useOutsideClick<HTMLFormElement>(() => { |
| 22 | + setIsTagDropdownOpen(false); |
| 23 | + }); |
| 24 | + |
| 25 | + const { data: allTags } = useGetTags(); |
| 26 | + |
| 27 | + const watchedTags = form.watch("tags"); |
| 28 | + |
| 29 | + // 필터링된 태그 계산 |
| 30 | + const filteredTags = allTags?.filter( |
| 31 | + (tag) => |
| 32 | + !watchedTags.includes(tag.name) && tag.name.toLowerCase().includes(newTag.toLowerCase()) |
| 33 | + ); |
| 34 | + |
| 35 | + const onAddTag = (e: React.FormEvent<HTMLFormElement>) => { |
| 36 | + e.preventDefault(); |
| 37 | + |
| 38 | + const trimmedTag = newTag.trim(); |
| 39 | + |
| 40 | + if (watchedTags.includes(trimmedTag)) { |
| 41 | + return infoToast("이미 존재하는 태그입니다."); |
| 42 | + } |
| 43 | + |
| 44 | + onUpdateTag(trimmedTag); |
| 45 | + }; |
| 46 | + |
| 47 | + const onUpdateTag = (tag: string) => { |
| 48 | + const newTags = [...watchedTags, tag]; |
| 49 | + form.setValue("tags", newTags); |
| 50 | + setNewTag(""); |
| 51 | + }; |
| 52 | + |
| 53 | + const onRemoveTag = (index: number) => { |
| 54 | + const newTags = watchedTags.filter((_, i) => i !== index); |
| 55 | + form.setValue("tags", newTags); |
| 56 | + tagInputRef.current?.focus(); |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <div> |
| 61 | + <h2 className="text-muted-foreground mb-2 text-sm font-medium">태그</h2> |
| 62 | + <div className="space-y-3"> |
| 63 | + {/* 기존 태그들 */} |
| 64 | + {watchedTags.length > 0 && ( |
| 65 | + <div className="flex flex-wrap gap-2"> |
| 66 | + {watchedTags.map((tag, index) => ( |
| 67 | + <button |
| 68 | + key={index} |
| 69 | + type="button" |
| 70 | + className="bg-primary/10 text-primary hover:bg-primary/20 flex items-center space-x-1 rounded-full px-3 py-1 text-sm transition-all duration-200 hover:scale-105" |
| 71 | + onClick={() => onRemoveTag(index)} |
| 72 | + aria-label={`${tag} 태그 제거`} |
| 73 | + > |
| 74 | + <span>{tag}</span> |
| 75 | + <X className="h-3 w-3" /> |
| 76 | + </button> |
| 77 | + ))} |
| 78 | + </div> |
| 79 | + )} |
| 80 | + |
| 81 | + {/* 새 태그 추가 */} |
| 82 | + <div className="space-y-2"> |
| 83 | + <form ref={tagFormRef} className="relative flex items-center gap-2" onSubmit={onAddTag}> |
| 84 | + <Input |
| 85 | + ref={tagInputRef} |
| 86 | + value={newTag} |
| 87 | + onChange={(e) => setNewTag(e.target.value)} |
| 88 | + placeholder="새 태그 입력" |
| 89 | + className="flex-1" |
| 90 | + onFocus={() => setIsTagDropdownOpen(true)} |
| 91 | + /> |
| 92 | + <Button |
| 93 | + type="submit" |
| 94 | + size="sm" |
| 95 | + variant="outline" |
| 96 | + aria-label="태그 추가" |
| 97 | + className="aspect-square h-full shrink-0" |
| 98 | + > |
| 99 | + <Plus /> |
| 100 | + </Button> |
| 101 | + {isTagDropdownOpen && ( |
| 102 | + <div className="absolute -bottom-2 max-h-40 w-full translate-y-full overflow-auto rounded-md border bg-white shadow-lg"> |
| 103 | + {!filteredTags || filteredTags.length === 0 ? ( |
| 104 | + <p className="text-muted-foreground block w-full px-3 py-2 text-left"> |
| 105 | + 태그가 없어요. |
| 106 | + </p> |
| 107 | + ) : ( |
| 108 | + filteredTags.map((tag) => ( |
| 109 | + <button |
| 110 | + key={`${tag.id}-${tag.name}`} |
| 111 | + type="button" |
| 112 | + className="hover:bg-accent text-foreground line-clamp-1 block w-full px-3 py-2 text-left" |
| 113 | + onClick={() => onUpdateTag(tag.name)} |
| 114 | + > |
| 115 | + <span>{tag.name}</span> |
| 116 | + </button> |
| 117 | + )) |
| 118 | + )} |
| 119 | + </div> |
| 120 | + )} |
| 121 | + </form> |
| 122 | + <p className="text-muted-foreground text-xs"> |
| 123 | + Enter 혹은 + 버튼을 통해 태그를 추가할 수 있어요. |
| 124 | + </p> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + ); |
| 129 | +} |
0 commit comments