Implementing multiple search states with plugins

Hi, I’m trying to figure it out this. I was wondering how to implement multiple search states using plugins. The documentation mentions that we should use the getSources() and check if the query is empty display something and if not show something else. What I’m trying to do is that when the user focus it will show the recent searchs via the createLocalStorageRecentSearchesPlugin and when they type it will hide the recent search and only display the search results.

Thanks

Hi @jesus.goron,

Typically, you can use logic similar to the following within your getSources() to accomplish what you want:

          getSources({ query, state }) {
            if (!query) {
              return [
                 ...
              ];
            }

            return [
              {
                ...
              }
            ]                

But I’m not sure how this works with plugins – I’ve asked internally. I’m guessing you’d want to inject the instantiated function into the getSources() yourself rather than using the plugins array, but I don’t have a specific example.

1 Like

Hi @chuck.meyer . Thanks for the reply :), I ended up using something similar to what you mentioned above. But instead of using the getSources() I used the transformSource(). This seems to do the trick.

transformSource({ source, state }) {
      if(state.query) {
        return null
      }

      return {}

});
1 Like

That’s brilliant! Thanks for circling back to share it.