Skip to content

Commit

Permalink
fix(a11y): close search bar when pressing escape
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer authored and AntonLantukh committed Feb 22, 2024
1 parent a0cca10 commit 7a14497
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
12 changes: 2 additions & 10 deletions packages/ui-react/src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,8 @@ const Header: React.FC<Props> = ({

return searchActive ? (
<div className={styles.searchContainer}>
<SearchBar {...searchBarProps} />
<IconButton
className={styles.iconButton}
aria-label="Close search"
onClick={() => {
if (onCloseSearchButtonClick) {
onCloseSearchButtonClick();
}
}}
>
<SearchBar {...searchBarProps} onClose={onCloseSearchButtonClick} />
<IconButton className={styles.iconButton} aria-label="Close search" onClick={onCloseSearchButtonClick}>
<Icon icon={CloseIcon} />
</IconButton>
</div>
Expand Down
4 changes: 3 additions & 1 deletion packages/ui-react/src/components/SearchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ export type Props = {
query?: string;
onQueryChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
onClearButtonClick?: () => void;
onClose?: () => void;
inputRef?: React.MutableRefObject<HTMLInputElement>;
};

const SearchBar: React.FC<Props> = ({ query, onQueryChange, onClearButtonClick, inputRef }: Props) => {
const SearchBar: React.FC<Props> = ({ query, onQueryChange, onClearButtonClick, onClose, inputRef }: Props) => {
const { t } = useTranslation('search');

return (
Expand All @@ -26,6 +27,7 @@ const SearchBar: React.FC<Props> = ({ query, onQueryChange, onClearButtonClick,
type="text"
value={query}
onChange={onQueryChange}
onKeyDown={(event) => event.key === 'Escape' && onClose?.()}
aria-label={t('search_bar.search_label')}
placeholder={t('search_bar.search_placeholder')}
ref={inputRef}
Expand Down

0 comments on commit 7a14497

Please sign in to comment.