fix(filters): sync values when operators change
This commit is contained in:
1 parent
1976622494
commit
8928c5f117
2 files changed
+28
-9
No files matched your search
@@ -45,3 +45,19 @@ test('keeps commas in regex filter values', () => {
|
||||
|
||||
expect(screen.getByLabelText('Filter value')).toHaveValue(value);
|
||||
});
|
||||
|
||||
test('keeps commas when changing to a search operator', () => {
|
||||
const value = '^[a-zA-Z0-9]{21,22}$';
|
||||
const props = {
|
||||
type: 'path',
|
||||
startDate: new Date('2026-08-01'),
|
||||
endDate: new Date('2026-08-02'),
|
||||
name: 'path',
|
||||
value,
|
||||
};
|
||||
const { rerender } = render(<FilterRecord {...props} operator="eq" />);
|
||||
|
||||
rerender(<FilterRecord {...props} operator="re" />);
|
||||
|
||||
expect(screen.getByLabelText('Filter value')).toHaveValue(value);
|
||||
});
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
Select,
|
||||
TextField,
|
||||
} from '@umami/react-zen';
|
||||
import { useState } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Empty } from '@/components/common/Empty';
|
||||
import { MultiSelect, MultiSelectItem } from '@/components/common/MultiSelect';
|
||||
import { useFilters, useFormat, useWebsiteValuesQuery } from '@/components/hooks';
|
||||
@@ -29,6 +29,12 @@ export interface FilterRecordProps {
|
||||
onChange?: (name: string, value: string) => void;
|
||||
}
|
||||
|
||||
function getSelectedValues(value: string | string[], operator: string) {
|
||||
if (Array.isArray(value)) return value;
|
||||
if (!value) return [];
|
||||
return isSearchOperator(operator) ? [value] : value.split(',');
|
||||
}
|
||||
|
||||
export function FilterRecord({
|
||||
websiteId,
|
||||
type,
|
||||
@@ -43,14 +49,7 @@ export function FilterRecord({
|
||||
}: FilterRecordProps) {
|
||||
const { fields, operators } = useFilters();
|
||||
const isSearch = isSearchOperator(operator);
|
||||
const initValues = Array.isArray(value)
|
||||
? value
|
||||
: value
|
||||
? isSearch
|
||||
? [value]
|
||||
: value.split(',')
|
||||
: [];
|
||||
const [selected, setSelected] = useState<string[]>(initValues);
|
||||
const [selected, setSelected] = useState<string[]>(() => getSelectedValues(value, operator));
|
||||
const [search, setSearch] = useState('');
|
||||
const { formatValue } = useFormat();
|
||||
const { data, isLoading } = useWebsiteValuesQuery({
|
||||
@@ -62,6 +61,10 @@ export function FilterRecord({
|
||||
});
|
||||
const items = data?.filter(({ value }) => value) || [];
|
||||
|
||||
useEffect(() => {
|
||||
setSelected(getSelectedValues(value, operator));
|
||||
}, [operator, value]);
|
||||
|
||||
const handleSearch = (value: string) => {
|
||||
setSearch(value);
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user