简体   繁体   English

在 material-ui 中向 DatePicker 添加自定义按钮

[英]Adding a custom button to DatePicker in material-ui

I'm using a Datepicker for a project.我正在为一个项目使用 Datepicker。 I want to add a custom information button in the top right corner of the calendar, example shown in the image below我想在日历的右上角添加一个自定义信息按钮,示例如下图所示

日历和我想要的自定义按钮

Didn't find anything useful in the docs , maybe you could help, if this is possible to do and how.文档中没有找到任何有用的东西,也许你可以提供帮助,如果可以这样做以及如何做。

--- EDIT --- - - 编辑 - -

I added as suggested a button which I want to put on top of the calendar, the code of the button:我按照建议添加了一个我想放在日历顶部的按钮,该按钮的代码:

const useStyles = makeStyles({
  paper: {
    outline: 'none',
    padding: '10px',
    position: 'absolute',
    left: '50%',
    top: '50%',
    WebkitTransform: 'translate(-50%, -50%)',
    transform: ' translate(-50%, -50%)',
    width: 'auto',
    backgroundColor: 'white',
    textAlign: 'center',
  },
  infoButtonShown: {
    display: 'inline-flex',
    position: 'absolute',
    left: '50%',
    top: '50%',
    WebkitTransform: 'translate(-50%, -50%)',
    transform: ' translate(-50%, -50%)',
    color: 'black',
  },
  infoButtonHidden: {
    display: 'none',
    position: 'absolute',
    left: '50%',
    top: '50%',
    WebkitTransform: 'translate(-50%, -50%)',
    transform: ' translate(-50%, -50%)',
    color: 'black',
  },
});

<IconButton
    className={ isInfoOpen ? classes.infoButtonShown : 
                             classes.infoButtonHidden
              }>
    <InfoIcon fontSize="small" />
</IconButton>

But it doesn't show on top of the calendar.但它不会显示在日历的顶部。 I tried to play with various values of the z-index for the button, but with no success.我尝试使用按钮的各种 z-index 值,但没有成功。 Any ideas how can I put it on top of the calendar?有什么想法可以把它放在日历上吗?

--- EDIT 2 --- --- 编辑 2 ---

As Ibrahim supposed, I'm trying to render a custom toolbar for this matter using ToolbarComponent:正如易卜拉欣所说,我正在尝试使用 ToolbarComponent 为这个问题呈现一个自定义工具栏:

import { DatePickerToolbar } from '@material-ui/pickers/DatePicker/DatePickerToolbar';

ToolbarComponent={(props) => (
    <div>
        <DatePickerToolbar {...props} />
        <IconButton
            className={
                isInfoOpen
                    ? classes.infoButtonShown
                    : classes.infoButtonHidden
            }
        >
            <InfoIcon fontSize="small" />
        </IconButton>
    </div>
)}

But it throws an error:但它会引发错误:

× TypeError: Cannot read property 'trackMetric' of null × TypeError: 无法读取 null 的属性 'trackMetric'

I'm super lost, also there is no documentation on this DatePickerToolbar, I needed to specify the whole path for it to make it stop complaining that it doesn't finds it in the @material-ui/pickers package.我非常迷茫,这个 DatePickerToolbar 上也没有文档,我需要为其指定整个路径,以使其停止抱怨它在 @material-ui/pickers package 中找不到它。 Is there any valid toolbar I can use for this matter?我可以使用任何有效的工具栏来解决这个问题吗?

You can replace the toolbar by a custom toolbar, just replace the default component by the new one with your new element您可以用自定义工具栏替换工具栏,只需用新元素替换默认组件即可

ToolbarComponent工具栏组件

see the doc文档

import { ToolbarComponentDefault } from '@material-ui/pickers'



 <DatePicker 
  ToolbarComponent={(props) => (<div><ToolbarComponentDefault {...props /><OtherComponent /></div>))
/>

This was a little bit tricky but I eventually ended up doing something like this:这有点棘手,但我最终做了这样的事情:

Using the ToolBarComponent attribute使用 ToolBarComponent 属性

ToolbarComponent={(props) => (
                <div>
                  <CustomToolbar {...props} />
                  <IconButton
                    className={
                      isInfoOpen
                        ? classes.infoButtonShown
                        : classes.infoButtonHidden
                    }
                    onClick={() => setInfoModalOpen(true)}
                  >
                    <InfoIcon fontSize="small" />
                  </IconButton>
                </div>
              )}

Also, needed to make a component CustomToolBar to override the default toolbar.此外,需要制作一个组件 CustomToolBar 来覆盖默认工具栏。 Means I need to render the date by my self ( This is for hebrew version ):意味着我需要自己呈现日期(这是希伯来语版本):

const CustomToolbar = function(props: any) {


  const { date, isLandscape, openView, setOpenView, title } = props;

  const handleChangeViewClick = (view: any) => (e: any) => {
    setOpenView(view);
  };

  const classes = useStyles();
  const daysToHebrew = [
    "יום א'",
    "יום ב'",
    "יום ג'",
    "יום ד'",
    "יום ה'",
    "יום ו'",
    'שבת',
  ];
  const monthToHebrew = [
    "ינו'",
    "פבר'",
    'מרץ',
    "אפר'",
    'מאי',
    'יוני',
    'יולי',
    "אוג'",
    "ספט'",
    "אוק'",
    "נוב'",
    "דצמ'",
  ];

  return (
    <PickerToolbar
      className={classes.toolbar}
      title={title}
      isLandscape={isLandscape}
    >
      <ToolbarButton
        className={classes.h6ToolBar}
        onClick={handleChangeViewClick('year')}
        variant="subtitle1"
        label={date.getFullYear().toString()}
        selected={openView === 'year'}
      />
      <ToolbarButton
        onClick={handleChangeViewClick('date')}
        variant="h4"
        selected={openView === 'date'}
        label={`${daysToHebrew[date.getDay()]}, ${
          monthToHebrew[date.getMonth()]
        } ${date.getDate()}`}
      />
    </PickerToolbar>
  );
};

Hope it helps for people who also seek similar result.希望它对也寻求类似结果的人有所帮助。

Add it next to DatePicker component, and position it over the datepicker with CSS.将其添加到 DatePicker 组件旁边,并将 position 与 CSS 一起添加到 datepicker 上。

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

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