Unify category autocomplete fields
All checks were successful
Deploy Sokko G / deploy (push) Successful in 17s

This commit is contained in:
Shinuwa 2026-09-04 14:09:08 +02:00
parent e3d823d0b1
commit 055fb78d60
21 changed files with 739 additions and 168 deletions

View file

@ -0,0 +1,146 @@
// Rôle : fournit un champ NDS d'autocomplete libre pour les catégories.
import { forwardRef, useId, useMemo, useRef, useState } from "react";
function normalizeOptions(options) {
const seen = new Set();
return (options || [])
.map((option) => String(option || "").trim())
.filter((option) => {
if (!option || seen.has(option)) return false;
seen.add(option);
return true;
});
}
export const CategoryAutocompleteInput = forwardRef(function CategoryAutocompleteInput({
className = "nds-input",
id,
name,
options,
placeholder,
value,
onChange,
onFocus,
onBlur,
onKeyDown,
autoComplete = "off",
maxOptions = 8,
...props
}, forwardedRef) {
const generatedId = useId();
const inputId = id || `category-autocomplete-${generatedId}`;
const rootRef = useRef(null);
const [open, setOpen] = useState(false);
const [activeIndex, setActiveIndex] = useState(-1);
const cleanOptions = useMemo(() => normalizeOptions(options), [options]);
const cleanValue = String(value || "");
const filteredOptions = useMemo(() => {
const query = cleanValue.trim().toLowerCase();
const matches = query
? cleanOptions.filter((option) => option.toLowerCase().includes(query))
: cleanOptions;
return matches.slice(0, maxOptions);
}, [cleanOptions, cleanValue, maxOptions]);
const menuId = `${inputId}-menu`;
const showMenu = open && filteredOptions.length > 0;
function setInputRef(element) {
if (typeof forwardedRef === "function") forwardedRef(element);
else if (forwardedRef) forwardedRef.current = element;
}
function emitChange(nextValue) {
onChange?.({ target: { name, value: nextValue } });
}
function selectOption(option) {
emitChange(option);
setOpen(false);
setActiveIndex(-1);
}
function handleChange(event) {
onChange?.(event);
setOpen(true);
setActiveIndex(-1);
}
function handleFocus(event) {
setOpen(true);
onFocus?.(event);
}
function handleBlur(event) {
if (rootRef.current?.contains(event.relatedTarget)) return;
setOpen(false);
setActiveIndex(-1);
onBlur?.(event);
}
function handleKeyDown(event) {
if (!filteredOptions.length) {
onKeyDown?.(event);
return;
}
if (event.key === "ArrowDown") {
event.preventDefault();
setOpen(true);
setActiveIndex((index) => Math.min(index + 1, filteredOptions.length - 1));
} else if (event.key === "ArrowUp") {
event.preventDefault();
setOpen(true);
setActiveIndex((index) => Math.max(index - 1, 0));
} else if (event.key === "Enter" && open && activeIndex >= 0) {
event.preventDefault();
selectOption(filteredOptions[activeIndex]);
} else if (event.key === "Escape" && open) {
setOpen(false);
setActiveIndex(-1);
} else {
onKeyDown?.(event);
}
}
return (
<div className="nds-autocomplete-field" ref={rootRef}>
<input
{...props}
ref={setInputRef}
id={inputId}
className={`${className} nds-category-autocomplete-input`.trim()}
name={name}
type="text"
value={cleanValue}
placeholder={placeholder}
autoComplete={autoComplete}
onChange={handleChange}
onFocus={handleFocus}
onBlur={handleBlur}
onKeyDown={handleKeyDown}
role="combobox"
aria-autocomplete="list"
aria-expanded={showMenu}
aria-controls={menuId}
aria-activedescendant={activeIndex >= 0 ? `${menuId}-${activeIndex}` : undefined}
/>
{showMenu && (
<div className="nds-autocomplete-menu" id={menuId} role="listbox">
{filteredOptions.map((option, index) => (
<button
className={`nds-autocomplete-option ${index === activeIndex ? "is-active" : ""}`}
id={`${menuId}-${index}`}
key={option}
type="button"
role="option"
aria-selected={index === activeIndex}
onMouseDown={(event) => event.preventDefault()}
onClick={() => selectOption(option)}
>
{option}
</button>
))}
</div>
)}
</div>
);
});