From 88e9d957fd8a57c1133ba9d7310ea7d928f8f9aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Tolnai?= Date: Fri, 20 Dec 2024 11:27:14 +0100 Subject: [PATCH 1/2] add hidden option to AdHocFilters --- .../src/variables/adhoc/AdHocFiltersVariable.tsx | 14 +++++++++----- .../adhoc/AdHocFiltersVariableUrlSyncHandler.ts | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.tsx b/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.tsx index a05cb82c5..4ca33c2dd 100644 --- a/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.tsx +++ b/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.tsx @@ -22,6 +22,8 @@ export interface AdHocFilterWithLabels extends AdHocVariableFilter { valueLabels?: string[]; // this is used to externally trigger edit mode in combobox filter UI forceEdit?: boolean; + // hide the filter from AdHocFiltersVariableRenderer and the URL + hidden?: boolean; } export type AdHocControlsLayout = ControlsLayout | 'combobox'; @@ -402,11 +404,13 @@ export function AdHocFiltersVariableRenderer({ model }: SceneComponentProps - {filters.map((filter, index) => ( - - - - ))} + {filters + .filter((filter) => !filter.hidden) + .map((filter, index) => ( + + + + ))} {!readOnly && } diff --git a/packages/scenes/src/variables/adhoc/AdHocFiltersVariableUrlSyncHandler.ts b/packages/scenes/src/variables/adhoc/AdHocFiltersVariableUrlSyncHandler.ts index bb0c4bc87..56550207c 100644 --- a/packages/scenes/src/variables/adhoc/AdHocFiltersVariableUrlSyncHandler.ts +++ b/packages/scenes/src/variables/adhoc/AdHocFiltersVariableUrlSyncHandler.ts @@ -27,6 +27,7 @@ export class AdHocFiltersVariableUrlSyncHandler implements SceneObjectUrlSyncHan const value = filters .filter(isFilterComplete) + .filter((filter) => !filter.hidden) .map((filter) => toArray(filter).map(escapeUrlPipeDelimiters).join('|')); return { [this.getKey()]: value }; } From f8370fd2ce7324a2a21cfa6c1fa30f53c56be61b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Tolnai?= Date: Fri, 20 Dec 2024 14:42:59 +0100 Subject: [PATCH 2/2] add tests --- .../adhoc/AdHocFiltersVariable.test.tsx | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.test.tsx b/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.test.tsx index 87964784a..b15830515 100644 --- a/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.test.tsx +++ b/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.test.tsx @@ -474,6 +474,16 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => { }); }); + it('does not render hidden filter in url', () => { + const { filtersVar } = setup(); + + act(() => { + filtersVar._updateFilter(filtersVar.state.filters[0], { hidden: true }); + }); + + expect(locationService.getLocation().search).toBe('?var-filters=key2%7C%3D%7Cval2'); + }); + it('overrides state when url has empty key', () => { const { filtersVar } = setup(); @@ -1047,6 +1057,23 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => { expect(await screen.findByText('key2 = val2')).toBeInTheDocument(); }); + it('does not display hidden filters', async () => { + act(() => { + const { filtersVar } = setup(); + + filtersVar.setState({ + filters: [ + ...filtersVar.state.filters, + { key: 'hidden_key', operator: '=', value: 'hidden_val', hidden: true }, + ], + }); + }); + + expect(await screen.findByText('key1 = val1')).toBeInTheDocument(); + expect(await screen.findByText('key2 = val2')).toBeInTheDocument(); + expect(await screen.queryAllByText('hidden_key = hidden_val')).toEqual([]); + }); + it('focusing the input opens the key dropdown', async () => { await userEvent.click(screen.getByRole('combobox'));