Skip to content

Commit

Permalink
Add "Not equal to" option to Text filter search (#1652)
Browse files Browse the repository at this point in the history
* Add Not equal to option for text filter

* Update and refactor getFocusedElementId
  • Loading branch information
laurakwhit authored Sep 28, 2023
1 parent 423ecfc commit 97bdaa0
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 44 deletions.
23 changes: 17 additions & 6 deletions src/lib/components/workflow/filter-search/conditional-menu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,36 @@
defaultConditionOptions;
export let disabled = false;
export let inputId: string;
export let noBorderLeft = false;
export let noBorderRight = false;
$: options, ($filter.conditional = options[0].value);
$: selectedOption =
options.find((o) => o.value === $filter.conditional) ?? options[0];
$: filterConditionalOption = options.find(
(o) => o.value === $filter.conditional,
);
$: options, updateFilterConditional();
$: selectedOption = filterConditionalOption ?? options[0];
$: selectedLabel = selectedOption?.label ?? selectedOption?.value;
function updateFilterConditional() {
if (!filterConditionalOption) {
$filter.conditional = options[0].value;
}
}
</script>

<MenuContainer>
<MenuButton
unround
class="!border-l-0 !border-r-0"
class="{noBorderRight ? '!border-r-0' : ''} {noBorderLeft
? '!border-l-0'
: ''} whitespace-nowrap"
id="conditional-menu-button"
controls="conditional-menu"
{disabled}
>
{selectedLabel}
</MenuButton>
<Menu id="conditional-menu">
<Menu id="conditional-menu" class="whitespace-nowrap">
{#each options as { value, label }}
<MenuItem
on:click={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
{ value: 'BETWEEN', label: translate('between') },
{ value: '>=', label: translate('after') },
]}
noBorderLeft
noBorderRight
/>
<MenuContainer>
<MenuButton
Expand Down
17 changes: 1 addition & 16 deletions src/lib/components/workflow/filter-search/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import { workflowFilters } from '$lib/stores/filters';
import { refresh } from '$lib/stores/workflows';
import {
getFocusedElementId,
isBooleanFilter,
isDateTimeFilter,
isListFilter,
isNumberFilter,
isStatusFilter,
isTextFilter,
Expand Down Expand Up @@ -99,21 +99,6 @@
onSearch();
}
function getFocusedElementId(attribute: string) {
if (isStatusFilter(attribute)) return 'status-filter';
if (isTextFilter(attribute)) return 'text-filter-search';
if (isListFilter(attribute)) return 'list-filter-search';
if (isNumberFilter(attribute) || isDateTimeFilter(attribute))
return 'conditional-menu-button';
if (isBooleanFilter(attribute)) return 'boolean-filter';
return '';
}
function updateFocusedElementId() {
if ($activeQueryIndex !== null) {
$focusedElementId = getFocusedElementId($filter.attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</script>

<div class="flex items-center">
<ConditionalMenu inputId="number-filter-search" />
<ConditionalMenu inputId="number-filter-search" noBorderLeft noBorderRight />
<NumberInput
label={translate('number-input-placeholder')}
labelHidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@
import { workflowFilters } from '$lib/stores/filters';
import { sortedSearchAttributeOptions } from '$lib/stores/search-attributes';
import {
isBooleanFilter,
isDateTimeFilter,
getFocusedElementId,
isListFilter,
isNumberFilter,
isStatusFilter,
isTextFilter,
} from '$lib/utilities/query/filter-search';
import { emptyFilter } from '$lib/utilities/query/to-list-workflow-filters';
Expand All @@ -28,24 +24,11 @@
const { filter, activeQueryIndex, focusedElementId } =
getContext<FilterContext>(FILTER_CONTEXT);
function getFocusedElementId(attribute: string) {
if (isStatusFilter(attribute)) return 'status-filter';
if (isTextFilter(attribute)) return 'text-filter-search';
if (isListFilter(attribute)) return 'list-filter-search';
if (isNumberFilter(attribute) || isDateTimeFilter(attribute))
return 'conditional-menu-button';
if (isBooleanFilter(attribute)) return 'boolean-filter';
return '';
}
function isOptionDisabled(value: string, filters: WorkflowFilter[]) {
return filters.some(
(filter) => filter.conditional === '=' && filter.attribute === value,
(filter) =>
(filter.conditional === '=' || filter.conditional === '!=') &&
filter.attribute === value,
);
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/components/workflow/filter-search/text-filter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import Input from '$lib/holocene/input/input.svelte';
import { translate } from '$lib/i18n/translate';
import ConditionalMenu from './conditional-menu.svelte';
import { FILTER_CONTEXT, type FilterContext } from './index.svelte';
const { filter, handleSubmit } = getContext<FilterContext>(FILTER_CONTEXT);
Expand All @@ -19,6 +20,14 @@
}
</script>

<ConditionalMenu
options={[
{ value: '=', label: translate('equal-to') },
{ value: '!=', label: translate('not-equal-to') },
]}
inputId="text-filter-search"
noBorderLeft
/>
<Input
label={$filter.attribute}
labelHidden
Expand Down
2 changes: 2 additions & 0 deletions src/lib/i18n/locales/en/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,6 @@ export const Strings = {
'based-on-time-preface': 'Based on',
'download-json': 'Download JSON',
'skip-nav': 'Skip to Main Content',
'equal-to': 'Equal to',
'not-equal-to': 'Not equal to',
} as const;
17 changes: 17 additions & 0 deletions src/lib/utilities/query/filter-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,20 @@ export function isBooleanFilter(
const searchAttributeType = get(attributes)[attribute];
return searchAttributeType === 'Bool';
}

export function getFocusedElementId(attribute: string) {
if (isStatusFilter(attribute)) return 'status-filter';

if (
isTextFilter(attribute) ||
isNumberFilter(attribute) ||
isDateTimeFilter(attribute)
)
return 'conditional-menu-button';

if (isListFilter(attribute)) return 'list-filter-search';

if (isBooleanFilter(attribute)) return 'boolean-filter';

return '';
}

0 comments on commit 97bdaa0

Please sign in to comment.