Open In App

React MUI FormControl 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 FormControl API. The FormControl helps to provide context such as filled, focused, error, or required for form inputs. 



Import FormControl API:

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

 



FormControl Props:

CSS Rules:

Syntax:

<FormControl ></FormControl >

Creating React Application And Installing Module:

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 module using the following command:

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

Project Structure: It will look like the following.

 

Example 1: In this example, we added two input fields, in two FormControl Components. To the first, we are passing fullWidth and color props and two the second we are passing size and margin props.

App.js




import { Input, FormControl, InputLabel } from '@mui/material';
  
export default function App() {
    return (
        <div style={{ margin: 10 }}>
            <h1 style={{ color: "green" }}>
                GeeksforGeeks
            </h1>
            <h4>React MUI FormControl API</h4>
  
            <FormControl fullWidth color='success'>
                <InputLabel>Username: </InputLabel>
                <Input />
            </FormControl>
            <FormControl size="small" margin='dense'>
                <InputLabel>Age: </InputLabel>
                <Input />
            </FormControl>
        </div>
    );
}

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

npm start

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

 

Example 2: In this example, we are creating two states using react hook useState naming name and isBool. We are adding a function named onChangeHandler that checks whether the input text contains only a letter or not and changes the states. To the FormControl component, we are passing isBool to the error prop. When we are typing anything in the input field the on-change function onChangeHandler gets triggered.

App.js




import { FormControl, InputLabel, Input } from '@mui/material';
import { useState } from 'react';
  
export default function App() {
    const [name, setName] = useState('');
    const [isBool, setIsBool] = useState(false)
  
    const onChangeHandler = (e) => {
        setName(e.target.value)
        if (!name.match(/^[A-Za-z\s]*$/)) {
            setIsBool(true);
            alert('error: allows letters only')
        } else {
            setIsBool(false);
  
        }
    }
  
    return (
        <div style={{ margin: 10 }}>
            <h1 style={{ color: "green" }}>
                GeeksforGeeks
            </h1>
            <h4>React MUI FormControl API</h4>
            <FormControl error={isBool}>
                <InputLabel>Name: </InputLabel>
                <Input id="my-input" onChange={onChangeHandler} />
            </FormControl>
  
        </div>
    );
}

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

npm 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/form-control/


Article Tags :