Open In App

React MUI FilledInput API

Last Updated : 27 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Material-UI or MUI 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 FilledInput API. The FilledInput element allows us to place the input elements with the required id and other classes on the web-page. The API provides a lot of functionality and we will learn to implement them.

Import FilledInput API

import FilledInput from '@mui/material/FilledInput';
// or
import {FilledInput} from "@mui/material";

Props: All available props for this component.

  • autoComplete (string): It helps to fill up the forms faster with some predefined or previously used values. 
  • autoFocus (bool):  If set true, the input element is focused initially. The default value is false.
  • classes (object): It overrides or extends styles to the component.
  • color (‘primary’ | ‘secondary’ | string): The color of the component. The default value is ‘primary’.
  • components ( { Input?, Root? } ): It is used to set the components used for each slot inside.
  • componentsProps (props): It is used to apply the props for slot components. Deprecated, use slotProps instead.
  • defaultValue (any): It is used to set a default value of the input element.
  • disabled (bool): If set to true, the input element is disabled. The default value is false.
  • disableUnderline (bool): If set to true, the input will not have an underline. The default value is false.
  • endAdornment (node): The end InputAdornment for this component. 
  • error (bool): If set to true, the input will indicate an error. The default value is false.
  • fullWidth (bool): If set to true, the input will take the full width. The default value is false.
  • hiddenLabel (bool): If set to true, the label is hidden. The default value is false.
  • id (string): It sets the id of the element.
  • inputComponent (elementType): It sets the type of input element. The default value is ‘input’.
  • inputProps (object): It sets the props for the input component.
  • inputRef (ref): It is used to pass a reference to the input element.
  • margin (dense/none): If set to dense, the component will adjust the vertical padding. The default value is ‘none’.
  • maxRows (number): It sets the maximum number of rows if the multiline is set to true.
  • minRows (number): It is used to set the minimum number of rows if the multiline is set to true.
  • multiline (bool): If set to true, the TextArea auto resizes. The default value is false.
  • name (string): The name of the input element.
  • onChange (function): The function is called when the value inside the input is changed.
  • placeholder (string): It sets the hint displayed in the input element.
  • readOnly (bool): If set to true, the user will be able to read the input value only. The default value is false.
  • required (bool): If set to true, the input element can’t be left empty.
  • rows (number): Number of rows to display when multiline is set to true.
  • slotProps (props): It is used to apply the props for slot components.
  • sx (Array<function | object | bool> | function | object): The system prop that allows defining system overrides as well as additional CSS styles.
  • type (string): It is used to set the type of input element. The default value is ‘text’.
  • value (any): The value of the input element.

CSS Rules:  

  • root ( .MuiFilledInput-root): The style applied to the root element.
  • colorSecondary (.MuiFilledInput-colorSecondary): The styles applied to the root element if color secondary.
  • underline ( .MuiFilledInput-underline): The style applied to the root element unless disableUnderline is set to true.
  • focused (.Mui-focused): The style applied to the root element if the component is focused.
  • disabled (.Mui-disabled): The style applied to the root element if disabled is set to true.
  • adornedStart (.MuiFilledInput-adornedStart ): The style applied to the root element if the startAdornment is provided.
  • adornedEnd (.MuiFilledInput-adornedEnd ): The style applied to the root element if the endAdornment is provided.
  • error (.Mui-error): The style applied to the root element if the error is true.
  • sizeSmall ( .MuiFilledInput-sizeSmall): The style is applied to the input element if the size is small.
  • multiline ( .MuiFilledInput-multiline): The style applied to the root element if multiline is true.
  • hiddenLabel ( .MuiFilledInput-hiddenLabel): The style applied to the root element if hiddenLabel is true.
  • input ( .MuiFilledInput-input): The style applied to the input element.
  • inputSizeSmall (.MuiFilledInput-inputSizeSmall): The style applied to the input element if size is small.
  • inputHiddenLabel (.MuiFilledInput-inputHiddenLabel): The style applied to the input element if in.
  • inputMultiline (.MuiFilledInput-inputMultiline): The style applied to the input element if multiline is true.
  • inputAdornedStart (.MuiFilledInput-inputAdornedStart): The style applied to the input element if startAdornment is provided.
  • inputAdornedEnd (.MuiFilledInput-inputAdornedEnd): The style applied to the input element if endAdornment is provided.

Syntax: Create a FilledInput element as follows:

