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
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
29 changes: 27 additions & 2 deletions packages/react/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,14 @@ const Panel: React.FC<SelectPanelProps> = ({
[internalOpen],
)

// used in SelectPanel.SearchInput
const moveFocusToList = () => {
const selector = 'ul[role=listbox] li:not([role=none])'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The selector looks a bit too fragile, makes me a bit nervous 😬 but the query makes a lot of sense. Can we add tests/stories to make sure we always get the intended first element? (It is not a blocker, feel free to merge the PR)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will take it up in a new PR!

// being specific about roles because there can be another ul (tabs in header) and an ActionList.Group (li[role=none])
const firstListElement = dialogRef.current?.querySelector(selector) as HTMLLIElement | undefined
firstListElement?.focus()
}

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

Expand Down Expand Up @@ -265,6 +275,7 @@ const Panel: React.FC<SelectPanelProps> = ({
searchQuery,
setSearchQuery,
selectionVariant,
moveFocusToList,
}}
>
<Box
Expand Down Expand Up @@ -373,11 +384,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 @@ -386,6 +401,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 @@ -409,6 +433,7 @@ const SelectPanelSearchInput: React.FC<TextInputProps> = ({onChange: propsOnChan
}
sx={{'&:has(input:placeholder-shown) .TextInput-action': {display: 'none'}}}
onChange={internalOnChange}
onKeyDown={internalKeyDown}
{...props}
/>
)
Expand Down