简体   繁体   English

在 DataGrid Toolbar 的弹出组件 Material-UI 中添加自定义样式

[英]Add custom style inside DataGrid Toolbar's popup component Material-UI

I'm creating a custom Data Grid Toolbar component by modifying existing Grid Toolbar components from Material-UI .我正在通过修改Material-UI 中的现有Grid Toolbar组件来创建自定义Data Grid Toolbar Grid Toolbar组件。

Here is the official example for the Grid Toolbar components:Grid Toolbar组件的官方示例:

If we click one of the Grid Toolbar components, it will show a popup/popper as seen in the screenshot below.如果我们单击Grid Toolbar组件之一,它将显示一个弹出窗口/弹出窗口,如下面的屏幕截图所示。

在此处输入图片说明

What I want to do is to change all font sizes inside every popup/popper from each Grid Toolbar component.我想要做的是更改每个Grid Toolbar组件中每个弹出窗口/弹出窗口中的所有字体大小。

I try to change one text for example.例如,我尝试更改一个文本。 As we can see from the screenshot below, if we inspect the text then change the font-size and color properties directly, it would change.正如我们从下面的屏幕截图中看到的,如果我们检查文本然后直接更改font-sizecolor属性,它会发生变化。

在此处输入图片说明

But if I grab and copy the selector (in this case is .MuiTypography-body1 ), then I paste it in my code, there nothing changes (the font-size and color properties don't change).但是如果我抓取并复制选择器(在这种情况下是.MuiTypography-body1 ),然后我将它粘贴到我的代码中,没有任何变化( font-sizecolor属性不会改变)。

Here is the simple code:这是简单的代码:

const CustomGridToolbarColumns = withStyles((theme) => ({
  root: {
    color: "dodgerblue",
    "& .MuiTypography-body1": {
      fontSize: 20,
      color: "red"
    }
  }
}))(GridToolbarColumnsButton);

I also want to change all font-size and color properties inside each popup/popper of each Grid Toolbar component.我还想更改每个Grid Toolbar组件的每个弹出窗口/弹出窗口内的所有font-sizecolor属性。 I inspect the popup/popper then change the font-size and color properties but still don't work as seen in the screenshot below.我检查了弹出窗口/弹出窗口,然后更改了font-sizecolor属性,但仍然无法正常工作,如下面的屏幕截图所示。

在此处输入图片说明

Here are the dependencies (in package.json file):以下是依赖项(在 package.json 文件中):

"@material-ui/core": "^4.12.3",
"@material-ui/styles": "^4.11.4",
"@mui/x-data-grid-pro": "^4.0.0",

Here is the full code: https://codesandbox.io/s/data-grid-material-ui-custom-toolbar-kd9gj这是完整的代码: https : //codesandbox.io/s/data-grid-material-ui-custom-toolbar-kd9gj

So my questions are:所以我的问题是:

  1. how can we change some properties inside the popup/popper of every Grid Toolbar component?我们如何更改每个Grid Toolbar组件的 popup/popper 内的某些属性?
  2. how can we change all properties inside the popup/popper of every Grid Toolbar component?我们如何更改每个Grid Toolbar组件的 popup/popper 内的所有属性?

在此处输入图片说明

You can see from inspecting the element that the component you need to apply the style to is called GridPanel .通过检查元素,您可以看到需要应用样式的组件称为GridPanel This is a wrapper component of the filters and columns panel.这是过滤器和列面板的包装组件。 See all the component slots here .在此处查看所有组件插槽。

import { DataGrid, GridToolbar, GridPanel } from "@mui/x-data-grid";

const MyGridPanel = withStyles((theme) => ({
  root: {
    "& .MuiTypography-root": {
      color: "dodgerblue",
      fontSize: 20
    }
  }
}))(GridPanel);
<DataGrid
  {...data}
  components={{
    Toolbar: GridToolbar,
    Panel: MyGridPanel
  }}
/>

In order to change the styles of the other 2 GridMenu s (density/export), you need to target the MuiDataGrid-gridMenuList className .为了更改其他 2 个GridMenu s(密度/导出)的样式,您需要以MuiDataGrid-gridMenuList className为目标。 Currently I see there is no other way around that because DataGrid does not allow you to customize the GridMenu component:目前我认为没有其他方法可以解决这个问题,因为DataGrid不允许您自定义GridMenu组件:

<GlobalStyles
  styles={{
    ".MuiDataGrid-gridMenuList": {
      backgroundColor: "pink",

      "& .MuiMenuItem-root": {
        fontSize: 26
      }
    }
  }}
/>

Live Demo现场演示

代码沙盒演示

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

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