Install ink-select-input via npm
masterInstall the package using npm to use the select input component in your Ink applications.
npm install ink-select-inputrepository·master·Indexed 41 days ago
https://github.com/vadimdemedes/ink-select-inputA selection input component for the Ink framework (version 6.2.0) that allows developers to create interactive CLI selection lists. It supports keyboard navigation via arrow keys, j/k keys, and number keys, and provides props for focus control, initial index selection, and custom rendering of indicators and items.
Install the package using npm to use the select input component in your Ink applications.
npm install ink-select-inputTo use SelectInput, provide an array of items (each containing a label and value) and an onSelect callback. Users can navigate using arrow keys, j/k keys, or number keys, and select an item by pressing return.
import React from 'react';
import {render} from 'ink';
import SelectInput from 'ink-select-input';
const Demo = () => {
const handleSelect = item => {
// `item` = { label: 'First', value: 'first' }
};
const items = [
{
label: 'First',
value: 'first'
},
{
label: 'Second',
value: 'second'
},
{
label: 'Third',
value: 'third'
}
];
return <SelectInput items={items} onSelect={handleSelect} />;
};
render(<Demo />);isFocused prop (boolean, default: true) determines whether the component listens to user input. This is useful when managing multiple input components simultaneously to ensure input is routed only to the active component.onSelect prop (function) is a callback triggered when a user selects an item. The function receives the selected item object as its argument.The items prop defines the list of options displayed to the user. Each item must be an object with the following structure:
label (required): The text displayed in the list.value (required): The value associated with the item.key (optional): A unique key for the item. If not provided, value is used as the key.initialIndex prop (number, default: 0) sets the index of the item in the items array that is initially highlighted/selected.You can override the default visual elements of the SelectInput using the following props:
indicatorComponent (Component): A custom component to replace the default selection indicator.itemComponent (Component): A custom component to replace the default rendering of individual items.onHighlight prop (function) is a callback triggered whenever the user moves the highlight to a different item. The function receives the currently highlighted item object as its argument.limit prop (number) restricts the number of items visible in the list at once.The default export from this package is the SelectInput component. It is the primary component used to create a selectable list in an Ink application.
import SelectInput from 'ink-select-input';
// Use <SelectInput /> within your Ink component treeThe SelectInput component renders a selectable list in an Ink terminal application. It supports keyboard navigation (up/down arrows or 'j'/'k' keys), selection via the Enter key, and direct selection using number keys (1-9) for visible items.
Key features:
j/k to move the highlight.Enter to select the highlighted item, or press a number 1-9 to select a visible item directly.isFocused prop to enable or disable input listening, which is useful when multiple input components exist in the same terminal interface.import SelectInput from 'ink-select-input';
const items = [
{ label: 'Option 1', value: 'opt1' },
{ label: 'Option 2', value: 'opt2' },
];
<SelectInput
items={items}
onSelect={(item) => console.log('Selected:', item.value)}
/>The SelectInput component accepts the following props:
| Prop | Type | Default | Description |
|---|---|---|---|
items | Array<Item<V>> | [] | The list of items to display. Each must have label and value. |
isFocused | boolean | true | If true, the component listens to user input. |
initialIndex | number | 0 | The index of the item that is initially selected. |
limit | number | undefined | The maximum number of items to display at once. If items.length > limit, the list becomes a scrollable/rotatable view. |
indicatorComponent | FC<IndicatorProps> | Indicator | A custom component to override the default selection indicator. |
itemComponent | FC<ItemProps> | ItemComponent | A custom component to override the default item rendering. |
onSelect | (item: Item<V>) => void | undefined | Callback triggered when an item is selected (via Enter or number keys). |
onHighlight | (item: Item<V>) => void | undefined | Callback triggered when the highlight moves to a new item. |