11import { html } from 'lit'
22import { defineStoryRender } from '@/storybook'
3+ import { defineAsyncComboboxOptionsProvider } from './Combobox'
34
45import '@/components/combobox-option'
56
@@ -10,45 +11,96 @@ const meta = {
1011 args : {
1112 label : 'What is the best food?' ,
1213 options : 'Pizza, Ramen, Tacos' ,
13- asyncOptions : false
14+ asyncJSOptions : false ,
15+ asyncHtmlOptions : false ,
1416 } ,
1517 argTypes : {
1618 label : { control : 'text' } ,
1719 options : { control : 'text' } ,
18- asyncOptions : { control : 'boolean' } ,
20+ asyncJSOptions : { control : 'boolean' } ,
21+ asyncHtmlOptions : { control : 'boolean' } ,
1922 } ,
2023} as const
2124
22- const render = defineStoryRender < typeof meta . argTypes > ( ( { label, options, asyncOptions } ) => {
23- const parsedOptions = options . split ( ',' ) . map ( option => option . trim ( ) )
25+ const pokemonProvider = defineAsyncComboboxOptionsProvider ( async ( query ) => {
26+ const response = await fetch ( 'https://beta.pokeapi.co/graphql/v1beta' , {
27+ method : 'POST' ,
28+ headers : { 'Content-Type' : 'application/json' } ,
29+ body : JSON . stringify ( {
30+ query : `
31+ query searchPokemon($search: String!) {
32+ pokemon_v2_pokemon(where: {name: {_ilike: $search}}, limit: 20) {
33+ id
34+ name
35+ }
36+ }
37+ ` ,
38+ variables : { search : `%${ query } %` } ,
39+ } ) ,
40+ } )
41+
42+ const { data } = await response . json ( )
43+
44+ return data . pokemon_v2_pokemon . map ( pokemon => ( {
45+ value : pokemon . id . toString ( ) ,
46+ label : pokemon . name . charAt ( 0 ) . toUpperCase ( ) + pokemon . name . slice ( 1 ) ,
47+ } ) )
48+ } )
49+
50+ const render = defineStoryRender < typeof meta . argTypes > (
51+ ( { label, options, asyncJSOptions, asyncHtmlOptions } ) => {
52+ if ( asyncJSOptions ) {
53+ return html `
54+ < solid-ui-combobox
55+ label ="${ label } "
56+ .asyncOptionsProvider =${ pokemonProvider }
57+ > </ solid-ui-combobox >
58+ `
59+ }
60+
61+ if ( asyncHtmlOptions ) {
62+ return html `
63+ < solid-ui-combobox
64+ label ="${ label } "
65+ async-options-url ="https://api.disneyapi.dev/character?name=%search% "
66+ async-options-results-field ="data "
67+ async-options-label-field ="name "
68+ async-options-value-field ="_id "
69+ > </ solid-ui-combobox >
70+ `
71+ }
72+
73+ const parsedOptions = options . split ( ',' ) . map ( ( option ) => option . trim ( ) )
2474
25- if ( asyncOptions ) {
2675 return html `
27- < solid-ui-combobox
28- label ="${ label } "
29- async-options-url ="https://api.disneyapi.dev/character?name=%search% "
30- async-options-results-field ="data "
31- async-options-label-field ="name "
32- async-options-value-field ="_id "
33- > </ solid-ui-combobox >
76+ < solid-ui-combobox label ="${ label } ">
77+ ${ parsedOptions . map (
78+ ( option ) =>
79+ html `< solid-ui-combobox-option value ="${ option } "
80+ > ${ option } </ solid-ui-combobox-option
81+ > `
82+ ) }
83+ </ solid-ui-combobox >
3484 `
3585 }
36-
37- return html `
38- < solid-ui-combobox label ="${ label } ">
39- ${ parsedOptions . map ( option => html `< solid-ui-combobox-option value ="${ option } "> ${ option } </ solid-ui-combobox-option > ` ) }
40- </ solid-ui-combobox >
41- `
42- } )
86+ )
4387
4488export default meta
4589
4690export const Primary = { render }
4791
48- export const Async = {
92+ export const AsyncWithJS = {
93+ args : {
94+ label : 'Who is the best Pokemon?' ,
95+ asyncJSOptions : true ,
96+ } ,
97+ render,
98+ }
99+
100+ export const AsyncWithHtml = {
49101 args : {
50102 label : 'Who is the best Disney character?' ,
51- asyncOptions : true
103+ asyncHtmlOptions : true ,
52104 } ,
53- render
105+ render,
54106}
0 commit comments