简体   繁体   English

更改 Material Ui 日期选择器的背景颜色

[英]Change background color of Material Ui datepicker

I want to change the background color of my material ui datepicker modal我想更改我的材料 ui datepicker modal 的背景颜色

import { createMuiTheme } from "@material-ui/core";从“@material-ui/core”导入{ createMuiTheme };

const materialTheme = createMuiTheme({
    overrides: {
        MuiPickersToolbar: {
            toolbar: {
                backgroundColor: 'red',
            },
        },
        MuiPickersDay: {
            day: {
                color: 'black',

            },
            daySelected: {
                backgroundColor: '#33abb6',
            },
            dayDisabled: {
                color: '#ccc',
            },
            current: {
                color: 'red',
            },
        },
        MuiPickersModal: {
            dialogAction: {
                color: '#33abb6',
            },
        },
    },
});


export default materialTheme

In the above code i was able to change colors of date and few others but not the total background color在上面的代码中,我能够更改日期的 colors 和其他一些代码,但不能更改总背景颜色

Are there any documentation from which i can get these class names or any other option是否有任何文档可以从中获取这些 class 名称或任何其他选项

Try in CSS:在 CSS 中尝试:

.MuiPaper-root {
  background-color: #eaea87;
}

MuiPickers is using Dialog Material Ui, so override all dialog component that be used in this pickers. MuiPickers 使用对话框材质 Ui,因此覆盖此选择器中使用的所有对话框组件。 I'm not sure with this solution below.我不确定下面的这个解决方案。 You can try this, hope it's worked.你可以试试这个,希望它有效。

   const materialTheme = createMuiTheme({
        overrides: {
            MuiPickersToolbar: {
                toolbar: {
                    backgroundColor: 'red',
                },
            },
            MuiPickersDay: {
                day: {
                    color: 'black',
    
                },
                daySelected: {
                    backgroundColor: '#33abb6',
                },
                dayDisabled: {
                    color: '#ccc',
                },
                current: {
                    color: 'red',
                },
            },
            MuiPickersModal: {
                dialogAction: {
                    color: '#33abb6', 
                    backgroundColor: 'YOUR HEX HERE',
                },
            },
        },
    });

I think the good way is send style in DialogProps我认为最好的方法是在 DialogProps 中发送样式

https://material-ui-pickers.dev/api/DateTimePicker (section modal wrapper) https://material-ui-pickers.dev/api/DateTimePicker (部分模态包装器)

so then you can override all dialog modal.那么你可以覆盖所有对话框模式。

In recent version of MUI (v5.3.1) I resolved this issue by adding sx={{ backgroundColor: 'white' }}<\/code> to TextField<\/code> in renderInput<\/code> prop as below:在最新版本的 MUI (v5.3.1) 中,我通过在renderInput<\/code>的TextField<\/code>中添加sx={{ backgroundColor: 'white' }}<\/code>解决了这个问题,如下所示:

<MobileDatePicker
  label="Date"
  value={date}
  onChange={(newValue) => {
  setDate(newValue);
  }}
  renderInput={(params) => (
    <TextField
      sx={{ backgroundColor: 'white' }}
      fullWidth
      {...params}
    />
  )}
/>

You can use createTheme to provide component overrides ( see docs ):您可以使用createTheme提供组件覆盖( 请参阅文档):

const theme = createTheme({
  components: {
    // Name of the component
    MuiInputBase: {
      styleOverrides: {
        // Name of the slot
        root: {
          // Some CSS
          backgroundColor: "white",
        },
      },
    },
  },
});

You can see the name of the component to use by inspect element and looking at the class names, and you can find the slots in the component definition, eg this is the slots for the MuiInput component .您可以通过检查元素查看要使用的组件的名称并查看 class 名称,您可以在组件定义中找到插槽, 例如这是MuiInput组件的插槽

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM