在创建具有自定义属性的自定义样式的 Mui v5 组件时出现问题。

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

Issue when creating custom styled Mui v5 component with custom props

问题

我正在尝试使用 MUI 的 styled 函数创建一个样式化组件。

// 导入
import styled from '@mui/system/styled';
import Select, { SelectChangeEvent, SelectProps } from '@mui/material/Select';

// 扩展属性
interface SelectComponentAddedProps extends SelectProps {
  bool: boolean;
}

// MUI 样式化组件
// 为选择组件添加样式
export const StyledSelect = styled(Select)<SelectComponentAddedProps>(({ theme, bool }) => ({
  width: bool ? 'fit-content' : 300,
  textAlign: 'start',
  margin: '5px',
  fontWeight: 800,
}));
     
// 渲染中的组件
<StyledSelect
  variant={variant}
  labelId='select-label'
  name={name}
  disabled={disabled}
  value={value}
  renderValue={(value: unknown) => {
    if (typeof value === 'string') {
      return renderValue(name, value, options.length);
    }
  }}
  onChange={onChangeFn}
  displayEmpty
  disableUnderline
>

我收到以下错误消息:

React 不认识 DOM 元素上的 disableUnderline 属性。如果你有意让它出现在 DOM 中作为自定义属性,请将其拼写为小写的 disableunderline。如果你是从父组件意外传递它,请从 DOM 元素中删除它。

如何能够创建一个带有自定义属性的样式化组件,使其与组件的基本属性兼容?

英文:

I am trying to created a styled component using the styled funcion from mui

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Imports 
import styled from &#39;@mui/system/styled&#39;;
import Select, { SelectChangeEvent, SelectProps } from &#39;@mui/material/Select&#39;;

// Extended props
interface SelectComponentAddedProps extends SelectProps {
  bool: boolean;
}

// Mui Styled Component
// Style for the select component
export const StyledSelect = styled(Select)&lt;SelectComponentAddedProps&gt;(({ theme, bool }) =&gt; ({
  width: bool ? &#39;fit-content&#39; : 300,
  textAlign: &#39;start&#39;,
  margin: &#39;5px&#39;,
  fontWeight: 800,
}));
 
// Component in render 
&lt;StyledSelect
variant={variant}
labelId=&#39;select-label&#39;
name={name}
disabled={disabled}
value={value}
renderValue={(value: unknown) =&gt; {
if (typeof value === &#39;string&#39;) {
return renderValue(name, value, options.length);
}
}}
onChange={onChangeFn}
displayEmpty
disableUnderline
&gt;

<!-- end snippet -->

and I am getting the follwoing error massage

> Blockquote react-dom.development.js:86 Warning: React does not recognize the disableUnderline prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase disableunderline instead. If you accidentally passed it from a parent component, remove it from the DOM element.

How to be able to create a styled component with my custom props that will be compatible with the basic props of the component ?

答案1

得分: 2

// 在你的样式组件中使用“bool”属性,但不要将其传递到Select组件
export const StyledSelect = styled(Select, {
  shouldForwardProp: (prop) =&gt; prop !== 'bool', // &lt;----
})&lt;SelectComponentAddedProps&gt;(({ bool }) =&gt; ({
  width: bool ? 'fit-content' : 300,
  textAlign: 'start',
  margin: '5px',
  fontWeight: 800,
}));

// 在你的基础组件中:
&lt;StyledSelect
  // ...
  bool={/*...*/} // 将“bool”属性传递给样式组件
/&gt;
英文:

If your styled component is dependent on a prop from your base component to generate its styles, then you need to pass that prop to your styled component as well. However, you don't want to propagate it further into the Select component.

// Use the &quot;bool&quot; prop in your styles, but don&#39;t forward it to the Select component
export const StyledSelect = styled(Select, {
  shouldForwardProp: (prop) =&gt; prop !== &#39;bool&#39;, // &lt;----
})&lt;SelectComponentAddedProps&gt;(({ bool }) =&gt; ({
  width: bool ? &#39;fit-content&#39; : 300,
  textAlign: &#39;start&#39;,
  margin: &#39;5px&#39;,
  fontWeight: 800,
}));

// In your base component:
&lt;StyledSelect
  // ...
  bool={/*...*/} // Pass the &quot;bool&quot; prop to the Styled Component
/&gt;

huangapple
  • 本文由 发表于 2023年7月3日 01:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600156.html
匿名

发表评论

匿名网友

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

确定