-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix showing proper highlight index when value is object in Combobox (#…
…815) * Fix show highlight index * Add changeset * Fix highlighted index * Fix return * Small fixes * Remove index check * Improvments * Clear input when select value * Allow select label to be ReactNode * Fix dependency useCombobox * Refactor to hooks, add support for select highlight * Refactor to use single hook for comobobox and select * Restore select option react node * Add handle to clear input to useHighlightedIndex * Update src/components/BaseSelect/useHighlightedIndex.ts Co-authored-by: Wojciech Mista <[email protected]> * Update src/components/BaseSelect/useHighlightedIndex.ts Co-authored-by: Wojciech Mista <[email protected]> --------- Co-authored-by: Wojciech Mista <[email protected]>
- Loading branch information
Showing
5 changed files
with
111 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@saleor/macaw-ui": patch | ||
--- | ||
|
||
You can now see selected option in combobox dropdown when provided value does not match same referance with options |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
UseComboboxStateChangeTypes, | ||
UseSelectStateChangeTypes, | ||
useCombobox as useDownshiftCombobox, | ||
useSelect as useDownshiftSelect, | ||
} from "downshift"; | ||
import { useEffect, useState } from "react"; | ||
|
||
import { Option } from "~/components/BaseSelect"; | ||
|
||
export function useHighlightedIndex<T extends Option>( | ||
items: T[], | ||
selectedItem: T | null | undefined | ||
): { | ||
highlightedIndex: number | undefined; | ||
onHighlightedIndexChange: ( | ||
index: number | undefined, | ||
type: UseComboboxStateChangeTypes | UseSelectStateChangeTypes | ||
) => void; | ||
} { | ||
// Initially we don't show any item as highlighted | ||
const [highlightedIndex, setHighlightedIndex] = useState<number | undefined>( | ||
-1 | ||
); | ||
|
||
// When data from API comes we can calulate intial highlighted index | ||
// Or when we change selected item | ||
useEffect(() => { | ||
// If we don't have selected item leave highlighted index as -1 | ||
if (!selectedItem) { | ||
return; | ||
} | ||
|
||
// Find hilighted index in items to select base on selected item value | ||
// If there is no match, leave highlighted index as -1 | ||
setHighlightedIndex(getIndexToHighlight(items, selectedItem)); | ||
}, [items, selectedItem]); | ||
|
||
const handleHighlightedIndexChange = ( | ||
highlightedIndex: number | undefined, | ||
type: UseComboboxStateChangeTypes | UseSelectStateChangeTypes | ||
) => { | ||
switch (type) { | ||
// Restore highlighted index to -1 when input value is changed and there is no selected item | ||
case useDownshiftCombobox.stateChangeTypes.InputChange: | ||
setHighlightedIndex(!selectedItem ? -1 : highlightedIndex); | ||
break; | ||
// Restore highlighted index to last selected item when leaving menu | ||
case useDownshiftCombobox.stateChangeTypes.MenuMouseLeave: | ||
case useDownshiftSelect.stateChangeTypes.MenuMouseLeave: | ||
setHighlightedIndex( | ||
selectedItem | ||
? getIndexToHighlight(items, selectedItem) | ||
: highlightedIndex | ||
); | ||
break; | ||
case useDownshiftCombobox.stateChangeTypes.ItemClick: | ||
case useDownshiftSelect.stateChangeTypes.ItemClick: | ||
case useDownshiftCombobox.stateChangeTypes.ItemMouseMove: | ||
case useDownshiftSelect.stateChangeTypes.ItemMouseMove: | ||
setHighlightedIndex(highlightedIndex); | ||
break; | ||
} | ||
}; | ||
|
||
return { | ||
highlightedIndex, | ||
onHighlightedIndexChange: handleHighlightedIndexChange, | ||
}; | ||
} | ||
|
||
function getIndexToHighlight<T extends Option>( | ||
items: T[], | ||
selectedItem: T | ||
): number { | ||
if (typeof selectedItem === "string") { | ||
return items.findIndex((item) => item.value === selectedItem); | ||
} | ||
|
||
return items.findIndex((item) => item.value === selectedItem?.value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters