From 14e2806142afc781755242e53a7a02715954834d Mon Sep 17 00:00:00 2001 From: Ilya Mukhortov Date: Fri, 30 Oct 2026 16:39:47 +1000 Subject: [PATCH] Add rich autocomplete values --- src/kit/autocomplete.tsx | 140 ++++++++++++++++++++++++--------------- 1 file changed, 88 insertions(+), 52 deletions(-) diff --git a/src/kit/autocomplete.tsx b/src/kit/autocomplete.tsx index aeac43c..8c67320 100644 --- a/src/kit/autocomplete.tsx +++ b/src/kit/autocomplete.tsx @@ -22,6 +22,7 @@ export function Autocomplete({ getKey, getLabel, renderOption, + renderValue, onSearchChange, searchDelay = 300, minSearchLength = 0, @@ -44,6 +45,8 @@ export function Autocomplete({ getLabel: (option: T) => string /** Строка списка; по умолчанию — `getLabel`. */ renderOption?: (option: T) => React.ReactNode + /** Расширенное представление выбранного значения внутри поля. */ + renderValue?: (option: T) => React.ReactNode /** Задан — варианты ищет сервер, сами не фильтруем. */ onSearchChange?: (search: string) => void searchDelay?: number @@ -96,6 +99,7 @@ export function Autocomplete({ const activeKey = keys.includes(active) ? active : (keys[0] ?? "") // вариант выбран и пользователь ещё не печатает — подсказка «от N символов» не нужна const hasContent = tooShort ? !!hint && !(query === null && value) : true + const richValue = value && query === null && renderValue ? renderValue(value) : null const pick = (option: T) => { onChange(option) @@ -104,6 +108,16 @@ export function Autocomplete({ setOpen(false) } + const beginEdit = () => { + if (disabled) return + setQuery(text) + setOpen(true) + requestAnimationFrame(() => { + inputRef.current?.focus() + inputRef.current?.select() + }) + } + const restore = () => { setOpen(false) setQuery(null) @@ -122,59 +136,81 @@ export function Autocomplete({ > !next && setOpen(false)}> - - - - - { - setText(next) - setQuery(next) - setOpen(true) - if (!next && value) onChange(null) - }} - onFocus={() => setOpen(true)} - onClick={() => setOpen(true)} - onBlur={restore} - onKeyDown={(e) => { - if (!open && (e.key === "ArrowDown" || e.key === "ArrowUp")) setOpen(true) - }} - autoFocus={autoFocus} - disabled={disabled} - placeholder={placeholder} - aria-label={ariaLabel ?? placeholder} - aria-invalid={invalid} - autoComplete="off" - className="h-full min-w-0 flex-1 bg-transparent pr-2 text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50" - /> - {(loading || (text && !disabled)) && ( - - {loading && } - {text && !disabled && ( - +
+ + {!richValue && + + } + {richValue && ( + + )} + { + setText(next) + setQuery(next) + setOpen(true) + if (!next && value) onChange(null) + }} + onFocus={() => { + if (richValue) setQuery(text) + setOpen(true) + }} + onClick={() => setOpen(true)} + onBlur={restore} + onKeyDown={(e) => { + if (!open && (e.key === "ArrowDown" || e.key === "ArrowUp")) setOpen(true) + }} + autoFocus={autoFocus} + disabled={disabled} + placeholder={placeholder} + aria-label={ariaLabel ?? placeholder} + aria-invalid={invalid} + autoComplete="off" + className={cn( + "h-full min-w-0 flex-1 bg-transparent pr-2 text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50", + richValue && "sr-only", )} - - )} - + /> + {(loading || (text && !disabled)) && ( + + {loading && } + {text && !disabled && ( + + )} + + )} + +