Open In App

React MUI Input API

MUI or Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.

In this article, we will discuss the React MUI Input API. The Input element allows us to place the input elements with the required id and other classes on the webpage. It supports Text fields, buttons, etc. The API provides a lot of functionality and we will learn to implement them.



Import Input API

import Input from '@mui/material/Input';
// or
import { Input } from '@mui/material';

 



Props List: Here is the list of different props used with this component. We can access them and modify them according to our needs.

CSS Rules:

Syntax: Create an Input element as follows:

<Input
    value={values.amount}
    onChange={handleChange("amount")}
/>

Installing and Creating React app and adding the MUI dependencies:

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react 
npm install @emotion/styled @mui/lab @mui/icons-material

Project Structure: The project should look like the below:

 

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have some Input Elements with adornments.




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import IconButton from "@mui/material/IconButton";
import Input from "@mui/material/Input";
import FilledInput from "@mui/material/FilledInput";
import OutlinedInput from "@mui/material/OutlinedInput";
import InputLabel from "@mui/material/InputLabel";
import InputAdornment from "@mui/material/InputAdornment";
import FormHelperText from "@mui/material/FormHelperText";
import FormControl from "@mui/material/FormControl";
import TextField from "@mui/material/TextField";
import Visibility from "@mui/icons-material/Visibility";
import VisibilityOff from "@mui/icons-material/VisibilityOff";
  
function App() {
    const [values, setValues] = React.useState({
        amount: "",
        password: "",
        weight: "",
        weightRange: "",
        showPassword: false,
    });
  
    const handleChange = (prop) => (event) => {
        setValues({ ...values, [prop]: event.target.value });
    };
  
    const handleClickShowPassword = () => {
        setValues({
            ...values,
            showPassword: !values.showPassword,
        });
    };
  
    const handleMouseDownPassword = (event) => {
        event.preventDefault();
    };
  
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI Input API</strong>
            </div>
            <br />
            <Box sx={{ display: "flex", flexWrap: "wrap" }}>
                <div>
                    <FormControl fullWidth sx={{ m: 1, width: "25ch" }} 
                        variant="filled">
                        <InputLabel htmlFor="filled-adornment-amount">
                            Amount
                        </InputLabel>
                        <Input
                            id="filled-adornment-amount"
                            value={values.amount}
                            onChange={handleChange("amount")}
                            startAdornment={
                                <InputAdornment position="start">
                                    $
                                </InputAdornment>
                            }
                        />
                    </FormControl>
                </div>
                <div>
                    <TextField
                        label="Weight"
                        id="standard-start-adornment"
                        sx={{ m: 1, width: "25ch" }}
                        InputProps={{
                            startAdornment: (
                                <InputAdornment position="start">
                                    kg
                                </InputAdornment>
                            ),
                        }}
                        variant="standard"
                    />
  
                    <FormControl sx={{ m: 1, width: "25ch" }} 
                        variant="standard">
                        <InputLabel 
                        htmlFor="standard-adornment-password">
                            Password
                        </InputLabel>
                        <Input
                            id="standard-adornment-password"
                            type={values.showPassword ? "text" : "password"}
                            value={values.password}
                            onChange={handleChange("password")}
                            endAdornment={
                                <InputAdornment position="end">
                                    <IconButton
                                        aria-label="toggle password visibility"
                                        onClick={handleClickShowPassword}
                                        onMouseDown={handleMouseDownPassword}
                                    >
                                        {values.showPassword 
                                        ? <VisibilityOff /> 
                                        : <Visibility />}
                                    </IconButton>
                                </InputAdornment>
                            }
                        />
                    </FormControl>
                </div>
            </Box>
        </div>
    );
}
export default App;

Output:

 

Example 2: In the following example, we have full-width input.




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import IconButton from "@mui/material/IconButton";
import Input from "@mui/material/Input";
import FilledInput from "@mui/material/FilledInput";
import OutlinedInput from "@mui/material/OutlinedInput";
import InputLabel from "@mui/material/InputLabel";
import InputAdornment from "@mui/material/InputAdornment";
import FormHelperText from "@mui/material/FormHelperText";
import FormControl from "@mui/material/FormControl";
import TextField from "@mui/material/TextField";
import Visibility from "@mui/icons-material/Visibility";
import VisibilityOff from "@mui/icons-material/VisibilityOff";
  
function App() {
    const [values, setValues] = React.useState({
        amount: "",
        password: "",
        weight: "",
        weightRange: "",
        showPassword: false,
    });
  
    const handleChange = (prop) => (event) => {
        setValues({ ...values, [prop]: event.target.value });
    };
  
    const handleClickShowPassword = () => {
        setValues({
            ...values,
            showPassword: !values.showPassword,
        });
    };
  
    const handleMouseDownPassword = (event) => {
        event.preventDefault();
    };
  
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI Input API</strong>
            </div>
            <br />
            <Box sx={{ display: "flex", flexWrap: "wrap" }}>
                <div>
                    <TextField
                        label="Weight"
                        id="standard-start-adornment"
                        sx={{ m: 1, width: "25ch" }}
                        InputProps={{
                            startAdornment: (
                                <InputAdornment position="start">
                                    kg
                                </InputAdornment>
                            ),
                        }}
                        variant="standard"
                    />
                    <FormControl variant="standard" 
                        sx={{ m: 1, mt: 0, width: "25ch" }}>
                        <FormHelperText id="standard-weight-helper-text">
                            Height
                        </FormHelperText>
                        <Input
                            label="Height"
                            id="standard-adornment-weight"
                            value={values.weight}
                            endAdornment={
                              <InputAdornment position="end">
                                  cm
                              </InputAdornment>
                            }
                            aria-describedby="standard-weight-helper-text"
                            onChange={handleChange("weight")}
                        />
                    </FormControl>
                    <FormControl sx={{ m: 1, width: "25ch" }} 
                    variant="standard">
                        <InputLabel htmlFor="standard-adornment-password">
                            Password
                        </InputLabel>
                        <Input
                            id="standard-adornment-password"
                            type={values.showPassword 
                                ? "text" 
                                : "password"}
                            value={values.password}
                            onChange={handleChange("password")}
                            endAdornment={
                                <InputAdornment position="end">
                                    <IconButton
                                        aria-label="toggle password visibility"
                                        onClick={handleClickShowPassword}
                                        onMouseDown={handleMouseDownPassword}
                                    >
                                        {values.showPassword 
                                            ? <VisibilityOff /> 
                                            : <Visibility />}
                                    </IconButton>
                                </InputAdornment>
                            }
                        />
                    </FormControl>
                    <FormControl fullWidth sx={{ m: 1 }} 
                        variant="standard">
                        <InputLabel htmlFor="standard-adornment-amount">
                            Amount
                        </InputLabel>
                        <Input
                            id="standard-adornment-amount"
                            value={values.amount}
                            onChange={handleChange("amount")}
                            startAdornment={
                                <InputAdornment position="start">
                                    $
                                </InputAdornment>
                            }
                        />
                    </FormControl>
                </div>
            </Box>
        </div>
    );
}
export default App;

Output:

 

Reference: https://mui.com/material-ui/api/input/


Article Tags :