<FilledInput
    value={values.username}
    onChange={handleChange("username")}
/>

Installing and Creating React app and adding the MUI dependencies:

Step 1: Create a React application using the following command:

npx create-react-app foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd foldername

Step 3: After creating the ReactJS application, Install the required MUI dependencies using the following command:

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

Project Structure: It will look like the following.

 

Now let’s see some examples that demonstrate the use of MUI’s FIlledInput component in react.

Example 1: In the following example, we have created a Sign in form using FilledInput components.

App.js

Javascript




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import IconButton from "@mui/material/IconButton";
import FilledInput from "@mui/material/FilledInput";
import InputLabel from "@mui/material/InputLabel";
import InputAdornment from "@mui/material/InputAdornment";
import FormControl from "@mui/material/FormControl";
import Visibility from "@mui/icons-material/Visibility";
import VisibilityOff from "@mui/icons-material/VisibilityOff";
import { Button, Typography } from "@mui/material";
 
function App() {
    const [values, setValues] = React.useState({
        username: "",
        password: "",
        showPassword: false,
    });
 
    const handleChange = (prop) => (event) => {
        setValues({ ...values, [prop]: event.target.value });
    };
 
    const handleClickShowPassword = () => {
        setValues({
            ...values,
            showPassword: !values.showPassword,
        });
    };
 
    const handleMouseDownPassword = (event) => {
        event.preventDefault();
    };
 
    const handleFormSubmit = (event) => {
        event.preventDefault();
        alert("Signed in successfully");
    };
 
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <Typography
                    variant="h3"
                    sx={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </Typography>
                <Typography variant="p" fontWeight={600}>
                    React MUI FilledInput API
                </Typography>
            </div>
            <br />
            <Box
                component="form"
                onSubmit={handleFormSubmit}
                sx={{
                    width: "fit-content",
                    margin: "auto",
                    display: "flex",
                    flexDirection: "column",
                    gap: "10px",
                    maxWidth: "300px",
                }}
            >
                <FormControl fullWidth variant="filled">
                    <InputLabel htmlFor="filled-adornment-username">
                        Username
                    </InputLabel>
                    <FilledInput
                        id="filled-adornment-amount"
                        value={values.username}
                        onChange={handleChange("username")}
                        startAdornment={<InputAdornment
                        position="start">@</InputAdornment>}
                        required
                    />
                </FormControl>
                <FormControl variant="filled">
                    <InputLabel htmlFor="filled-adornment-password">
                        Password
                    </InputLabel>
                    <FilledInput
                        id="filled-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>
                        }
                        required
                    />
                </FormControl>
                <Button type="submit" variant="contained">
                    Sign in
                </Button>
            </Box>
        </div>
    );
}
export default App;


Step to Run Application: Run the application using the following command from the root directory of the project:

npm run start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

 

Example 2: In the following example, we will see a multiline input using FilledInput component.

App.js

Javascript




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import FilledInput from "@mui/material/FilledInput";
import InputLabel from "@mui/material/InputLabel";
import FormControl from "@mui/material/FormControl";
import { Button, Typography } from "@mui/material";
 
function App() {
    const [values, setValues] = React.useState({
        comment: "",
    });
 
    const handleChange = (prop) => (event) => {
        setValues({ ...values, [prop]: event.target.value });
    };
 
    const handleFormSubmit = (event) => {
        event.preventDefault();
        alert("Signed in successfully");
    };
 
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <Typography
                    variant="h3"
                    sx={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </Typography>
                <Typography variant="p" fontWeight={600}>
                    React MUI FilledInput API
                </Typography>
            </div>
            <br />
            <Box
                component="form"
                onSubmit={handleFormSubmit}
                sx={{
                    width: "100%",
                    margin: "auto",
                    display: "flex",
                    flexDirection: "column",
                    gap: "10px",
                    maxWidth: "300px",
                }}
            >
                <FormControl fullWidth variant="filled">
                    <InputLabel htmlFor="comment">Comment</InputLabel>
                    <FilledInput
                        id="comment"
                        value={values.comment}
                        onChange={handleChange("comment")}
                        required
                        fullWidth
                        multiline
                        maxRows={5}
                        minRows={3}
                    />
                </FormControl>
                <Button type="submit" variant="contained">
                    Post
                </Button>
            </Box>
        </div>
    );
}
export default App;


Step to Run Application: Run the application using the following command from the root directory of the project:

npm run start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

 

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads