refactor(cli): changed SearchBox component to support generic types
The SearchBox component in the CLI package now supports generic types, enhancing its flexibility. The onChange method now returns selected item details, instead of just returning the key. The SearchBox props have been modified to support the generic type T.
This commit is contained in:
parent
0146162775
commit
dd0ed06545
@ -5,18 +5,18 @@ import {Divider} from './Divider.js'
|
||||
import Spinner from 'ink-spinner'
|
||||
import {Box, Text, useInput, useApp, type Key} from 'ink'
|
||||
|
||||
export function SearchBox({
|
||||
export function SearchBox<T extends {key: string; displayName: string}>({
|
||||
components,
|
||||
helper,
|
||||
initialQuery,
|
||||
onKeyDown,
|
||||
onChange,
|
||||
}: {
|
||||
components: {key: string; displayName: string}[]
|
||||
components: T[]
|
||||
helper: string
|
||||
initialQuery?: string
|
||||
onKeyDown?: (i: string, k: Key, app: ReturnType<typeof useApp>) => void
|
||||
onChange?: (key: string) => void
|
||||
onChange?: (item: T) => void
|
||||
}) {
|
||||
const [query, setQuery] = useState<string>(initialQuery ?? '')
|
||||
const [isLoading, setLoading] = useState<boolean>(false)
|
||||
@ -38,7 +38,10 @@ export function SearchBox({
|
||||
}, [query])
|
||||
|
||||
useEffect(() => {
|
||||
onChange?.(suggestions[selected])
|
||||
if (onChange) {
|
||||
const found = components.find(({key}) => key === suggestions[selected])
|
||||
found && onChange(found)
|
||||
}
|
||||
}, [selected, onChange])
|
||||
|
||||
const app = useApp()
|
||||
|
Loading…
x
Reference in New Issue
Block a user