Grapes homepage1.57.0
  • Guide
  • Tokens
  • Components
    Grapes on GithubGrapes on Figma
    Interaction
    • Button
    • IconButton
    • FloatingActionBar
    • Link
    Icons
    • Icon
    • HighlightIcon
    Form
    • AmountInput
    • Autocomplete
    • AutocompleteMultiple
    • AutocompletePlace
    • CheckboxBox
    • CheckboxField
    • DatePicker
    • FormField
    • Input
    • OptionGroup
    • PasswordInput
    • PhoneInput
    • RadioBox
    • RadioField
    • RadioGroup
    • Select
    • SwitchField
    • TextArea
    • TextInput
    • Upload
    • UploadButton
    Feedback
    • Badge
    • Banner
    • Callout
    • EmptyState
    • Modal
    • ModalSlideshow
    • DeprecatedModalSlideshow
    • PageModal
    • Skeleton
    • Tag
    • Toast
    • Tooltip
    Data display
    • Accordion
    • Avatar
    • Box
    • Calendar
    • CalendarRange
    • CollapsibleList
    • FileCard
    • InfoTip
    • ListBox
    • ListView
    • Panel
    • SidePanel
    • DeprecatedPreview
    • Table
    • Timeline
    • useDateFormatter
    Navigation
    • DropdownItem
    • DropdownMenu
    • Navigation
    • NavigationItem
    • Popover
    • Tabs

    TextInput

    A text field is an input that allows a user to write or edit text.

    View source code
    • Usage
    • Props
    • Accessibility

    Basic usage

    const Demo = () => {
      const [value, setValue] = useState("");
    
      return (
        <TextInput
          placeholder="Enter account number"
          value={value}
          onChange={(event) => {
            setValue(event.target.value);
          }}
        />
      );
    };
    

    Variant

    Use the variant prop to control the visual style of the TextInput. The TextInput comes with two visual styles: default (default) and magicGradient.

    <TextInput variant="magicGradient" {...otherProps} />
    

    Addon

    The leftAddon and rightAddon props allow you to add a component at the beginning or end of the TextInput.

    <>
      <TextInput
        leftAddon={
          <Icon
            color="var(--grapes-color-content-brand-default)"
            name="robot"
            className="ml-8"
          />
        }
        {...otherProps}
      />
      <TextInput
        rightAddon={<Button variant="tertiaryNeutral" text="OK" />}
        {...otherProps}
      />
    </>
    

    Pattern validation

    The pattern prop lets you specify a pattern that the input should follow. The Constraint validation API makes it possible to check that the input's value corresponds to the pattern.

    You can try it out with the input below, which only accepts digits and up to 8 characters.

    const Demo = () => {
      const [value, setValue] = useState("");
      return (
        <TextInput
          value={value}
          pattern="\d{0,8}"
          onChange={(event) => {
            if (event.target.checkValidity()) {
              setValue(event.target.value);
            }
          }}
        />
      );
    };