Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datagrid perf fixes part 2 #1262

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ColumnPreview } from "./helpers/ColumnPreview";
import { useSelectActionHelper } from "./helpers/SelectActionHelper";
import { useFocusTargetController } from "@mendix/widget-plugin-grid/keyboard-navigation/useFocusTargetController";
import "./ui/DatagridPreview.scss";
import { HelpersProvider } from "./helpers/helpers-context";

// Fix type definition for Selectable
// TODO: Open PR to fix in appdev.
Expand Down Expand Up @@ -80,67 +81,73 @@ export function preview(props: DatagridPreviewProps): ReactElement {
const eventsController = { getProps: () => Object.create({}) };

return (
<Widget
CellComponent={Cell}
className={props.class}
columnsDraggable={props.columnsDraggable}
columnsFilterable={props.columnsFilterable}
columnsHidable={props.columnsHidable}
columnsResizable={props.columnsResizable}
columnsSortable={props.columnsSortable}
visibleColumns={columns}
availableColumns={[]}
columnsSwap={noop}
columnsCreateSizeSnapshot={noop}
data={data}
emptyPlaceholderRenderer={useCallback(
(renderWrapper: (children: ReactNode) => ReactElement) => (
<EmptyPlaceholder caption="Empty list message: Place widgets here">
{renderWrapper(null)}
</EmptyPlaceholder>
),
[EmptyPlaceholder]
)}
exporting={false}
filterRenderer={useCallback(
(renderWrapper, columnIndex) => {
const column = props.columns.at(columnIndex);
return column?.filter ? (
<column.filter.renderer caption="Place filter widget here">
<HelpersProvider
value={{
selectActionHelper,
selectionHelper: undefined,
focusController,
cellEventsController: eventsController,
checkboxEventsController: eventsController,
preview: true
}}
>
<Widget
CellComponent={Cell}
className={props.class}
columnsDraggable={props.columnsDraggable}
columnsFilterable={props.columnsFilterable}
columnsHidable={props.columnsHidable}
columnsResizable={props.columnsResizable}
columnsSortable={props.columnsSortable}
visibleColumns={columns}
availableColumns={[]}
columnsSwap={noop}
columnsCreateSizeSnapshot={noop}
data={data}
emptyPlaceholderRenderer={useCallback(
(renderWrapper: (children: ReactNode) => ReactElement) => (
<EmptyPlaceholder caption="Empty list message: Place widgets here">
{renderWrapper(null)}
</column.filter.renderer>
) : (
renderWrapper(null)
);
},
[props.columns]
)}
headerContent={
<props.filtersPlaceholder.renderer caption="Place widgets like filter widget(s) and action button(s) here">
<div />
</props.filtersPlaceholder.renderer>
}
hasMoreItems={false}
headerWrapperRenderer={selectableWrapperRenderer(previewColumns)}
numberOfItems={props.pageSize ?? numberOfItems}
page={0}
paginationType={props.pagination}
pageSize={props.pageSize ?? numberOfItems}
showPagingButtons={props.showPagingButtons}
loadMoreButtonCaption={props.loadMoreButtonCaption}
paging={props.pagination === "buttons"}
pagingPosition={props.pagingPosition}
preview
processedRows={0}
styles={parseStyle(props.style)}
selectionStatus={"none"}
id={gridId}
gridInteractive={!!(props.itemSelection !== "None" || props.onClick)}
selectActionHelper={selectActionHelper}
cellEventsController={eventsController}
checkboxEventsController={eventsController}
focusController={focusController}
/>
</EmptyPlaceholder>
),
[EmptyPlaceholder]
)}
exporting={false}
filterRenderer={useCallback(
(renderWrapper, columnIndex) => {
const column = props.columns.at(columnIndex);
return column?.filter ? (
<column.filter.renderer caption="Place filter widget here">
{renderWrapper(null)}
</column.filter.renderer>
) : (
renderWrapper(null)
);
},
[props.columns]
)}
headerContent={
<props.filtersPlaceholder.renderer caption="Place widgets like filter widget(s) and action button(s) here">
<div />
</props.filtersPlaceholder.renderer>
}
hasMoreItems={false}
headerWrapperRenderer={selectableWrapperRenderer(previewColumns)}
numberOfItems={props.pageSize ?? numberOfItems}
page={0}
paginationType={props.pagination}
pageSize={props.pageSize ?? numberOfItems}
showPagingButtons={props.showPagingButtons}
loadMoreButtonCaption={props.loadMoreButtonCaption}
paging={props.pagination === "buttons"}
pagingPosition={props.pagingPosition}
preview
processedRows={0}
styles={parseStyle(props.style)}
id={gridId}
gridInteractive={!!(props.itemSelection !== "None" || props.onClick)}
/>
</HelpersProvider>
);
}

