Search code examples
javascriptreactjsmaterial-uimaterial-table

react Material-table unable to change each icon set color?


I am using the material-table library and trying to change each icon color. Would you please help me to do so? I tried custom CSS but it's changing all icon color at once not the specific one? here is my code sandbox link. In short: I want to change color of each of three icons add, edit, delete

Here is my code I am using a material-table library with material-ui. Also is it possible to change these default icons?

List.js

import React, { useState } from 'react';
import './App.css';
import MaterialTable from 'material-table'

import makeStyles from "@material-ui/core/styles/makeStyles";


const useStyles = makeStyles(theme => ({
    rootTable: {
        '& .MuiIconButton-colorInherit' : {
            color: '#6a2cd8'
        },
        '& .MuiIconButton-root .MuiIconButton-colorInherit': {
            color: 'red'
        },
        width: theme.spacing(150)
    }
}));


const empList = [
  { id: 1, name: "Neeraj", email: '[email protected]', phone: 9876543210, city: "Bangalore" },
  { id: 2, name: "Raj", email: '[email protected]', phone: 9812345678, city: "Chennai" },
  { id: 3, name: "David", email: '[email protected]', phone: 7896536289, city: "Jaipur" },
  { id: 4, name: "Vikas", email: '[email protected]', phone: 9087654321, city: "Hyderabad" },
]

function App() {

  const [data, setData] = useState(empList)
  const columns = [
    { title: "ID", field: "id", editable: false },
    { title: "Name", field: "name" },
    { title: "Email", field: "email" },
    { title: "Phone Number", field: 'phone', },
    { title: "City", field: "city", }
  ]


  const classes = useStyles();

  return (
    <div className="App">
      <h1 align="center">React-App</h1>
      <h4 align='center'>Material Table with CRUD operation</h4>
      <div className={classes.rootTable}>
      <MaterialTable
        title="Employee Data"
        data={data}
        columns={columns}
        editable={{
          onRowAdd: (newRow) => new Promise((resolve, reject) => {
            const updatedRows = [...data, { id: Math.floor(Math.random() * 100), ...newRow }]
            setTimeout(() => {
              setData(updatedRows)
              resolve()
            }, 2000)
          }),
          onRowDelete: selectedRow => new Promise((resolve, reject) => {
            const index = selectedRow.tableData.id;
            const updatedRows = [...data]
            updatedRows.splice(index, 1)
            setTimeout(() => {
              setData(updatedRows)
              resolve()
            }, 2000)
          }),
          onRowUpdate:(updatedRow,oldRow)=>new Promise((resolve,reject)=>{
            const index=oldRow.tableData.id;
            const updatedRows=[...data]
            updatedRows[index]=updatedRow
            setTimeout(() => {
              setData(updatedRows)
              resolve()
            }, 2000)
          })

        }}
        options={{
          actionsColumnIndex: -1, addRowPosition: "first"
        }}
      />
      </div>
    </div>
  );
}

export default App;


Solution

  • You can import icons you wish to change their color from @material-ui/icons, change the color you want, and pass them as icons prop to MaterialTable: codesandbox.

    import { Edit } from "@material-ui/icons";
    ...
    icons={{
      Edit: React.forwardRef((props, ref) => (
       <Edit style={{ color: "green" }} {...props} ref={ref} />
      ))
    }}