Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelectPanel2: Improve keyboard navigation from search input #4199

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/fresh-parents-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

experimental/SelectPanel: Improve keyboard navigation from search input
27 changes: 25 additions & 2 deletions src/drafts/SelectPanel2/SelectPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const SelectPanelContext = React.createContext<{
searchQuery: string
setSearchQuery: React.Dispatch<React.SetStateAction<string>>
selectionVariant: ActionListProps['selectionVariant'] | 'instant'
moveFocusToList: () => void
}>({
title: '',
description: undefined,
Expand All @@ -46,6 +47,7 @@ const SelectPanelContext = React.createContext<{
searchQuery: '',
setSearchQuery: () => {},
selectionVariant: 'multiple',
moveFocusToList: () => {},
})

export type SelectPanelProps = {
Expand Down Expand Up @@ -161,6 +163,12 @@ const Panel: React.FC<SelectPanelProps> = ({
[internalOpen],
)

// used in SelectPanel.SearchInput
const moveFocusToList = () => {
const firstListElement = dialogRef.current?.querySelector('ul[role=listbox] li') as HTMLLIElement | undefined
firstListElement?.focus()
}

/* Dialog */
const dialogRef = React.useRef<HTMLDialogElement>(null)

Expand Down Expand Up @@ -264,6 +272,7 @@ const Panel: React.FC<SelectPanelProps> = ({
searchQuery,
setSearchQuery,
selectionVariant,
moveFocusToList,
}}
>
<Box
Expand Down Expand Up @@ -372,11 +381,15 @@ const SelectPanelHeader: React.FC<React.PropsWithChildren> = ({children, ...prop
)
}

const SelectPanelSearchInput: React.FC<TextInputProps> = ({onChange: propsOnChange, ...props}) => {
const SelectPanelSearchInput: React.FC<TextInputProps> = ({
onChange: propsOnChange,
onKeyDown: propsOnKeyDown,
...props
}) => {
// TODO: use forwardedRef
const inputRef = React.createRef<HTMLInputElement>()

const {setSearchQuery} = React.useContext(SelectPanelContext)
const {setSearchQuery, moveFocusToList} = React.useContext(SelectPanelContext)

const internalOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
// If props.onChange is given, the application controls search,
Expand All @@ -385,6 +398,15 @@ const SelectPanelSearchInput: React.FC<TextInputProps> = ({onChange: propsOnChan
else setSearchQuery(event.target.value)
}

const internalKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'ArrowDown') {
event.preventDefault() // prevent scroll
moveFocusToList()
}

if (typeof propsOnKeyDown === 'function') propsOnKeyDown(event)
}

return (
<TextInput
ref={inputRef}
Expand All @@ -408,6 +430,7 @@ const SelectPanelSearchInput: React.FC<TextInputProps> = ({onChange: propsOnChan
}
sx={{'&:has(input:placeholder-shown) .TextInput-action': {display: 'none'}}}
onChange={internalOnChange}
onKeyDown={internalKeyDown}
{...props}
/>
)
Expand Down
Loading