简体   繁体   English

如何从 Material-UI 自定义 DateTimePicker

[英]How to customise DateTimePicker from Material-UI

I am trying to customise the DateTimePicker from Material-UI .我正在尝试从Material-UI自定义DateTimePicker Here is its documentation: https://material-ui-pickers.dev/api/DateTimePicker这是它的文档: https : //material-ui-pickers.dev/api/DateTimePicker

There is no section for the styling.没有关于样式的部分。 I want to change the main color for all the coloured components.我想更改所有彩色组件的主要颜色。 What I've tried so far is using the general theme documentation and try to change the style of the theme:到目前为止我尝试过的是使用通用theme文档并尝试更改主题的样式:

const theme = createMuiTheme({
  status: {
    danger: '#FF72B1',
  },
  dateTimePicker: {
    headerColor: '#FF72B1',
    selectColor: '#FF72B1',
  },
  datePicker: {
    selectColor: '#FF72B1',
    headerColor: '#FF72B1'
  }
});

function App() {
  return (
    <ThemeProvider theme={theme}>
      <Routes />
    </ThemeProvider>
  )
}

As far as I understood from the theme documentation, the only thing that I've done so far is defining variables with styles, but they are not going to be applied.据我从theme文档中了解到,到目前为止我所做的唯一一件事就是定义带有样式的变量,但它们不会被应用。 I have to specify them in the exact component, and here comes the tricky part.我必须在确切的组件中指定它们,这是棘手的部分。

In my Material-UI DateTimePicker :在我的Material-UI DateTimePicker

function MaterialDateTimePicker() {
  const classes = useStyles()
  return (
    <Fragment>
      <DateTimePicker
        label={label}
        inputVariant="outlined"
        value={value}
        onChange={onChange}
        classes={{
          root: classes.root,
          checked: classes.checked,
        }}
      />
    </Fragment>
  );
}

I have tried to applied the styling:我尝试应用样式:

const useStyles = makeStyles(theme => ({
  root: {
    color: '#FF72B1',
    backgroundColor: 'orange',
    '& > .MuiPickerDTToolbar-toolbar.MuiToolbar-root': {
      backgroundColor: 'green'
    }
  },
  checked: {},
}));

This is how I've been trying to style components with this library, based on research, reading the docu, and some SO answers:基于研究、阅读文档和一些 SO 答案,这就是我一直在尝试使用此库设计组件样式的方式:

How to change material UI select border and label 如何更改材质 UI 选择边框和标签

So basically you have to go to the documentation, try to find the .XXX class that matches the component that you want to customise, and if documentation is missing, you have to go to the DOM, and start looking for this class.所以基本上你必须去文档,尝试找到与你要定制的组件匹配的 .XXX 类,如果缺少文档,你必须去 DOM,并开始寻找这个类。

I did that, but I have a couple of questions:我这样做了,但我有几个问题:

1) In my particular case, I have the problem that on my DateTimePicker I apply the root classes, which are on the input component level. 1) 在我的特殊情况下,我遇到的问题是在我的DateTimePicker应用了input组件级别的root类。 The calendar that pops up, is not a children of this component, it's open by javascript, therefore I don't know how to access it.弹出的日历不是这个组件的子组件,它是由javascript打开的,因此我不知道如何访问它。

This syntax does not work any longer:此语法不再起作用:

root: {
    color: '#FF72B1',
    backgroundColor: 'orange',
    '& > .MuiPickerDTToolbar-toolbar.MuiToolbar-root': {
      backgroundColor: 'green'
    }
  },

Because root is the input, not the calendar that pop ups.因为root是输入,而不是弹出的日历。

2) Is this really the way to go with this library? 2)这真的是这个图书馆的方式吗? Because all the answers on SO and complains go on this direction.因为所有关于 SO 和抱怨的答案都朝着这个方向发展。 Does anybody know another strategy?有人知道另一种策略吗?

3) In @material-ui/pickers node_modules folder I couldn't find the css file. 3) 在@material-ui/pickers node_modules文件夹中,我找不到 css 文件。 I would like to pick it and customise there, like it's possible for react-dates library etc. Is that possible?我想选择它并在那里进行自定义,就像react-dates库等一样。这可能吗? Where are the css stylings? css样式在哪里?

I've prepared a sandbox with what I've tried:我准备了一个沙箱,其中包含我尝试过的内容:

https://codesandbox.io/s/inspiring-napier-zh4os https://codesandbox.io/s/inspiring-napier-zh4os

(Unfortunately the utils library is installed but not working, locally in my computer the picker works fine, I just can't style it) (不幸的是,utils 库已安装但无法正常工作,在我的计算机本地,选择器工作正常,我只是无法对其进行样式设置)

I'm working with this right now, wat i did to partially override the styles is to wrap in a ThemeProvider (you can pass your theme trow your component)我现在正在处理这个,我所做的部分覆盖样式是包装在 ThemeProvider 中(你可以通过你的组件传递你的主题)

 <MuiPickersUtilsProvider locale={deLocale} utils={DateFnsUtils}>
  <Grid container justify="space-around">
    <ThemeProvider theme={defaultMaterialTheme}>
      <KeyboardDatePicker
        ...
      />
    </ThemeProvider>
  </Grid>
</MuiPickersUtilsProvider>

And your theme could be something, like this你的主题可能是这样的

import { createMuiTheme } from '@material-ui/core'

const defaultMaterialTheme = createMuiTheme({
  overrides: {
    MuiPickersCalendarHeader: {
      switchHeader: {
        color: '#6A148E',
        textTransform: 'uppercase',
      },
      dayLabel: {
        textTransform: 'uppercase',
      },
    },
    MuiPickersDay: {
      day: {
        color: '#707070',
      },
      daySelected: {
        backgroundColor: '#6A148E',
        '&:hover': {
          backgroundColor: '#6A148E',
        },
      },
      current: {
        color: '#6A148E',
      },
    },
    MuiSvgIcon: {
      root: {
        fill: '#6A148E',
      },
    },
    MuiOutlinedInput: {
      root: {
        '&:hover': {
          border: '10px solid red !important', // i'm struggling with this :/
        },
      },
    },
   
  },
})

export default defaultMaterialTheme

Hope it's help希望有帮助

I think you just need to cancel out the webkit appearance.我认为您只需要取消 webkit 外观即可。 There are a couple of ways to cancel out specific webkit styles (so you can add your own).有几种方法可以取消特定的 webkit 样式(因此您可以添加自己的样式)。 Try the following:请尝试以下操作:

-webkit-appearance: none;

ReactJS inline styles: webkitAppearance: "none"; ReactJS 内联样式: webkitAppearance: "none";

Also check out other -webkit-[] functions... There are functions for more specific elements such as borders, colours, etc...还可以查看其他 -webkit-[] 函数......还有一些用于更具体元素的函数,例如边框、颜色等......

Hope this helps :)希望这可以帮助 :)

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

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