saleor-dashboard/src/components/CollectionWithDividers/CollectionWithDividers.tsx
Dominik Żegleń 935a6f4542
Reduce bundle size (#1103)
* Add analysis tools

* Use deep imports to reduce bundle size

* Remove tslint config

* Remove unused packages

* Remove lodash-es references

* Use root level mui imports

* Remove mui from restricted imports
2021-05-14 10:15:15 +02:00

46 lines
1 KiB
TypeScript

import { Divider } from "@material-ui/core";
import initial from "lodash/initial";
import React from "react";
interface CollectionWithDividersProps<T> {
DividerComponent?: React.FunctionComponent;
renderEmpty?: (collection: T[]) => any;
collection: T[];
renderItem: (
item: T | undefined,
index: number | undefined,
collection: T[]
) => any;
}
function CollectionWithDividers<T>({
collection,
renderItem,
DividerComponent,
renderEmpty
}: CollectionWithDividersProps<T>) {
const hasNoItemsAndPlaceholder = !renderEmpty && !collection.length;
if (hasNoItemsAndPlaceholder) {
return null;
}
if (!collection.length) {
return !!renderEmpty ? renderEmpty(collection) : null;
}
const SelectedDividerComponent = DividerComponent || Divider;
return initial(
collection.reduce(
(result, item, index) => [
...result,
renderItem(item, index, collection),
<SelectedDividerComponent />
],
[]
)
);
}
export default CollectionWithDividers;