最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change background color of Material Ui datepicker - Stack Overflow

programmeradmin4浏览0评论

I want to change the background color of my material ui datepicker modal

import { createMuiTheme } from "@material-ui/core";

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

Are there any documentation from which i can get these class names or any other option

I want to change the background color of my material ui datepicker modal

import { createMuiTheme } from "@material-ui/core";

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

Are there any documentation from which i can get these class names or any other option

Share Improve this question asked Nov 12, 2020 at 9:00 NagarajNagaraj 651 gold badge2 silver badges7 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

Try in CSS:

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

In recent version of MUI (v5.3.1) I resolved this issue by adding sx={{ backgroundColor: 'white' }} to TextField in renderInput prop as below:

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

You can use createTheme to provide ponent overrides (see docs):

const theme = createTheme({
  ponents: {
    // Name of the ponent
    MuiInputBase: {
      styleOverrides: {
        // Name of the slot
        root: {
          // Some CSS
          backgroundColor: "white",
          // add variant styles like so
          "&.Mui-disabled": {
            "backgroundColor": "#cccccc"
          }
        },
      },
    },
  },
});

You can see the name of the ponent to use by inspect element and looking at the class names, and you can find the slots in the ponent definition, e.g. this is the slots for the MuiInput ponent.

Also see this source on bining class names to target disabled, hover, active etc.

MuiPickers is using Dialog Material Ui, so override all dialog ponent that be used in this pickers. 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

https://material-ui-pickers.dev/api/DateTimePicker (section modal wrapper)

so then you can override all dialog modal.

发布评论

评论列表(0)

  1. 暂无评论