Rework the daily card page around what the player knows and hints opened in any order
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6993080d81
commit
880f007cce
24 changed files with 812 additions and 415 deletions
|
|
@ -1,15 +1,23 @@
|
|||
// Rôle : fournit un champ NDS d'autocomplete libre pour les catégories.
|
||||
// Rôle : fournit un champ NDS d'autocomplete, libre pour les catégories ou en mode recherche (`searchTexts`) : la liste attend une saisie,
|
||||
// surligne le texte trouvé, dit pourquoi une option apparaît et l'absence de résultat, et Entrée prend la première option.
|
||||
import { forwardRef, useEffect, useId, useMemo, useRef, useState } from "react";
|
||||
|
||||
// Recherche sans casse ni accents : « neant » trouve « Néant ».
|
||||
function searchKey(value) {
|
||||
return value.normalize("NFD").replace(/\p{Diacritic}/gu, "").toLowerCase();
|
||||
return value.normalize("NFD").replace(/\p{Diacritic}/gu, "").replace(/[’`]/g, "'").replace(/œ/gi, "oe").toLowerCase();
|
||||
}
|
||||
|
||||
// Texte trouvé dans l'option, surligné ; tel quel quand la recherche n'a pas la même longueur que le texte (« œ »).
|
||||
function HighlightedText({ text, query }) {
|
||||
const start = searchKey(text).indexOf(query);
|
||||
if (!query || start < 0 || searchKey(text).length !== text.length) return text;
|
||||
return <>{text.slice(0, start)}<mark>{text.slice(start, start + query.length)}</mark>{text.slice(start + query.length)}</>;
|
||||
}
|
||||
|
||||
// Mots proches d'une option (`keywords`, une Map option → mots) : ils ne comptent qu'à partir de trois lettres, par leur début.
|
||||
const MIN_KEYWORD_QUERY = 3;
|
||||
function matchesKeyword(words, query) {
|
||||
return words.some((word) => searchKey(word).startsWith(query));
|
||||
function matchingKeyword(words, query) {
|
||||
return words.find((word) => searchKey(word).startsWith(query));
|
||||
}
|
||||
|
||||
function normalizeOptions(options) {
|
||||
|
|
@ -29,6 +37,8 @@ export const CategoryAutocompleteInput = forwardRef(function CategoryAutocomplet
|
|||
name,
|
||||
options,
|
||||
keywords,
|
||||
usedOptions,
|
||||
searchTexts,
|
||||
placeholder,
|
||||
value,
|
||||
onChange,
|
||||
|
|
@ -46,17 +56,19 @@ export const CategoryAutocompleteInput = forwardRef(function CategoryAutocomplet
|
|||
const [activeIndex, setActiveIndex] = useState(-1);
|
||||
const cleanOptions = useMemo(() => normalizeOptions(options), [options]);
|
||||
const cleanValue = String(value || "");
|
||||
const query = searchKey(cleanValue.trim());
|
||||
const filteredOptions = useMemo(() => {
|
||||
const query = searchKey(cleanValue.trim());
|
||||
if (!query) return cleanOptions.slice(0, maxOptions);
|
||||
const matches = cleanOptions.filter((option) => searchKey(option).includes(query));
|
||||
const close = keywords && query.length >= MIN_KEYWORD_QUERY
|
||||
? cleanOptions.filter((option) => !searchKey(option).includes(query) && matchesKeyword(keywords.get(option) || [], query))
|
||||
? cleanOptions.filter((option) => !searchKey(option).includes(query) && matchingKeyword(keywords.get(option) || [], query))
|
||||
: [];
|
||||
return [...matches, ...close].slice(0, maxOptions);
|
||||
}, [cleanOptions, cleanValue, keywords, maxOptions]);
|
||||
}, [cleanOptions, keywords, maxOptions, query]);
|
||||
// Options déjà prises : citées en fin de liste, sans pouvoir être choisies.
|
||||
const usedMatches = searchTexts && query ? (usedOptions || []).filter((option) => searchKey(option).includes(query)) : [];
|
||||
const menuId = `${inputId}-menu`;
|
||||
const showMenu = open && filteredOptions.length > 0;
|
||||
const showMenu = open && (searchTexts ? Boolean(query) : filteredOptions.length > 0);
|
||||
|
||||
// Au clavier, l'option active reste visible quand la liste défile.
|
||||
useEffect(() => {
|
||||
|
|
@ -98,6 +110,7 @@ export const CategoryAutocompleteInput = forwardRef(function CategoryAutocomplet
|
|||
|
||||
function handleKeyDown(event) {
|
||||
if (!filteredOptions.length) {
|
||||
if (event.key === "Escape") setOpen(false);
|
||||
onKeyDown?.(event);
|
||||
return;
|
||||
}
|
||||
|
|
@ -112,6 +125,9 @@ export const CategoryAutocompleteInput = forwardRef(function CategoryAutocomplet
|
|||
} else if (event.key === "Enter" && open && activeIndex >= 0) {
|
||||
event.preventDefault();
|
||||
selectOption(filteredOptions[activeIndex]);
|
||||
} else if (event.key === "Enter" && open && searchTexts && query && !cleanOptions.some((option) => searchKey(option) === query)) {
|
||||
event.preventDefault();
|
||||
selectOption(filteredOptions[0]);
|
||||
} else if (event.key === "Escape" && open) {
|
||||
setOpen(false);
|
||||
setActiveIndex(-1);
|
||||
|
|
@ -155,9 +171,12 @@ export const CategoryAutocompleteInput = forwardRef(function CategoryAutocomplet
|
|||
onMouseDown={(event) => event.preventDefault()}
|
||||
onClick={() => selectOption(option)}
|
||||
>
|
||||
{option}
|
||||
{searchTexts ? <HighlightedText text={option} query={query} /> : option}
|
||||
{searchTexts && !searchKey(option).includes(query) && <small>{searchTexts.keyword.replace("{word}", matchingKeyword(keywords.get(option) || [], query))}</small>}
|
||||
</button>
|
||||
))}
|
||||
{usedMatches.map((option) => <div className="nds-autocomplete-option" key={option} role="option" aria-disabled="true" aria-selected="false" onMouseDown={(event) => event.preventDefault()}>{option}<small>{searchTexts.used}</small></div>)}
|
||||
{searchTexts && !filteredOptions.length && !usedMatches.length && <div className="nds-autocomplete-option" role="option" aria-disabled="true" aria-selected="false" onMouseDown={(event) => event.preventDefault()}>{searchTexts.none.replace("{query}", cleanValue.trim())}</div>}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue