Skip to content

Commit

Permalink
ListNext fix keyboard nav and expose hook setters (#2112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fercas123 authored Aug 1, 2023
1 parent ce0c73b commit 8c77cdb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/rich-horses-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@salt-ds/lab": minor
---

- Fixed tab in `ListNext` keyboard navigation
- Exposed `setSelectedItem` and `setHighlightedItem` from useList.
2 changes: 1 addition & 1 deletion packages/lab/src/list-next/ListNext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface ListNextProps
/* Value for the controlled version. */
selected?: string;
/* Callback for change event. */
onChange?: (e: SyntheticEvent, data: { value: string }) => void;
onChange?: (event: SyntheticEvent, data: { value: string }) => void;
/* Initial selection. */
defaultSelected?: string;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/lab/src/list-next/useList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export const useList = ({
};

// takes care of keydown when using keyboard navigation
const keyDownHandler = (event: KeyboardEvent<HTMLUListElement>) => {
const keyDownHandler = (event: KeyboardEvent<HTMLElement>) => {
const { key } = event;
const currentItem = getActiveItem();
let nextItem = currentItem;
Expand Down Expand Up @@ -280,9 +280,10 @@ export const useList = ({
break;
case "PageDown":
case "PageUp":
default:
event.preventDefault();
break;
default:
break;
}
};

Expand All @@ -308,6 +309,8 @@ export const useList = ({
activeDescendant,
selectedItem,
highlightedItem,
setSelectedItem,
setHighlightedItem,
contextValue,
focusVisibleRef,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/lab/stories/list-next/list.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ export const Controlled: Story<ListNextProps> = ({ onChange, ...rest }) => {

const handleClick = (index: number) => {
setSelectedItem(usStateExampleData[index]);
setHighlightedIndex(index);
};

const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
const inputValue = event.target.value.toLowerCase();
setSelectedItem(inputValue);
const firstMatchingItem =
inputValue.length - 1 >= 0
? usStateExampleData.findIndex((item) =>
Expand Down

1 comment on commit 8c77cdb

@vercel
Copy link

@vercel vercel bot commented on 8c77cdb Aug 1, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.