ink-select-input

repository·master·Indexed 41 days ago

https://github.com/vadimdemedes/ink-select-input

A 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.

Tokens
1.7K
Snippets
7
Records
15
Agent score
49%

What's inside ink-select-input

  1. Basic usage of SelectInput

    master

    To 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 />);
  2. Configure items in SelectInput

    master

    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.
  3. Customize SelectInput components

    master

    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.
  4. Use the SelectInput component

    master

    The 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:

    • Navigation: Use arrow keys or j/k to move the highlight.
    • Selection: Press Enter to select the highlighted item, or press a number 1-9 to select a visible item directly.
    • Focus Control: Use the 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)} 
    />
  5. Configure SelectInput props

    master

    The SelectInput component accepts the following props:

    PropTypeDefaultDescription
    itemsArray<Item<V>>[]The list of items to display. Each must have label and value.
    isFocusedbooleantrueIf true, the component listens to user input.
    initialIndexnumber0The index of the item that is initially selected.
    limitnumberundefinedThe maximum number of items to display at once. If items.length > limit, the list becomes a scrollable/rotatable view.
    indicatorComponentFC<IndicatorProps>IndicatorA custom component to override the default selection indicator.
    itemComponentFC<ItemProps>ItemComponentA custom component to override the default item rendering.
    onSelect(item: Item<V>) => voidundefinedCallback triggered when an item is selected (via Enter or number keys).
    onHighlight(item: Item<V>) => voidundefinedCallback triggered when the highlight moves to a new item.