Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ToggleWidgetValues to accept multiple values #2182

Closed
timkelty opened this issue Jun 5, 2017 · 15 comments
Closed

Allow ToggleWidgetValues to accept multiple values #2182

timkelty opened this issue Jun 5, 2017 · 15 comments

Comments

@timkelty
Copy link
Contributor

timkelty commented Jun 5, 2017

Do you want to request a feature or report a bug?
feature

Feature: What is your use case for such a feature?
I have an attribute I'd like to use a toggle widget for, but I'd like to have the "on" value be one of many options.

Feature: What is your proposed API entry? The new option to add? What is the behavior?
I'd like to use it like this:

search.addWidget(instantsearch.widgets.toggle({
  container: '#toggle',
  attributeName: `hasMedia`,
  on: [true, 'true', 1, '1'],
}));

An alternate implementation might allow you to pass a fn with an argument of the value of the specified attr, returning a bool:

search.addWidget(instantsearch.widgets.toggle({
  container: '#toggle',
  attributeName: `hasMedia`,
  on: (value) => {
    return value && value !== 'off';
  },
}));

What is the version you are using? Always use the latest one before opening a bug issue.
1.11.12/2.beta-5

@iam4x iam4x self-assigned this Jun 6, 2017
@iam4x
Copy link
Contributor

iam4x commented Jun 6, 2017

I will discuss about this feature with @bobylito but I'm pretty sure it's a good thing that we can add into the v2 without a breaking changes for older implementations 👍

@timkelty
Copy link
Contributor Author

timkelty commented Jun 6, 2017

@iam4x that's what I was hoping, thanks!

@bobylito
Copy link
Contributor

bobylito commented Jun 6, 2017

Hi @timkelty how is the component supposed to figure out which value to select? Or will it add all the filters at once when on?

@timkelty
Copy link
Contributor Author

timkelty commented Jun 6, 2017

@bobylito right - so in the array example, any one of those values would be considered "on".

In the fn example, it would only be on if you returned true.

@bobylito
Copy link
Contributor

bobylito commented Jun 6, 2017

So if I understand correctly you have different values that are "on" in a single attribute of the dataset, right? Wouldn't it be easier to have a single value? Can you elaborate on the use case maybe?

@timkelty
Copy link
Contributor Author

timkelty commented Jun 6, 2017

@bobylito It would be simpler yes, and my use-case is actually kind of dumb...basically my indexer can run out of my database or directly from the request. If it populates from the database, certain values get "minimized", e.g. true/false to 1/0.

Anyway - while my use case isn't great, (because I could just hack into my indexing stuff and fix that), I thought it could be useful other ways.

For example - Consider a "Has Images" toggle. To do this, you'd have to index a new hasImages bool attribute.

However, if you accept a fn as I suggest, you could do something like (with images an array of your images):

search.addWidget(instantsearch.widgets.toggle({
  container: '#toggle',
  attributeName: `images`,
  on: (value) => {
    return value && value.count;
  },
}));

@bobylito
Copy link
Contributor

bobylito commented Jun 6, 2017

Ok I got the problem of heterogenous data for a single attribute. And that could happen when scrapping some website. I would argue that if you know how to map the different values to a single on state, then this mapping could be done at indexing time. But it's understood that sometimes it's not possible.

About the function syntax that you propose, what's the value? It is the value of the facet? How do you see it working? Could it based on regexp or rules? Would something like that make sense:

search.addWidget(instantsearch.widgets.toggle({
  container: '#cat-toggle',
  attributeName: `imageDescription`,
  on: (value) => {
    return value.indexOf('cat') !== -1; // all values that have cats in it will be caught as `on` values
  },
}));

If so, the facet values are not likely to be exhaustive and they can change with the current search parameters which can make that feature a bit random, no?

@timkelty
Copy link
Contributor Author

timkelty commented Jun 6, 2017

It is the value of the facet?

In your example, the value of imageDescription each record.
However that would involve passing each record through this function, which I'm sure is not what would be happening.

...So maybe a function doesn't make sense.

Initially I was thinking an array, because it felt similar to how you would pass a FacetList to SearchParameters.disjunctiveFacetsRefinements.

@timkelty
Copy link
Contributor Author

timkelty commented Nov 16, 2018

@bobylito @iam4x Been over a year…any progress here?

This came up again for me…this time I have a similar but different use-case:

I want a toggle widget for "Include discontinued products", so when off I want to filter on isDiscontinued:false, but when on, I want to show all.

If the undefined logic that off has were applied to on as well, I could achieve this like so:

search.addWidget(instantsearch.widgets.toggle({
  container: '#toggle',
  attributeName: 'isDiscontinued',
  on: undefined,
  off: false
}));

Unfortunately, the undefined logic only seems to work for off.
I could also get it to work if they accepted arrays, so I could do on: [true,false].

It seems silly that I could make this work if I changed the toggle to "Hide Discontinued Products" instead of "Show Discontinued Products".

@Kocal
Copy link
Contributor

Kocal commented May 28, 2020

Hi!

I'm having a similar problem but for a "favorites" system.

Users of our applications can add hits in their favorites, and we would like to use the toggleConnector (Vue connector widget in fact) to toggle over hits' ids:

search.addWidget(instantsearch.widgets.toggle({
  container: '#toggle',
  attributeName: 'id',
  on: [3, 39, 44], // user's favorites hits
}));

I will try to send a PR as soon as possible.

EDIT: PR opened at #4420

@Kocal
Copy link
Contributor

Kocal commented Jun 5, 2020

Should this issue be closed since #4420 has been merged, or we are waiting for a new release?

@Haroenv
Copy link
Contributor

Haroenv commented Jun 5, 2020

ah yes, it didn't have fixes in. Closing this while awaiting the next release indeed (not today because it's Friday)

@Haroenv Haroenv closed this as completed Jun 5, 2020
@eunjae-lee
Copy link
Contributor

I'll release this on Monday.

@Kocal
Copy link
Contributor

Kocal commented Jun 5, 2020

Nice, thanks you!

@eunjae-lee
Copy link
Contributor

@Kocal It's available@4.6.0.
https://github.com/algolia/instantsearch.js/blob/master/CHANGELOG.md#460-2020-06-08

Thank you for your contribution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants