11import type { FC } from 'react' ;
22import { useCallback , useState } from 'react' ;
33import { useTimeout } from '../../../src' ;
4- import { SearchCombobox } from '../../../src/tailwind' ;
4+ import { SearchCombobox , TagsAutocomplete } from '../../../src/tailwind' ;
55
66const colors = [
77 { name : 'red' , value : '#FF0000' } ,
@@ -33,7 +33,7 @@ const ColorItem: FC<typeof colors[number]> = ({ name, value }) => (
3333 </ div >
3434) ;
3535
36- export const SearchComboboxPage : FC = ( ) => {
36+ const SyncSearch : FC = ( ) => {
3737 const [ syncSearchResults , setSyncSearchResults ] = useState < Map < string , typeof colors [ number ] > > ( ) ;
3838 const [ syncSelectedItem , setSyncSelectedItem ] = useState < typeof colors [ number ] > ( ) ;
3939 const onSyncSearch = useCallback ( ( searchTerm : string ) => {
@@ -49,6 +49,26 @@ export const SearchComboboxPage: FC = () => {
4949 ) ) ;
5050 } , [ ] ) ;
5151
52+ return (
53+ < div className = "tw:flex tw:flex-col tw:gap-y-2" >
54+ < h2 > Sync search</ h2 >
55+ < SearchCombobox
56+ onSearch = { onSyncSearch }
57+ onSelectSearchResult = { setSyncSelectedItem }
58+ renderSearchResult = { ( color ) => < ColorItem { ...color } /> }
59+ searchResults = { syncSearchResults }
60+ placeholder = "Search colors synchronously..."
61+ />
62+ { syncSelectedItem && (
63+ < div className = "tw:flex tw:gap-3" >
64+ Last selected color is: < ColorItem { ...syncSelectedItem } />
65+ </ div >
66+ ) }
67+ </ div >
68+ ) ;
69+ } ;
70+
71+ const AsyncSearch : FC = ( ) => {
5272 const { setTimeout } = useTimeout ( 1000 ) ;
5373 const [ asyncSearchResults , setAsyncSearchResults ] = useState < Map < string , typeof colors [ number ] > > ( ) ;
5474 const [ asyncSelectedItem , setAsyncSelectedItem ] = useState < typeof colors [ number ] > ( ) ;
@@ -70,38 +90,51 @@ export const SearchComboboxPage: FC = () => {
7090 } ) ;
7191 } , [ setTimeout ] ) ;
7292
93+ return (
94+ < div className = "tw:flex tw:flex-col tw:gap-y-2" >
95+ < h2 > Async search</ h2 >
96+ < SearchCombobox
97+ onSearch = { onAsyncSearch }
98+ onSelectSearchResult = { setAsyncSelectedItem }
99+ renderSearchResult = { ( color ) => < ColorItem { ...color } /> }
100+ searchResults = { asyncSearchResults }
101+ placeholder = "Search colors asynchronously..."
102+ loading = { asyncLoading }
103+ />
104+ { asyncSelectedItem && (
105+ < div className = "tw:flex tw:gap-3" >
106+ Last selected color is: < ColorItem { ...asyncSelectedItem } />
107+ </ div >
108+ ) }
109+ </ div >
110+ ) ;
111+ } ;
112+
113+ const TagsAutocompleteExample : FC < { immutable : boolean } > = ( { immutable } ) => {
114+ const [ selectedTags , setSelectedTags ] = useState ( immutable ? [ ] : [ 'blue' , 'yellow' ] ) ;
115+
116+ return (
117+ < TagsAutocomplete
118+ tags = { colors . map ( ( { name } ) => name ) }
119+ selectedTags = { selectedTags }
120+ onTagsChange = { setSelectedTags }
121+ getColorForTag = { ( tag ) => colors . find ( ( { name } ) => name === tag ) ?. value ?? '#99A1AF' }
122+ placeholder = { immutable ? 'Select tags from list...' : 'Select or add tags...' }
123+ immutable = { immutable }
124+ />
125+ ) ;
126+ } ;
127+
128+ export const SearchComboboxPage : FC = ( ) => {
73129 return (
74130 < div className = "tw:flex tw:flex-col tw:gap-y-4" >
131+ < SyncSearch />
132+ < AsyncSearch />
133+
75134 < div className = "tw:flex tw:flex-col tw:gap-y-2" >
76- < h2 > Sync search</ h2 >
77- < SearchCombobox
78- onSearch = { onSyncSearch }
79- onSelectSearchResult = { setSyncSelectedItem }
80- renderSearchResult = { ( color ) => < ColorItem { ...color } /> }
81- searchResults = { syncSearchResults }
82- placeholder = "Search colors synchronously..."
83- />
84- { syncSelectedItem && (
85- < div className = "tw:flex tw:gap-3" >
86- Last selected color is: < ColorItem { ...syncSelectedItem } />
87- </ div >
88- ) }
89- </ div >
90- < div className = "tw:flex tw:flex-col tw:gap-y-2" >
91- < h2 > Async search</ h2 >
92- < SearchCombobox
93- onSearch = { onAsyncSearch }
94- onSelectSearchResult = { setAsyncSelectedItem }
95- renderSearchResult = { ( color ) => < ColorItem { ...color } /> }
96- searchResults = { asyncSearchResults }
97- placeholder = "Search colors asynchronously..."
98- loading = { asyncLoading }
99- />
100- { asyncSelectedItem && (
101- < div className = "tw:flex tw:gap-3" >
102- Last selected color is: < ColorItem { ...asyncSelectedItem } />
103- </ div >
104- ) }
135+ < h2 > Tags autocomplete</ h2 >
136+ < TagsAutocompleteExample immutable = { false } />
137+ < TagsAutocompleteExample immutable = { true } />
105138 </ div >
106139 </ div >
107140 ) ;
0 commit comments