From 4db9a5fb303013a80b464e8e683031bd7c17dc22 Mon Sep 17 00:00:00 2001 From: "manuel.carrera" Date: Wed, 20 Sep 2023 15:47:17 -0600 Subject: [PATCH] fix: Clean up some logic --- .../combobox/lib/hooks/useMoveCursorToIndex.tsx | 14 ++++++-------- modules/react/select/lib/hooks/useSelectInput.ts | 6 +++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/modules/react/combobox/lib/hooks/useMoveCursorToIndex.tsx b/modules/react/combobox/lib/hooks/useMoveCursorToIndex.tsx index f3521acd98..b3de0bb16e 100644 --- a/modules/react/combobox/lib/hooks/useMoveCursorToIndex.tsx +++ b/modules/react/combobox/lib/hooks/useMoveCursorToIndex.tsx @@ -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 diff --git a/modules/react/select/lib/hooks/useSelectInput.ts b/modules/react/select/lib/hooks/useSelectInput.ts index b510d33e4e..65d5015848 100644 --- a/modules/react/select/lib/hooks/useSelectInput.ts +++ b/modules/react/select/lib/hooks/useSelectInput.ts @@ -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 || @@ -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