From 082e01599bc1e53b18f22a4cfed08834f2a291d5 Mon Sep 17 00:00:00 2001 From: Wind-Explorer Date: Wed, 14 Aug 2024 14:52:43 +0800 Subject: [PATCH] Propered forums tag component --- client/src/components/TagInput.tsx | 125 ++++++++++++++--------------- 1 file changed, 62 insertions(+), 63 deletions(-) diff --git a/client/src/components/TagInput.tsx b/client/src/components/TagInput.tsx index 8588367..80217ce 100644 --- a/client/src/components/TagInput.tsx +++ b/client/src/components/TagInput.tsx @@ -1,78 +1,77 @@ import React, { useState } from "react"; -import { Button } from "@nextui-org/react"; +import { Button, Chip } from "@nextui-org/react"; import NextUIFormikTagInput from "./NextUIFormikTagInput"; type TagInputProps = { - tags: string[]; - setTags: (tags: string[]) => void; + tags: string[]; + setTags: (tags: string[]) => void; }; const TagInput: React.FC = ({ tags, setTags }) => { - const [inputTag, setInputTag] = useState(""); - const [error, setError] = useState(null); + const [inputTag, setInputTag] = useState(""); + const [error, setError] = useState(null); - // Handle input change and dynamic duplicate check - const handleInputChange = (e: React.ChangeEvent) => { - const newTag = e.target.value.trim(); - setInputTag(newTag); + // Handle input change and dynamic duplicate check + const handleInputChange = (e: React.ChangeEvent) => { + const newTag = e.target.value.trim(); + setInputTag(newTag); - // Dynamic duplicate check - if (tags.some(tag => tag.toLowerCase() === newTag.toLowerCase())) { - setError("Tag already added."); - } else { - setError(null); - } - }; + // Dynamic duplicate check + if (tags.some((tag) => tag.toLowerCase() === newTag.toLowerCase())) { + setError("Tag already added."); + } else { + setError(null); + } + }; - const handleAddTag = () => { - if (error) { - return; // Prevent adding if there's an error - } + const handleAddTag = () => { + if (error) { + return; // Prevent adding if there's an error + } - if (inputTag.trim() !== "") { - setTags([...tags, inputTag.trim()]); - setInputTag(""); // Clear input field - } - }; + if (inputTag.trim() !== "") { + setTags([...tags, inputTag.trim()]); + setInputTag(""); // Clear input field + } + }; - const handleRemoveTag = (index: number) => { - setTags(tags.filter((_, i) => i !== index)); - }; + const handleRemoveTag = (index: number) => { + setTags(tags.filter((_, i) => i !== index)); + }; - return ( -
-
- - {error &&
{error}
} - -
-
- {tags.map((tag, index) => ( -
- {tag} - -
- ))} -
-
- ); + return ( +
+
+ + {error &&
{error}
} + +
+
+ {tags.map((tag, index) => ( + handleRemoveTag(index)} + > + {tag} + + ))} +
+
+ ); }; -export default TagInput; \ No newline at end of file +export default TagInput;