Skip to content

Commit

Permalink
feat: when facet isMulti=false use clearFilters() to clear the filters
Browse files Browse the repository at this point in the history
  • Loading branch information
laszlocseh committed Apr 17, 2024
1 parent 2b92a5d commit b3a797a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
27 changes: 22 additions & 5 deletions searchlib/components/Facets/ActiveFilters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import React from 'react';
import { Button, Card } from 'semantic-ui-react';
import { Icon, Term } from '@eeacms/search/components';
import { useSearchContext } from '@eeacms/search/lib/hocs';
import { useAppConfig, useSearchContext } from '@eeacms/search/lib/hocs';

const ActiveFilters = (props) => {
const { onRemove, field } = props;
const { appConfig } = useAppConfig();
const { clearFilters } = useSearchContext();

const filterConfig = appConfig.facets.find(
(f) => (f.id || f.field) === field,
);
const searchContext = useSearchContext();
const { filters = [] } = searchContext;

Expand Down Expand Up @@ -36,11 +41,19 @@ const ActiveFilters = (props) => {
// (v) => v !== option,
// );
// // setActiveFilter(filteredValues);
onRemove(option);
if (!filterConfig.isMulti) {
clearFilters();
} else {
onRemove(option);
}
}}
onKeyDown={(e) => {
if (e.key === 'Enter') {
onRemove(option);
if (!filterConfig.isMulti) {
clearFilters();
} else {
onRemove(option);
}
}
}}
>
Expand All @@ -60,7 +73,9 @@ const ActiveFilters = (props) => {
className="clear-btn"
content="Clear all"
onClick={() => {
if (Array.isArray(activeFilter)) {
if (!filterConfig.isMulti) {
clearFilters();
} else if (Array.isArray(activeFilter)) {
(activeFilter || []).forEach((v) => {
onRemove(v);
});
Expand All @@ -70,7 +85,9 @@ const ActiveFilters = (props) => {
}}
onKeyDown={(e) => {
if (e.key === 'Enter') {
if (Array.isArray(activeFilter)) {
if (!filterConfig.isMulti) {
clearFilters();
} else if (Array.isArray(activeFilter)) {
(activeFilter || []).forEach((v) => {
onRemove(v);
});
Expand Down
17 changes: 15 additions & 2 deletions searchlib/components/Facets/Unconnected/SingleTermFacet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Icon } from 'semantic-ui-react';
import cx from 'classnames';
import { Resizable, ToggleSort, Term } from '@eeacms/search/components';
import { useSort } from '@eeacms/search/lib/hocs';
import { useAppConfig } from '@eeacms/search/lib/hocs';
import { useAppConfig, useSearchContext } from '@eeacms/search/lib/hocs';

function getFilterValueDisplay(filterValue) {
if (filterValue === undefined || filterValue === null) return '';
Expand Down Expand Up @@ -68,6 +68,11 @@ const SingleTermFacetViewComponent = (props) => {
onSearch,
field,
} = props;
const searchContext = useSearchContext();
const { filters = [] } = searchContext;

const initialValue =
(filters.find((f) => f.field === field) || {})?.values || [];

const { appConfig } = useAppConfig();
const facetConfig = appConfig.facets.find((f) => f.field === field);
Expand All @@ -91,6 +96,13 @@ const SingleTermFacetViewComponent = (props) => {
field,
);

const sortedOptionsAdjusted = sortedOptions.map((item) => {
if (initialValue.includes(item.value)) {
item.selected = true;
}
return item;
});

return (
<fieldset className={cx('sui-facet searchlib-multiterm-facet', className)}>
{/*<legend className="sui-facet__title">{label}</legend>*/}
Expand Down Expand Up @@ -145,7 +157,8 @@ const SingleTermFacetViewComponent = (props) => {
</div>
<Resizable>
<FacetOptions
sortedOptions={sortedOptions}
// sortedOptions={sortedOptions}
sortedOptions={sortedOptionsAdjusted}
label={label}
onSelect={onSelect}
onRemove={onRemove}
Expand Down
7 changes: 5 additions & 2 deletions searchlib/components/FilterList/ActiveFilterValue.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react';
import { useAppConfig } from '@eeacms/search/lib/hocs';
import { useAppConfig, useSearchContext } from '@eeacms/search/lib/hocs';
import { Term } from '@eeacms/search/components';
import { Label, Icon } from 'semantic-ui-react';

const ActiveFilterValue = (props) => {
const { field, values, type, removeFilter } = props;
const { appConfig } = useAppConfig();
const { clearFilters } = useSearchContext();

const filterConfig = appConfig.facets.find(
(f) => (f.id || f.field) === field,
Expand Down Expand Up @@ -37,7 +38,9 @@ const ActiveFilterValue = (props) => {
name="close"
tabIndex={0}
onClick={() => {
if (values.length === 1) {
if (!filterConfig.isMulti) {
clearFilters();
} else if (values.length === 1) {
removeFilter(field, null, type || filterConfig.filterType);
} else {
removeFilter(field, value, type || filterConfig.filterType);
Expand Down
9 changes: 6 additions & 3 deletions searchlib/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ export function applyConfigurationSchema(config) {
config.disjunctiveFacets = [...(config.disjunctiveFacets || [])];
const { facets = [] } = config;
facets.forEach((facet) => {
if (facet.isMulti && !config.disjunctiveFacets.includes(facet.field)) {
if (
facet.factory === 'SingleTermFacet' ||
(facet.isMulti && !config.disjunctiveFacets.includes(facet.field))
) {
config.disjunctiveFacets.push(facet.field);
}
});
Expand Down Expand Up @@ -291,8 +294,8 @@ export function getBuckets({

let filtered_data = blacklist.length
? unfiltered_data.filter(
(bucket) => blacklist.indexOf(bucket.value) === -1,
)
(bucket) => blacklist.indexOf(bucket.value) === -1,
)
: unfiltered_data;

filtered_data = whitelist.length
Expand Down

0 comments on commit b3a797a

Please sign in to comment.