diff --git a/packages/cli/src/commands/search.tsx b/packages/cli/src/commands/search.tsx index b26e786..a120d26 100644 --- a/packages/cli/src/commands/search.tsx +++ b/packages/cli/src/commands/search.tsx @@ -1,17 +1,21 @@ -import {Command} from '@oclif/core' +import {Command, Args} from '@oclif/core' import {render} from 'ink' import {SearchBox} from '../components/SearchBox.js' import {getAvailableComponentNames, getRegistry} from '../helpers/registry.js' import React from 'react' export default class Search extends Command { + static override args = { + query: Args.string({description: 'search query'}), + } + static override description = 'Search components.' static override examples = ['<%= config.bin %> <%= command.id %>'] - static override flags = {} - public async run(): Promise { + const {args} = await this.parse(Search) + const registryResult = await getRegistry() if (!registryResult.ok) { this.error(registryResult.message) @@ -19,6 +23,13 @@ export default class Search extends Command { const registry = registryResult.registry const componentNames = await getAvailableComponentNames(registry) - render() + await render( + k.escape && app.exit()} + />, + ).waitUntilExit() } } diff --git a/packages/cli/src/components/SearchBox.tsx b/packages/cli/src/components/SearchBox.tsx index e40ee21..4d1e9ff 100644 --- a/packages/cli/src/components/SearchBox.tsx +++ b/packages/cli/src/components/SearchBox.tsx @@ -3,20 +3,23 @@ import {getSuggestion} from '../helpers/search.js' import Input from 'ink-text-input' import {Divider} from './Divider.js' import Spinner from 'ink-spinner' -import {Box, Text} from 'ink' +import {Box, Text, useInput, useApp, type Key} from 'ink' export function SearchBox({ components, helper, initialQuery, + onKeyDown, }: { components: string[] helper: string initialQuery?: string + onKeyDown?: (i: string, k: Key, app: ReturnType) => void }) { const [query, setQuery] = useState(initialQuery ?? '') const [isLoading, setLoading] = useState(false) const [suggestions, setSuggestions] = useState([]) + const [selected, setSelected] = useState(0) useEffect(() => { setLoading(true) @@ -26,6 +29,18 @@ export function SearchBox({ }) }, [query]) + const app = useApp() + + useInput((i, k) => { + if (k.downArrow) { + setSelected((p) => (p >= suggestions.length - 1 ? 0 : p + 1)) + } + if (k.upArrow) { + setSelected((p) => (p <= 0 ? suggestions.length - 1 : p - 1)) + } + onKeyDown?.(i, k, app) + }) + return ( {helper} @@ -40,10 +55,10 @@ export function SearchBox({ ) : ( - {suggestions.map((name) => { + {suggestions.map((name, index) => { return ( - {name} + {name} ) })}