This chapter explains how to use MUI components in our frontend. We will use the Button component to show the styled buttons. We will also use MUI icons and the IconButton component. The modal form input fields are replaced by TextField components.
In this chapter, we will cover the following topics:
The Spring Boot application that we created in Chapter 5, Securing and Testing Your Backend, is required, together with the modification from Chapter 10, Setting Up the Frontend for Our Spring Boot RESTful Web Service (the unsecured backend).
We also need the React app that we used in Chapter 11, Adding CRUD Functionalities.
The following code samples available at the GitHub link will also be required: https://github.com/PacktPublishing/Full-Stack-Development-with-Spring-Boot-and-React/tree/main/Chapter12.
Check out the following video to see the Code in Action: https://bit.ly/3x04wnw
Execute the following steps to implement the Button component:
// AddCar.js
import Button from '@mui/material/Button';
The following code shows the AddCar component's return statement with the changes:
// AddCar.js
return(
<div>
<Button variant="contained" onClick={handleClickOpen}>
New Car
</Button>
<Dialog open={open} onClose={handleClose}>
<DialogTitle>New car</DialogTitle>
<DialogContent>
<input placeholder="Brand" name="brand"
value={car.brand}
onChange={handleChange}/><br/>
<input placeholder="Model" name="model"
value={car.model}
onChange={handleChange}/><br/>
<input placeholder="Color" name="color"
value={car.color}
onChange={handleChange}/><br/>
<input placeholder="Year" name="year"
value={car.year} onChange={handleChange}
/><br/>
<input placeholder="Price" name="price"
value={car.price} onChange=
{handleChange}/><br/>
</DialogContent>
<DialogActions>
<Button onClick={handleClose}>Cancel
</Button>
<Button onClick={handleSave}>Save</Button>
</DialogActions>
</Dialog>
</div>
);
Now, the NEW CAR button is also rendered using the MUI Button component, but the layout is not looking good because the button is touching the toolbar.
import Stack from '@mui/material/Stack';
// Carlist.js
<Stack mt={2} mb={2}>
<AddCar addCar={addCar} />
</Stack>
Now, the NEW CAR button should look as shown in the following screenshot:
The modal form buttons should look like the following:
We also need to change the buttons in the EditCar component. The button that opens the modal form is the Edit button, which is shown in the table, and that will be changed later using icons.
Let's change CANCEL and SAVE buttons as we did with the new car form:
// EditCar.js
import Button from '@mui/material/Button';
return(
<div>
<button onClick={handleClickOpen}>Edit</button>
<Dialog open={open} onClose={handleClose}>
<DialogTitle>Edit car</DialogTitle>
<DialogContent>
<input placeholder="Brand" name="brand"
value={car.brand}
onChange={handleChange}/><br/>
<input placeholder="Model" name="model"
value={car.model} onChange=
{handleChange}/><br/>
<input placeholder="Color" name="color"
value={car.color} onChange=
{handleChange}/><br/>
<input placeholder="Year" name="year"
value={car.year} onChange=
{handleChange}/><br/>
<input placeholder="Price" name="price"
value={car.price} onChange=
{handleChange}/><br/>
</DialogContent>
<DialogActions>
<Button onClick={handleClose}>Cancel
</Button>
<Button onClick={handleSave}>Save</Button>
</DialogActions>
</Dialog>
</div>
);
Now, the buttons in the edit form are also implemented using the MUI Button component. Next, we will use the IconButton component for the Edit and DELETE buttons in the grid.
MUI provides prebuilt SVG icons that we have to install by using the following command in the terminal:
npm install @mui/icons-material
Let's first implement the DELETE button in the grid. The MUI IconButton component can be used to render icon buttons. The @mui/icons-material package that we just installed contains lots of icons that can be used with MUI.
You can find the list of icons available from the MUI documentation. We need an icon for our DELETE button; therefore, we will use an icon called DeleteIcon:
// Carlist.js
import IconButton from '@mui/material/IconButton';
import DeleteIcon from '@mui/icons-material/Delete';
// Carlist.js
const columns = [
{field: 'brand', headerName: 'Brand', width: 200},
{field: 'model', headerName: 'Model', width: 200},
{field: 'color', headerName: 'Color', width: 200},
{field: 'year', headerName: 'Year', width: 150},
{field: 'price', headerName: 'Price', width: 150},
{
field: '_links.car.href',
headerName: '',
sortable: false,
filterable: false,
renderCell: row =>
<EditCar
data={row}
updateCar={updateCar} />
},
{
field: '_links.self.href',
headerName: '',
sortable: false,
filterable: false,
renderCell: row =>
<IconButton onClick={() => onDelClick
(row.id)}>
<DeleteIcon color="error" />
</IconButton>
}
];
Now, the DELETE button in the grid should look like the following screenshot:
Next, we will implement the Edit button using the IconButton component.
// EditCar.js
import IconButton from '@mui/material/IconButton';
import EditIcon from '@mui/icons-material/Edit';
// EditCar.js return
<IconButton onClick={handleClickOpen}>
<EditIcon color="primary" />
</IconButton>
Finally, both buttons are rendered as icons, as shown in the following screenshot:
Next, we will implement text fields using the MUI TextField component.
In this section, we'll change the text inputs in the modal forms using the MUI TextField and Stack components:
import TextField from '@mui/material/TextField';
import Stack from '@mui/material/Stack';
The text fields are wrapped inside the Stack component to get spacing between the components and to set the top margin:
// AddCar.js, do the same modifications to the EditCar.js file
return(
<div>
<Button variant="contained"
onClick={handleClickOpen}>New Car</Button>
<Dialog open={open} onClose={handleClose}>
<DialogTitle>New car</DialogTitle>
<DialogContent>
<Stack spacing={2} mt={1}>
<TextField label="Brand" name="brand"
autoFocus
variant="standard" value={car.brand}
onChange={handleChange}/>
<TextField label="Model" name="model"
variant="standard" value={car.model}
onChange={handleChange}/>
<TextField label="Color" name="color"
variant="standard" value={car.color}
onChange={handleChange}/>
<TextField label="Year" name="year"
variant="standard" value={car.year}
onChange={handleChange}/>
<TextField label="Price" name="price"
variant="standard" value={car.price}
onChange={handleChange}/>
</Stack>
</DialogContent>
<DialogActions>
<Button onClick={handleClose}>Cancel
</Button>
<Button onClick={handleSave}>Save</Button>
</DialogActions>
</Dialog>
</div>
);
After the modifications, the modal form should look like the following:
Now, we have completed the styling of our frontend using MUI components.
In this chapter, we finalized our frontend using MUI, which is the React component library that implements Google's Material Design. We replaced the buttons with the MUI Button and IconButton components. Our modal form got a new look by using the MUI TextField component. After these modifications, our frontend looks more professional and uniform.
In the next chapter, we will focus on frontend testing.
Packt has other great resources available for learning about React. These are as follows:
18.226.180.161