-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66b59af
commit 93d8c3d
Showing
6 changed files
with
190 additions
and
132 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
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,73 @@ | ||
import { useFilter } from '@react-aria/i18n'; | ||
import { Node } from '@react-types/shared'; | ||
|
||
import { ListItem, ListProps } from '../List'; | ||
import { P } from '../Text'; | ||
|
||
import { StyledList } from './Select.style'; | ||
|
||
type SelectObject = Record<any, any>; | ||
|
||
type SearchableProps<T = SelectObject> = { | ||
items: Node<T>[]; | ||
inputValue: string; | ||
onValueChange: (value: string) => void; | ||
}; | ||
|
||
type SearchableFilter<T = SelectObject> = (value: Node<T>) => boolean; | ||
|
||
type Props<T = SelectObject> = { | ||
items: Node<T>[]; | ||
searchable?: boolean | SearchableFilter<T> | SearchableProps<T>; | ||
searchTerm?: string; | ||
}; | ||
|
||
type InheritAttrs = Omit<ListProps, keyof Props | 'children' | 'items'>; | ||
|
||
type SelectModalListProps<T = SelectObject> = Props<T> & InheritAttrs; | ||
|
||
const SelectModalList = <T extends SelectObject = SelectObject>({ | ||
items: itemsProp, | ||
searchable, | ||
searchTerm, | ||
...props | ||
}: SelectModalListProps<T>): JSX.Element => { | ||
const { contains } = useFilter({ | ||
sensitivity: 'base' | ||
}); | ||
|
||
const isSearchResultList = typeof searchable === 'object'; | ||
|
||
const items = isSearchResultList | ||
? searchable.items | ||
: searchable && searchTerm | ||
? [...(itemsProp || [])]?.filter( | ||
typeof searchable === 'function' ? searchable : (item) => contains(item.textValue, searchTerm) | ||
) | ||
: itemsProp; | ||
|
||
if (!items.length) | ||
return ( | ||
<P align='center'>{isSearchResultList && searchTerm ? `No results found for ${searchTerm}` : 'No options'}</P> | ||
); | ||
|
||
return ( | ||
<StyledList {...props} gap='s' selectionMode='single'> | ||
{items.map((item) => ( | ||
<ListItem | ||
key={item.key} | ||
alignItems='center' | ||
alignSelf='auto' | ||
gap='xs' | ||
justifyContent='space-between' | ||
textValue={item.textValue} | ||
> | ||
{item.rendered} | ||
</ListItem> | ||
))} | ||
</StyledList> | ||
); | ||
}; | ||
|
||
export { SelectModalList }; | ||
export type { SelectModalListProps }; |
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
Oops, something went wrong.