Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Added TagInput component.

nzbdav-devdcf111b9b957
diff --git a/frontend/app/components/tag-input/tag-input.module.css b/frontend/app/components/tag-input/tag-input.module.css
new file mode 100644
index 00000000..94ac25f1
--- /dev/null
+++ b/frontend/app/components/tag-input/tag-input.module.css
@@ -0,0 +1,76 @@
+.container {
+ position: relative;
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: 6px;
+ padding: 6px 10px;
+ border: 1px solid var(--bs-border-color);
+ border-radius: var(--bs-border-radius);
+ background-color: var(--bs-body-bg);
+ cursor: text;
+ min-height: 38px;
+}
+
+.container:focus-within {
+ border-color: #86b7fe;
+ box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
+}
+
+.tag {
+ display: inline-flex;
+ align-items: center;
+ gap: 4px;
+ padding: 2px 8px;
+ background-color: var(--bs-secondary-bg);
+ border: 1px solid var(--bs-border-color);
+ border-radius: 16px;
+ font-size: 0.875rem;
+ line-height: 1.4;
+ color: var(--bs-body-color);
+}
+
+.removeButton {
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ width: 16px;
+ height: 16px;
+ padding: 0;
+ border: none;
+ background: transparent;
+ color: var(--bs-secondary-color);
+ font-size: 1rem;
+ line-height: 1;
+ cursor: pointer;
+ border-radius: 50%;
+}
+
+.removeButton:hover {
+ background-color: var(--bs-danger);
+ color: white;
+}
+
+.input {
+ flex: 1 1 auto;
+ min-width: 50px;
+ max-width: 100%;
+ border: none;
+ outline: none;
+ background: transparent;
+ font-size: 1rem;
+ color: var(--bs-body-color);
+ padding: 2px 0;
+}
+
+.input::placeholder {
+ color: rgba(255, 255, 255, 0.25);
+}
+
+.measure {
+ position: absolute;
+ visibility: hidden;
+ white-space: pre;
+ font-size: 1rem;
+ padding: 2px 0;
+}
diff --git a/frontend/app/components/tag-input/tag-input.tsx b/frontend/app/components/tag-input/tag-input.tsx
new file mode 100644
index 00000000..129dd9c3
--- /dev/null
+++ b/frontend/app/components/tag-input/tag-input.tsx
@@ -0,0 +1,138 @@
+import { useCallback, useRef, useState, useEffect, type ChangeEvent, type KeyboardEvent } from "react";
+import styles from "./tag-input.module.css";
+import { classNames } from "~/utils/styling";
+
+const delimiter = ",";
+type TagInputProps = {
+ className?: string;
+ value: string;
+ onChange: (value: string) => void;
+ placeholder?: string;
+ id?: string;
+ "aria-describedby"?: string;
+};
+
+export function TagInput({
+ className,
+ value,
+ onChange,
+ placeholder,
+ id,
+ "aria-describedby": ariaDescribedBy,
+}: TagInputProps) {
+ const [inputValue, setInputValue] = useState("");
+ const [inputWidth, setInputWidth] = useState<number | undefined>(undefined);
+ const inputRef = useRef<HTMLInputElement>(null);
+ const measureRef = useRef<HTMLSpanElement>(null);
+
+ const tags = value
+ .split(delimiter)
+ .map(t => t.trim())
+ .filter(t => t.length > 0);
+
+ const displayText = inputValue || (tags.length === 0 ? placeholder : "") || "";
+
+ // Measure text width and update input width
+ useEffect(() => {
+ if (measureRef.current) {
+ const width = measureRef.current.offsetWidth + 4; // Add small buffer
+ setInputWidth(width);
+ }
+ }, [displayText]);
+
+ const updateTags = useCallback((newTags: string[]) => {
+ onChange(newTags.join(`${delimiter} `));
+ }, [onChange, delimiter]);
+
+ const addTag = useCallback((tag: string) => {
+ const trimmed = tag.trim();
+ if (trimmed && !tags.includes(trimmed)) {
+ updateTags([...tags, trimmed]);
+ }
+ setInputValue("");
+ }, [tags, updateTags]);
+
+ const removeTag = useCallback((index: number) => {
+ updateTags(tags.filter((_, i) => i !== index));
+ }, [tags, updateTags]);
+
+ const handleInputChange = useCallback((e: ChangeEvent<HTMLInputElement>) => {
+ const newValue = e.target.value;
+
+ // Find the first occurrence of comma or space
+ const commaIndex = newValue.indexOf(",");
+ const spaceIndex = newValue.indexOf(" ");
+ const delimiterIndex = commaIndex === -1 ? spaceIndex
+ : spaceIndex === -1 ? commaIndex
+ : Math.min(commaIndex, spaceIndex);
+
+ if (delimiterIndex !== -1) {
+ const beforeDelimiter = newValue.slice(0, delimiterIndex).trim();
+ const afterDelimiter = newValue.slice(delimiterIndex + 1);
+
+ if (beforeDelimiter) {
+ const newTags = tags.includes(beforeDelimiter) ? tags : [...tags, beforeDelimiter];
+ updateTags(newTags);
+ }
+ setInputValue(afterDelimiter);
+ } else {
+ setInputValue(newValue);
+ }
+ }, [tags, updateTags]);
+
+ const handleKeyDown = useCallback((e: KeyboardEvent<HTMLInputElement>) => {
+ if (e.key === "Enter") {
+ e.preventDefault();
+ if (inputValue.trim()) {
+ addTag(inputValue);
+ }
+ } else if (e.key === "Backspace" && inputValue === "" && tags.length > 0) {
+ removeTag(tags.length - 1);
+ }
+ }, [inputValue, tags, addTag, removeTag]);
+
+ const handleContainerClick = useCallback(() => {
+ inputRef.current?.focus();
+ }, []);
+
+ return (
+ <div className={classNames([styles.container, className])} onClick={handleContainerClick}>
+ {tags.map((tag, index) => (
+ <span key={index} className={styles.tag}>
+ {tag}
+ <button
+ type="button"
+ className={styles.removeButton}
+ onClick={(e) => {
+ e.stopPropagation();
+ removeTag(index);
+ }}
+ aria-label={`Remove ${tag}`}
+ >
+ &times;
+ </button>
+ </span>
+ ))}
+ <span ref={measureRef} className={styles.measure}>
+ {displayText}
+ </span>
+ <input
+ ref={inputRef}
+ type="text"
+ className={styles.input}
+ style={{ width: inputWidth }}
+ value={inputValue}
+ onChange={handleInputChange}
+ onKeyDown={handleKeyDown}
+ onBlur={() => {
+ if (inputValue.trim()) {
+ addTag(inputValue);
+ }
+ }}
+ placeholder={tags.length === 0 ? placeholder : undefined}
+ id={id}
+ aria-describedby={ariaDescribedBy}
+ />
+ </div>
+ );
+}