Improve arrow key behavior, aria descriptions on Tag Input

This commit is contained in:
Sam Becker 2024-02-07 18:50:14 -06:00
parent 618ba90508
commit affcdc48dc
3 changed files with 32 additions and 13 deletions

View File

@ -136,15 +136,19 @@ export default function TagInput({
setInputText(''); setInputText('');
break; break;
case 'ArrowDown': case 'ArrowDown':
if (shouldShowMenu) {
setSelectedOptionIndex(i => { setSelectedOptionIndex(i => {
if (i === undefined) { if (i === undefined) {
return 1; return optionsFiltered.length > 1 ? 1 : 0;
} else if (i >= optionsFiltered.length - 1) { } else if (i >= optionsFiltered.length - 1) {
return 0; return 0;
} else { } else {
return i + 1; return i + 1;
} }
}); });
} else {
setShouldShowMenu(true);
}
break; break;
case 'ArrowUp': case 'ArrowUp':
setSelectedOptionIndex(i => { setSelectedOptionIndex(i => {
@ -275,7 +279,11 @@ export default function TagInput({
'text-xl shadow-lg dark:shadow-xl', 'text-xl shadow-lg dark:shadow-xl',
)} )}
> >
{optionsFiltered.map(({ value, annotation }, index) => {optionsFiltered.map(({
value,
annotation,
annotationAria,
}, index) =>
<div <div
key={value} key={value}
role="option" role="option"
@ -305,8 +313,13 @@ export default function TagInput({
{value} {value}
</span> </span>
{annotation && {annotation &&
<span className="whitespace-nowrap text-dim text-sm"> <span
className="whitespace-nowrap text-dim text-sm"
aria-label={annotationAria}
>
<span aria-hidden={Boolean(annotationAria)}>
{annotation} {annotation}
</span>
</span>} </span>}
</div>)} </div>)}
</div> </div>

View File

@ -151,6 +151,8 @@ export default function PhotoForm({
.map(({ tag, count }) => ({ .map(({ tag, count }) => ({
value: tag, value: tag,
annotation: `× ${count}`, annotation: `× ${count}`,
annotationAria:
`tagged in ${count} photo${count === 1 ? '' : 's'}`,
})) }))
) )
.map(([key, { .map(([key, {

View File

@ -26,7 +26,11 @@ export type FieldSetType =
'password' | 'password' |
'checkbox'; 'checkbox';
export type AnnotatedTag = { value: string, annotation?: string }; export type AnnotatedTag = {
value: string,
annotation?: string,
annotationAria?: string,
};
type FormMeta = { type FormMeta = {
label: string label: string