Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(web): keep pending filter selections when the filter options refresh (#20581)

Xavierandoba654ee451c0
diff --git a/web/src/components/list-filter-bar/__tests__/filter-popover.test.tsx b/web/src/components/list-filter-bar/__tests__/filter-popover.test.tsx
new file mode 100644
index 0000000000..f9b8e6e37a
--- /dev/null
+++ b/web/src/components/list-filter-bar/__tests__/filter-popover.test.tsx
@@ -0,0 +1,70 @@
+import { fireEvent, render, screen } from '@testing-library/react';
+import { FilterPopover } from '../filter-popover';
+import { FilterCollection } from '../interface';
+
+const buildFilters = (pdfCount: number): FilterCollection[] => [
+ {
+ field: 'type',
+ label: 'Type',
+ list: [
+ { id: 'pdf', label: 'PDF', count: pdfCount },
+ { id: 'docx', label: 'DOCX', count: 1 },
+ ],
+ },
+];
+
+// jsdom does not provide ResizeObserver, which the Radix popover relies on.
+beforeAll(() => {
+ global.ResizeObserver = class {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ };
+});
+
+// The checkbox has no accessible name; it sits next to its label text.
+const checkboxOf = (label: string) =>
+ screen.getByText(label).parentElement!.querySelector('[role="checkbox"]')!;
+
+describe('FilterPopover', () => {
+ it('keeps unsubmitted selections when the filter options refresh', () => {
+ const value = { type: [] };
+ const { rerender } = render(
+ <FilterPopover value={value} filters={buildFilters(3)}>
+ <button>open</button>
+ </FilterPopover>,
+ );
+ fireEvent.click(screen.getByText('open'));
+
+ const pdf = checkboxOf('PDF');
+ fireEvent.click(pdf);
+ expect(pdf).toBeChecked();
+
+ // The document list poll refetches the options with new counts.
+ rerender(
+ <FilterPopover value={value} filters={buildFilters(4)}>
+ <button>open</button>
+ </FilterPopover>,
+ );
+
+ expect(checkboxOf('PDF')).toBeChecked();
+ });
+
+ it('takes a newly applied value', () => {
+ const { rerender } = render(
+ <FilterPopover value={{ type: [] }} filters={buildFilters(3)}>
+ <button>open</button>
+ </FilterPopover>,
+ );
+ fireEvent.click(screen.getByText('open'));
+ expect(checkboxOf('DOCX')).not.toBeChecked();
+
+ rerender(
+ <FilterPopover value={{ type: ['docx'] }} filters={buildFilters(3)}>
+ <button>open</button>
+ </FilterPopover>,
+ );
+
+ expect(checkboxOf('DOCX')).toBeChecked();
+ });
+});
diff --git a/web/src/components/list-filter-bar/filter-popover.tsx b/web/src/components/list-filter-bar/filter-popover.tsx
index a870e50b0b..5ad0f27731 100644
--- a/web/src/components/list-filter-bar/filter-popover.tsx
+++ b/web/src/components/list-filter-bar/filter-popover.tsx
@@ -25,6 +25,7 @@ import {
useCallback,
useEffect,
useMemo,
+ useRef,
useState,
} from 'react';
import { useForm } from 'react-hook-form';
@@ -158,11 +159,18 @@ function CheckboxFormMultiple({
setOpen(false);
}, [fieldsDict, onChange, setOpen]);
+ // The form takes the applied value when the popover opens and when a new
+ // value is applied, never when the filter options refresh: the document list
+ // refetches them on every poll while documents parse, and a reset there
+ // would discard the selections not yet submitted.
+ const filtersReady = filters.length > 0;
+ const fieldsDictRef = useRef(fieldsDict);
+ fieldsDictRef.current = fieldsDict;
useEffect(() => {
- if (filters.length > 0) {
- form.reset(value || fieldsDict);
+ if (filtersReady) {
+ form.reset(value || fieldsDictRef.current);
}
- }, [form, value, filters, fieldsDict]);
+ }, [form, value, filtersReady]);
const filterList = useMemo(() => {
const filterSet = filterGroup