Open In App

React MUI FormControlLabel API

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  FormControlLabel API. The FormControlLabel helps to add labels to the control components like radio, checkboxes, etc.



Import Statement:

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

 



FormControlLabel Props:

CSS Rules:

Syntax:

<FormControlLabel></FormControlLabel>

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 @emotion/react @emotion/styled

Project Structure: It will look like the following.

 

Example 1: In this example, we added two FormControlLabel Component with controls set as Radio elements.




import * as React from 'react';
import { FormControlLabel, Radio } from '@mui/material';
  
export default function App() {
  
    return (
        <div style={{ margin: 10 }}>
            <h1 style={{ color: "green" }}>GeeksforGeeks</h1>
            <h4>React MUI FormControlLabel API</h4>
            <b>GeeksforGeeks User? </b>
            <FormControlLabel label="YES"
                value="yes" control={<Radio />} />
            <FormControlLabel label="NO" value="no"
                control={<Radio />} disabled />
        </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: We are adding Checkboxes as the control for the FormControlLabel Component. 




import * as React from 'react';
import { FormControl, FormControlLabel, Checkbox } from '@mui/material';
  
export default function App() {
  
    return (
        <div style={{ margin: 10 }}>
            <h1 style={{ color: "green" }}>GeeksforGeeks</h1>
            <h4>React MUI FormControlLabel API</h4>
            <b>
                Select the Programming languages
                you want to learn?
            </b>
            <FormControl>
                <FormControlLabel label="C++"
                    control={<Checkbox value="C++" />} />
                <FormControlLabel label="Python"
                    control={<Checkbox value="Python" />} />
                <FormControlLabel label="C#"
                    control={<Checkbox value="C#" />} />
                <FormControlLabel label="Java"
                    control={<Checkbox value="Java" />} />
                <FormControlLabel label="Go"
                    control={<Checkbox value="Go" />} />
                <FormControlLabel label="Ruby"
                    control={<Checkbox value="Ruby" />} />
            </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-label/


Article Tags :