()
| 2 | import color from 'picocolors'; |
| 3 | |
| 4 | async function main() { |
| 5 | console.clear(); |
| 6 | |
| 7 | p.intro(`${color.bgCyan(color.black(' Autocomplete Example '))}`); |
| 8 | |
| 9 | p.note( |
| 10 | ` |
| 11 | ${color.cyan('This example demonstrates the type-ahead autocomplete feature:')} |
| 12 | - ${color.yellow('Type')} to filter the list in real-time |
| 13 | - Use ${color.yellow('up/down arrows')} to navigate the filtered results |
| 14 | - Press ${color.yellow('Enter')} to select the highlighted option |
| 15 | - Press ${color.yellow('Ctrl+C')} to cancel |
| 16 | `, |
| 17 | 'Instructions' |
| 18 | ); |
| 19 | |
| 20 | const countries = [ |
| 21 | { value: 'us', label: 'United States', hint: 'NA' }, |
| 22 | { value: 'ca', label: 'Canada', hint: 'NA' }, |
| 23 | { value: 'mx', label: 'Mexico', hint: 'NA' }, |
| 24 | { value: 'br', label: 'Brazil', hint: 'SA' }, |
| 25 | { value: 'ar', label: 'Argentina', hint: 'SA' }, |
| 26 | { value: 'uk', label: 'United Kingdom', hint: 'EU' }, |
| 27 | { value: 'fr', label: 'France', hint: 'EU' }, |
| 28 | { value: 'de', label: 'Germany', hint: 'EU' }, |
| 29 | { value: 'it', label: 'Italy', hint: 'EU' }, |
| 30 | { value: 'es', label: 'Spain', hint: 'EU' }, |
| 31 | { value: 'pt', label: 'Portugal', hint: 'EU' }, |
| 32 | { value: 'ru', label: 'Russia', hint: 'EU/AS' }, |
| 33 | { value: 'cn', label: 'China', hint: 'AS' }, |
| 34 | { value: 'jp', label: 'Japan', hint: 'AS' }, |
| 35 | { value: 'in', label: 'India', hint: 'AS' }, |
| 36 | { value: 'kr', label: 'South Korea', hint: 'AS' }, |
| 37 | { value: 'au', label: 'Australia', hint: 'OC' }, |
| 38 | { value: 'nz', label: 'New Zealand', hint: 'OC' }, |
| 39 | { value: 'za', label: 'South Africa', hint: 'AF' }, |
| 40 | { value: 'eg', label: 'Egypt', hint: 'AF' }, |
| 41 | ]; |
| 42 | |
| 43 | const result = await p.autocomplete({ |
| 44 | message: 'Select a country', |
| 45 | options: countries, |
| 46 | placeholder: 'Type to search countries...', |
| 47 | maxItems: 8, |
| 48 | }); |
| 49 | |
| 50 | if (p.isCancel(result)) { |
| 51 | p.cancel('Operation cancelled.'); |
| 52 | process.exit(0); |
| 53 | } |
| 54 | |
| 55 | const selected = countries.find((c) => c.value === result); |
| 56 | p.outro(`You selected: ${color.cyan(selected?.label)} (${color.yellow(selected?.hint)})`); |
| 57 | } |
| 58 | |
| 59 | main().catch(console.error); |
no test coverage detected