Material Input

Installation

bash
npx gbs-add-block@latest -a MaterialInput

The MaterialInput component is a customizable and accessible text input field with a floating label, built using React. It supports various states such as focus, error, and disabled, along with optional helper text.

Props Table

PropTypeDefault ValueDescription
labelstringundefinedThe label for the input field. Displays above the input when focused or contains a value.
typestring"text"Specifies the input type (e.g., "text", "password", "email").
valuestringundefinedThe current value of the input field.
onChange(value: string) => voidundefinedCallback function invoked when the value of the input changes.
errorstring or undefinedundefinedAn error message displayed below the input field when set.
helperTextstring or undefinedundefinedA helper message displayed below the input field.
requiredbooleanfalseSpecifies whether the input field is required. Displays an asterisk (*) next to the label.
disabledbooleanfalseDisables the input field when set to true.
classNamestring""Additional CSS classes for customizing the outer container of the component.

Example

Enter your full name.

Usage Example

tsx
import React, { useState } from "react";
import MaterialInput from "./MaterialInput";

const Example = () => {
  const [inputValue, setInputValue] = useState("");
  const [error, setError] = useState("");

  const handleChange = (value: string) => {
    setInputValue(value);

    if (!value.trim()) {
      setError("This field is required.");
    } else {
      setError("");
    }
  };

  return (
    <div className="p-4">
      <MaterialInput
        label="Name"
        type="text"
        value={inputValue}
        onChange={handleChange}
        error={error}
        helperText="Enter your full name."
        required={true}
        className="mb-4"
      />
    </div>
  );
};

export default Example;