Expand Down
168 changes: 70 additions & 98 deletions packages/pluggableWidgets/datagrid-web/src/Datagrid.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { useSelectionHelper } from "@mendix/widget-plugin-grid/selection";
import { useOnResetFiltersEvent } from "@mendix/widget-plugin-external-events/hooks";
import { generateUUID } from "@mendix/widget-plugin-platform/framework/generate-uuid";
import { observer } from "mobx-react-lite";
import { ReactElement, ReactNode, createElement, useCallback, useEffect, useMemo } from "react";
import { DatagridContainerProps } from "../typings/DatagridProps";
import { Cell } from "./components/Cell";
import { Widget } from "./components/Widget";
import { WidgetHeaderContext } from "./components/WidgetHeaderContext";
import "./ui/Datagrid.scss";
import { useShowPagination } from "./utils/useShowPagination";
import { useSelectActionHelper } from "./helpers/SelectActionHelper";
import { useClickActionHelper } from "@mendix/widget-plugin-grid/helpers/ClickActionHelper";
import { useCellEventsController } from "./features/row-interaction/CellEventsController";
import { useCheckboxEventsController } from "./features/row-interaction/CheckboxEventsController";
import { useFocusTargetController } from "@mendix/widget-plugin-grid/keyboard-navigation/useFocusTargetController";
import { useOnResetFiltersEvent } from "@mendix/widget-plugin-external-events/hooks";
import { ProgressStore } from "./features/data-export/ProgressStore";
import { useDataExport } from "./features/data-export/useDataExport";
import { IColumnGroupStore } from "./helpers/state/ColumnGroupStore";
import { observer } from "mobx-react-lite";
import { RootGridStore } from "./helpers/state/RootGridStore";
import { useRootStore } from "./helpers/state/useRootStore";
import { useDataExport } from "./features/data-export/useDataExport";
import { ProgressStore } from "./features/data-export/ProgressStore";
import "./ui/Datagrid.scss";
import { useShowPagination } from "./utils/useShowPagination";
import { HelpersProvider, useProvideGridHelpers } from "./helpers/helpers-context";

