Skip to content

Commit

Permalink
When removing an active filter value, take the "missing" setting into…
Browse files Browse the repository at this point in the history
… consideration
  • Loading branch information
tiberiuichim committed Jun 15, 2023
1 parent f230033 commit 5160c41
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
4 changes: 2 additions & 2 deletions searchlib/components/FilterList/ActiveFilterValue.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ const ActiveFilterValue = (props) => {
name="close"
tabIndex={0}
onClick={() => {
removeFilter(field, value, type || filterConfig.filterType);
removeFilter(field, null, type || filterConfig.filterType);
}}
onKeyDown={(e) => {
if (e.key === 'Enter') {
removeFilter(field, value, type || filterConfig.filterType);
removeFilter(field, null, type || filterConfig.filterType);
}
}}
/>
Expand Down
39 changes: 39 additions & 0 deletions searchlib/components/SearchApp/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
doFilterValuesMatch,
} from '@eeacms/search/lib/search/helpers';
import { getDefaultFilters } from '@eeacms/search/lib/utils';
import { removeSingleFilterValue } from '@elastic/search-ui/lib/cjs/helpers';

export function resetFilters() {
const { appConfig, searchContext } = this;
Expand Down Expand Up @@ -71,3 +72,41 @@ export function addFilter() {

// return addFilter.apply(null, arguments);
}

export function removeFilter(name, value, type) {
const { driver, appConfig } = this;
const filter = appConfig.facets.filter((f) => f.field === name)[0];

const { filters } = driver.state;

let updatedFilters = filters;

if (!value && type) {
if (!filter.missing) {
updatedFilters = filters.filter(
(filter) => !(filter.field === name && filter.type === type),
);
} else {
updatedFilters = [
...filters.filter((filter) => filter.field !== name),
{ ...filter.missing, field: name },
];
}
} else if (value) {
updatedFilters = removeSingleFilterValue(filters, name, value, type);
} else {
if (filter.missing) {
updatedFilters = [
...filters.filter((filter) => filter.field !== name),
{ ...filter.missing, field: name },
];
} else {
updatedFilters = filters.filter((filter) => filter.field !== name);
}
}

driver._updateSearchResults({
current: 1,
filters: updatedFilters,
});
}
16 changes: 14 additions & 2 deletions searchlib/components/SearchApp/useSearchApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import {
onAutocompleteResultClick,
} from '@eeacms/search/lib/request';

import { clearFilters, resetFilters, resetSearch, addFilter } from './request';
import {
removeFilter,
clearFilters,
resetFilters,
resetSearch,
addFilter,
} from './request';
import useFacetsWithAllOptions from './useFacetsWithAllOptions';
import { useLoadingState } from './state';
import { SearchDriver } from '@elastic/search-ui';
Expand Down Expand Up @@ -137,7 +143,13 @@ export default function useSearchApp(props) {
driver,
facetOptions,
};
const funcs = { resetFilters, resetSearch, clearFilters, addFilter };
const funcs = {
resetFilters,
resetSearch,
clearFilters,
removeFilter,
addFilter,
};
Object.keys(funcs).forEach((name) => {
searchContext[name] = funcs[name].bind({
appConfig,
Expand Down

0 comments on commit 5160c41

Please sign in to comment.