Skip to content

Commit 17ffbbd

Browse files
committed
fix: add sort api for preview
1 parent 564a053 commit 17ffbbd

4 files changed

Lines changed: 48 additions & 16 deletions

File tree

packages/pluggableWidgets/dropdown-sort-web/src/DropdownSort.editorPreview.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function preview(props: DropdownSortPreviewProps): ReactElement {
1717
screenReaderButtonCaption={props.screenReaderButtonCaption}
1818
screenReaderInputCaption={props.screenReaderInputCaption}
1919
styles={parseStyle(props.style)}
20+
isPreview
2021
/>
2122
);
2223
}

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

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Pagination as PagingButtons } from "@mendix/widget-plugin-grid/components/Pagination";
2+
import { getGlobalSortContext, SortAPI } from "@mendix/widget-plugin-sorting/react/context";
23
import classNames from "classnames";
3-
import { createContext, createElement, PropsWithChildren, ReactElement, ReactNode, useContext } from "react";
4+
import { createContext, createElement, PropsWithChildren, ReactElement, ReactNode, useContext, useState } from "react";
45
import { GalleryPreviewProps } from "../typings/GalleryProps";
56
import { LoadMoreButton } from "./components/LoadMore";
67
import "./ui/GalleryPreview.scss";
78

89
const PropsCtx = createContext<GalleryPreviewProps>({} as GalleryPreviewProps);
10+
const SortAPI = getGlobalSortContext({ isPreview: true });
911

1012
function useProps(): GalleryPreviewProps {
1113
return useContext(PropsCtx);
@@ -89,13 +91,16 @@ const TopControls = (): ReactNode => {
8991

9092
const Header = (): ReactNode => {
9193
const props = useProps();
94+
const sortAPI = useProvideSortAPI();
9295

9396
return (
94-
<section className="widget-gallery-header widget-gallery-filter">
95-
<props.filtersPlaceholder.renderer>
96-
<div />
97-
</props.filtersPlaceholder.renderer>
98-
</section>
97+
<SortAPI.Provider value={sortAPI}>
98+
<section className="widget-gallery-header widget-gallery-filter">
99+
<props.filtersPlaceholder.renderer>
100+
<div />
101+
</props.filtersPlaceholder.renderer>
102+
</section>
103+
</SortAPI.Provider>
99104
);
100105
};
101106

@@ -190,3 +195,28 @@ function useCustomPagination(location: "top" | "bottom"): boolean {
190195
const props = useProps();
191196
return props.useCustomPagination && (props.pagingPosition === location || props.pagingPosition === "both");
192197
}
198+
function useProvideSortAPI(): SortAPI {
199+
const [sortAPI] = useState({
200+
version: 1,
201+
host: new SortStoreHost()
202+
} as SortAPI);
203+
204+
return sortAPI;
205+
}
206+
207+
class SortStoreHost {
208+
usedBy: string | null = null;
209+
210+
get sortOrder(): any[] {
211+
return [];
212+
}
213+
214+
observe(_: any): void {}
215+
216+
unobserve(): void {}
217+
218+
lock(id: string): () => void {
219+
this.usedBy = id;
220+
return () => (this.usedBy = null);
221+
}
222+
}

packages/shared/widget-plugin-sorting/src/react/context.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ export interface SortAPI {
1111

1212
const SORT_PATH = "com.mendix.widgets.web.sortable.sortContext";
1313

14-
// this is a magical way to check if we are running in design preview
15-
const isDesignPreview = window.navigator.appVersion?.startsWith("Mendix Modeler");
16-
17-
export function getGlobalSortContext(): Context<SortAPI | null> {
18-
const scope = isDesignPreview ? window.top : window;
14+
export function getGlobalSortContext(
15+
{ isPreview }: { isPreview: boolean } = { isPreview: false }
16+
): Context<SortAPI | null> {
17+
const scope = isPreview ? window.top : window;
1918
return ((scope as any)[SORT_PATH] ??= createContext<SortAPI | null>(null));
2019
}
2120

22-
export function useSortAPI(): Result<SortAPI, Error> {
23-
const api = useContext(getGlobalSortContext());
21+
export function useSortAPI(options: { isPreview: boolean } = { isPreview: false }): Result<SortAPI, Error> {
22+
const api = useContext(getGlobalSortContext(options));
2423
if (api === null) {
2524
return error(new Error("Error: widget is out of context. Please place the widget inside the Gallery header."));
2625
}

packages/shared/widget-plugin-sorting/src/react/hocs/withSortAPI.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { observer } from "mobx-react-lite";
44
import { createElement, FC, ReactElement } from "react";
55
import { SortAPI, useLockSortAPI, useSortAPI } from "../context";
66

7-
export function withSortAPI<P extends object>(Component: FC<P & { sortAPI: SortAPI }>): FC<P> {
7+
export function withSortAPI<P extends object>(
8+
Component: FC<P & { sortAPI: SortAPI }>
9+
): FC<P & { isPreview?: boolean }> {
810
const SortAPIGuard = observer(function SortAPIGuard(props: P & { sortAPI: SortAPI }): ReactElement {
911
const sortAPI = useLockSortAPI(props.sortAPI);
1012

@@ -15,8 +17,8 @@ export function withSortAPI<P extends object>(Component: FC<P & { sortAPI: SortA
1517
return <Component {...props} sortAPI={sortAPI.value} />;
1618
});
1719

18-
function SortAPIInjector(props: P): ReactElement {
19-
const sortAPI = useSortAPI();
20+
function SortAPIInjector(props: P & { isPreview?: boolean }): ReactElement {
21+
const sortAPI = useSortAPI({ isPreview: props.isPreview ?? false });
2022

2123
if (sortAPI.hasError) {
2224
return <Alert bootstrapStyle="danger">{sortAPI.error.message}</Alert>;

0 commit comments

Comments
 (0)