-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<Collection> improvements: isPagination/isSearchable (#507)
* <Collection> improvements: isPagination/isSearchable
- Loading branch information
Showing
17 changed files
with
473 additions
and
71 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
117 changes: 108 additions & 9 deletions
117
packages/react/src/primitives/Collection/Collection.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 |
---|---|---|
@@ -1,16 +1,115 @@ | ||
import * as React from 'react'; | ||
import { CollectionProps } from '../types'; | ||
import classNames from 'classnames'; | ||
import debounce from 'lodash/debounce'; | ||
import { useCallback, useState } from 'react'; | ||
import { Flex } from '../Flex'; | ||
import { Grid } from '../Grid'; | ||
import { Pagination, usePagination } from '../Pagination'; | ||
import { SearchField } from '../SearchField'; | ||
import { ComponentClassNames } from '../shared/constants'; | ||
import { SharedText } from '../shared/i18n'; | ||
import { strHasLength } from '../shared/utils'; | ||
import { | ||
CollectionProps, | ||
GridCollectionProps, | ||
ListCollectionProps, | ||
} from '../types'; | ||
import { getItemsAtPage, itemHasText } from './utils'; | ||
|
||
export const Collection = <CollectionItemType,>({ | ||
items, | ||
const DEFAULT_PAGE_SIZE = 10; | ||
const TYPEAHEAD_DELAY_MS = 300; | ||
|
||
const ListCollection = <Item,>({ | ||
children, | ||
className, | ||
direction = 'column', | ||
type = 'list', | ||
items, | ||
...rest | ||
}: CollectionProps<CollectionItemType>) => ( | ||
<Flex direction={direction} className={className} {...rest}> | ||
{Array.isArray(items) && items.map(children)} | ||
}: ListCollectionProps<Item>) => ( | ||
<Flex direction={direction} {...rest}> | ||
{Array.isArray(items) ? items.map(children) : null} | ||
</Flex> | ||
); | ||
|
||
const GridCollection = <Item,>({ | ||
children, | ||
items, | ||
...rest | ||
}: GridCollectionProps<Item>) => ( | ||
<Grid {...rest}>{Array.isArray(items) ? items.map(children) : null}</Grid> | ||
); | ||
|
||
export const Collection = <Item,>({ | ||
className, | ||
isSearchable, | ||
isPaginated, | ||
items, | ||
itemsPerPage = DEFAULT_PAGE_SIZE, | ||
searchFilter = itemHasText, | ||
searchPlaceholder, | ||
type = 'list', | ||
testId, | ||
...rest | ||
}: CollectionProps<Item>): JSX.Element => { | ||
const [searchText, setSearchText] = useState<string>(); | ||
|
||
const onSearch = useCallback(debounce(setSearchText, TYPEAHEAD_DELAY_MS), [ | ||
setSearchText, | ||
]); | ||
|
||
// Make sure that items are iterable | ||
items = Array.isArray(items) ? items : []; | ||
|
||
// Filter items by text | ||
if (isSearchable && strHasLength(searchText)) { | ||
items = items.filter((item) => searchFilter(item, searchText)); | ||
} | ||
|
||
// Pagination | ||
const pagination = usePagination({ | ||
totalPages: Math.floor(items.length / itemsPerPage), | ||
}); | ||
|
||
if (isPaginated) { | ||
items = getItemsAtPage(items, pagination.currentPage, itemsPerPage); | ||
} | ||
|
||
const collection = | ||
type === 'list' ? ( | ||
<ListCollection | ||
className={ComponentClassNames.CollectionItems} | ||
items={items} | ||
{...rest} | ||
/> | ||
) : type === 'grid' ? ( | ||
<GridCollection | ||
className={ComponentClassNames.CollectionItems} | ||
items={items} | ||
{...rest} | ||
/> | ||
) : null; | ||
|
||
return ( | ||
<Flex | ||
testId={testId} | ||
className={classNames(ComponentClassNames.Collection, className)} | ||
> | ||
{isSearchable ? ( | ||
<Flex className={ComponentClassNames.CollectionSearch}> | ||
<SearchField | ||
label={SharedText.Collection.SearchFieldLabel} | ||
placeholder={searchPlaceholder} | ||
onChange={(e) => onSearch(e.target.value)} | ||
onClear={() => setSearchText('')} | ||
/> | ||
</Flex> | ||
) : null} | ||
|
||
{collection} | ||
|
||
{isPaginated ? ( | ||
<Flex className={ComponentClassNames.CollectionPagination}> | ||
<Pagination {...pagination} /> | ||
</Flex> | ||
) : null} | ||
</Flex> | ||
); | ||
}; |
Oops, something went wrong.