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

    Input

    Input is a lower-level constructs upon which all Grapes inputs are built.

    View source code
    • Usage
    • Props
    Usage

    Consider using this component as a last resort when none of the other available Grapes components meet your requirements, such as:

    • TextInput
    • AmountInput

    The primary difference between using an Input component versus a TextInput component is the ability to specify the type attribute with Input. Here are the list of type available.

    Build a number input

    const Demo = () => {
      const [value, setValue] = useState(0);
    
      return (
        <FormField label="Quantity">
          <Input
            placeholder="Enter a quantity"
            type="number"
            value={value}
            onChange={(event) => {
              setValue(event.target.valueAsNumber);
            }}
          />
        </FormField>
      );
    };
    

    Build a email input

    You can take advantage of the browser's built-in email validation to verify whether an email address is valid.

    Usage

    Built-in email validation is often too permissive, so it's recommended to use a custom regex or a validation library like yup for more accurate email validation.

    const Demo = () => {
      const [value, setValue] = useState(0);
      const [error, setError] = useState(false);
    
      return (
        <FormField label="Email" alertMessage={error ? "Email invalid" : undefined}>
          <Input
            placeholder="Enter your email"
            type="email"
            value={value}
            onChange={(event) => {
              setError(!event.target.checkValidity()); 
              setValue(event.target.value);
            }}
          />
        </FormField>
      );
    };
    

    Build a generic input with constraint

    You can use the prop min and max to defines a minimal and maximal value for your input and then leverage the validation constraint API to verify if the input meets the defined constraints.

    const Demo = () => {
      const [value, setValue] = useState(0);
    
      return (
        <FormField label="Quantity">
          <Input
            placeholder="Enter a quantity"
            type="number"
            min={3} 
            max={5} 
            value={value}
            onChange={(event) => {
              if (event.target.checkValidity()) {
                setValue(
                  Number.isNaN(event.target.valueAsNumber)
                    ? ""
                    : event.target.valueAsNumber
                );
              }
            }}
          />
        </FormField>
      );
    };
    

    You can also use the prop pattern for text input:

    const Demo = () => {
      const [value, setValue] = useState("");
    
      return (
        <FormField label="Name">
          <Input
            placeholder="Enter a name"
            type="text"
            // Only lowercase letters, with a maximum length of 8 characters.
            pattern="[a-w]{0,8}"
            value={value}
            onChange={(event) => {
              if (event.target.checkValidity()) {
                setValue(event.target.value);
              }
            }}
          />
        </FormField>
      );
    };