|
| 1 | +import * as React from 'react'; |
| 2 | +import { Autocomplete } from '../src/components/autocomplete/autocomplete'; |
| 3 | + |
| 4 | +export default { |
| 5 | + title: 'Autocomplete', |
| 6 | + component: Autocomplete, |
| 7 | +}; |
| 8 | + |
| 9 | +// Sample data |
| 10 | +const fruits = [ |
| 11 | + 'Apple', |
| 12 | + 'Banana', |
| 13 | + 'Cherry', |
| 14 | + 'Date', |
| 15 | + 'Elderberry', |
| 16 | + 'Fig', |
| 17 | + 'Grape', |
| 18 | + 'Honeydew', |
| 19 | + 'Kiwi', |
| 20 | + 'Lemon', |
| 21 | + 'Mango', |
| 22 | + 'Orange', |
| 23 | + 'Papaya', |
| 24 | + 'Quince', |
| 25 | + 'Raspberry', |
| 26 | + 'Strawberry', |
| 27 | + 'Tangerine', |
| 28 | + 'Watermelon', |
| 29 | +]; |
| 30 | + |
| 31 | +const complexItems = [ |
| 32 | + { value: 'react', label: 'React' }, |
| 33 | + { value: 'vue', label: 'Vue.js' }, |
| 34 | + { value: 'angular', label: 'Angular' }, |
| 35 | + { value: 'svelte', label: 'Svelte' }, |
| 36 | + { value: 'nextjs', label: 'Next.js' }, |
| 37 | + { value: 'nuxtjs', label: 'Nuxt.js' }, |
| 38 | +]; |
| 39 | + |
| 40 | +// Basic autocomplete story |
| 41 | +export const Default = () => { |
| 42 | + const [value, setValue] = React.useState(''); |
| 43 | + |
| 44 | + return ( |
| 45 | + <Autocomplete |
| 46 | + items={fruits} |
| 47 | + value={value} |
| 48 | + filterSuggestions={true} |
| 49 | + autoHighlight={true} |
| 50 | + onChange={(e, val) => setValue(val)} |
| 51 | + onSelect={(val) => setValue(val)} |
| 52 | + inputProps={{ |
| 53 | + placeholder: 'Type to search fruits...', |
| 54 | + style: { width: '300px', padding: '8px' }, |
| 55 | + }} |
| 56 | + /> |
| 57 | + ); |
| 58 | +}; |
| 59 | + |
| 60 | +// Complex items with custom labels |
| 61 | +export const OptionItems = () => { |
| 62 | + const [value, setValue] = React.useState(''); |
| 63 | + |
| 64 | + return ( |
| 65 | + <Autocomplete |
| 66 | + items={complexItems} |
| 67 | + value={value} |
| 68 | + filterSuggestions={true} |
| 69 | + autoHighlight={true} |
| 70 | + onChange={(e, val) => setValue(val)} |
| 71 | + onSelect={(val) => setValue(val)} |
| 72 | + inputProps={{ |
| 73 | + placeholder: 'Select a framework...', |
| 74 | + style: { width: '300px', padding: '8px' }, |
| 75 | + }} |
| 76 | + /> |
| 77 | + ); |
| 78 | +}; |
| 79 | + |
| 80 | +// Custom render item |
| 81 | +export const CustomRenderItem = () => { |
| 82 | + const [value, setValue] = React.useState(''); |
| 83 | + |
| 84 | + return ( |
| 85 | + <Autocomplete |
| 86 | + items={complexItems} |
| 87 | + value={value} |
| 88 | + filterSuggestions={true} |
| 89 | + autoHighlight={true} |
| 90 | + onChange={(e, val) => setValue(val)} |
| 91 | + onSelect={(val) => setValue(val)} |
| 92 | + inputProps={{ |
| 93 | + placeholder: 'Select a framework...', |
| 94 | + style: { width: '300px', padding: '8px' }, |
| 95 | + }} |
| 96 | + renderItem={(item) => ( |
| 97 | + <div |
| 98 | + style={{ display: 'flex', alignItems: 'center', padding: '4px 0' }} |
| 99 | + > |
| 100 | + <span |
| 101 | + style={{ |
| 102 | + backgroundColor: '#007acc', |
| 103 | + color: 'white', |
| 104 | + padding: '2px 6px', |
| 105 | + borderRadius: '3px', |
| 106 | + fontSize: '12px', |
| 107 | + marginRight: '8px', |
| 108 | + }} |
| 109 | + > |
| 110 | + JS |
| 111 | + </span> |
| 112 | + {item.label} |
| 113 | + </div> |
| 114 | + )} |
| 115 | + /> |
| 116 | + ); |
| 117 | +}; |
| 118 | + |
| 119 | +// Custom input render |
| 120 | +export const CustomInput = () => { |
| 121 | + const [value, setValue] = React.useState(''); |
| 122 | + |
| 123 | + return ( |
| 124 | + <Autocomplete |
| 125 | + items={fruits} |
| 126 | + value={value} |
| 127 | + filterSuggestions={true} |
| 128 | + autoHighlight={true} |
| 129 | + onChange={(e, val) => setValue(val)} |
| 130 | + onSelect={(val) => setValue(val)} |
| 131 | + renderInput={(props) => ( |
| 132 | + <div style={{ position: 'relative' }}> |
| 133 | + <input |
| 134 | + {...props} |
| 135 | + style={{ |
| 136 | + width: '300px', |
| 137 | + padding: '12px 40px 12px 12px', |
| 138 | + border: '2px solid #007acc', |
| 139 | + borderRadius: '8px', |
| 140 | + fontSize: '16px', |
| 141 | + outline: 'none', |
| 142 | + }} |
| 143 | + placeholder='🔍 Search fruits...' |
| 144 | + /> |
| 145 | + <span |
| 146 | + style={{ |
| 147 | + position: 'absolute', |
| 148 | + right: '12px', |
| 149 | + top: '50%', |
| 150 | + transform: 'translateY(-50%)', |
| 151 | + color: '#666', |
| 152 | + fontSize: '18px', |
| 153 | + }} |
| 154 | + > |
| 155 | + ⌄ |
| 156 | + </span> |
| 157 | + </div> |
| 158 | + )} |
| 159 | + /> |
| 160 | + ); |
| 161 | +}; |
| 162 | + |
| 163 | +// Glob pattern matching |
| 164 | +export const GlobPattern = () => { |
| 165 | + const [value, setValue] = React.useState(''); |
| 166 | + |
| 167 | + const fileItems = [ |
| 168 | + 'component.ts', |
| 169 | + 'component.spec.ts', |
| 170 | + 'component.scss', |
| 171 | + 'service.ts', |
| 172 | + 'service.spec.ts', |
| 173 | + 'model.ts', |
| 174 | + 'utils.ts', |
| 175 | + 'utils.spec.ts', |
| 176 | + 'index.ts', |
| 177 | + 'main.scss', |
| 178 | + 'theme.scss', |
| 179 | + ]; |
| 180 | + |
| 181 | + return ( |
| 182 | + <div> |
| 183 | + <div style={{ marginBottom: '10px', fontSize: '14px', color: '#666' }}> |
| 184 | + Try glob patterns like: *.ts, *.spec.*, component.*, etc. |
| 185 | + </div> |
| 186 | + <Autocomplete |
| 187 | + items={fileItems} |
| 188 | + value={value} |
| 189 | + glob={true} |
| 190 | + filterSuggestions={false} |
| 191 | + autoHighlight={true} |
| 192 | + onChange={(e, val) => setValue(val)} |
| 193 | + onSelect={(val) => setValue(val)} |
| 194 | + inputProps={{ |
| 195 | + placeholder: 'Try patterns like *.ts, *.spec.*, component.*', |
| 196 | + style: { width: '350px', padding: '8px' }, |
| 197 | + }} |
| 198 | + /> |
| 199 | + </div> |
| 200 | + ); |
| 201 | +}; |
0 commit comments