简体   繁体   English

如何在 StoryBook 和 React TypeScript 中创建动态 Material UI Icon Atom?

[英]How to create a dynamic Material UI Icon Atom in StoryBook and React TypeScript?

I am using StoryBook with React , TypeScript , and Material UI .我将StoryBookReactTypeScriptMaterial UI一起使用。 I want to create an Icon Atom that behaves dynamically so that it can render the icon which I want to pass in as a prop .我想创建一个动态行为的Icon Atom ,以便它可以呈现我想要作为prop传入的图标。

Right Now, I am using the below-mentioned approach, but there are some problems with this.现在,我正在使用下面提到的方法,但这存在一些问题。

  1. I am unable to change the Icon for a particular story.我无法更改特定故事的图标。 That's not the dynamic behavior I want.这不是我想要的动态行为。
  2. I also want to pass in some props such as fontSize and color to my Material UI Icon so that it can use them to render differently based on the value of props .我还想将一些props (例如fontSizecolor )传递给我的Material UI Icon ,以便它可以根据props的值使用它们进行不同的渲染。

I want my component to behave according to the above two points.我希望我的组件按照上述两点运行。 Please help !!请帮忙 !!

Icon.stories.tsx File Icon.stories.tsx文件

import React from "react";
import { ComponentStory, ComponentMeta } from "@storybook/react";
import MyIcon from "./Icon";
import AccessTimeIcon from "@mui/icons-material/AccessTime";
import PersonOutlineIcon from "@mui/icons-material/PersonOutline";

export default {
  title: "Atoms/MyIcon",
  component: MyIcon,
  argTypes: {
    color: {
      options: [
        "primary",
        "secondary",
        "warning",
        "error",
        "info",
        "success",
        "action",
      ],
      control: { type: "radio" },
    },
    fontSize: {
      options: ["small", "medium", "large"],
      control: { type: "radio" },
    },
  },
} as ComponentMeta<typeof MyIcon>;

const Template: ComponentStory<typeof MyIcon> = (args) => <MyIcon {...args} />;

export const Clock = Template.bind({});
Clock.args = {
  fontSize: "medium",
  color: "primary",
  materialIcon: <AccessTimeIcon />,
};

export const Person = Template.bind({});
Person.args = {
  fontSize: "large",
  color: "warning",
  materialIcon: <PersonOutlineIcon />,
};

Icon.tsx File Icon.tsx文件

import React from "react";
import type { IconProps } from "@mui/material/Icon";

interface MyIconProps extends IconProps {
  materialIcon: React.ReactElement;
}

const MyIcon = ({ materialIcon, fontSize, color }: MyIconProps) => {
  return <>{materialIcon}</>;
};
export default MyIcon;

One approach is to use a library like material-icons-react which provides a react component that you can render with custom props.一种方法是使用像material-icons-react这样的库,它提供了一个可以使用自定义道具渲染的反应组件。

import MaterialIcon, {colorPalette} from 'material-icons-react';

<MaterialIcon icon="dashboard" />

// different sizes
<MaterialIcon icon="dashboard" size='large' />
<MaterialIcon icon="dashboard" size={100} />

// colours
<MaterialIcon icon="dashboard" color={colorPalette.amber._200} />
<MaterialIcon icon="dashboard" color={colorPalette.amber.A700} />
<MaterialIcon icon="dashboard" color='#7bb92f' />

Another approach is including the material icons via Google Web Fonts script.另一种方法是通过 Google Web Fonts 脚本包含素材图标。

Add this to your html entry point:将此添加到您的 html 入口点:

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

Then you can use the material-icons class name and put the icon name inside the element:然后您可以使用material-icons class 名称并将图标名称放在元素内:

<span class="material-icons">access_time_icon</span>

You can put this rendering in you Icon component and pass any styling props you want.您可以将此渲染放在您的 Icon 组件中并传递您想要的任何样式道具。

Note that if you use other icon variants, eg outlined, you need to add a separate font for each variant and use a different class like material-icons-outlined .请注意,如果您使用其他图标变体,例如轮廓,您需要为每个变体添加单独的字体并使用不同的 class ,例如material-icons-outlined

 <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <span class="material-icons">access_time_icon</span>

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

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