ink-text-input

repository·master·Indexed 41 days ago

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

A text input component for the Ink framework (version 6.0.0) that allows developers to create interactive CLI text fields. It provides both a controlled TextInput component for manual state management and an UncontrolledTextInput component for simpler implementations. Features include input masking for passwords, custom placeholders, and onSubmit callbacks.

Tokens
1.7K
Snippets
6
Records
8
Agent score
2%

What's inside ink-text-input

  1. Use UncontrolledTextInput for simpler state management

    master

    If you don't want to manage the input state manually, use the UncontrolledTextInput component. It handles internal value changes for you. To access the final input value, provide a function to the onSubmit prop, which is triggered when the user presses Enter. You can set an initial value using the initialValue prop.

    import React from 'react';
    import {render, Box, Text} from 'ink';
    import {UncontrolledTextInput} from 'ink-text-input';
    
    const SearchQuery = () => {
    	const handleSubmit = query => {
    		// Do something with query
    	};
    
    	return (
    		<Box>
    			<Box marginRight={1}>
    				<Text>Enter your query:</Text>
    			</Box>
    
    			<UncontrolledTextInput onSubmit={handleSubmit} />
    		</Box>
    	);
    };
    
    render(<SearchQuery />);
  2. Use ink-text-input as a controlled component

    master

    You can use TextInput as a controlled component by managing the value state in your parent component and updating it via the onChange prop. This is the standard way to keep your application state in sync with the input.

    import React, {useState} from 'react';
    import {render, Box, Text} from 'ink';
    import TextInput from 'ink-text-input';
    
    const SearchQuery = () => {
    	const [query, setQuery] = useState('');
    
    	return (
    		<Box>
    			<Box marginRight={1}>
    				<Text>Enter your query:</Text>
    			</Box>
    
    			<TextInput value={query} onChange={setQuery} />
    		</Box>
    	);
    };
    
    render(<SearchQuery />);
  3. Configure TextInput props

    master

    The TextInput component accepts the following props to control its behavior and appearance:

    ### value
    Type: `string`
    Value to display in a text input.
    
    ### placeholder
    Type: `string`
    Text to display when `value` is empty.
    
    ### focus
    Type: `boolean` (Default: `true`)
    Listen to user's input. Useful in case there are multiple input components at the same time and input must be "routed" to a specific component.
    
    ### showCursor
    Type: `boolean` (Default: `true`)
    Whether to show cursor and allow navigation inside text input with arrow keys.
    
    ### highlightPastedText
    Type: `boolean` (Default: `false`)
    Highlight pasted text.
    
    ### mask
    Type: `string`
    Replace all chars and mask the value. Useful for password inputs.
    
    ### onChange
    Type: `Function`
    Function to call when value updates.
    
    ### onSubmit
    Type: `Function`
    Function to call when `Enter` is pressed, where first argument is a value of the input.
  4. Use the TextInput component

    master

    The TextInput component is a controlled component for handling text input in Ink applications. You must manage the value state yourself and provide an onChange callback to update it. It supports features like masking (for passwords), custom placeholders, and cursor visibility.

    import React, { useState } from 'react';
    import TextInput from 'ink-text-input';
    
    const Example = () => {
    	const [text, setText] = useState('');
    
    	return (
    		<TextInput
    			value={text}
    			onChange={setText}
    			onSubmit={(value) => console.log('Submitted:', value)}
    		/>
    	);
    };
  5. Configure TextInput props

    master

    The TextInput component accepts the following props:

    PropTypeDescription
    valuestringRequired. The current text value to display.
    onChange(value: string) => voidRequired. Callback triggered when the value changes.
    placeholderstringOptional text to display when value is empty.
    focusbooleanWhether the component should listen to user input. Defaults to true.
    maskstringIf provided, replaces all characters in the value with this string (e.g., for passwords).
    showCursorbooleanWhether to show the cursor and allow arrow key navigation. Defaults to true.
    highlightPastedTextbooleanWhether to highlight text when it is pasted. Defaults to false.
    onSubmit(value: string) => voidOptional callback triggered when the Enter key is pressed.
  6. Define UncontrolledTextInput props

    master

    The UncontrolledTextInput component extends the standard Props but replaces the required value and onChange with an optional initialValue.

    PropTypeDescription
    initialValuestringThe starting value of the input. Defaults to ''.
    placeholderstringText to display when empty.
    focusbooleanWhether to listen to input.
    maskstringCharacter used to mask the input.
    showCursorbooleanWhether to show cursor/navigation.
    highlightPastedTextbooleanWhether to highlight pasted text.
    onSubmit(value: string) => voidCallback on Enter press.