-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Here are the main changes: - the combo box takes now a value of type T and not a string anymore - the combo box has a default behavior (filters on the labels) but a custom behaviour can be used - the hook useOutsideClick replaces the previous handleParentDivBlur function - a new story has been added to test the custom behavior Signed-off-by: Clara Ni <[email protected]>
- Loading branch information
Showing
8 changed files
with
279 additions
and
178 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
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export { default as ComboBox, type ComboBoxProps } from './ComboBox'; | ||
export { default, type ComboBoxProps } from './ComboBox'; | ||
|
||
export { default as useDefaultComboBox } from './useDefaultComboBox'; |
56 changes: 56 additions & 0 deletions
56
ui-core/src/components/inputs/ComboBox/useDefaultComboBox.ts
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,56 @@ | ||
import { useMemo, useState } from 'react'; | ||
|
||
const normalizeString = (str: string) => str.normalize('NFD').replace(/[\u0300-\u036f]/g, ''); | ||
|
||
const defaultFilterSuggestions = <T>( | ||
getSuggestionLabel: (suggestion: T) => string, | ||
suggestions: T[], | ||
query: string | ||
) => { | ||
const input = normalizeString(query).trim().toLowerCase(); | ||
if (!input) { | ||
return suggestions; | ||
} | ||
|
||
const getSuggestionScore = (suggestion: T) => { | ||
const suggestionLabel = normalizeString(getSuggestionLabel(suggestion).toLowerCase()); | ||
if (suggestionLabel.startsWith(input)) { | ||
return 2; | ||
} | ||
if (suggestionLabel.includes(input)) { | ||
return 1; | ||
} | ||
return 0; | ||
}; | ||
|
||
return suggestions | ||
.map((suggestion) => ({ | ||
suggestion, | ||
score: getSuggestionScore(suggestion), | ||
})) | ||
.filter(({ score }) => score > 0) | ||
.sort(({ score: scoreA }, { score: scoreB }) => scoreB - scoreA) | ||
.map(({ suggestion }) => suggestion); | ||
}; | ||
|
||
const useDefaultComboBox = <T>(suggestions: T[], getSuggestionLabel: (suggestion: T) => string) => { | ||
const [query, setQuery] = useState(''); | ||
|
||
const filteredSuggestions = useMemo( | ||
() => defaultFilterSuggestions(getSuggestionLabel, suggestions, query), | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[suggestions, query] | ||
); | ||
|
||
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setQuery(e.target.value); | ||
}; | ||
|
||
const resetSuggestions = () => { | ||
setQuery(''); | ||
}; | ||
|
||
return { suggestions: filteredSuggestions, onChange, resetSuggestions }; | ||
}; | ||
|
||
export default useDefaultComboBox; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.