Code Monkey home page Code Monkey logo

Comments (5)

CaptainCodeman avatar CaptainCodeman commented on August 24, 2024 1

Great question! Off the top of my head I can't think of anywhere I've used dynamic data like this ... well, I have data loaded, but it's effectively static to the control. You're really talking about using it for a possibly unbounded set of data (or more than you'd want to load and have in memory upfront).

I'll have a think about the best way to do it. I'd imagine having some function you can pass to the control factory to fetch the data is what's needed.

from svelte-headlessui.

CaptainCodeman avatar CaptainCodeman commented on August 24, 2024 1

Yeah, I meant a subset from a potentially larger set (the unbounded).

I'd probably include some form of virtualization for the list too, as you may have many more entries in a result than you want to load / render unless and until someone scrolls.

from svelte-headlessui.

wesharper avatar wesharper commented on August 24, 2024 1

The solution I'm using basically just hijacks the input change option in a search input and feeds the subsequent results in to the combobox, while providing my own logic for the isOpen state.

<script lang="ts">
  const combobox = createCombobox();
  let searchPromise: Promise<void>;
  let query: string;
  let searchResults: any[];

  async function search() {
    try {
      const result = await fetch(
        `${import.meta.env.VITE_API_BASE_URL}/api/v1/search?query=${query}`
      );
      searchResults = await result.json();
    }
  }
</script>

<input
  type="search"
  name="search"
  use:combobox.input
  on:input={() => {
    searchPromise = search();
  }}
  on:select={onSelect}
  on:keydown={scrollToActiveItem}
  bind:value={query}
/>
{#if Boolean(query)}
  <!-- optionally await searchPromise -->
  <ul
    use:combobox.items
  >
    {#each searchResults as result}
      {@const active = $combobox.active === result}
      <li
        <!-- optional custom active classes -->
        use:combobox.item={{ value: result }}
      >
        {result.toString()}
      </li>
    {/each}
  </ul>
{/if}

from svelte-headlessui.

coryvirok avatar coryvirok commented on August 24, 2024

That seems to be the approach used in simple-svelte-autocomplete which seems to work fairly well. Although it's overloaded with more functionality than I need.

Also, I wouldn't include "unbounded sets" of data in the requirement since pulling in the top 10-20 items is all I'm interested in.

from svelte-headlessui.

CaptainCodeman avatar CaptainCodeman commented on August 24, 2024

Try this:

  1. Create derived store for the filter (this is so it can react when the filter part changes, not when any other state changes):
const combobox = createCombobox()
const filter = derived(combobox, $combobox => $combobox.filter)
  1. Use that filter store to trigger the data fetch:
let filtered: any[] = []

$: fetchData($filter)

async function fetchData(filter: string) {
  if (browser) {
    const q = filter.toLowerCase().replace(/\s+/g, '')
    const resp = await fetch('/api/data?q=' + q)
    const data = await resp.json()
    filtered = data
  }
}

3: Use the filtered array to render the combobox items:

{#each filtered as value}
...
{/each}

from svelte-headlessui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.