英文:
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 -->
<TextField type="email" name="email" id="email" label="Email" variant="outlined" color="secondary" required />
<DatePicker label="Basic date picker" />
<!-- 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 -->
<TextField type="email" name="email" id="email" label="Email" variant="outlined" color="secondary" required />
<DatePicker label="Basic date picker" />
<!-- 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 DatePicker的slotProps
属性。
<!-- 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 -->
<DatePicker
label="Joining date"
slotProps={{
textField: {
name: "joiningDate",
id: "joiningDate",
color: "secondary",
required: true
}
}}
/>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论