给DatePicker设置次要主题颜色。

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

How to give secondary theme color to DatePicker

问题

I am new to material UI and I was playing around with components but I got stuck in an issue.

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

<!-- language: lang-html -->

&lt;TextField type=&quot;email&quot; name=&quot;email&quot; id=&quot;email&quot; label=&quot;Email&quot; variant=&quot;outlined&quot; color=&quot;secondary&quot; required /&gt;

&lt;DatePicker label=&quot;Basic date picker&quot; /&gt;

<!-- end snippet -->

Just like how I gave a color prop and variant prop to the TextField component, I want to give DatePicker the same props but the typescript gives me an error saying color doesn't exist on this type. I opened the documentation. I found a slots prop but it didn't help either. I can apply color via CSS but I wanna do things the right way. Using CSS will be redundant. Any idea how I cant solve this issue?

英文:

I am new to material UI and I was playing around with components but I got stuck in an issue.

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

<!-- language: lang-html -->

&lt;TextField type=&quot;email&quot; name=&quot;email&quot; id=&quot;email&quot; label=&quot;Email&quot; variant=&quot;outlined&quot; color=&quot;secondary&quot; required /&gt;

&lt;DatePicker label=&quot;Basic date picker&quot; /&gt;

<!-- end snippet -->

Just like how I gave a color prop and variant prop to the TextField component, I want to give DatePicker the same props but the typescript gives me an error saying color doesn't exist on this type. I opened the documentation. I found a slots prop but it didn't help either. I can apply color via CSS but I wanna do things the right way. Using CSS will be redundant. Any idea how I cant solve this issue?

答案1

得分: 0

我发现了DatePicker中的slotProps属性,这解决了我的问题。我们可以为DatePicker组件中使用的任何元素提供属性。请查看MUI DatePickerslotProps属性。

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

<!-- language: lang-html -->
    <DatePicker
      label="入职日期"
      slotProps={{
        textField: {
          name: "joiningDate",
          id: "joiningDate",
          color: "secondary",
          required: true
        }
      }}
    />
<!-- end snippet -->
英文:

I found out about the slotProps prop in the DatePicker and this fixed my problem. We can provide props to any element used in the DatePicker component. Check out the slotProps prop of MUI DatePicker

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

<!-- language: lang-html -->

&lt;DatePicker
  label=&quot;Joining date&quot;
  slotProps={{
    textField: {
      name: &quot;joiningDate&quot;,
      id: &quot;joiningDate&quot;,
      color: &quot;secondary&quot;,
      required: true
    }
  }}
/&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月18日 03:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275761.html
匿名

发表评论

匿名网友

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

确定