Skip to content

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

Fix: The selected invalid dataset cannot be deleted. (#19603)

balibabu12f41c5773d5
diff --git a/web/src/components/ui/__tests__/multi-select.test.tsx b/web/src/components/ui/__tests__/multi-select.test.tsx
index 3915b9a23b..437bf372d0 100644
--- a/web/src/components/ui/__tests__/multi-select.test.tsx
+++ b/web/src/components/ui/__tests__/multi-select.test.tsx
@@ -1,4 +1,4 @@
-import { render, screen } from '@testing-library/react';
+import { fireEvent, render, screen } from '@testing-library/react';
import { MultiSelect } from '../multi-select';
@@ -62,3 +62,59 @@ describe('MultiSelect badge labels', () => {
expect(screen.getByText('z')).toBeTruthy();
});
});
+
+describe('MultiSelect disabled options', () => {
+ const options = [
+ { label: 'Alpha', value: 'a', disabled: true },
+ { label: 'Beta', value: 'b' },
+ ];
+
+ it('removes a disabled selected value via its badge remove icon', () => {
+ const onValueChange = jest.fn();
+ render(
+ <MultiSelect
+ options={options}
+ defaultValue={['a', 'b']}
+ onValueChange={onValueChange}
+ />,
+ );
+
+ const badgeRow = screen.getByText('Alpha').parentElement!;
+ const removeIcon = badgeRow.querySelector('svg.lucide-circle-x');
+ expect(removeIcon).toBeTruthy();
+
+ fireEvent.click(removeIcon!);
+ expect(onValueChange).toHaveBeenCalledWith(['b']);
+ });
+
+ it('clears disabled selected values via the clear icon', () => {
+ const onValueChange = jest.fn();
+ const { container } = render(
+ <MultiSelect
+ options={options}
+ defaultValue={['a', 'b']}
+ onValueChange={onValueChange}
+ />,
+ );
+
+ fireEvent.click(container.querySelector('svg.lucide-x')!);
+ expect(onValueChange).toHaveBeenCalledWith([]);
+ });
+
+ it('select all picks only enabled options', () => {
+ const onValueChange = jest.fn();
+ render(
+ <MultiSelect
+ options={options}
+ defaultValue={[]}
+ onValueChange={onValueChange}
+ />,
+ );
+
+ fireEvent.click(screen.getByRole('button'));
+ // i18next is not initialized in jsdom, so the select-all label renders
+ // empty; it is always the first command item.
+ fireEvent.click(document.querySelector('[cmdk-item]')!);
+ expect(onValueChange).toHaveBeenCalledWith(['b']);
+ });
+});
diff --git a/web/src/components/ui/multi-select.tsx b/web/src/components/ui/multi-select.tsx
index 7f379bce51..b3b8554f47 100644
--- a/web/src/components/ui/multi-select.tsx
+++ b/web/src/components/ui/multi-select.tsx
@@ -288,23 +288,24 @@ export const MultiSelect = React.forwardRef<
);
}, [flatOptions]);
- const preserveDisabledValues = React.useCallback(
- (values: string[]) => {
- const disabledSelectedValues = selectedValues.filter((value) =>
- disabledValueSet.has(value),
- );
-
- return Array.from(
- new Set<string>([...disabledSelectedValues, ...values]),
- );
- },
- [disabledValueSet, selectedValues],
- );
+ const selectableValues = React.useMemo(() => {
+ return flatOptions
+ .filter((option) => !option.disabled)
+ .map((option) => option.value);
+ }, [flatOptions]);
- const canRemoveValue = React.useCallback(
- (value: string) => !disabledValueSet.has(value),
- [disabledValueSet],
- );
+ const allSelectableSelected =
+ selectableValues.length > 0 &&
+ selectableValues.every((value) => selectedValues.includes(value));
+
+ // A disabled option can't be picked in the dropdown, but a value that is
+ // already selected must stay removable — e.g. a knowledge base that had
+ // chunks when it was picked may have been emptied since.
+ const removeValue = (value: string) => {
+ const newSelectedValues = selectedValues.filter((v) => v !== value);
+ setSelectedValues(newSelectedValues);
+ onValueChange(newSelectedValues);
+ };
const handleInputKeyDown = (
event: React.KeyboardEvent<HTMLInputElement>,
@@ -313,16 +314,7 @@ export const MultiSelect = React.forwardRef<
setIsPopoverOpen(true);
} else if (event.key === 'Backspace' && !event.currentTarget.value) {
const newSelectedValues = [...selectedValues];
- const removableIndex = [...newSelectedValues]
- .reverse()
- .findIndex((value) => canRemoveValue(value));
- if (removableIndex < 0) {
- return;
- }
- newSelectedValues.splice(
- newSelectedValues.length - 1 - removableIndex,
- 1,
- );
+ newSelectedValues.pop();
setSelectedValues(newSelectedValues);
onValueChange(newSelectedValues);
}
@@ -341,9 +333,8 @@ export const MultiSelect = React.forwardRef<
};
const handleClear = () => {
- const nextValues = preserveDisabledValues([]);
- setSelectedValues(nextValues);
- onValueChange(nextValues);
+ setSelectedValues([]);
+ onValueChange([]);
};
const handleTogglePopover = () => {
@@ -351,22 +342,17 @@ export const MultiSelect = React.forwardRef<
};
const clearExtraOptions = () => {
- const newSelectedValues = preserveDisabledValues(
- selectedValues.slice(0, maxCount),
- );
+ const newSelectedValues = selectedValues.slice(0, maxCount);
setSelectedValues(newSelectedValues);
onValueChange(newSelectedValues);
};
const toggleAll = () => {
- if (selectedValues.length === flatOptions.length) {
+ if (allSelectableSelected) {
handleClear();
} else {
- const allValues = preserveDisabledValues(
- flatOptions.map((option) => option.value),
- );
- setSelectedValues(allValues);
- onValueChange(allValues);
+ setSelectedValues(selectableValues);
+ onValueChange(selectableValues);
}
};
@@ -437,15 +423,13 @@ export const MultiSelect = React.forwardRef<
>
{label ?? value}
</div>
- {canRemoveValue(value) && (
- <XCircle
- className="h-4 w-4 cursor-pointer"
- onClick={(event) => {
- event.stopPropagation();
- toggleOption(value);
- }}
- />
- )}
+ <XCircle
+ className="h-4 w-4 cursor-pointer"
+ onClick={(event) => {
+ event.stopPropagation();
+ removeValue(value);
+ }}
+ />
</div>
</Badge>
);
@@ -496,7 +480,7 @@ export const MultiSelect = React.forwardRef<
</Button>
</PopoverTrigger>
<PopoverContent
- className="w-auto p-0"
+ className="w-auto min-w-[var(--radix-popover-trigger-width)] p-0"
align="start"
onEscapeKeyDown={() => setIsPopoverOpen(false)}
onFocusOutside={(event) => event.preventDefault()}
@@ -529,7 +513,7 @@ export const MultiSelect = React.forwardRef<
<div
className={cn(
'mr-2 flex h-4 w-4 items-center justify-center rounded-sm border border-primary',
- selectedValues.length === flatOptions.length
+ allSelectableSelected
? 'bg-primary text-primary-foreground'
: 'opacity-50 [&_svg]:invisible',
)}