forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag-input.tsx
More file actions
115 lines (103 loc) · 3.58 KB
/
Copy pathtag-input.tsx
File metadata and controls
115 lines (103 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { X } from 'lucide-react'
import { useState, useRef, type KeyboardEvent } from 'react'
import { useTranslation } from 'react-i18next'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'
interface TagInputProps {
value: string[]
onChange: (tags: string[]) => void
placeholder?: string
className?: string
disabled?: boolean
}
export function TagInput({
value = [],
onChange,
placeholder,
className,
disabled = false,
}: TagInputProps) {
const { t } = useTranslation()
const placeholderText = placeholder ?? t('Add tags...')
const [inputValue, setInputValue] = useState('')
const inputRef = useRef<HTMLInputElement>(null)
const addTag = (tag: string) => {
const trimmed = tag.trim()
if (trimmed && !value.includes(trimmed)) {
onChange([...value, trimmed])
setInputValue('')
}
}
const removeTag = (tagToRemove: string) => {
onChange(value.filter((tag) => tag !== tagToRemove))
}
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter' || e.key === ',') {
e.preventDefault()
addTag(inputValue)
} else if (e.key === 'Backspace' && !inputValue && value.length > 0) {
removeTag(value[value.length - 1])
}
}
const handleBlur = () => {
if (inputValue.trim()) {
addTag(inputValue)
}
}
return (
<div
className={cn(
'border-input focus-within:border-ring focus-within:ring-ring/50 flex min-h-9 w-full flex-wrap items-center gap-2 rounded-md border bg-transparent px-3 py-2 text-base shadow-xs transition-[color,box-shadow] outline-none focus-within:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50 md:text-sm',
className
)}
onClick={() => inputRef.current?.focus()}
>
{value.map((tag) => (
<Badge key={tag} variant='secondary' className='gap-1 pr-1'>
{tag}
{!disabled && (
<Button
type='button'
variant='ghost'
size='icon-sm'
aria-label='Remove tag'
onClick={(e) => {
e.stopPropagation()
removeTag(tag)
}}
className='hover:bg-secondary-foreground/20 size-auto rounded-sm p-0'
>
<X className='h-3 w-3' aria-hidden='true' />
</Button>
)}
</Badge>
))}
<input
ref={inputRef}
type='text'
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyDown={handleKeyDown}
onBlur={handleBlur}
placeholder={value.length === 0 ? placeholderText : ''}
disabled={disabled}
className='placeholder:text-muted-foreground min-w-[120px] flex-1 border-0 bg-transparent shadow-none outline-none focus-visible:ring-0'
/>
</div>
)
}