Skip to content

Commit

Permalink
fix: exposes a generic set instead of setCursor
Browse files Browse the repository at this point in the history
  • Loading branch information
dzucconi committed Aug 26, 2021
1 parent 4dfd82d commit af08c3b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/useKeyboardListNavigation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe("useKeyboardListNavigation", () => {
expect(result.current.selected).toBe("first");
});

it("exposes a setCursor function that allows for updating the cursor manually", () => {
it("exposes a set function that allows for updating the cursor manually", () => {
const { result } = renderHook(() =>
useKeyboardListNavigation({ list, onEnter: noop })
);
Expand All @@ -430,15 +430,15 @@ describe("useKeyboardListNavigation", () => {
expect(result.current.selected).toBe("first");

act(() => {
result.current.setCursor(2);
result.current.set({ cursor: 2 });
});

expect(result.current.cursor).toBe(2);
expect(result.current.index).toBe(2);
expect(result.current.selected).toBe("third");

act(() => {
result.current.setCursor(-1);
result.current.set({ cursor: -1 });
});

expect(result.current.cursor).toBe(-1);
Expand Down
10 changes: 5 additions & 5 deletions src/useKeyboardListNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type UseKeyboardListNavigationAction =
| { type: "NEXT" }
| { type: "FIRST" }
| { type: "LAST" }
| { type: "SET"; payload: { cursor: number } };
| { type: "SET"; payload: { cursor?: number; interactive?: boolean } };

export type UseKeyboardListNavigationState = {
cursor: number;
Expand Down Expand Up @@ -36,7 +36,7 @@ const reducer =
case "LAST":
return { ...state, cursor: state.length - 1, interactive: true };
case "SET":
return { ...state, cursor: action.payload.cursor };
return { ...state, ...action.payload };
}
};

Expand Down Expand Up @@ -171,15 +171,15 @@ export const useKeyboardListNavigation = <T>({
dispatch({ type: "RESET" });
};

const setCursor = (cursor: number) => {
dispatch({ type: "SET", payload: { cursor } });
const set = (payload: { cursor?: number; interactive?: boolean }) => {
dispatch({ type: "SET", payload });
};

return {
...state,
index: interactiveIndex,
selected: list[interactiveIndex],
reset,
setCursor,
set,
};
};

0 comments on commit af08c3b

Please sign in to comment.