interface Props extends DatagridContainerProps {
columnsStore: IColumnGroupStore;
Expand Down Expand Up @@ -64,96 +59,73 @@ const Container = observer((props: Props): ReactElement => {
[props.datasource, props.pageSize, isInfiniteLoad, currentPage]
);

const selectionHelper = useSelectionHelper(props.itemSelection, props.datasource, props.onSelectionChange);

const selectActionHelper = useSelectActionHelper(props, selectionHelper);

const clickActionHelper = useClickActionHelper({
onClickTrigger: props.onClickTrigger,
onClick: props.onClick
});
useOnResetFiltersEvent(props.rootStore.staticInfo.name, props.rootStore.staticInfo.filtersChannelName);

const visibleColumnsCount = selectActionHelper.showCheckboxColumn
? columnsStore.visibleColumns.length + 1
: columnsStore.visibleColumns.length;

const focusController = useFocusTargetController({
rows: items.length,
columns: visibleColumnsCount,
pageSize: props.pageSize
});

const cellEventsController = useCellEventsController(selectActionHelper, clickActionHelper, focusController);

const checkboxEventsController = useCheckboxEventsController(selectActionHelper, focusController);
const helpers = useProvideGridHelpers(props);

return (
<Widget
className={props.class}
CellComponent={Cell}
columnsDraggable={props.columnsDraggable}
columnsFilterable={props.columnsFilterable}
columnsHidable={props.columnsHidable}
columnsResizable={props.columnsResizable}
columnsSortable={props.columnsSortable}
data={items}
emptyPlaceholderRenderer={useCallback(
(renderWrapper: (children: ReactNode) => ReactElement) =>
props.showEmptyPlaceholder === "custom" ? renderWrapper(props.emptyPlaceholder) : <div />,
[props.emptyPlaceholder, props.showEmptyPlaceholder]
)}
filterRenderer={useCallback(
(renderWrapper, columnIndex) => {
const columnFilter = columnsStore.columnFilters[columnIndex];
return renderWrapper(columnFilter.renderFilterWidgets());
},
[columnsStore.columnFilters]
)}
headerTitle={props.filterSectionTitle?.value}
headerContent={
props.filtersPlaceholder && (
<WidgetHeaderContext selectionHelper={selectionHelper} filtersStore={rootStore.headerFiltersStore}>
{props.filtersPlaceholder}
</WidgetHeaderContext>
)
}
hasMoreItems={props.datasource.hasMoreItems ?? false}
headerWrapperRenderer={useCallback((_columnIndex: number, header: ReactElement) => header, [])}
id={useMemo(() => `DataGrid${generateUUID()}`, [])}
numberOfItems={props.datasource.totalCount}
onExportCancel={abortExport}
page={currentPage}
pageSize={props.pageSize}
paginationType={props.pagination}
loadMoreButtonCaption={props.loadMoreButtonCaption?.value}
paging={useShowPagination({
pagination: props.pagination,
showPagingButtons: props.showPagingButtons,
totalCount: props.datasource.totalCount,
limit: props.datasource.limit
})}
pagingPosition={props.pagingPosition}
showPagingButtons={props.showPagingButtons}
rowClass={useCallback((value: any) => props.rowClass?.get(value)?.value ?? "", [props.rowClass])}
gridInteractive={!!(props.itemSelection || props.onClick)}
setPage={setPage}
styles={props.style}
selectionStatus={selectionHelper?.type === "Multi" ? selectionHelper.selectionStatus : "unknown"}
exporting={exportProgress.exporting}
processedRows={exportProgress.loaded}
exportDialogLabel={props.exportDialogLabel?.value}
cancelExportLabel={props.cancelExportLabel?.value}
selectRowLabel={props.selectRowLabel?.value}
visibleColumns={columnsStore.visibleColumns}
availableColumns={columnsStore.availableColumns}
columnsCreateSizeSnapshot={() => columnsStore.createSizeSnapshot()}
columnsSwap={(moved, [target, placement]) => columnsStore.swapColumns(moved, [target, placement])}
selectActionHelper={selectActionHelper}
cellEventsController={cellEventsController}
checkboxEventsController={checkboxEventsController}
focusController={focusController}
/>
<HelpersProvider value={helpers}>
<Widget
className={props.class}
CellComponent={Cell}
columnsDraggable={props.columnsDraggable}
columnsFilterable={props.columnsFilterable}
columnsHidable={props.columnsHidable}
columnsResizable={props.columnsResizable}
columnsSortable={props.columnsSortable}
data={items}
emptyPlaceholderRenderer={useCallback(
(renderWrapper: (children: ReactNode) => ReactElement) =>
props.showEmptyPlaceholder === "custom" ? renderWrapper(props.emptyPlaceholder) : <div />,
[props.emptyPlaceholder, props.showEmptyPlaceholder]
)}
filterRenderer={useCallback(
(renderWrapper, columnIndex) => {
const columnFilter = columnsStore.columnFilters[columnIndex];
return renderWrapper(columnFilter.renderFilterWidgets());
},
[columnsStore.columnFilters]
)}
headerTitle={props.filterSectionTitle?.value}
headerContent={
props.filtersPlaceholder && (
<WidgetHeaderContext filtersStore={rootStore.headerFiltersStore}>
{props.filtersPlaceholder}
</WidgetHeaderContext>
)
}
hasMoreItems={props.datasource.hasMoreItems ?? false}
headerWrapperRenderer={useCallback((_columnIndex: number, header: ReactElement) => header, [])}
id={useMemo(() => `DataGrid${generateUUID()}`, [])}
numberOfItems={props.datasource.totalCount}
onExportCancel={abortExport}
page={currentPage}
pageSize={props.pageSize}
paginationType={props.pagination}
loadMoreButtonCaption={props.loadMoreButtonCaption?.value}
paging={useShowPagination({
pagination: props.pagination,
showPagingButtons: props.showPagingButtons,
totalCount: props.datasource.totalCount,
limit: props.datasource.limit
})}
pagingPosition={props.pagingPosition}
showPagingButtons={props.showPagingButtons}
rowClass={useCallback((value: any) => props.rowClass?.get(value)?.value ?? "", [props.rowClass])}
gridInteractive={!!(props.itemSelection || props.onClick)}
setPage={setPage}
styles={props.style}
exporting={exportProgress.exporting}
processedRows={exportProgress.loaded}
exportDialogLabel={props.exportDialogLabel?.value}
cancelExportLabel={props.cancelExportLabel?.value}
selectRowLabel={props.selectRowLabel?.value}
visibleColumns={columnsStore.visibleColumns}
availableColumns={columnsStore.availableColumns}
columnsCreateSizeSnapshot={() => columnsStore.createSizeSnapshot()}
columnsSwap={(moved, [target, placement]) => columnsStore.swapColumns(moved, [target, placement])}
/>
</HelpersProvider>
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("Datagrid", () => {
});
window.IntersectionObserver = mockIntersectionObserver;
});
it("should not take more then 9s to hide 3 column", async () => {
it("should not take eternity to hide 3 column", async () => {
const props: DatagridContainerProps = {
advanced: false,
name: "datagrid",
Expand Down
15 changes: 5 additions & 10 deletions packages/pluggableWidgets/datagrid-web/src/components/Cell.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { createElement, ReactElement, useMemo } from "react";
import { observer } from "mobx-react-lite";
import { computed } from "mobx";
import { createElement, ReactElement, useMemo, memo } from "react";
import { GridColumn } from "../typings/GridColumn";
import { CellComponentProps } from "../typings/CellComponent";
import { CellElement } from "./CellElement";
import { useFocusTargetProps } from "@mendix/widget-plugin-grid/keyboard-navigation/useFocusTargetProps";

// eslint-disable-next-line prefer-arrow-callback
const component = observer(function Cell(props: CellComponentProps<GridColumn>): ReactElement {
function CellNode(props: CellComponentProps<GridColumn>): ReactElement {
const keyNavProps = useFocusTargetProps<HTMLDivElement>({
columnIndex: props.columnIndex ?? -1,
rowIndex: props.rowIndex
});
const handlers = useMemo(() => props.eventsController.getProps(props.item), [props.item, props.eventsController]);
const children = computed(() => props.column.renderCellContent(props.item)).get();

return (
<CellElement
Expand All @@ -33,10 +29,9 @@ const component = observer(function Cell(props: CellComponentProps<GridColumn>):
onKeyUp={handlers.onKeyUp}
onFocus={handlers.onFocus}
>
{children}
{props.column.renderCellContent(props.item)}
</CellElement>
);
});
}

// Override NamedExoticComponent type
export const Cell = component as (props: CellComponentProps<GridColumn>) => ReactElement;
export const Cell = memo(CellNode) as (props: CellComponentProps<GridColumn>) => ReactElement;
Loading
Loading