forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-combobox): add
useComboboxFilter()
hook (microsoft#30046)
- Loading branch information
1 parent
1f067f9
commit 42b8015
Showing
8 changed files
with
132 additions
and
43 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-combobox-85fa130b-07a9-4c4c-91e8-51adc92fe126.json
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,7 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "feat: add `useComboboxFilter()` hook", | ||
"packageName": "@fluentui/react-combobox", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-components-d2d4e116-c627-4027-b9f2-9087b343dc5c.json
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,7 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "feat: export `useComboboxFilter()` hook", | ||
"packageName": "@fluentui/react-components", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
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
72 changes: 72 additions & 0 deletions
72
packages/react-components/react-combobox/src/hooks/useComboboxFilter.tsx
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,72 @@ | ||
import * as React from 'react'; | ||
import { Option } from '../Option'; | ||
|
||
type UseComboboxFilterConfig<T extends { children: React.ReactNode; value: string } | string> = { | ||
/** Provides a custom filter for the option. */ | ||
filter?: (optionText: string, query: string) => boolean; | ||
|
||
/** Provides a custom message to display when there are no options. */ | ||
noOptionsMessage?: React.ReactNode; | ||
|
||
/** Provides a way to map an option object to a React key. By default, "value" is used. */ | ||
optionToReactKey?: (option: T) => string; | ||
|
||
/** Provides a way to map an option object to a text used for search. By default, "value" is used. */ | ||
optionToText?: (option: T) => string; | ||
|
||
/** Provides a custom render for the option. */ | ||
renderOption?: (option: T) => JSX.Element; | ||
}; | ||
|
||
function defaultFilter(optionText: string, query: string) { | ||
if (query === '') { | ||
return true; | ||
} | ||
|
||
return optionText.toLowerCase().includes(query.toLowerCase()); | ||
} | ||
|
||
function defaultToString(option: string | { value: string }) { | ||
return typeof option === 'string' ? option : option.value; | ||
} | ||
|
||
export function useComboboxFilter<T extends { children: React.ReactNode; value: string } | string>( | ||
query: string, | ||
options: T[], | ||
config: UseComboboxFilterConfig<T>, | ||
) { | ||
const { | ||
filter = defaultFilter, | ||
noOptionsMessage = "We couldn't find any matches.", | ||
optionToReactKey = defaultToString, | ||
optionToText = defaultToString, | ||
|
||
renderOption = (option: T) => { | ||
if (typeof option === 'string') { | ||
return <Option key={option}>{option}</Option>; | ||
} | ||
|
||
return ( | ||
<Option {...option} key={optionToReactKey(option)} text={optionToText(option)} value={option.value}> | ||
{option.children} | ||
</Option> | ||
); | ||
}, | ||
} = config; | ||
|
||
const filteredOptions = React.useMemo(() => { | ||
const searchValue = query.trim(); | ||
|
||
return options.filter(option => filter(optionToText(option), searchValue)); | ||
}, [options, optionToText, filter, query]); | ||
|
||
if (filteredOptions.length === 0) { | ||
return [ | ||
<Option aria-disabled="true" key="no-results" text=""> | ||
{noOptionsMessage} | ||
</Option>, | ||
]; | ||
} | ||
|
||
return filteredOptions.map(option => renderOption(option)); | ||
} |
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
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