From 52cb73ef7126a8445a3e5423339ef0e7c51d1415 Mon Sep 17 00:00:00 2001 From: Will Scullin Date: Thu, 21 Nov 2024 16:11:50 -0800 Subject: [PATCH] Various filter fixes --- .../AddFilter/StringFilterBuilder.tsx | 5 +++ .../FilterContextBar/FilterContextBar.tsx | 2 +- .../src/components/PillInput/PillInput.tsx | 4 --- packages/query-composer/src/core/filters.ts | 33 ++++++++++--------- .../query-composer/tests/core/filters.spec.ts | 4 +-- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/packages/query-composer/src/components/AddFilter/StringFilterBuilder.tsx b/packages/query-composer/src/components/AddFilter/StringFilterBuilder.tsx index 5506a572..d90ad0fe 100644 --- a/packages/query-composer/src/components/AddFilter/StringFilterBuilder.tsx +++ b/packages/query-composer/src/components/AddFilter/StringFilterBuilder.tsx @@ -187,6 +187,11 @@ function useStringEqualToOrNotBuilder( setValue={setSearchValue} /> ); + + if (!searchResults && !isLoading) { + return {builder, util: null}; + } + const util = ( diff --git a/packages/query-composer/src/components/FilterContextBar/FilterContextBar.tsx b/packages/query-composer/src/components/FilterContextBar/FilterContextBar.tsx index a4476aaf..f4efcc92 100644 --- a/packages/query-composer/src/components/FilterContextBar/FilterContextBar.tsx +++ b/packages/query-composer/src/components/FilterContextBar/FilterContextBar.tsx @@ -204,7 +204,7 @@ export const FilterContextBar: React.FC = ({ )} )} - {!showFieldResults && !showValueResults && ( + {searchTerm && !showFieldResults && !showValueResults && ( No results )} diff --git a/packages/query-composer/src/components/PillInput/PillInput.tsx b/packages/query-composer/src/components/PillInput/PillInput.tsx index 0d44bfc1..596f2349 100644 --- a/packages/query-composer/src/components/PillInput/PillInput.tsx +++ b/packages/query-composer/src/components/PillInput/PillInput.tsx @@ -70,8 +70,6 @@ export const PillInput: React.FC = ({ const value = controlledValue || uncontrolledValue; const setValue = setControlledValue || setUncontrolledValue; - console.info({selectedPill, activeElement: document.activeElement}); - const deletePill = useCallback( (idx: number) => { const newValues = [...values]; @@ -120,8 +118,6 @@ export const PillInput: React.FC = ({ }, [pills, selectedPill]); const onKeyUp = (event: React.KeyboardEvent) => { - console.info({event}); - if (event.key === 'Backspace') { if (selectedPill !== undefined) { deletePill(selectedPill); diff --git a/packages/query-composer/src/core/filters.ts b/packages/query-composer/src/core/filters.ts index 14688cf6..a8b0a3fe 100644 --- a/packages/query-composer/src/core/filters.ts +++ b/packages/query-composer/src/core/filters.ts @@ -47,7 +47,7 @@ export function numberFilterToString( field: string, filter: NumberFilter ): string { - const quotedField = maybeQuoteIdentifier(field); + const quotedField = field ? maybeQuoteIdentifier(field) : ''; switch (filter.type) { case 'is_equal_to': { if (filter.values.length === 0) { @@ -83,7 +83,7 @@ export function numberFilterToString( return `${quotedField} != null`; case 'custom': default: - return `${quotedField}: ${filter.partial}`; + return `${quotedField} ${filter.partial}`; } } @@ -91,7 +91,7 @@ export function stringFilterToString( field: string, filter: StringFilter ): string { - const quotedField = maybeQuoteIdentifier(field); + const quotedField = field ? maybeQuoteIdentifier(field) : ''; switch (filter.type) { case 'is_equal_to': { if (filter.values.length === 0) { @@ -193,7 +193,7 @@ export function stringFilterToString( return `${quotedField} != ''`; case 'custom': default: - return `${quotedField}: ${filter.partial}`; + return `${quotedField} ${filter.partial}`; } } @@ -201,28 +201,28 @@ export function booleanFilterToString( field: string, filter: BooleanFilter ): string { - const quotedField = maybeQuoteIdentifier(field); + const quotedField = field ? maybeQuoteIdentifier(field) : ''; switch (filter.type) { case 'is_false': return `not ${quotedField}`; case 'is_true': return `${quotedField}`; case 'is_true_or_null': - return `${quotedField}: true | null`; + return `${quotedField} = true | null`; case 'is_false_or_null': - return `${quotedField}: false | null`; + return `${quotedField} = false | null`; case 'is_null': return `${quotedField} = null`; case 'is_not_null': return `${quotedField} != null`; case 'custom': default: - return `${quotedField}: ${filter.partial}`; + return `${quotedField} ${filter.partial}`; } } export function timeFilterToString(field: string, filter: TimeFilter): string { - const quotedField = maybeQuoteIdentifier(field); + const quotedField = field ? maybeQuoteIdentifier(field) : ''; switch (filter.type) { case 'is_in_the_past': return `${quotedField} ? now - ${filter.amount} ${filter.unit} for ${filter.amount} ${filter.unit}`; @@ -365,8 +365,7 @@ export function stringFilterChangeType( return {type}; case 'custom': default: - // TODO extract the partial and fill it in here - return {type, partial: ''}; + return {type, partial: stringFilterToString('', filter).trim()}; } } @@ -418,7 +417,7 @@ export function numberFilterChangeType( case 'custom': default: // TODO extract the partial and fill it in here - return {type, partial: ''}; + return {type, partial: numberFilterToString('', filter).trim()}; } } @@ -458,7 +457,7 @@ export function timeFilterChangeType( case 'custom': default: // TODO extract the partial and fill it in here - return {type, partial: ''}; + return {type, partial: timeFilterToString('', filter).trim()}; } } @@ -469,7 +468,7 @@ export function booleanFilterChangeType( switch (type) { case 'custom': // TODO extract the partial and fill it in here - return {type, partial: ''}; + return {type, partial: booleanFilterToString('', filter).trim()}; default: return {type}; } @@ -491,9 +490,11 @@ const FIELD = `(?:${ID_WITH_DOTS})`; const FALSE_FILTER = new RegExp(`^not (${FIELD})$`); const TRUE_FILTER = new RegExp(`^(${FIELD})$`); -const TRUE_OR_NULL_FILTER = new RegExp(`^(${FIELD}):\\s*true\\s*\\|\\s*null$`); +const TRUE_OR_NULL_FILTER = new RegExp( + `^(${FIELD})\\s*=\\s*true\\s*\\|\\s*null$` +); const FALSE_OR_NULL_FILTER = new RegExp( - `^(${FIELD}):\\s*false\\s*\\|\\s*null$` + `^(${FIELD})\\s*=\\s*false\\s*\\|\\s*null$` ); const NULL_FILTER = new RegExp(`^(${FIELD})\\s*=\\s*null$`); const NOT_NULL_FILTER = new RegExp(`^(${FIELD})\\s*!=\\s*null$`); diff --git a/packages/query-composer/tests/core/filters.spec.ts b/packages/query-composer/tests/core/filters.spec.ts index 314bca4c..07ff07d4 100644 --- a/packages/query-composer/tests/core/filters.spec.ts +++ b/packages/query-composer/tests/core/filters.spec.ts @@ -22,8 +22,8 @@ import { const BOOLEAN_FILTER_TO_STRING: [BooleanFilter, string][] = [ [{type: 'is_true'}, 'a'], [{type: 'is_false'}, 'not a'], - [{type: 'is_true_or_null'}, 'a: true | null'], - [{type: 'is_false_or_null'}, 'a: false | null'], + [{type: 'is_true_or_null'}, 'a = true | null'], + [{type: 'is_false_or_null'}, 'a = false | null'], [{type: 'is_null'}, 'a = null'], [{type: 'is_not_null'}, 'a != null'], ];