简体   繁体   English

如何自定义反应材料ui下一个组件

[英]how to customize react material ui next component

I am using Material UI next branch i want to use the table component and i want to change the style of the TableHead Component. 我正在使用Material UI 下一个分支,我想使用表格组件,并且想要更改TableHead组件的样式。

My idea was to wrap the TableHead with MyTableHead and add my own style to override the current style. 我的想法是用MyTableHead包裹TableHead并添加我自己的样式以覆盖当前样式。

my code(based on this Composition ): 我的代码(基于此Composition ):

import React from 'react';
import injectSheet from 'react-jss'
import {
    TableHead,
} from 'material-ui/Table';

const styles = {
   th: {        
        fontSize:'20px',        
    },    
}

const _MyTableHead = (props) => {
    return (
        <TableHead className={props.classes.th} {...props}>
            {props.children}
        </TableHead>
    );
};
_MyTableHead.muiName = 'TableHead'    
const MyTableHead = injectSheet(styles)(_MyTableHead);


export {MyTableHead}

This does not work: 这不起作用:
1. my style is overrided by the original style 1.我的风格被原始风格覆盖
2. i get an error in the browser js console: 2.我在浏览器js控制台中收到错误:

Warning: Unknown props sheet , classes on tag. 警告:未知的道具sheet ,标签上的classes Remove these props from the element. 从元素中删除这些道具。 For details, see https://facebook.github.io/react/warnings/unknown-prop.html in thead (created by TableHead) in TableHead (at table.js:15) in _MyTableHead (created by Jss(_MyTableHead)) 有关详细信息,请参阅https://facebook.github.io/react/warnings/unknown-prop.html在TableHead THEAD(由TableHead创建)(在table.js:15)_MyTableHead(由JSS创建(_MyTableHead))

I think i am not doing it right, any idea ? 我想我做得不好,有什么想法吗?

The way to customize components in the next branch is not documented yet. 尚未记录在下一个分支中自定义组件的方法。
This is how it can be done: 这是可以做到的:

import React from 'react';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import {createMuiTheme} from 'material-ui/styles/theme'
import Button from 'material-ui/Button';

const muiTheme = createMuiTheme({
  overrides:{
    MuiButton:{
      root:{
        fontSize: 20,
      }
    }
  }  
});

class Main extends React.Component {
  render() {
    // MuiThemeProvider takes the theme as a property and passed it down the hierarchy
    // using React's context feature.
    return (
      <MuiThemeProvider muiTheme={muiTheme}>
        <Button raised>Default</Button>
      </MuiThemeProvider>
    );
  }
}

export default Main;

Example Class from MUINext 来自MUINext的示例类

Material UI regenerates new classes upon render, so you will never be able to override any one class with a preset class. 材质UI在渲染时会重新生成新类,因此您将永远无法使用预设类覆盖任何一个类。

Their class name will look something like MuiRoot-tableHead-23 . 他们的类名看起来像MuiRoot-tableHead-23 The last number is random. 最后一个数字是随机的。 Find the specific class that is overriding your class, and then use 找到覆盖您的类的特定类,然后使用

[class*=MuiRoot-tableHead]{
your css settings
}

If you have a theme file already setup, I'd suggest going with what yossi said. 如果您已经设置了主题文件,建议您使用yossi所说的内容。 Otherwise, you can manually override it using this method. 否则,您可以使用此方法手动覆盖它。

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

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