Skip to content

Commit

Permalink
fix: Clean up some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel.carrera committed Sep 20, 2023
1 parent 475573a commit 4db9a5f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
14 changes: 6 additions & 8 deletions modules/react/combobox/lib/hooks/useMoveCursorToIndex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ export const useMoveCursorToIndex = createElemPropsHook(useComboboxModel)(model
// If there is no selected item and items exists we want to set the cursor to the first item in the array
if (model.state.selectedIds.length === 0 && model.state.items.length > 0) {
model.events.goTo({id: model.state.items[0].id});
} else {
} else if (model.state.selectedIds.length > 0) {
// If the user wants an item selected by default by passing `initialSelectedId` we select that item
if (model.state.selectedIds.length > 0) {
const selectedItem = model.state.items.findIndex(
(item: {id: string}) => item.id === model.state.selectedIds[0]
);
model.events.goTo({id: model.state.items[selectedItem].id});
model.events.select({id: model.state.items[selectedItem].id});
}
const selectedItem = model.state.items.findIndex(
(item: {id: string}) => item.id === model.state.selectedIds[0]
);
model.events.goTo({id: model.state.items[selectedItem].id});
model.events.select({id: model.state.items[selectedItem].id});
}

// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
6 changes: 3 additions & 3 deletions modules/react/select/lib/hooks/useSelectInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const useSelectInput = composeHooks(
): number => {
for (let i = startIndex; i < endIndex; i++) {
const label = model.state.items[i].id.toLowerCase();

if (label.indexOf(startString.toLowerCase()) === 0) {
if (
!ignoreDisabled ||
Expand All @@ -52,9 +51,10 @@ export const useSelectInput = composeHooks(
const handleKeyboardTypeAhead = (key: string, numOptions: number) => {
// If the starting point is beyond the list of options, reset it
// to the beginning of the list
let start = keySofar.current.length === 0 ? cursorFocusedIndex + 1 : cursorFocusedIndex;
const startNumber =
keySofar.current.length === 0 ? cursorFocusedIndex + 1 : cursorFocusedIndex;

start = start === numOptions ? 0 : start;
const start = startNumber === numOptions ? 0 : startNumber;

// Keeps track of the current key types and adds to it
// if you type `de` vs `d` for denver
Expand Down

0 comments on commit 4db9a5f

Please sign in to comment.