Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
omni-code / tui / src / components / TextInput.tsx
Size: Mime:
import React from "react";
import { Box, Text, useInput, type Key } from "ink";
import { useTheme } from "../ThemeContext.js";

type Props = {
  value: string;
  onChange: (value: string) => void;
  onSubmit: (value: string) => void;
  onCancel: () => void;
  placeholder?: string;
  prompt?: string;
};

export function TextInput({ value, onChange, onSubmit, onCancel, placeholder, prompt }: Props) {
  const t = useTheme();

  useInput((input: string, key: Key) => {
    if (key.escape) {
      onCancel();
      return;
    }
    if (key.return) {
      onSubmit(value);
      return;
    }
    if (key.backspace || key.delete) {
      onChange(value.slice(0, -1));
      return;
    }
    if (!key.ctrl && !key.meta && input) {
      onChange(value + input);
    }
  });

  return (
    <Box>
      {prompt ? <Text color={t.accent}>{prompt} </Text> : null}
      <Text color={t.fg}>{value || (placeholder ? <Text color={t.fgSubtle}>{placeholder}</Text> : "")}</Text>
      <Text color={t.accent}>_</Text>
    </Box>
  );
}