简体   繁体   English

更改 React Material-UI DataGrid (table) 默认字体大小

[英]Change React Material-UI DataGrid (table) Default Font Size

Using Material-UI DataGrid, the default font in table is too small to see text.使用 Material-UI DataGrid,表格中的默认字体太小,看不到文字。

My code is literally a copy/paste of their demo on the page:我的代码实际上是他们在页面上的演示的复制/粘贴:

https://material-ui.com/components/data-grid/ https://material-ui.com/components/data-grid/

My fonts are very small in the table.我的字体在表格中非常小。 Rest of page not using Mui (menu etc.) are fine.不使用 Mui(菜单等)的页面的其余部分很好。 In Firefox dev tools Inspector I see在 Firefox 开发工具检查器中我看到

Inherited from div: .MuiDataGrid-root { font-size: 0.875rem; }继承自 div: .MuiDataGrid-root { font-size: 0.875rem; } .MuiDataGrid-root { font-size: 0.875rem; }

Changing that in the browser tool to something like 10px, or disabling it completely, confirms thats the value needing change.在浏览器工具中将其更改为 10px 之类的值,或完全禁用它,确认这就是需要更改的值。 It's being automatically added at end (inline) when compiled so it overwrites my previous style.它在编译时自动添加到末尾(内联),因此它会覆盖我以前的样式。 The silly thing is, I can't find where this is being added.愚蠢的是,我找不到要添加的位置。

I've looked at several sites using this same table as examples and none have this issue.我已经使用同一张表作为示例查看了几个站点,但没有一个站点存在此问题。 Can someone enlighten me where to look, how to fix the table having incorrect default font size?有人可以启发我在哪里看,如何修复默认字体大小不正确的表格?

I have these relevant bits:我有这些相关的位:

package.json:包.json:

"@material-ui/core": "^4.11.0",
"@material-ui/data-grid": "^4.0.0-alpha.6",
"fontsource-roboto": "^3.0.3",

Index.html索引.html

<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />

Abbreviated .jsx code:缩写 .jsx 代码:

import React from 'react';
import { DataGrid } from '@material-ui/data-grid';

const DemoTableMui = () => {
...

 const columns = [{ field: 'id', headerName: 'ID', width: 70 } ]
 const rows = [{ id: 1, lastName: 'Smith', firstName: 'Jon', age: 35 } ]


  return (
    <div style={{ height: 600, width: '100%' }}>
      <DataGrid rows={rows} columns={columns} pageSize={5} checkboxSelection />
    </div>
  );
};

export default DemoTableMui

Since the dev tool indicated it is inherited from div I did stuff like:由于开发工具表明它是从 div 继承的,所以我做了以下事情:

  return (
    <div style={{ height: 600, width: '100%', fontSize:25 }}>
    ...

But nothing had effect.但没有任何效果。 I'm sure the div it is referring to is buried down in the <DataGrid/> component...我确定它所指的 div 被埋在<DataGrid/>组件中......

Based on OP feedback, putting an answer here.根据 OP 反馈,在此处提供答案。

Isolating the CSS problem.隔离 CSS 问题。

  • if you are using a theme, start with the file that has the DIV root.如果您使用的是主题,请从具有 DIV 根的文件开始。 That is where styles are included.这就是包括样式的地方。
  • start looking the specific CSS class that is affecting, and instead of changing the value - which won't work as you have already seen.开始查看影响的特定 CSS 类,而不是更改值 - 正如您已经看到的那样,这将不起作用。
  • Make a copy of the CSS class, give it a different name, and set the desired value and then use this new class.复制 CSS 类,给它一个不同的名称,并设置所需的值,然后使用这个新类。

Alternatively (based on the OP update in the comments)或者(基于评论中的 OP 更新)

  • Consider abandoning any usage of themes just use the standard themes such as bootstrap (or react strap, if you are on react js), which wont have these problems.考虑放弃任何主题的使用,只需使用标准主题,例如引导程序(或反应带,如果您使用的是 react js),则不会有这些问题。
  • Themes tend to have a mix of native components and also standard components.主题往往混合了原生组件和标准组件。 In most cases, they will have two folders.在大多数情况下,它们将有两个文件夹。 one for bootstrap css files and another for theme css files.一个用于引导 css 文件,另一个用于主题 css 文件。
  • Unless you are already experienced with the theme, making changes can become tricky.除非您已经熟悉该主题,否则进行更改可能会变得棘手。 Use the standard bootstrap components, which use standardized names and hence, no complications.使用标准引导程序组件,这些组件使用标准化名称,因此没有复杂性。

So the best way to use material-UI IMO is to customize the theme at the root level, and then all of the components should inherit the styling in a uniform kind of way.所以使用material-UI IMO 的最佳方式是在根级别自定义主题,然后所有组件都应该以统一的方式继承样式。 This is what my App.tsx looks like这就是我的 App.tsx 的样子

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

const theme = createMuiTheme({
  palette: {
    type: 'dark'
  },
  typography: {
    fontSize: 12
  },
  // overrides: {
  //   MuiTypography: {
  //     colorInherit:{
  //       color: "#fff"
  //     }
  //   }
});

function App() {
  return (
    <MuiThemeProvider theme={theme}>
      <LeadPage/>
    </MuiThemeProvider>
  );
}

export default App;

Setting the typography will trickle down to the class .MuiDataGrid-root .设置排版将渗透到.MuiDataGrid-root类。 I left the palette up there which changes the font color of the grid, and the commented out code is a an example of overriding the class at the theme level, in case you need to tweak that grid further.我把调色板留在了那里,它改变了网格的字体颜色,注释掉的代码是在主题级别覆盖类的一个例子,以防您需要进一步调整该网格。 My example would be if you saw .MuiTypography-colorInherit in the debugger.我的例子是,如果您在调试器中看到.MuiTypography-colorInherit

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

... ...

import { createTheme } from '@material-ui/core';
export const muiTheme = createTheme({
  typography: {
    fontFamily: 'Poppins Regular',
  },
});

... ...

<MuiThemeProvider theme={muiTheme}>...</MuiThemeProvider>

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

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