Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Search.svelte
  2. <div class="search">
  3.   <div class="search__input">
  4.     <input type="text" class="textbox" placeholder="Search" on:input>
  5.     {results}
  6.   </div>
  7.   {#if Array.isArray(results) && results.length > 0}
  8.     <div class="search__results">
  9.       {#await results}
  10.         <p>Loading...</p>
  11.       {:then results}
  12.         {#if Array.isArray(results) && results.length > 0}
  13.           <p>Result(): {results}</p>
  14.         {/if}
  15.       {:catch error}
  16.         <p>Ooops! {error}</p>
  17.       {/await}
  18.     </div>
  19.   {/if}
  20. </div>
  21.  
  22. <script>
  23.   export let results
  24. </script>
  25.  
  26. // App.svelte
  27. <main>
  28.   <Search on:input={handleInput} results="{promise}"/>
  29. </main>
  30.  
  31.  
  32. <script>
  33.     import axios from 'axios'
  34.   import Search from './components/Search.svelte'
  35.  
  36.     let promise
  37.  
  38.   function handleInput({ target }) {
  39.     promise = getSearchResults(target.value);
  40.   }
  41.  
  42.   async function getSearchResults(value) {
  43.     const response = await axios.get('http://localhost:5000/data.json');
  44.       return response.data.words.filter(w => w.match(value)).slice(0, 10);
  45.   }
  46. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement