0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00

refactor(console): add a11y for language editor components (#2123)

This commit is contained in:
Xiao Yijun 2022-10-11 20:25:08 +08:00 committed by GitHub
parent ad5b7b1aea
commit 44772ba2e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 13 deletions

View file

@ -7,6 +7,7 @@ import Plus from '@/assets/images/plus.svg';
import SearchIcon from '@/assets/images/search.svg'; import SearchIcon from '@/assets/images/search.svg';
import Button from '@/components/Button'; import Button from '@/components/Button';
import TextInput from '@/components/TextInput'; import TextInput from '@/components/TextInput';
import { onKeyDownHandler } from '@/utilities/a11y';
import * as style from './AddLanguageSelector.module.scss'; import * as style from './AddLanguageSelector.module.scss';
@ -54,6 +55,12 @@ const AddLanguageSelector = ({ options, onSelect }: Props) => {
}; };
}, [isDropDownOpen, searchInputRef]); }, [isDropDownOpen, searchInputRef]);
const handleSelect = (languageTag: LanguageTag) => {
onSelect(languageTag);
setIsDropDownOpen(false);
setSearchInputValue('');
};
return ( return (
<div ref={selectorRef} className={style.languageSelector}> <div ref={selectorRef} className={style.languageSelector}>
<div className={style.input}> <div className={style.input}>
@ -81,15 +88,16 @@ const AddLanguageSelector = ({ options, onSelect }: Props) => {
{isDropDownOpen && filteredOptions.length > 0 && ( {isDropDownOpen && filteredOptions.length > 0 && (
<ul className={style.dropDown}> <ul className={style.dropDown}>
{filteredOptions.map((languageTag) => ( {filteredOptions.map((languageTag) => (
// TODO: @yijun
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-noninteractive-element-interactions
<li <li
key={languageTag} key={languageTag}
role="tab"
tabIndex={0}
className={style.dropDownItem} className={style.dropDownItem}
onKeyDown={onKeyDownHandler(() => {
handleSelect(languageTag);
})}
onClick={() => { onClick={() => {
onSelect(languageTag); handleSelect(languageTag);
setIsDropDownOpen(false);
setSearchInputValue('');
}} }}
> >
<div className={style.languageName}>{uiLanguageNameMapping[languageTag]}</div> <div className={style.languageName}>{uiLanguageNameMapping[languageTag]}</div>

View file

@ -2,6 +2,8 @@ import { languages, LanguageTag } from '@logto/language-kit';
import classNames from 'classnames'; import classNames from 'classnames';
import { useEffect, useRef } from 'react'; import { useEffect, useRef } from 'react';
import { onKeyDownHandler } from '@/utilities/a11y';
import * as style from './LanguageItem.module.scss'; import * as style from './LanguageItem.module.scss';
type Props = { type Props = {
@ -19,18 +21,22 @@ const LanguageItem = ({ languageTag, isSelected, onClick }: Props) => {
} }
}, [isSelected]); }, [isSelected]);
const handleSelect = () => {
if (isSelected) {
return;
}
onClick();
};
return ( return (
// TODO: @yijun
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
<div <div
ref={itemRef} ref={itemRef}
role="tab"
tabIndex={0}
aria-selected={isSelected}
className={classNames(style.languageItem, isSelected && style.selected)} className={classNames(style.languageItem, isSelected && style.selected)}
onClick={() => { onClick={handleSelect}
if (isSelected) { onKeyDown={onKeyDownHandler(handleSelect)}
return;
}
onClick();
}}
> >
<div className={style.languageName}>{languages[languageTag]}</div> <div className={style.languageName}>{languages[languageTag]}</div>
<div className={style.languageTag}>{languageTag}</div> <div className={style.languageTag}>{languageTag}</div>