简体   繁体   English

如何从一组对象中获取 map MUI 5 图标并更改其颜色?

[英]How to map MUI 5 icons from an array of objects and change its color?

I have an array of objects which bassically are sidebar items.我有一组对象,基本上是侧边栏项目。 Every object has a MUI 5 icon.每个 object 都有一个 MUI 5 图标。 In a sidebar component I render it in a map function. I'd like to have a selected item to be highlighted...I managed to change the font color but don't know how to change icon color.在侧边栏组件中,我将其呈现为 map function。我想要突出显示一个选定的项目...我设法更改了字体颜色,但不知道如何更改图标颜色。

So the question is how to change icon color in Sidebar.tsx?所以问题是如何更改 Sidebar.tsx 中的图标颜色?

routes.tsx路线.tsx

export const SIDEBAR_PATHS = [
  {
    id: 1,
    path: PATHS.projects,
    name: 'Projects',
    icon: <AccountTreeIcon />,
  },
  {
    id: 2,
    path: PATHS.faces,
    name: 'Faces',
    icon: <FaceRetouchingNaturalIcon />,
  },
];

Sidebart.tsx边栏.tsx

    {SIDEBAR_PATHS.map(({ id, path, name, icon }) => (
      <ListItem key={id} disablePadding>
        <ListItemButton disableRipple onClick={() => navigate(path)} selected={path === pathname}>
          {icon}
          <ListItemText primary={name} primaryTypographyProps={{ fontWeight: 500 }} />
        </ListItemButton>
      </ListItem>
    ))}

but don't know how to change icon color.但不知道如何更改图标颜色。

I'd change your icon field from JSX type (already called react component) to FC (a react component) so you can call it later and pass props into it.我会将您的icon字段从JSX类型(已经称为 React 组件)更改为FC (一个 React 组件),以便您稍后可以调用它并将道具传递给它。

Then, just tweak the color of the icon depending on the selected prop.然后,只需根据selected道具调整图标的颜色。

export const SIDEBAR_PATHS = [
  {
    id: 1,
    path: PATHS.projects,
    name: 'Projects',
    icon: AccountTreeIcon
  },
  {
    id: 2,
    path: PATHS.faces,
    name: 'Faces',
    icon: FaceRetouchingNaturalIcon,
  },
];

{SIDEBAR_PATHS.map(({ id, path, name, icon }) => {
  const Icon = icon;      

  return (
    <ListItem key={id} disablePadding>
      <ListItemButton disableRipple onClick={() => navigate(path)} selected={path === pathname}>
        <Icon style={{ color: selected ? 'green' : 'red'} />
              //  ^^^^^^^^^^^^^^^^ toggle the color of icon

        <ListItemText primary={name} primaryTypographyProps={{ fontWeight: 500 }} />
      </ListItemButton>
    </ListItem>
  );
})}

For mui 5 you could try this.对于 mui 5 你可以试试这个。 Mui provides SvgIcon component for the same purpose. Mui 出于同样的目的提供了 SvgIcon 组件。

import { SvgIcon } from "@mui/material";

....

{SIDEBAR_PATHS.map(({ id, path, name, icon }) => {
  return (
    <ListItem key={id} disablePadding>
      <ListItemButton disableRipple onClick={() => navigate(path)} selected={path === pathname}>
        <SvgIcon style={{ color: path === pathname ? 'green' : 'blue' }}{icon}</SvgIcon>
        <ListItemText primary={name} primaryTypographyProps={{ fontWeight: 500 }} />
      </ListItemButton>
    </ListItem>
  );
})}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM