如何创建一个支持子元素的 Mui 样式剪辑

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

How to create a Mui styled clip which support children

问题

以下是翻译好的内容:

如何创建一个支持单一声明和嵌套声明的样式芯片?我有以下的 **MUI v5** 样式芯片。

```tsx
interface StyledModalChipProps {
 theme: Theme
 children: React.ReactNode
}

export const StyledModalChip = styled(Chip)<StyledModalChipProps>(
 ({theme}) => ({
  margin: 5,
  padding: 2,
  borderRadius: 10,
  height: 'fit-content',
  display: 'flex',
  justifyContent: 'center',
  textAlign: 'center',
  alignItems: 'center',
  color: theme.palette.primary.contrastText,
  backgroundColor: theme.palette.primary.main,
  // 样式化标签
  '& .MuiChip-label': {
   fontWeight: 700,
   fontSize: '1.1rem',
  },
 })
)

也修复这个问题,我希望能够在两种不同的情况下使用它,就像这样。

// 情况1 直接使用标签
<StyledModalChip label="自定义数据" />

// 情况2 嵌套标签和图标
<StyledModalChip>
 <LocalHospitalIcon />
 <Typography>前往急诊</Typography>
</StyledModalChip>

当前的样式出现错误:

Type 'Element' is not assignable to type 'null | undefined'.ts(2322)
Chip.d.ts(24, 5): The expected type comes from property 'children' which is
declared here on type 'IntrinsicAttributes & { avatar?: ReactElement<any,
 string
 |
 JSXElementConstructor<any
 >> | undefined; children?: null | undefined; ... 12 more ...; variant?:
 OverridableStringUnion<...> | undefined; } & CommonProps & Omit<...> &
 MUIStyledCommonProps<...>'</any,

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

How to create a styled chip that supports both single declaration and nested declaration?. I have the following **MUI v5** styled chip.

```tsx
interface StyledModalChipProps {
 theme: Theme
 children: React.ReactNode
}

export const StyledModalChip = styled(Chip)&lt;StyledModalChipProps&gt;(
 ({theme}) =&gt; ({
  margin: 5,
  padding: 2,
  borderRadius: 10,
  height: &#39;fit-content&#39;,
  display: &#39;flex&#39;,
  justifyContent: &#39;center&#39;,
  textAlign: &#39;center&#39;,
  alignItems: &#39;center&#39;,
  color: theme.palette.primary.contrastText,
  backgroundColor: theme.palette.primary.main,
  // Style the label
  &#39;&amp; .MuiChip-label&#39;: {
   fontWeight: 700,
   fontSize: &#39;1.1rem&#39;,
  },
 })
)

Fix this as well, and I want to be able to use it in two separate cases like this.

// Case 1 directly with labe
&lt;StyledModalChip label=&quot;Custom Data&quot; /&gt;

// Case 2 nested label with icon
&lt;StyledModalChip&gt;
 &lt;LocalHospitalIcon /&gt;
 &lt;Typography&gt;Go to emergerny&lt;/Typography&gt;
&lt;/StyledModalChip&gt;

The current style is error out showing:

Type &#39;Element&#39; is not assignable to type &#39;null | undefined&#39;.ts(2322)
Chip.d.ts(24, 5): The expected type comes from property &#39;children&#39; which is
declared here on type &#39;IntrinsicAttributes &amp; { avatar?: ReactElement&lt;any,
 string
 |
 JSXElementConstructor&lt;any
 &gt;&gt; | undefined; children?: null | undefined; ... 12 more ...; variant?:
 OverridableStringUnion&lt;...&gt; | undefined; } &amp; CommonProps &amp; Omit&lt;...&gt; &amp;
 MUIStyledCommonProps&lt;...&gt;&#39;&lt;/any,

答案1

得分: 1

正如您在下面的图像中所看到的,Muichip不支持childrenMui Chip APIs 参考

如何创建一个支持子元素的 Mui 样式剪辑

而且明确指出,如果您想使用children,则需要使用component属性。

如果您需要更改children的结构,请使用component属性。

<StyledModalChip>
  <LocalHospitalIcon/>
  <Typography>前往急诊</Typography>
</StyledModalChip>

因此,您可以使用label来解析children,这意味着您可以将更复杂的JSX甚至其他组件传递作为标签。有些类似这样:

export const StyledModalChip = styled(({ children, ...props }: StyledModalChipProps) => (
  <Chip label={children} {...props} />  
))(({ theme }) => ({
// 您的样式。
}));

现在您可以使用这段代码:

<StyledModalChip>
  <LocalHospitalIcon/>
  <Typography>前往急诊</Typography>
</StyledModalChip>
英文:

As you can see in the down below image, Mui chip doesn't support children:- Mui Chip APIs ref.

如何创建一个支持子元素的 Mui 样式剪辑

And also it clearly said that if you want to use children. Then, you need to the component.

> Use the component prop if you need to change the children structure.

&lt;StyledModalChip&gt;
  &lt;LocalHospitalIcon/&gt;
  &lt;Typography&gt;Go to emergerny&lt;/Typography&gt;
&lt;/StyledModalChip&gt;

So, you can use label to parse children. which means you can pass more complex JSX or even other components as the label. Some things like this:

export const StyledModalChip = styled(({ children, ...props }: StyledModalChipProps) =&gt; (
  &lt;Chip label={children} {...props} /&gt;  
))(({ theme }) =&gt; ({
// your styles.
}));

Now you can use this code:

&lt;StyledModalChip&gt;
  &lt;LocalHospitalIcon/&gt;
  &lt;Typography&gt;Go to emergerny&lt;/Typography&gt;
&lt;/StyledModalChip&gt;

huangapple
  • 本文由 发表于 2023年6月26日 11:44:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553397.html
匿名

发表评论

匿名网友

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

确定