Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Making the TagsPanel a custom Multi-Select component (#1276)

Dinh Nguyen Phamf60919f11a1b
diff --git a/locale/en/client.json b/locale/en/client.json
index 1dd24da89..034af0b98 100644
--- a/locale/en/client.json
+++ b/locale/en/client.json
@@ -70,8 +70,10 @@
"label.moderation": "Moderation",
"label.name": "Name",
"label.none": "None",
+ "label.notagsavailable": "No tags available",
"label.notifications": "Notifications",
"label.or": "OR",
+ "label.selecttags": "Select tags...",
"label.subscribe": "Subscribe",
"label.tags": "Tags",
"label.unfollow": "Unfollow",
@@ -175,4 +177,4 @@
"signin.message.private.text": "If you have an account or an invitation, you may use following options to sign in.",
"signin.message.private.title": "<0>{0}</0> is a private space, you must sign in to participate and vote.",
"{count, plural, one {# tag} other {# tags}}": "{count, plural, one {# tag} other {# tags}}"
-}
\ No newline at end of file
+}
diff --git a/public/pages/ShowPost/components/TagsPanel.scss b/public/pages/ShowPost/components/TagsPanel.scss
new file mode 100644
index 000000000..0e7777964
--- /dev/null
+++ b/public/pages/ShowPost/components/TagsPanel.scss
@@ -0,0 +1,88 @@
+@import "~@fider/assets/styles/variables.scss";
+
+.dropdown-wrapper {
+ position: relative;
+ width: 100%;
+}
+
+.tags-list {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 0.5rem;
+}
+
+.selected-options-container {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ min-height: 3rem;
+ width: 100%;
+ padding: 0.5rem;
+ border: 1px solid get("colors.gray.500");
+ border-radius: 0.25rem;
+ background-color: get("colors.white");
+ cursor: pointer;
+}
+
+.selected-option {
+ display: flex;
+ align-items: center;
+ margin: 0.25rem;
+ background-color: get("colors.gray.200");
+ border-radius: 0.25rem;
+ color: get("colors.gray.900");
+}
+
+.remove-button {
+ margin: 0 0.25rem;
+ color: get("colors.gray.600");
+ background: none;
+ border: none;
+ font-size: 1rem;
+ cursor: pointer;
+ line-height: 1;
+ padding: 0 0.25rem;
+}
+
+.remove-button:hover {
+ color: get("colors.red.500");
+}
+
+.search-input {
+ flex-grow: 1;
+ min-width: 5rem;
+ margin: 0.25rem;
+ padding: 0.25rem;
+ border: none;
+ outline: none;
+}
+
+.search-input:focus {
+ outline: none;
+}
+
+.options-container {
+ position: absolute;
+ z-index: 10;
+ width: 100%;
+ margin-top: 0.25rem;
+ background-color: get("colors.white");
+ border: 1px solid get("colors.gray.500");
+ border-radius: 0.25rem;
+ max-height: 15rem;
+ overflow-y: auto;
+}
+
+.option-item {
+ padding: 0.5rem;
+ cursor: pointer;
+}
+
+.option-item:hover {
+ background-color: get("colors.gray.100");
+}
+
+.no-options {
+ padding: 0.5rem;
+ color: get("colors.gray.900");
+}
diff --git a/public/pages/ShowPost/components/TagsPanel.tsx b/public/pages/ShowPost/components/TagsPanel.tsx
index 57e3c53fe..2cd14e57c 100644
--- a/public/pages/ShowPost/components/TagsPanel.tsx
+++ b/public/pages/ShowPost/components/TagsPanel.tsx
@@ -1,12 +1,14 @@
-import React, { useState } from "react"
+import React, { useEffect, useRef, useState } from "react"
import { Post, Tag } from "@fider/models"
-import { actions } from "@fider/services"
-import { ShowTag, Button } from "@fider/components"
-import { TagListItem } from "./TagListItem"
+import { actions, sortTags } from "@fider/services"
+import { ShowTag } from "@fider/components"
import { useFider } from "@fider/hooks"
import { HStack, VStack } from "@fider/components/layout"
import { Trans } from "@lingui/react/macro"
+import { i18n } from "@lingui/core"
+
+import "./TagsPanel.scss"
export interface TagsPanelProps {
post: Post
@@ -19,6 +21,10 @@ export const TagsPanel = (props: TagsPanelProps) => {
const [isEditing, setIsEditing] = useState(false)
const [assignedTags, setAssignedTags] = useState(props.tags.filter((t) => props.post.tags.indexOf(t.slug) >= 0))
+ const [query, setQuery] = useState("")
+
+ const dropdownRef = useRef<HTMLDivElement>(null)
+ const inputRef = useRef<HTMLInputElement>(null)
const assignOrUnassignTag = async (tag: Tag) => {
const idx = assignedTags.indexOf(tag)
@@ -43,33 +49,102 @@ export const TagsPanel = (props: TagsPanelProps) => {
const onSubtitleClick = () => {
if (canEdit) {
setIsEditing(!isEditing)
+ // Immediately focus on the input element when editing starts
+ if (inputRef.current) {
+ inputRef.current.focus()
+ }
+ setQuery("")
}
}
+ const handleOptionClick = (tag: Tag) => {
+ assignOrUnassignTag(tag)
+ // Keep focus on the input element after selection
+ if (inputRef.current) {
+ inputRef.current.focus()
+ }
+ }
+
+ const filteredOptions = props.tags.filter(
+ (option) => option.name.toLowerCase().includes(query.toLowerCase()) && !assignedTags.some((tag) => tag.slug === option.slug)
+ )
+
+ // Close dropdown when clicking outside
+ useEffect(() => {
+ const handleClickOutside = (event: { target: any }) => {
+ if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
+ setIsEditing(false)
+ }
+ }
+
+ document.addEventListener("mousedown", handleClickOutside)
+ return () => {
+ document.removeEventListener("mousedown", handleClickOutside)
+ }
+ }, [])
+
+ useEffect(() => {
+ if (isEditing && inputRef.current) {
+ inputRef.current.focus()
+ }
+ }, [isEditing])
+
if (!canEdit && assignedTags.length === 0) {
return null
}
const tagsList = (
- <HStack spacing={2} align="center">
- {assignedTags.length > 0 && assignedTags.map((tag) => <ShowTag key={tag.id} tag={tag} link />)}
+ <div className="tags-list">
+ {assignedTags.length > 0 && sortTags(assignedTags).map((tag) => <ShowTag key={tag.id} tag={tag} link />)}
<HStack spacing={1} align="center" className="clickable" onClick={onSubtitleClick}>
<span>
<Trans id="label.edittags">Edit tags</Trans>
</span>
</HStack>
- </HStack>
+ </div>
)
+ // Dynamic multiselect dropdown for tags selection
const editTagsList = props.tags.length > 0 && (
- <VStack justify="between" className="flex-items-start">
- {props.tags.map((tag) => (
- <TagListItem key={tag.id} tag={tag} assigned={assignedTags.indexOf(tag) >= 0} onClick={assignOrUnassignTag} />
- ))}
- <Button variant="secondary" size="small" onClick={onSubtitleClick}>
- <Trans id="action.close">Close</Trans>
- </Button>
- </VStack>
+ <div className="dropdown-wrapper" ref={dropdownRef}>
+ {/* Selected options and search input */}
+ <div className="selected-options-container">
+ {sortTags(assignedTags).map((tag) => (
+ <div key={tag.id} className="selected-option">
+ <ShowTag key={tag.id} tag={tag} link />
+ <button onClick={() => handleOptionClick(tag)} className="remove-button">
+ x
+ </button>
+ </div>
+ ))}
+ {/* Search box to enter query string */}
+ <input
+ type="text"
+ value={query}
+ ref={inputRef}
+ onChange={(e) => setQuery(e.target.value)}
+ className="search-input"
+ placeholder={assignedTags.length ? "" : i18n._("label.selecttags", { message: "Select Tags..." })}
+ />
+ </div>
+
+ {/* Dropdown options after items are filtered */}
+ {isEditing && (
+ <div className="options-container">
+ {filteredOptions.length > 0 ? (
+ sortTags(filteredOptions).map((tag) => (
+ <div key={tag.id} className="option-item" onClick={() => handleOptionClick(tag)}>
+ <ShowTag key={tag.id} tag={tag} />
+ </div>
+ ))
+ ) : (
+ <div className="no-options">
+ <Trans id="labels.notagsavailable">No tags available</Trans>
+ </div>
+ )}
+ </div>
+ )}
+ </div>
)
if (fider.isReadOnly) {
diff --git a/public/services/utils.spec.ts b/public/services/utils.spec.ts
index 927efe5dc..596b9672b 100644
--- a/public/services/utils.spec.ts
+++ b/public/services/utils.spec.ts
@@ -1,4 +1,4 @@
-import { classSet, formatDate, timeSince, fileToBase64 } from "./utils"
+import { classSet, formatDate, timeSince, fileToBase64, sortTags } from "./utils"
import { readFileSync } from "fs"
// replaces non-breaking spaces with normal spaces
@@ -105,3 +105,22 @@ test("Can convert file to base64", async () => {
const base64 = await fileToBase64(favicon)
expect(base64).toBe(Buffer.from(content).toString("base64"))
})
+
+test("Can sort tags by not isPublic and then name", () => {
+ const tags = [
+ { name: "a", id: 1, color: "Blue", slug: "a", isPublic: true },
+ { name: "b", id: 2, color: "Red", slug: "b", isPublic: false },
+ { name: "ac", id: 3, color: "White", slug: "c", isPublic: true },
+ { name: "d", id: 4, color: "Green", slug: "d", isPublic: false },
+ { name: "e", id: 5, color: "Yellow", slug: "e", isPublic: true },
+ ]
+ const sortedTags = sortTags(tags)
+
+ expect(sortedTags).toStrictEqual([
+ { name: "b", id: 2, color: "Red", slug: "b", isPublic: false },
+ { name: "d", id: 4, color: "Green", slug: "d", isPublic: false },
+ { name: "a", id: 1, color: "Blue", slug: "a", isPublic: true },
+ { name: "ac", id: 3, color: "White", slug: "c", isPublic: true },
+ { name: "e", id: 5, color: "Yellow", slug: "e", isPublic: true },
+ ])
+})
diff --git a/public/services/utils.ts b/public/services/utils.ts
index fbb80db96..cb19d5061 100644
--- a/public/services/utils.ts
+++ b/public/services/utils.ts
@@ -1,3 +1,4 @@
+import { Tag } from "@fider/models"
import { Fider } from "."
export const delay = (ms: number) => {
@@ -149,3 +150,16 @@ export const clearUrlHash = (replace?: boolean) => {
window.history.replaceState("", document.title, oldURL)
}
}
+
+// Helper function to sort tags by 'isPublic' status and then by name
+export const sortTags = (tags: Tag[]) => {
+ return tags.sort((a, b) => {
+ // First compare by 'isPublic' status (false comes before true)
+ if (a.isPublic !== b.isPublic) {
+ return a.isPublic ? 1 : -1
+ }
+
+ // If 'isPublic' status is the same, sort alphabetically by name
+ return a.name.localeCompare(b.name)
+ })
+}