How to make mui select with icon in react js?
February 04, 2023Hi Friends 👋,
Welcome To aGuideHub!
To make mui select with icon in react js, first, import icon
in mui library. With the help of renderValue
, we can insert an icon in the select. It will make mui select with an icon in react js.
Today, I am going to show you, how to make mui select with icon in react js.
Installation
Install the following packages to use mui select with icon in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
MUI material make select with icon example.
The below code is an example of making mui select with icon using mui.
App.js
import * as React from "react";
import Box from "@mui/material/Box";
import MenuItem from "@mui/material/MenuItem";
import Select from "@mui/material/Select";
import CurrencyExchangeIcon from '@mui/icons-material/CurrencyExchange';
import SvgIcon from "@mui/material/SvgIcon";
const currencies = [
{
value: "USD",
label: "$"
},
{
value: "EUR",
label: "€"
},
{
value: "BTC",
label: "฿"
},
{
value: "JPY",
label: "¥"
}
];
export default function SelectTextFields() {
return (
<Box>
<Select
sx={{ width: 130 }}
defaultValue=""
displayEmpty
renderValue={(value) => {
console.log(value);
return (
<Box sx={{ display: "flex", gap: 1 }}>
<SvgIcon color="primary">
<CurrencyExchangeIcon/>
</SvgIcon>
{value}
</Box>
);
}}
>
{currencies.map((option) => (
<MenuItem key={option.value} value={option.value}>
{option.value}
</MenuItem>
))}
</Select>
</Box>
);
}
In the above code example, I have used the @mui/material
component and made mui select with icon in react js
Check the output of the above code example.
Here, we are provided code sandbox links for the above program make mui select with icon. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