From a050d58e918b5684803559b0545eed09daf0f1d0 Mon Sep 17 00:00:00 2001 From: Meliora Ho Date: Wed, 3 Apr 2024 02:28:04 +0000 Subject: [PATCH] enabled add, delete, and deselect tags --- compass/components/Table/Index.tsx | 4 +- .../components/TagsInput/DropdownAction.tsx | 17 +++++++-- compass/components/TagsInput/Index.tsx | 37 ++++++++++++------- compass/components/TagsInput/Tag.tsx | 8 ++-- compass/components/TagsInput/TagDropdown.tsx | 6 +-- compass/components/TagsInput/TagsArray.tsx | 8 ++-- 6 files changed, 50 insertions(+), 30 deletions(-) diff --git a/compass/components/Table/Index.tsx b/compass/components/Table/Index.tsx index 53a0e13..625dca3 100644 --- a/compass/components/Table/Index.tsx +++ b/compass/components/Table/Index.tsx @@ -12,6 +12,8 @@ import { useState } from "react"; import { RowOptionMenu } from "./RowOptionMenu"; import { RowOpenAction } from "./RowOpenAction"; import { TableAction } from "./TableAction"; +import { AtSymbolIcon, Bars2Icon } from "@heroicons/react/24/solid"; +import TagsInput from "../TagsInput/Index"; const usersExample = usersImport as unknown as User[]; @@ -40,7 +42,7 @@ export const Table = () => { cell: (info) => , }), columnHelper.accessor("role", { - cell: (info) => info.renderValue(), + cell: (info) => , }), columnHelper.accessor("email", { header: () => <> Email, diff --git a/compass/components/TagsInput/DropdownAction.tsx b/compass/components/TagsInput/DropdownAction.tsx index fcddf48..737ec69 100644 --- a/compass/components/TagsInput/DropdownAction.tsx +++ b/compass/components/TagsInput/DropdownAction.tsx @@ -1,7 +1,7 @@ -import { EllipsisHorizontalIcon } from "@heroicons/react/24/solid"; +import { EllipsisHorizontalIcon, TrashIcon } from "@heroicons/react/24/solid"; import { useState } from "react"; -export const DropdownAction = ({ tag }) => { +export const DropdownAction = ({ tag, handleDeleteTag, handleAddTag }) => { const [isVisible, setVisible] = useState(false); const [inputValue, setInputValue] = useState(tag); @@ -9,6 +9,11 @@ export const DropdownAction = ({ tag }) => { setVisible(false); }; + const editTagOption = () => { + handleDeleteTag(tag) + handleAddTag(inputValue) + } + const handleInputChange = (e: React.ChangeEvent) => { setInputValue(e.target.value); }; @@ -21,14 +26,20 @@ export const DropdownAction = ({ tag }) => { return (
{isVisible ? ( -
+
+
) : ( = ({ presetValue, presetOptions }) => { const [inputValue, setInputValue] = useState(''); const [cellSelected, setCellSelected] = useState(false); - const [tags, setTags] = useState([presetValue]); - const [options, setOptions] = useState(presetOptions); + const [tags, setTags] = useState>(new Set(presetValue ? [presetValue] : [])); + const [options, setOptions] = useState>(new Set(presetOptions)); const handleInputChange = (e: React.ChangeEvent) => { setInputValue(e.target.value); @@ -19,43 +19,52 @@ const TagsInput: React.FC = ({ presetValue, presetOptions }) => const handleAddTag = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && inputValue.trim()) { - setTags(prevTags => [...prevTags, inputValue]); - setOptions(prevOptions => [...prevOptions, inputValue]); + // To add to a Set, you need to create a new Set for immutability in React state updates. + setTags(prevTags => new Set(prevTags).add(inputValue)); + setOptions(prevOptions => new Set(prevOptions).add(inputValue)); setInputValue(''); } }; - const handleDeleteTag = (tagToDelete: string) => { - setTags(tags.filter(tag => tag !== tagToDelete)); - }; + const handleSelectTag = (tagToAdd: string) => { + if (!tags.has(tagToAdd)){ // Corrected syntax for checking if a Set contains an item + setTags(prevTags => new Set(prevTags).add(tagToAdd)); + } + } + const handleDeleteTag = (tagToDelete: string) => { + setTags(prevTags => { + const updatedTags = new Set(prevTags); + updatedTags.delete(tagToDelete); + return updatedTags; + }); + }; const handleBlur = () => { setCellSelected(false); }; return ( -
setCellSelected(true)}> +
setCellSelected(true)}> {!cellSelected ? ( - + ) : ( -
+
-
- +
+
Select an option or create one - +
diff --git a/compass/components/TagsInput/Tag.tsx b/compass/components/TagsInput/Tag.tsx index 59ede37..e7093fd 100644 --- a/compass/components/TagsInput/Tag.tsx +++ b/compass/components/TagsInput/Tag.tsx @@ -1,12 +1,10 @@ import { XMarkIcon } from "@heroicons/react/24/solid" -export const Tag = ({children, active = false}) => { - const handleClick = () => { - - } +export const Tag = ({children, handleDelete, active = false}) => { + return ( {children} - {active && } + {active && } ) } \ No newline at end of file diff --git a/compass/components/TagsInput/TagDropdown.tsx b/compass/components/TagsInput/TagDropdown.tsx index b1b5f9b..535bf7f 100644 --- a/compass/components/TagsInput/TagDropdown.tsx +++ b/compass/components/TagsInput/TagDropdown.tsx @@ -2,11 +2,11 @@ import { Tag } from "./Tag"; import { DropdownAction } from "./DropdownAction"; -export const TagDropdown = ({ tags }) => { +export const TagDropdown = ({ tags, handleAdd }) => { return (
- {tags.map((tag, index) => ( -
+ {Array.from(tags).map((tag, index) => ( +
handleAdd(tag)} key={index} className="items-center rounded-md p-1 flex flex-row justify-between hover:bg-gray-100"> {tag}
diff --git a/compass/components/TagsInput/TagsArray.tsx b/compass/components/TagsInput/TagsArray.tsx index 98769c7..486a8a4 100644 --- a/compass/components/TagsInput/TagsArray.tsx +++ b/compass/components/TagsInput/TagsArray.tsx @@ -1,12 +1,12 @@ import { Tag } from "./Tag" -export const TagsArray = ({ tags, active = false }) => { +export const TagsArray = ({ tags, handleDelete, active = false }) => { return( -
+
{ - tags.map((tag) => { + Array.from(tags).map((tag) => { return ( - {tag} + {tag} ) }) }