|
1 | | -import { useCallback, useState, useEffect, type KeyboardEvent } from "react"; |
| 1 | +import { useCallback, useState, type KeyboardEvent } from "react"; |
2 | 2 | import { AnimatePresence, motion } from "framer-motion"; |
3 | 3 | import { Input } from "~/components/primitives/Input"; |
4 | 4 | import { RunTag } from "./RunTag"; |
@@ -26,39 +26,30 @@ export function RunTagInput({ |
26 | 26 | maxTagLength = 128, |
27 | 27 | onTagsChange, |
28 | 28 | }: TagInputProps) { |
29 | | - // Use controlled tags if provided, otherwise use default |
30 | | - const initialTags = controlledTags ?? defaultTags; |
31 | | - |
32 | | - const [tags, setTags] = useState<string[]>(initialTags); |
| 29 | + const [internalTags, setInternalTags] = useState<string[]>(defaultTags); |
| 30 | + const tags = controlledTags ?? internalTags; |
33 | 31 | const [inputValue, setInputValue] = useState(""); |
34 | 32 |
|
35 | | - // Sync internal state with external tag changes |
36 | | - useEffect(() => { |
37 | | - if (controlledTags !== undefined) { |
38 | | - setTags(controlledTags); |
39 | | - } |
40 | | - }, [controlledTags]); |
41 | | - |
42 | 33 | const addTag = useCallback( |
43 | 34 | (tagText: string) => { |
44 | 35 | const trimmedTag = tagText.trim(); |
45 | 36 | if (trimmedTag && !tags.includes(trimmedTag) && tags.length < maxTags) { |
46 | 37 | const newTags = [...tags, trimmedTag]; |
47 | | - setTags(newTags); |
| 38 | + if (controlledTags === undefined) setInternalTags(newTags); |
48 | 39 | onTagsChange?.(newTags); |
49 | 40 | } |
50 | 41 | setInputValue(""); |
51 | 42 | }, |
52 | | - [tags, onTagsChange, maxTags] |
| 43 | + [tags, controlledTags, onTagsChange, maxTags] |
53 | 44 | ); |
54 | 45 |
|
55 | 46 | const removeTag = useCallback( |
56 | 47 | (tagToRemove: string) => { |
57 | 48 | const newTags = tags.filter((tag) => tag !== tagToRemove); |
58 | | - setTags(newTags); |
| 49 | + if (controlledTags === undefined) setInternalTags(newTags); |
59 | 50 | onTagsChange?.(newTags); |
60 | 51 | }, |
61 | | - [tags, onTagsChange] |
| 52 | + [tags, controlledTags, onTagsChange] |
62 | 53 | ); |
63 | 54 |
|
64 | 55 | const handleKeyDown = useCallback( |
|
0 commit comments