Instantsearch.js: menuSelect Widget: hide when only one option (other than seeAllOption)

Created on 11 Jul 2018  路  7Comments  路  Source: algolia/instantsearch.js

Describe the bug 馃悰
When using a menuSelect widget, if there is only one option (other than the seeAllOption), this widget should be hidden since selecting that single option is the same as selecting the seeAllOption

To Reproduce 馃攳
Steps to reproduce the behavior:

  1. Add a menuSelect widget that will only have one option
  2. See that selecting that option is the same as the seeAllOption

Expected behavior 馃挱
If there is only one option other than the seeAllOption this widget should be hidden.

Screenshots 馃枼
screen shot 2018-07-11 at 11 36 38 am

Feedback

Most helpful comment

Since InstantSearch.js 3, you can use the panel widget like this:

const brandMenuSelect = instantsearch.widgets.panel({
  hidden: ({ results }) => results.getFacetValues('brand').data.length <= 1,
})(instantsearch.widgets.menuSelect);

search.addWidget(
  brandMenuSelect({
    container: '#brand-list',
    attribute: 'brand',
  })
);

All 7 comments

Thanks for the feedback. We're actually looking for revamping the conditions for the autohideContainer feature.

Is there any timeline for v3.0?

Not at the moment @davist11

馃憤

do you have any suggestions for how to handle this issue with the current version?

The most straightforward and hacky way would be to use the helper, listen to the result event, check if there is only one value returned and hide the container of your menu:

const search = instantsearch(/* config */);
// ...
search.start();
search.helper.on('result', (results) => {
  const values = results.getFacetValues('yourMenuFacet');
  if(values.length <= 1) hide();
  else show();
});

https://community.algolia.com/algoliasearch-helper-js/reference.html#SearchResults#getFacetValues

Awesome, thank you.

For reference for anyone else who needs something like this, I had to update the conditional like so:

if (Array.isArray(values.data) && values.data.length <= 1) {
    hide()
} else {
    show()
}

Since InstantSearch.js 3, you can use the panel widget like this:

const brandMenuSelect = instantsearch.widgets.panel({
  hidden: ({ results }) => results.getFacetValues('brand').data.length <= 1,
})(instantsearch.widgets.menuSelect);

search.addWidget(
  brandMenuSelect({
    container: '#brand-list',
    attribute: 'brand',
  })
);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

zeke picture zeke  路  3Comments

ChristopherDosin picture ChristopherDosin  路  4Comments

bobylito picture bobylito  路  3Comments

bobylito picture bobylito  路  3Comments

zackify picture zackify  路  4Comments