Skip to content

Commit

Permalink
Various filter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
whscullin committed Nov 22, 2024
1 parent 5b7df62 commit 52cb73e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ function useStringEqualToOrNotBuilder(
setValue={setSearchValue}
/>
);

if (!searchResults && !isLoading) {
return {builder, util: null};
}

const util = (
<ScrollMain>
<ContextMenuContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export const FilterContextBar: React.FC<FilterContextBarProps> = ({
)}
</>
)}
{!showFieldResults && !showValueResults && (
{searchTerm && !showFieldResults && !showValueResults && (
<EmptyMessage>No results</EmptyMessage>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ export const PillInput: React.FC<PillInputProps> = ({
const value = controlledValue || uncontrolledValue;
const setValue = setControlledValue || setUncontrolledValue;

console.info({selectedPill, activeElement: document.activeElement});

const deletePill = useCallback(
(idx: number) => {
const newValues = [...values];
Expand Down Expand Up @@ -120,8 +118,6 @@ export const PillInput: React.FC<PillInputProps> = ({
}, [pills, selectedPill]);

const onKeyUp = (event: React.KeyboardEvent) => {
console.info({event});

if (event.key === 'Backspace') {
if (selectedPill !== undefined) {
deletePill(selectedPill);
Expand Down
33 changes: 17 additions & 16 deletions packages/query-composer/src/core/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -83,15 +83,15 @@ export function numberFilterToString(
return `${quotedField} != null`;
case 'custom':
default:
return `${quotedField}: ${filter.partial}`;
return `${quotedField} ${filter.partial}`;
}
}

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) {
Expand Down Expand Up @@ -193,36 +193,36 @@ export function stringFilterToString(
return `${quotedField} != ''`;
case 'custom':
default:
return `${quotedField}: ${filter.partial}`;
return `${quotedField} ${filter.partial}`;
}
}

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}`;
Expand Down Expand Up @@ -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()};
}
}

Expand Down Expand Up @@ -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()};
}
}

Expand Down Expand Up @@ -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()};
}
}

Expand All @@ -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};
}
Expand All @@ -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$`);
Expand Down
4 changes: 2 additions & 2 deletions packages/query-composer/tests/core/filters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
Expand Down

0 comments on commit 52cb73e

Please sign in to comment.