Install ink-text-input
masterInstall the ink-text-input package via npm to use the text input component in your Ink applications.
npm install ink-text-inputrepository·master·Indexed 41 days ago
https://github.com/vadimdemedes/ink-text-inputA 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.
Install the ink-text-input package via npm to use the text input component in your Ink applications.
npm install ink-text-inputIf 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 />);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 />);Use the mask prop to replace all characters in the input with a specific character. This is useful for creating password fields.
<TextInput value="Hello" mask="*" />
//=> "*****"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.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)}
/>
);
};The TextInput component accepts the following props:
| Prop | Type | Description |
|---|---|---|
value | string | Required. The current text value to display. |
onChange | (value: string) => void | Required. Callback triggered when the value changes. |
placeholder | string | Optional text to display when value is empty. |
focus | boolean | Whether the component should listen to user input. Defaults to true. |
mask | string | If provided, replaces all characters in the value with this string (e.g., for passwords). |
showCursor | boolean | Whether to show the cursor and allow arrow key navigation. Defaults to true. |
highlightPastedText | boolean | Whether to highlight text when it is pasted. Defaults to false. |
onSubmit | (value: string) => void | Optional callback triggered when the Enter key is pressed. |
The UncontrolledTextInput component extends the standard Props but replaces the required value and onChange with an optional initialValue.
| Prop | Type | Description |
|---|---|---|
initialValue | string | The starting value of the input. Defaults to ''. |
placeholder | string | Text to display when empty. |
focus | boolean | Whether to listen to input. |
mask | string | Character used to mask the input. |
showCursor | boolean | Whether to show cursor/navigation. |
highlightPastedText | boolean | Whether to highlight pasted text. |
onSubmit | (value: string) => void | Callback on Enter press. |