如何设置Material UI图标的fontSize属性?

huangapple go评论66阅读模式
英文:

How to type Material UI Icon's fontSize prop?

问题

import { SvgIconComponent } from '@mui/icons-material'
import { Typography, TypographyProps } from '@mui/material'

type Props = TypographyProps & {
  Icon: SvgIconComponent
  iconFontSize: /* 插入类型这里! */
}

export const IconTypography = ({
  Icon,
  iconFontSize = 'inherit',
  columnGap = 1,
  children,
  ...props
}: Props) => {
  return (
    <Typography display="flex" alignItems="center" columnGap={columnGap} {...props}>
      <Icon fontSize={iconFontSize} />
      {children}
    </Typography>
  )
}
英文:

I've composed the component below, and I need to apply a type for the custom iconFontSize prop. How can I do this?

import { SvgIconComponent } from &#39;@mui/icons-material&#39;
import { Typography, TypographyProps} from &#39;@mui/material&#39;

type Props = TypographyProps &amp; {
  Icon: SvgIconComponent
  iconFontSize: /* insert type here! */
}

export const IconTypography = ({
  Icon,
  iconFontSize = &#39;inherit&#39;,
  columnGap = 1,
  children,
  ...props
}: Props) =&gt; {
  return (
    &lt;Typography display=&quot;flex&quot; alignItems=&quot;center&quot; columnGap={columnGap} {...props}&gt;
      &lt;Icon fontSize={iconFontSize} /&gt;
      {children}
    &lt;/Typography&gt;
  )
}

Thanks in advance!

答案1

得分: 1

你可以将 `iconFontSize` 定义为联合类型的	'inherit' | 'large' | 'medium' | 'small'

```typescript
type Props = TypographyProps &amp; {
  Icon: SvgIconComponent
  iconFontSize: "inherit" | "small" | "medium" | "large" 
}

如果你想使用 MUI 类型定义文件中的确切类型,你也可以这样做:


import { OverridableStringUnion } from '@mui/types';

fontSize?: OverridableStringUnion<
  'inherit' | 'large' | 'medium' | 'small',
  SvgIconPropsSizeOverrides
>;

OverridableStringUnion 类型本质上是这样定义的:

export type OverridableStringUnion<T extends string | number, U = {}> = GenerateStringUnion<
  Overwrite<Record<T, true>, U>
>;

在 MUI 文档中查看类型定义:https://mui.com/material-ui/api/svg-icon/


<details>
<summary>英文:</summary>

You can type `iconFontSize` as a union type of 	&#39;inherit&#39; | &#39;large&#39; | &#39;medium&#39; | &#39;small&#39;:

```typescript
type Props = TypographyProps &amp; {
  Icon: SvgIconComponent
  iconFontSize: &quot;inherit&quot; | &quot;small&quot; | &quot;medium&quot; | &quot;large&quot; 
}

If you want to use the exact type from the MUI type definition file, you can alternatively use:


import { OverridableStringUnion } from &#39;@mui/types&#39;;

fontSize?: OverridableStringUnion&lt;
  &#39;inherit&#39; | &#39;large&#39; | &#39;medium&#39; | &#39;small&#39;,
  SvgIconPropsSizeOverrides
&gt;;

The OverridableStringUnion type is in turn defined as such:

export type OverridableStringUnion&lt;T extends string | number, U = {}&gt; = GenerateStringUnion&lt;
  Overwrite&lt;Record&lt;T, true&gt;, U&gt;
&gt;;

See the type definition in the MUI docs here: https://mui.com/material-ui/api/svg-icon/

huangapple
  • 本文由 发表于 2023年6月2日 03:00:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定