From 167fa009ae62f7edb3a67af12009caf0dc18bfa9 Mon Sep 17 00:00:00 2001 From: Meliora Ho Date: Thu, 4 Apr 2024 20:28:41 +0000 Subject: [PATCH] Edited the dropdown action --- compass/components/Table/Index.tsx | 6 +- .../TagsInput/CreateNewTagAction.tsx | 10 +++ .../components/TagsInput/DropdownAction.tsx | 35 ++++---- compass/components/TagsInput/Index.tsx | 88 ++++++++++++++----- compass/components/TagsInput/Tag.tsx | 21 +++-- compass/components/TagsInput/TagDropdown.tsx | 10 +-- compass/components/TagsInput/TagsArray.tsx | 1 + 7 files changed, 115 insertions(+), 56 deletions(-) create mode 100644 compass/components/TagsInput/CreateNewTagAction.tsx diff --git a/compass/components/Table/Index.tsx b/compass/components/Table/Index.tsx index 625dca3..7b5ee92 100644 --- a/compass/components/Table/Index.tsx +++ b/compass/components/Table/Index.tsx @@ -21,17 +21,17 @@ type User = { id: number; created_at: any; username: string; - role: "ADMIN" | "EMPLOYEE" | "VOLUNTEER"; + role: "administrator" | "employee" | "volunteer"; email: string; - program: "DOMESTIC" | "ECONOMIC" | "COMMUNITY"; + program: "domestic" | "economic" | "community"; experience: number; group?: string; }; + export const Table = () => { const columnHelper = createColumnHelper(); - const columns = [ columnHelper.display({ id: "options", diff --git a/compass/components/TagsInput/CreateNewTagAction.tsx b/compass/components/TagsInput/CreateNewTagAction.tsx new file mode 100644 index 0000000..f906270 --- /dev/null +++ b/compass/components/TagsInput/CreateNewTagAction.tsx @@ -0,0 +1,10 @@ +import { Tag } from "./Tag" + +export const CreateNewTagAction = ({ input }) => { + return ( +
+

Create

+ {input} +
+ ) +} \ No newline at end of file diff --git a/compass/components/TagsInput/DropdownAction.tsx b/compass/components/TagsInput/DropdownAction.tsx index 737ec69..7c9b7d1 100644 --- a/compass/components/TagsInput/DropdownAction.tsx +++ b/compass/components/TagsInput/DropdownAction.tsx @@ -1,51 +1,48 @@ import { EllipsisHorizontalIcon, TrashIcon } from "@heroicons/react/24/solid"; import { useState } from "react"; -export const DropdownAction = ({ tag, handleDeleteTag, handleAddTag }) => { +export const DropdownAction = ({ tag, handleDeleteTag, handleEditTag }) => { const [isVisible, setVisible] = useState(false); const [inputValue, setInputValue] = useState(tag); - const handleBlur = () => { - setVisible(false); + + const editTagOption = (e) => { + if (e.key === 'Enter') { + handleEditTag(tag, inputValue) + setVisible(false); + } }; - const editTagOption = () => { - handleDeleteTag(tag) - handleAddTag(inputValue) - } const handleInputChange = (e: React.ChangeEvent) => { setInputValue(e.target.value); }; - const handleClick = (event: React.MouseEvent) => { - event.stopPropagation(); // This stops the click event from propagating - setVisible(!isVisible); // Toggle visibility - }; return (
- {isVisible ? ( + setVisible(!isVisible)} + /> + {isVisible && (
-
- ) : ( - )}
); diff --git a/compass/components/TagsInput/Index.tsx b/compass/components/TagsInput/Index.tsx index 3c8d43a..ca0c947 100644 --- a/compass/components/TagsInput/Index.tsx +++ b/compass/components/TagsInput/Index.tsx @@ -1,16 +1,22 @@ -import React, { useState } from 'react'; -import 'tailwindcss/tailwind.css'; -import { TagsArray } from './TagsArray'; // Corrected path assumption -import { TagDropdown } from './TagDropdown'; +import React, { useState } from "react"; +import "tailwindcss/tailwind.css"; +import { TagsArray } from "./TagsArray"; +import { TagDropdown } from "./TagDropdown"; +import { CreateNewTagAction } from "./CreateNewTagAction"; interface TagsInputProps { presetOptions: string[]; } -const TagsInput: React.FC = ({ presetValue, presetOptions }) => { - const [inputValue, setInputValue] = useState(''); +const TagsInput: React.FC = ({ + presetValue, + presetOptions, +}) => { + const [inputValue, setInputValue] = useState(""); const [cellSelected, setCellSelected] = useState(false); - const [tags, setTags] = useState>(new Set(presetValue ? [presetValue] : [])); + const [tags, setTags] = useState>( + new Set(presetValue ? [presetValue] : []) + ); const [options, setOptions] = useState>(new Set(presetOptions)); const handleInputChange = (e: React.ChangeEvent) => { @@ -18,31 +24,63 @@ const TagsInput: React.FC = ({ presetValue, presetOptions }) => }; const handleAddTag = (e: React.KeyboardEvent) => { - if (e.key === 'Enter' && inputValue.trim()) { - // 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(''); + if (e.key === "Enter" && inputValue.trim()) { + setTags((prevTags) => new Set(prevTags).add(inputValue)); + setOptions((prevOptions) => new Set(prevOptions).add(inputValue)); + setInputValue(""); } }; 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)); + 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 => { + setTags((prevTags) => { const updatedTags = new Set(prevTags); updatedTags.delete(tagToDelete); return updatedTags; }); }; - const handleBlur = () => { - setCellSelected(false); + + const handleDeleteTagOption = (tagToDelete: string) => { + setOptions((prevOptions) => { + const updatedOptions = new Set(prevOptions); + updatedOptions.delete(tagToDelete); + return updatedOptions; + }); + if (tags.has(tagToDelete)) { + handleDeleteTag(tagToDelete); + } }; + const handleEditTag = (oldTag: string, newTag: string) => { + if (oldTag !== newTag) { + setTags((prevTags) => { + const tagsArray = Array.from(prevTags); + const oldTagIndex = tagsArray.indexOf(oldTag); + if (oldTagIndex !== -1) { + tagsArray.splice(oldTagIndex, 1, newTag); + } + return new Set(tagsArray); + }); + + setOptions((prevOptions) => { + const optionsArray = Array.from(prevOptions); + const oldTagIndex = optionsArray.indexOf(oldTag); + if (oldTagIndex !== -1) { + optionsArray.splice(oldTagIndex, 1, newTag); + } + return new Set(optionsArray); + }); + } + }; + + + return (
setCellSelected(true)}> {!cellSelected ? ( @@ -50,7 +88,7 @@ const TagsInput: React.FC = ({ presetValue, presetOptions }) => ) : (
-
+
= ({ presetValue, presetOptions }) => />
- Select an option or create one - +

Select an option or create one

+ + {inputValue.length > 0 && ( + + )}
diff --git a/compass/components/TagsInput/Tag.tsx b/compass/components/TagsInput/Tag.tsx index e7093fd..1d5999c 100644 --- a/compass/components/TagsInput/Tag.tsx +++ b/compass/components/TagsInput/Tag.tsx @@ -1,10 +1,15 @@ -import { XMarkIcon } from "@heroicons/react/24/solid" +import { XMarkIcon } from "@heroicons/react/24/solid"; -export const Tag = ({children, handleDelete, active = false}) => { +export const Tag = ({ children, handleDelete, active = false }) => { - return ( - {children} - {active && } - - ) -} \ No newline at end of file + return ( + + {children} + {active && handleDelete && ( + + )} + + ); +}; diff --git a/compass/components/TagsInput/TagDropdown.tsx b/compass/components/TagsInput/TagDropdown.tsx index 535bf7f..6f4558a 100644 --- a/compass/components/TagsInput/TagDropdown.tsx +++ b/compass/components/TagsInput/TagDropdown.tsx @@ -2,13 +2,13 @@ import { Tag } from "./Tag"; import { DropdownAction } from "./DropdownAction"; -export const TagDropdown = ({ tags, handleAdd }) => { +export const TagDropdown = ({ tags, handleEditTag,handleDeleteTag,handleAdd }) => { return ( -
+
{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 486a8a4..59d8aa1 100644 --- a/compass/components/TagsInput/TagsArray.tsx +++ b/compass/components/TagsInput/TagsArray.tsx @@ -1,6 +1,7 @@ import { Tag } from "./Tag" export const TagsArray = ({ tags, handleDelete, active = false }) => { + return(
{