Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useTypeFilter, useSearchActions } from '../search.store'
import { useEuiTheme, EuiButton, EuiSkeletonRectangle } from '@elastic/eui'
import { css } from '@emotion/react'
import { useRef, useCallback, MutableRefObject } from 'react'

interface SearchFiltersProps {
counts: {
Expand All @@ -9,14 +10,54 @@ interface SearchFiltersProps {
totalCount: number
}
isLoading: boolean
inputRef?: React.RefObject<HTMLInputElement>
itemRefs?: MutableRefObject<(HTMLAnchorElement | null)[]>
resultsCount?: number
}

export const SearchFilters = ({ counts, isLoading }: SearchFiltersProps) => {
export const SearchFilters = ({
counts,
isLoading,
inputRef,
itemRefs,
resultsCount = 0,
}: SearchFiltersProps) => {
const { euiTheme } = useEuiTheme()
const selectedFilter = useTypeFilter()
const { setTypeFilter } = useSearchActions()
const { apiResultsCount, docsResultsCount, totalCount } = counts

const filterRefs = useRef<(HTMLButtonElement | null)[]>([])

const handleFilterKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLButtonElement>, filterIndex: number) => {
const filterCount = 3 // ALL, DOCS, API

if (e.key === 'ArrowUp') {
e.preventDefault()
// Go back to input
inputRef?.current?.focus()
} else if (e.key === 'ArrowDown') {
e.preventDefault()
// Go to first result if available
if (resultsCount > 0) {
itemRefs?.current[0]?.focus()
}
} else if (e.key === 'ArrowLeft') {
e.preventDefault()
if (filterIndex > 0) {
filterRefs.current[filterIndex - 1]?.focus()
}
} else if (e.key === 'ArrowRight') {
e.preventDefault()
if (filterIndex < filterCount - 1) {
filterRefs.current[filterIndex + 1]?.focus()
}
}
},
[inputRef, itemRefs, resultsCount]
)

const buttonStyle = css`
border-radius: 99999px;
padding-inline: ${euiTheme.size.m};
Expand All @@ -34,6 +75,8 @@ export const SearchFilters = ({ counts, isLoading }: SearchFiltersProps) => {
gap: ${euiTheme.size.s};
padding-inline: ${euiTheme.size.base};
`}
role="group"
aria-label="Search filters"
>
<EuiSkeletonRectangle
isLoading={isLoading}
Expand All @@ -47,6 +90,12 @@ export const SearchFilters = ({ counts, isLoading }: SearchFiltersProps) => {
fill={selectedFilter === 'all'}
isLoading={isLoading}
onClick={() => setTypeFilter('all')}
onKeyDown={(e: React.KeyboardEvent<HTMLButtonElement>) =>
handleFilterKeyDown(e, 0)
}
buttonRef={(el: HTMLButtonElement | null) => {
filterRefs.current[0] = el
}}
css={buttonStyle}
aria-label={`Show all results, ${totalCount} total`}
aria-pressed={selectedFilter === 'all'}
Expand All @@ -66,6 +115,12 @@ export const SearchFilters = ({ counts, isLoading }: SearchFiltersProps) => {
fill={selectedFilter === 'doc'}
isLoading={isLoading}
onClick={() => setTypeFilter('doc')}
onKeyDown={(e: React.KeyboardEvent<HTMLButtonElement>) =>
handleFilterKeyDown(e, 1)
}
buttonRef={(el: HTMLButtonElement | null) => {
filterRefs.current[1] = el
}}
css={buttonStyle}
aria-label={`Filter to documentation results, ${docsResultsCount} available`}
aria-pressed={selectedFilter === 'doc'}
Expand All @@ -85,6 +140,12 @@ export const SearchFilters = ({ counts, isLoading }: SearchFiltersProps) => {
fill={selectedFilter === 'api'}
isLoading={isLoading}
onClick={() => setTypeFilter('api')}
onKeyDown={(e: React.KeyboardEvent<HTMLButtonElement>) =>
handleFilterKeyDown(e, 2)
}
buttonRef={(el: HTMLButtonElement | null) => {
filterRefs.current[2] = el
}}
css={buttonStyle}
aria-label={`Filter to API results, ${apiResultsCount} available`}
aria-pressed={selectedFilter === 'api'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export const SearchResults = ({
<SearchFilters
counts={counts}
isLoading={isInitialLoading}
inputRef={inputRef}
itemRefs={itemRefs}
resultsCount={results.length}
/>

<EuiSpacer size="m" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ export const useSearchKeyboardNavigation = (
}
}

const focusNextItem = () => {
if (resultsCount > 1) {
// First item is already visually selected, so go to second item
const targetIndex = Math.min(selectedIndex + 1, resultsCount - 1)
itemRefs.current[targetIndex]?.focus()
} else {
// Only 1 or 0 results, go to button
buttonRef.current?.focus()
}
}

const handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
e.preventDefault()
Expand All @@ -36,19 +47,12 @@ export const useSearchKeyboardNavigation = (
} else {
askAi()
}
} else if (e.key === 'ArrowDown') {
} else if (
e.key === 'ArrowDown' ||
(e.key === 'Tab' && !e.shiftKey && selectedIndex !== NO_SELECTION)
) {
e.preventDefault()
if (resultsCount > 1) {
// First item is already visually selected, so go to second item
const targetIndex = Math.min(
selectedIndex + 1,
resultsCount - 1
)
itemRefs.current[targetIndex]?.focus()
} else {
// Only 1 or 0 results, go to button
buttonRef.current?.focus()
}
focusNextItem()
} else if (
e.key === 'Tab' &&
e.shiftKey &&
Expand Down
Loading