Spaces:
Running
Running
File size: 7,629 Bytes
a577d9a 303655d a577d9a 303655d a577d9a | 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | import React, { useEffect, useMemo, useRef, useState } from 'react'
function normalizeSearchText(value) {
return String(value || '')
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.toLowerCase()
.trim()
}
function normalizeOption(option) {
if (typeof option === 'string') {
const text = String(option || '').trim()
return text ? { value: text, label: text, secondary: '' } : null
}
if (!option || typeof option !== 'object') return null
const rawValue = option.value ?? option.id ?? option.key ?? ''
const value = String(rawValue || '').trim()
if (!value) return null
const label = String(option.label ?? option.nome_modelo ?? option.arquivo ?? value).trim() || value
const secondary = String(option.secondary ?? option.arquivo ?? '').trim()
return { value, label, secondary }
}
export default function SinglePillAutocomplete({
value,
onChange,
options = [],
placeholder = 'Selecione um item',
panelTitle = '',
emptyMessage = 'Nenhuma sugestao encontrada.',
loading = false,
disabled = false,
onOpenChange = null,
}) {
const rootRef = useRef(null)
const inputRef = useRef(null)
const [query, setQuery] = useState('')
const [open, setOpen] = useState(false)
const [activeIndex, setActiveIndex] = useState(-1)
const selectedValue = String(value || '')
const normalizedOptions = useMemo(() => {
const unique = []
const seen = new Set()
;(options || []).forEach((item) => {
const normalized = normalizeOption(item)
if (!normalized) return
if (seen.has(normalized.value)) return
seen.add(normalized.value)
unique.push(normalized)
})
return unique
}, [options])
const selectedOption = useMemo(
() => normalizedOptions.find((item) => item.value === selectedValue) || null,
[normalizedOptions, selectedValue],
)
const queryNormalized = normalizeSearchText(query)
const filteredOptions = useMemo(() => {
if (loading) return []
if (!queryNormalized) return normalizedOptions.slice(0, 160)
return normalizedOptions
.filter((item) => (
normalizeSearchText(item.label).includes(queryNormalized)
|| normalizeSearchText(item.secondary).includes(queryNormalized)
))
.slice(0, 160)
}, [loading, normalizedOptions, queryNormalized])
useEffect(() => {
if (!open) return undefined
function onDocumentMouseDown(event) {
if (!rootRef.current) return
if (!rootRef.current.contains(event.target)) setOpen(false)
}
document.addEventListener('mousedown', onDocumentMouseDown)
return () => document.removeEventListener('mousedown', onDocumentMouseDown)
}, [open])
useEffect(() => {
if (typeof onOpenChange !== 'function') return
onOpenChange(Boolean(open && !disabled))
}, [open, disabled, onOpenChange])
useEffect(() => () => {
if (typeof onOpenChange === 'function') onOpenChange(false)
}, [onOpenChange])
useEffect(() => {
if (!open || filteredOptions.length === 0) {
setActiveIndex(-1)
return
}
if (activeIndex >= filteredOptions.length) setActiveIndex(filteredOptions.length - 1)
}, [activeIndex, filteredOptions, open])
function emitChange(nextValue) {
if (typeof onChange === 'function') onChange(String(nextValue || ''))
}
function selectOption(option) {
if (!option) return
emitChange(option.value)
setQuery('')
setOpen(false)
setActiveIndex(-1)
}
function clearSelection(event) {
event.preventDefault()
event.stopPropagation()
emitChange('')
setQuery('')
setOpen(true)
setActiveIndex(-1)
window.requestAnimationFrame(() => inputRef.current?.focus())
}
function onInputChange(event) {
if (disabled) return
setQuery(event.target.value)
setOpen(true)
setActiveIndex(-1)
}
function onInputFocus() {
if (disabled) return
setOpen(true)
setActiveIndex(-1)
}
function onInputKeyDown(event) {
if (disabled) return
if (event.key === 'Escape') {
setOpen(false)
return
}
if (event.key === 'Backspace' && !query && selectedOption) {
emitChange('')
setOpen(true)
return
}
if (!filteredOptions.length) return
if (event.key === 'ArrowDown') {
event.preventDefault()
setOpen(true)
setActiveIndex((prev) => (prev < 0 ? 0 : (prev + 1) % filteredOptions.length))
return
}
if (event.key === 'ArrowUp') {
event.preventDefault()
setOpen(true)
setActiveIndex((prev) => {
if (prev < 0) return filteredOptions.length - 1
return (prev - 1 + filteredOptions.length) % filteredOptions.length
})
return
}
if (event.key === 'Enter') {
event.preventDefault()
if (activeIndex >= 0 && activeIndex < filteredOptions.length) {
selectOption(filteredOptions[activeIndex])
return
}
if (filteredOptions.length === 1) {
selectOption(filteredOptions[0])
}
}
}
return (
<div className={`chip-autocomplete chip-autocomplete-single${open ? ' is-open' : ''}${disabled ? ' is-disabled' : ''}`} ref={rootRef}>
<div className={`chip-autocomplete-single-control${disabled ? ' is-disabled' : ''}`}>
{selectedOption ? (
<span className="chip-autocomplete-selected chip-autocomplete-selected-inline">
<span>{selectedOption.label}</span>
{!disabled ? (
<button
type="button"
className="chip-autocomplete-selected-remove"
onMouseDown={clearSelection}
onClick={(event) => {
event.preventDefault()
event.stopPropagation()
}}
aria-label={`Remover ${selectedOption.label}`}
>
×
</button>
) : null}
</span>
) : null}
<input
ref={inputRef}
type="text"
className="chip-autocomplete-single-input"
value={query}
onChange={onInputChange}
onFocus={onInputFocus}
onKeyDown={onInputKeyDown}
placeholder={selectedOption ? '' : placeholder}
autoComplete="off"
autoCorrect="off"
autoCapitalize="none"
spellCheck={false}
disabled={disabled}
/>
</div>
{open && !disabled ? (
<div className="chip-autocomplete-panel" role="listbox">
{panelTitle ? <div className="chip-autocomplete-panel-head">{panelTitle}</div> : null}
{loading ? (
<div className="chip-autocomplete-empty">Carregando lista...</div>
) : filteredOptions.length ? (
<div className="chip-autocomplete-chip-wrap">
{filteredOptions.map((item, idx) => (
<button
type="button"
key={`single-chip-${item.value}-${idx}`}
className={`chip-autocomplete-chip${idx === activeIndex ? ' is-active' : ''}`}
onMouseDown={(event) => {
event.preventDefault()
event.stopPropagation()
selectOption(item)
}}
title={item.secondary ? `${item.label} | ${item.secondary}` : item.label}
>
{item.label}
</button>
))}
</div>
) : (
<div className="chip-autocomplete-empty">
{emptyMessage}
</div>
)}
</div>
) : null}
</div>
)
}
|