Skip to content

Commit

Permalink
Better focus management on open and on search
Browse files Browse the repository at this point in the history
  • Loading branch information
frankieyan committed Aug 11, 2023
1 parent d3f9696 commit 64efcf4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
7 changes: 7 additions & 0 deletions src/combobox/combobox.module.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/* Item being hovered or focused */
.comboboxItem[data-active-item],
/* Item currently selected */
.comboboxItem[aria-selected='true'] {
background-color: hsl(204 100% 40%);
color: white;
}

.popover {
max-height: 150px;
overflow-y: scroll;
}
43 changes: 24 additions & 19 deletions src/combobox/combobox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ const team = [
export function ComboboxStory() {
const [searchValue, setSearchValue] = useState('')
const [selectedValue, setSelectedValue] = useState('')
const [activeId, setActiveId] = useState<string | null | undefined>(null)

console.log({ activeId })
const [isOpen, setOpen] = useState(false)

const matches = useMemo(
() =>
Expand All @@ -75,22 +73,12 @@ export function ComboboxStory() {
[searchValue],
)

useEffect(
function updateActiveId() {
console.log({
searchValue,
matches: JSON.stringify(matches.map((match) => match.name)),
})
setActiveId(matches[0]?.name)
},
[searchValue, matches, setActiveId],
)

const onClose = useCallback(
function onClose(open: boolean) {
if (!open) {
setSearchValue(selectedValue)
}
setOpen(open)
},
[setSearchValue, selectedValue],
)
Expand All @@ -103,7 +91,6 @@ export function ComboboxStory() {

const onSelect = useCallback(
(value: string) => {
console.log({ value })
setSearchValue(value)
setSelectedValue(value)
},
Expand All @@ -114,11 +101,29 @@ export function ComboboxStory() {
combobox,
value: selectedValue,
setValue: onSelect,
defaultActiveId: selectedValue ?? null,
activeId,
setActiveId,
defaultActiveId: selectedValue ?? undefined,
})

const { move } = select

useEffect(
function focusItemOnSearch() {
if (searchValue) {
move(matches[0]?.name)
}
},
[searchValue, matches, move],
)

useEffect(
function focusItemOnOpen() {
if (isOpen) {
move(selectedValue)
}
},
[isOpen, selectedValue, move],
)

return (
<div className="wrapper">
<Box maxWidth="small">
Expand All @@ -133,7 +138,7 @@ export function ComboboxStory() {
store={combobox}
gutter={8}
sameWidth
className="popover"
className={styles.popover}
render={<SelectList store={select} />}
>
{team.map((value) => (
Expand Down

0 comments on commit 64efcf4

Please sign in to comment.