Skip to content

Commit

Permalink
bug: a filter with a single option which is selected should not be hi…
Browse files Browse the repository at this point in the history
…dden (#840)

Co-authored-by: Wesley Walser <[email protected]>
  • Loading branch information
wwalser and Wesley Walser authored Jul 15, 2022
1 parent 43aab9d commit 8afc849
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-vans-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sajari/react-search-ui': patch
---

bug: a filter with a single option which is selected should not be hidden
9 changes: 8 additions & 1 deletion packages/search-ui/src/Filter/ColorFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ const ColorFilter = ({ name, title, ...rest }: Omit<ColorFilterProps, 'type'>) =
[JSON.stringify(filtered)],
);

if ((isEmpty(filtered) && isEmpty(selected)) || filtered.length === 1) {
if (isEmpty(filtered) && isEmpty(selected)) {
return null;
}

// We want to hide filters which only contain a single option
// But many queries only return relevant aggregate filters which have counts.
// If a filter is selected, it can be the only aggregate with a count
if (filtered.length === 1 && isEmpty(selected)) {
return null;
}

Expand Down
9 changes: 8 additions & 1 deletion packages/search-ui/src/Filter/ListFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ const ListFilter = (props: Omit<ListFilterProps, 'type'>) => {
setQuery(value || '');
}, []);

if ((isEmpty(options) && isEmpty(selected)) || options.length === 1) {
if (isEmpty(options) && isEmpty(selected)) {
return null;
}

// We want to hide filters which only contain a single option
// But many queries only return relevant aggregate filters which have counts.
// If a filter is selected, it can be the only aggregate with a count
if (options.length === 1 && isEmpty(selected)) {
return null;
}

Expand Down
7 changes: 7 additions & 0 deletions packages/search-ui/src/Filter/SelectFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ const SelectFilter = (props: Omit<SelectFilterProps, 'type'>) => {
return null;
}

// We want to hide filters which only contain a single option
// But many queries only return relevant aggregate filters which have counts.
// If a filter is selected, it can be the only aggregate with a count
if (options.length === 1 && isEmpty(selected)) {
return null;
}

const getSelectText = (labels: Array<string | number>) => {
if (!labels.length) {
return t('select');
Expand Down
9 changes: 8 additions & 1 deletion packages/search-ui/src/Filter/TabFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ const TabFilter = (props: Omit<TabFilterProps, 'type'>) => {
const sliced = limit && options.length > limit ? options.slice(0, limit) : options;
const { disableDefaultStyles = false, customClassNames, currency, language } = useSearchUIContext();

if (isEmpty(sliced) || sliced.length === 1) {
if (isEmpty(sliced)) {
return null;
}

// We want to hide filters which only contain a single option
// But many queries only return relevant aggregate filters which have counts.
// If a filter is selected, it can be the only aggregate with a count
if (sliced.length === 1 && isEmpty(selected)) {
return null;
}

Expand Down

0 comments on commit 8afc849

Please sign in to comment.