Skip to content

Commit 9ef25b1

Browse files
committed
feat: add "load more" button to gallery
1 parent fc4126f commit 9ef25b1

File tree

6 files changed

+55
-5
lines changed

6 files changed

+55
-5
lines changed

packages/modules/data-widgets/src/themesource/datawidgets/web/_gallery.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,8 @@ $gallery-screen-md: 768px;
8888
.widget-gallery-item-button {
8989
width: inherit;
9090
}
91+
92+
.widget-gallery-load-more {
93+
display: flex;
94+
justify-content: center;
95+
}

packages/pluggableWidgets/gallery-web/src/components/Gallery.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { GalleryTopBar } from "./GalleryTopBar";
1313
import { ListBox } from "./ListBox";
1414
import { ListItem } from "./ListItem";
1515

16+
import { LoadMore, LoadMoreButton as LoadMorePreview } from "src/components/LoadMore";
1617
import { PaginationEnum, ShowPagingButtonsEnum } from "typings/GalleryProps";
1718
import { ItemEventsController } from "../typings/ItemEventsController";
1819

@@ -69,8 +70,10 @@ export function Gallery<T extends ObjectItem>(props: GalleryProps<T>): ReactElem
6970
</div>
7071
) : null;
7172

72-
const showTopBar = props.paging && (props.paginationPosition === "top" || props.paginationPosition === "both");
73-
const showFooter = props.paging && (props.paginationPosition === "bottom" || props.paginationPosition === "both");
73+
const showTopPagination =
74+
props.paging && (props.paginationPosition === "top" || props.paginationPosition === "both");
75+
const showBottomPagination =
76+
props.paging && (props.paginationPosition === "bottom" || props.paginationPosition === "both");
7477

7578
return (
7679
<GalleryRoot
@@ -79,7 +82,7 @@ export function Gallery<T extends ObjectItem>(props: GalleryProps<T>): ReactElem
7982
selectable={false}
8083
data-focusindex={props.tabIndex || 0}
8184
>
82-
{showTopBar && <GalleryTopBar>{pagination}</GalleryTopBar>}
85+
<GalleryTopBar>{showTopPagination && pagination}</GalleryTopBar>
8386
{props.showHeader && <GalleryHeader aria-label={props.headerTitle}>{props.header}</GalleryHeader>}
8487
<GalleryContent hasMoreItems={props.hasMoreItems} setPage={props.setPage} isInfinite={!props.paging}>
8588
{props.items.length > 0 && (
@@ -115,7 +118,15 @@ export function Gallery<T extends ObjectItem>(props: GalleryProps<T>): ReactElem
115118
<div className="empty-placeholder">{children}</div>
116119
</section>
117120
))}
118-
{showFooter && <GalleryFooter>{pagination}</GalleryFooter>}
121+
<GalleryFooter>
122+
{showBottomPagination && pagination}
123+
<div className="widget-gallery-load-more">
124+
{props.preview && props.paginationType === "loadMore" && (
125+
<LoadMorePreview>Load more</LoadMorePreview>
126+
)}
127+
{!props.preview && <LoadMore>Load more</LoadMore>}
128+
</div>
129+
</GalleryFooter>
119130
</GalleryRoot>
120131
);
121132
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import cn from "classnames";
2+
import { observer } from "mobx-react-lite";
3+
import { createElement } from "react";
4+
import { useGalleryRootScope } from "src/helpers/root-context";
5+
6+
export function LoadMoreButton(props: JSX.IntrinsicElements["button"]): React.ReactNode {
7+
return <button {...props} className={cn("btn btn-primary widget-gallery-load-more-btn", props.className)}></button>;
8+
}
9+
10+
export const LoadMore = observer(function LoadMore(props: { children: React.ReactNode }): React.ReactNode {
11+
const {
12+
rootStore: { paging }
13+
} = useGalleryRootScope();
14+
15+
if (paging.pagination !== "loadMore") {
16+
return null;
17+
}
18+
19+
if (!paging.hasMoreItems) {
20+
return null;
21+
}
22+
23+
return <LoadMoreButton onClick={() => paging.setPage(n => n + 1)}>{props.children}</LoadMoreButton>;
24+
});

packages/shared/widget-plugin-grid/src/query/DatasourceController.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ export class DatasourceController implements ReactiveController, QueryController
9090
return this.datasource.totalCount;
9191
}
9292

93+
get hasMoreItems(): boolean {
94+
return this.datasource.hasMoreItems ?? false;
95+
}
96+
9397
/**
9498
* Returns computed value that holds controller copy.
9599
* Recomputes the copy every time the datasource changes.

packages/shared/widget-plugin-grid/src/query/PaginationController.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export class PaginationController implements ReactiveController {
6767
}
6868
}
6969

70+
get hasMoreItems(): boolean {
71+
return this._query.hasMoreItems;
72+
}
73+
7074
private _setInitParams(): void {
7175
if (this.pagination === "buttons" || this.showTotalCount) {
7276
this._query.requestTotalCount(true);

packages/shared/widget-plugin-grid/src/query/query-controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ type Members =
88
| "requestTotalCount"
99
| "totalCount"
1010
| "limit"
11-
| "offset";
11+
| "offset"
12+
| "hasMoreItems";
1213

1314
export interface QueryController extends Pick<ListValue, Members> {
1415
refresh(): void;
1516
setPageSize(size: number): void;
17+
hasMoreItems: boolean;
1618
isLoading: boolean;
1719
isRefreshing: boolean;
1820
isFetchingNextBatch: boolean;

0 commit comments

Comments
 (0)