简体   繁体   English

如何在物料ui SVG ICON中使用自定义SVG文件

[英]How to use a custom SVG file in material ui SVG ICON

I am trying to use a chip with SVG delete icon, 我正在尝试使用带有SVG删除图标的chip

The icon code is 图标代码是

const icon = (props) => {
    return (
        <SvgIcon>
            <img src={'ic_check.svg'} style={{width: '20px'}} width={'20px'}/>
        </SvgIcon>
    )
};

The content of SVG file SVG文件的内容

<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
    <g fill="none" fill-rule="evenodd">
        <rect width="20" height="20" x="2" y="2" fill="#6FB934" rx="10"/>
        <path fill="#FFF" fill-rule="nonzero" d="M9.5 15.475L6.025 12l-1.183 1.175L9.5 17.833l10-10-1.175-1.175z"/>
        <path d="M0 0h24v24H0z"/>
    </g>
</svg>

and the chip finally is 最后是芯片

<Chip
      label={ViewUtils.NOT_EXPIRED}
      className={classes.chip}
      onDelete={() => {}}
      deleteIcon={<icon/>}/>

But this is not working and I checked for the path and it is correct as I can render same svg in img tag. 但这不起作用,我检查了路径,它是正确的,因为我可以在img标签中呈现相同的svg。

Nayan SvgIcon takes a svg path that you can further style. Nayan SvgIcon采用svg路径,您可以进一步设计风格。 But in your case your svg is already styled. 但在你的情况下你的svg已经设计了风格。 It doesn't take svg file directory path which actually lose SvgIcon API purpose. 它不需要svg文件目录路径实际上失去了SvgIcon API的目的。 You just need to remove SvgIcon from img tag: 你只需要从img标签中删除SvgIcon

<Chip
  label={ViewUtils.NOT_EXPIRED}
  className={classes.chip}
  onDelete={() => {console.log('You Deleted this icon')}}
  deleteIcon={icon}
 />

and Make you svg as const or import from assets file directory I haven't tried that, 并使你svg作为const或从资产文件目录导入我没有尝试过,

const icon = <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
               <g fill="none" fill-rule="evenodd">
                <rect width="20" height="20" x="2" y="2" fill="#6FB934" rx="10"/>
                <path fill="#FFF" fill-rule="nonzero" d="M9.5 15.475L6.025 12l-1.183 1.175L9.5 17.833l10-10-1.175-1.175z"/>
                <path d="M0 0h24v24H0z"/>
             </g>
           </svg>

There is a reason why we can't make <icon/> component. 我们不能制作<icon/>组件是有原因的。 If we make it as component as following: 如果我们将其作为组件如下:

const Icon = (props) => {
return (
<SvgIcon>
  <svg
    xmlns="http://www.w3.org/2000/svg"
    width="24"
    height="24"
    viewBox="0 0 24 24"
  >
    <g fill="none" fill-rule="evenodd">
      <rect width="20" height="20" x="2" y="2" fill="#6FB934" rx="10" />
      <path
        fill="#FFF"
        fill-rule="nonzero"
        d="M9.5 15.475L6.025 12l-1.183 1.175L9.5 17.833l10-10-1.175-1.175z"
      />
      <path d="M0 0h24v24H0z" />
    </g>
  </svg>
</SvgIcon>
)
};

It works like a charm but onDelete doesn't get fired on this component.I've reported this issue as well on material UI. 它就像一个魅力,但onDelete不会被解雇这个组件。我已经在材料UI上报告了这个问题。 In first case onDelete gets called every time. 在第一种情况下,每次都会调用onDete。 Feel free to ask any question. 随意提出任何问题。

EDITED Fixed the above issue for Icon as Component rather than const. EDITED修复了Icon作为Component而不是const的上述问题。 here is the codesandbox link for working example: https://codesandbox.io/s/98842r4yy4 这是工作示例的codesandbox链接: https ://codesandbox.io/s/98842r4yy4

I am using React and typescript, created a component and added the tags from HTML and it worked normally. 我正在使用React和typescript,创建了一个组件并从HTML添加了标签并且它正常工作。

export const IconTable: React.FC = () => {
    return (
        <svg width="130" height="130" viewBox="0 0 1024 1024">
            <path d="M505.947 123.597a23.096 23.096 0 0 0-16.99-7.477h-6.837c-17.929 0-32.631 13.468-34.198 "/>
        </svg>
);

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

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