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.
- autoComplete(string): It helps to fill up the forms faster with some predefined or previously used values.
- autoFocus(bool): If set to true, the input element is focussed during the first mount. False has been set as the default value.
- classes(object): It overrides or applies styles to the element.
- color(primary/secondary): It is used to set the color of the component. The default value is primary.
- components(Input): It is used to set the components used for each slot inside the InputBase.
- componentProps(props): It is used to apply the props for each slot inside the Input.
- defaultValue(any): The default value of the input element.
- disabled(bool): If set to true, the input element is disabled. False has been set as the default value.
- disableUnderline(bool): If set to true, the input will not have an underline. False has been set as the default value.
- endAdornment(node): It is the end InputAdornment for this component.
- error(bool): If set to true, the input will indicate an error. False has been set as the default value.
- fullWidth(bool): If set to true, the input will take the full width. False has been set as the default value.
- id(string): It is used to set the id of the element.
- inputComponent(elementType): It is used to set the type of input element. The default value is input.
- inputProps(object): It is used to set 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 is used to set 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. False has been set as the default value.
- name(string): The name of the input element.
- onChange(func): This is the callback function used when the value inside the input is changed.
- placeholder(string): It is used to set the hint displayed in the input element.
- readOnly(bool): If set to true, the user will be able to read the input value only. False has been set as the default value.
- required(bool): If set to true, the input element needs to be filled up by the user.
- rows(number): Number of rows to display when multiline is set to true.
- sx (Array<func / object/bool> / func / object): The system prop 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(.MuiInput-root): It is the style applied to the root element.
- formControl(.MuiInput-formControl): It is the style applied to the root element if the component is a descendant of FormControl.
- focused(.Mui-focused): It is the style applied to the root element if the component is focused.
- disabled(.Mui-disabled): It is the style applied to the root element if disabled is set to true.
- colorSecondary(.MuiInput-colorSecondary): It is the style applied to the root element if the color is set to secondary.
- underline(.MuiInput-underline): It is the style applied to the root element unless disableUnderline is set to true.
- error(.Mui-error): It is the style class applied to the root element if the error is set to true.
- sizeSmall(.MuiInput-sizeSmall): It is the style applied to the input element if the size is set to small.
- multiline(.MuiInput-multiline): It is the style applied to the root element if multiline is set to true.
- fullWidth(.MuiInput-fullWidth): It is the style applied to the root element if fullWidth is set to true.
- input(.MuiInput-input): It is the style applied to the input element.
- inputSizeSmall(.MuiInput-inputSizeSmall): It is the style applied to the input element if size is set to small.
- inputMultiline(.MuiInput-inputMultiline): It is the style applied to the input element if multiline is set to true.
- inputTypeSearch(.MuiInput-inputTypeSearch): It is the style applied to the input element if type is set to search.
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.
App.js
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.
App.js
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/
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability. "Hello, World!" Program in ReactJavaScriptimport React from 'react'; function App() {
6 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides methods to interact with the Document Object Model, or DOM. This package allows developers to access and modify the DOM. It is a package in React that provides DOM-specific methods that can be used at the top level of a web app to enable an efficient wa
3 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsIn lists, React makes it easier to render multiple elements dynamically from arrays or objects, ensuring efficient and reusable code. Since nearly 85% of React Projects involve displaying data collections- like user profiles, product catalogs, or tasks- understanding how to work with lists.To render
4 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. the When rendering a list, you need to assign a unique key prop to each element i
4 min read
Components in React
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects