react ~4 KB 0 deps v1.0.0 β†— GitHub β†—

@mshafiqyajid/react-text-input

Headless text input hook + styled component. Supports text/email/password/url/search/tel. Size + tone variants, prefix/suffix icon slots, optional clear (βœ•) button, hint + error message states, controlled or uncontrolled.

Playground #

Hint text appears here.
Props
TSX
import { TextInputStyled } from "@mshafiqyajid/react-text-input/styled";
import "@mshafiqyajid/react-text-input/styles.css";

<TextInputStyled
  value={value}
  onChange={setValue}
  placeholder="Type something…"
  type="email"
  label="Email"
  block
  clearable
/>

Install #

npm install @mshafiqyajid/react-text-input

Quick start #

import { TextInputStyled } from "@mshafiqyajid/react-text-input/styled";
import "@mshafiqyajid/react-text-input/styles.css";

const [email, setEmail] = useState("");
<TextInputStyled
  type="email"
  placeholder="you@example.com"
  value={email}
  onChange={setEmail}
  clearable
/>

Validation #

Pass an error message to flip tone to danger and render the message under the field. Use hint for plain helper text.

<TextInputStyled
  type="email"
  value={email}
  onChange={setEmail}
  error={!email.includes("@") ? "Invalid email" : undefined}
/>

Prefix / suffix #

<TextInputStyled
  prefix={<SearchIcon />}
  placeholder="Search…"
/>

<TextInputStyled
  prefix={<span>$</span>}
  suffix={<span>USD</span>}
  placeholder="0.00"
/>

Headless #

import { useTextInput } from "@mshafiqyajid/react-text-input";

const { inputProps, value, clear, isEmpty } = useTextInput({
  defaultValue: "",
});

return (
  <span>
    <input {...inputProps} />
    {!isEmpty && <button onClick={clear}>Γ—</button>}
  </span>
);

API #

PropTypeDefaultDescription
valuestringβ€”Controlled value
defaultValuestring""Uncontrolled initial value
onChange(value: string) => voidβ€”Called on every change
type"text" | "email" | "password" | "url" | "search" | "tel""text"Input type
size"sm" | "md" | "lg""md"Size
tone"neutral" | "primary" | "success" | "danger""neutral"Color
labelReactNodeβ€”Field label rendered above the input
requiredbooleanfalseAppend a red asterisk to the label
blockbooleanfalseFull-width
clearablebooleanfalseShow clear (βœ•) button when value is non-empty
passwordTogglebooleanfalseFor type="password": show an eye toggle that reveals/hides
loadingbooleanfalseShow a spinner suffix while async work runs
successbooleanfalseApply success tone + show a green check suffix
maxLengthnumberβ€”Max characters; auto-shows a counter
showCountbooleanautoForce-show or hide the character counter
prefixReactNodeβ€”Slot before the input
suffixReactNodeβ€”Slot after the input
hintReactNodeβ€”Helper text below the field
errorReactNodeβ€”Error message β€” auto-applies danger tone
disabledbooleanfalseDisable
readOnlybooleanfalseRead only
Edit this page on GitHub