autocomplete icon indicating copy to clipboard operation
autocomplete copied to clipboard

No Results option if search is empty

Open nfq opened this issue 4 years ago • 12 comments

I was wondering how to show a 'No Results found' message if no results are found?

nfq avatar Jul 06 '20 23:07 nfq

Are you using the vue component? If so, you can do something similar to the example for the default slot.

trevoreyre avatar Jul 07 '20 06:07 trevoreyre

I'm using the vanilla JS component!

nfq avatar Jul 07 '20 21:07 nfq

Ah, in that case you can use the onUpdate function to show/hide a no results message. You have to manage it manually, so it's not an ideal solution, but it's doable. Here's an example: https://codepen.io/trevoreyre/pen/oNbdBGM.

trevoreyre avatar Jul 08 '20 19:07 trevoreyre

This is great, thank you. Right now, it shows 'No Results' while searching a big data set on a remote API, even if a result is eventually found. Is there anyway to show the message if no results are found, rather than while typing? Using setTimeout seems less than ideal. I'm honestly not sure how best to approach this.

nfq avatar Jul 11 '20 01:07 nfq

I had a further idea: the spinner seems to work 100% correctly. Isn't there a way to trigger a 'No results' message after the data-loading is complete?

nfq avatar Jul 20 '20 00:07 nfq

That's not a bad idea. There are internal loading and loaded events. I wouldn't be opposed to exposing those as events if you're willing to put up a PR for it. It would look pretty similar to PR #82 which added the onUpdate event.

trevoreyre avatar Jul 21 '20 19:07 trevoreyre

I'll give it a shot. How do I build and test the project locally? Rollup?

nfq avatar Jul 22 '20 00:07 nfq

For local development you should be able to npm start to run Storybook. By default it starts the JavaScript storybook at localhost:4003 and the Vue storybook at localhost:4004. Then I would add a new story that tests the events you're trying to add, like this story that tests the onUpdate event.

trevoreyre avatar Jul 22 '20 15:07 trevoreyre

Working on this now. However, struggling to pass the 'results' param to the onLoading or onLoaded handler. Here's the handler:

handleLoaded = (results, event) => {
  this.loading = false
  this.updateStyle()
  this.onLoaded(results, event)
}

And here's the Storybook story:

export const Loaded = () => {
  const root = createRoot()
  const elem = document.querySelector('.loading')
  root.innerHTML = `
    <input
      class='autocomplete-input'
      placeholder='Search Wikipedia'
      aria-label='Search Wikipedia'
    >
    <ul class='autocomplete-result-list'></ul>
    <p class='loading' style='position: absolute; top: -40px;'>Loaded</p>
  `
  new Autocomplete(root, {
    search: searchWikipedia,
    getResultValue: result => result.title,
    onLoading: (event) => {
      console.log('searching...')
    },
    onLoaded: (results, event) => {
      if (results.length === 0) {
        console.log('no results')
      } else {
       console.log('found x results')
      }
    onSubmit: result =>
      window.open(`${wikiUrl}/wiki/${encodeURI(result.title)}`),
  })
  return root
}

nfq avatar Aug 16 '20 00:08 nfq

I'm not sure I understand the issue you're having. Do you mean in your handleLoaded handler, the results argument is not defined? Do you have a branch I could take a look at?

trevoreyre avatar Aug 17 '20 19:08 trevoreyre

That'd be nice to expose that loaded event. I'm having the same issue: I need to show a message when no result is returned.

cedm avatar Jan 21 '22 02:01 cedm

I'm using Vue so I'm going for you defaultSlot solution, but it would be definitely nicer and simpler to have a #no-results slot instead.

augnustin avatar Mar 22 '23 14:03 augnustin