How to make mui select with icon in react js?

Hi 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.

React, Mui, select, with, icon

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.

Try it Yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts