From df067b2c6466a521420296890e370de750a8b367 Mon Sep 17 00:00:00 2001 From: gjulivan Date: Fri, 6 Nov 2026 09:36:57 +0100 Subject: [PATCH] fix: datagrid filter set limit when no lazy load --- .../datagrid-dropdown-filter-web/CHANGELOG.md | 1 + .../datagrid-web/typings/DatagridProps.d.ts | 4 +- .../src/__tests__/RefFilterStore.spec.ts | 139 +++++++++++++++++- .../src/stores/RefFilterStore.ts | 8 +- 4 files changed, 147 insertions(+), 5 deletions(-) diff --git a/packages/pluggableWidgets/datagrid-dropdown-filter-web/CHANGELOG.md b/packages/pluggableWidgets/datagrid-dropdown-filter-web/CHANGELOG.md index c45f57280c..384d7299af 100644 --- a/packages/pluggableWidgets/datagrid-dropdown-filter-web/CHANGELOG.md +++ b/packages/pluggableWidgets/datagrid-dropdown-filter-web/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Fixed - We fixed an issue where pages with many drop-down filters became slow to scroll. +- We fixed an issue where, with lazy loading turned off, the drop-down filter loaded only part of the options after the user returned to the page. Because of this, a selected option could lose its caption and the filter was no longer applied to the data grid. ## [3.11.3] - 2026-07-27 diff --git a/packages/pluggableWidgets/datagrid-web/typings/DatagridProps.d.ts b/packages/pluggableWidgets/datagrid-web/typings/DatagridProps.d.ts index 6edf2ec82c..875acf213a 100644 --- a/packages/pluggableWidgets/datagrid-web/typings/DatagridProps.d.ts +++ b/packages/pluggableWidgets/datagrid-web/typings/DatagridProps.d.ts @@ -52,9 +52,9 @@ export interface ColumnsType { hidable: HidableEnum; allowEventPropagation: boolean; width: WidthEnum; + size: number; minWidth: MinWidthEnum; minWidthLimit: number; - size: number; alignment: AlignmentEnum; columnClass?: ListExpressionValue; wrapText: boolean; @@ -99,9 +99,9 @@ export interface ColumnsPreviewType { hidable: HidableEnum; allowEventPropagation: boolean; width: WidthEnum; + size: number | null; minWidth: MinWidthEnum; minWidthLimit: number | null; - size: number | null; alignment: AlignmentEnum; columnClass: string; wrapText: boolean; diff --git a/packages/shared/widget-plugin-dropdown-filter/src/__tests__/RefFilterStore.spec.ts b/packages/shared/widget-plugin-dropdown-filter/src/__tests__/RefFilterStore.spec.ts index c7b800b61f..93611de993 100644 --- a/packages/shared/widget-plugin-dropdown-filter/src/__tests__/RefFilterStore.spec.ts +++ b/packages/shared/widget-plugin-dropdown-filter/src/__tests__/RefFilterStore.spec.ts @@ -1,3 +1,27 @@ +jest.mock( + "mendix/filters/builders", + () => { + const node = + (type: string) => + (...args: unknown[]) => ({ type, args }); + return { + association: node("association"), + attribute: node("attribute"), + contains: node("contains"), + equals: node("equals"), + literal: node("literal"), + or: node("or"), + and: node("and"), + notEqual: node("notEqual") + }; + }, + { virtual: true } +); + +import { AssociationMetaData, AttributeMetaData, ListValue, ObjectItem } from "mendix"; +import { GateProvider } from "@mendix/widget-plugin-mobx-kit/GateProvider"; +import { RefFilterStore, RefFilterStoreProps } from "../stores/RefFilterStore"; + // describe("RefFilterStore", () => { // afterEach(() => _resetGlobalState()); @@ -249,5 +273,118 @@ // }); describe("RefFilterStore", () => { - it.todo("should be implemented"); + describe("options datasource limit", () => { + beforeEach(() => jest.useFakeTimers()); + afterEach(() => jest.useRealTimers()); + + it("does not change the limit in the constructor when lazy loading is off", () => { + const { datasource } = setupStore({ fetchOptionsLazy: false }, { run: false }); + expect(datasource.setLimit).not.toHaveBeenCalled(); + }); + + it("resets a restored limit to the default on setup when lazy loading is off", () => { + // The runtime restores the last used limit when the user returns to the page. + const { datasource } = setupStore({ fetchOptionsLazy: false }, { limit: 100 }); + expect(datasource.setLimit).toHaveBeenCalledTimes(1); + expect(datasource.setLimit).toHaveBeenLastCalledWith(undefined); + }); + + it("does not limit options on search or search clear when lazy loading is off", () => { + const { store, datasource } = setupStore({ fetchOptionsLazy: false }); + datasource.setLimit.mockClear(); + + store.search.setBuffer("ZZAccount"); + jest.advanceTimersByTime(SEARCH_DELAY); + expect(datasource.setFilter).toHaveBeenCalledTimes(1); + + store.search.clear(); + jest.advanceTimersByTime(SEARCH_DELAY); + expect(datasource.setFilter).toHaveBeenCalledTimes(2); + expect(datasource.setFilter).toHaveBeenLastCalledWith(undefined); + + expect(datasource.setLimit).not.toHaveBeenCalled(); + }); + + it("sets limit to 0 in the constructor when lazy loading is on", () => { + const { datasource } = setupStore({ fetchOptionsLazy: true }, { run: false }); + expect(datasource.setLimit).toHaveBeenLastCalledWith(0); + }); + + it("limits options on search when lazy loading is on", () => { + const { store, datasource } = setupStore({ fetchOptionsLazy: true }); + datasource.setLimit.mockClear(); + + store.search.setBuffer("ZZAccount"); + jest.advanceTimersByTime(SEARCH_DELAY); + + expect(datasource.setLimit).toHaveBeenLastCalledWith(100); + }); + }); + + describe("selected options", () => { + it("shows the caption of a restored selection outside the first 100 options", () => { + const items = Array.from({ length: 150 }, (_, i) => item(`obj_${i}`)); + const { store } = setupStore({ fetchOptionsLazy: false }, { items }); + + store.fromJSON(["obj_149"]); + + expect(store.selectedOptions).toEqual([{ caption: "Caption obj_149", value: "obj_149", selected: true }]); + }); + }); }); + +const SEARCH_DELAY = 300; + +type MockListValue = ListValue & { setLimit: jest.Mock; setFilter: jest.Mock }; + +function item(id: string): ObjectItem { + return { id } as ObjectItem; +} + +function mockList({ + items = [], + limit = Number.POSITIVE_INFINITY +}: { + items?: ObjectItem[]; + limit?: number; +}): MockListValue { + const list = { + status: "available", + items, + limit, + offset: 0, + hasMoreItems: false, + filter: undefined, + setFilter: jest.fn(), + setOffset: jest.fn(), + setSortOrder: jest.fn(), + requestTotalCount: jest.fn(), + reload: jest.fn() + } as unknown as MockListValue; + list.setLimit = jest.fn((next?: number) => { + (list as { limit: number }).limit = next ?? Number.POSITIVE_INFINITY; + }); + return list; +} + +function setupStore( + props: Pick, + { items, limit, run = true }: { items?: ObjectItem[]; limit?: number; run?: boolean } = {} +): { store: RefFilterStore; datasource: MockListValue } { + const datasource = mockList({ items, limit }); + const gateProvider = new GateProvider({ + ...props, + refEntity: { id: "Module.Task_Account" } as unknown as AssociationMetaData, + refCaption: { get: (obj: ObjectItem) => ({ value: `Caption ${obj.id}` }) }, + refOptions: datasource, + searchAttrId: "Module.Account.Name" as AttributeMetaData["id"] + }); + const store = new RefFilterStore({ gate: gateProvider.gate, initCond: null }); + if (run) { + disposers.push(store.setup()); + } + return { store, datasource }; +} + +const disposers: Array<() => void> = []; +afterEach(() => disposers.splice(0).forEach(dispose => dispose())); diff --git a/packages/shared/widget-plugin-dropdown-filter/src/stores/RefFilterStore.ts b/packages/shared/widget-plugin-dropdown-filter/src/stores/RefFilterStore.ts index 2b71ce96d0..75fecabfa3 100644 --- a/packages/shared/widget-plugin-dropdown-filter/src/stores/RefFilterStore.ts +++ b/packages/shared/widget-plugin-dropdown-filter/src/stores/RefFilterStore.ts @@ -166,7 +166,9 @@ export class RefFilterStore extends BaseSelectStore { ); } else { this.setFetchReady(true); - this.loadMore(); + // The runtime restores the last used limit when the user returns to the page, + // so reset it to the default (no limit) to load all options. + this.datasource.setLimit(undefined); } return dispose; @@ -184,7 +186,9 @@ export class RefFilterStore extends BaseSelectStore { ? contains(attribute(this.searchAttrId), literal(search)) : undefined; this.datasource.setFilter(cond); - this.datasource.setLimit(this.searchSize); + if (this.lazyMode) { + this.datasource.setLimit(this.searchSize); + } }; return [data, effect, { delay: 300 }];