【问题标题】:Material UI Tab label font-size is really smallMaterial UI Tab label font-size 真的很小
【发布时间】:2018-11-30 15:11:47
【问题描述】:

我开始使用material ui tabs,但标签标签的字体大小有问题,因为它们真的很小...

这是我的代码:

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Tabs from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';
import Typography from '@material-ui/core/Typography';

function TabContainer(props) {
  return (
    <Typography component="div" style={{ padding: 8 * 3 }}>
      {props.children}
    </Typography>
  );
}

TabContainer.propTypes = {
  children: PropTypes.node.isRequired,
};

const styles = theme => ({
  root: {
    flexGrow: 1,
    backgroundColor: theme.palette.background.paper,
  },
  tabRoot: {
    backgroundColor: theme.palette.background.paper,
    flexGrow: 1,
    color: 'black',
    fontWeight: 'bold'
  }
});

class SimpleTabs extends React.Component {
  state = {
    value: 0,
  };

  handleChange = (event, value) => {
    this.setState({ value });
  };

  render() {
    const { classes } = this.props;
    const { value } = this.state;

    return (
      <div className={classes.root}>
        <AppBar position="static">
          <Tabs className={classes.tabRoot} value={value} onChange={this.handleChange}>
            <Tab label="Item One" />
            <Tab label="Item Two" />
          </Tabs>
        </AppBar>
        {value === 0 && <TabContainer>Item One</TabContainer>}
        {value === 1 && <TabContainer>Item Two</TabContainer>}
        {value === 2 && <TabContainer>Item Three</TabContainer>}
      </div>
    );
  }
}

SimpleTabs.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(SimpleTabs);

有没有办法增加这些标签的大小?

【问题讨论】:

  • 您可以随时使用style 属性覆盖它。另外在这种情况下使用flexGrow 的目的是什么?
  • @AseemUpadhyay 我尝试在TabTabs 上添加 style={{fontSize: '14px'}} 但它什么也没做。如果我不添加flexGrow,那么我会在选项卡下方获得额外的边框颜色

标签: reactjs material-ui


【解决方案1】:

Material-ui Tab组件标签prop类型为节点。所以如果你想添加样式,你需要将纯文本放在 div 或 span 中,或者另一个 html 组件并添加 className。

<Tab label={<span className={classes.tabLabel}>Label</span>}/>

【讨论】:

  • 这里的classes.tabLabel是什么
【解决方案2】:

1. 您可以设置选项卡的样式,并使用自己的包装器组件,例如喜欢TabBigger:

import React from 'react';
import { withStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Tabs from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';
import Typography from '@material-ui/core/Typography';

const styles = theme => ({
    root: { backgroundColor: '#0af' },
    tabRoot: { backgroundColor: '#0a6' },
    label: {
        backgroundColor: '#aa0',
        fontSize: '22px'
    },
});

function TabContainer(props) {
    return (
        <Typography component="div" style={{ padding: 8 * 3 }}>
            {props.children}
        </Typography>
    );
}

const TabBigger = withStyles(styles)(( props )=>{
    return (
        <Tab className={props.classes.label} {...props}/>
    );
});

class SimpleTabs extends React.Component {
    state = {
        value: 0,
    };

    handleChange = (event, value) => {
        this.setState({ value });
    };

    render() {
        const { classes } = this.props;
        const { value } = this.state;

        return (
            <div className={classes.root}>
                <AppBar position="static">
                    <Tabs className={classes.tabRoot} value={value} onChange={this.handleChange}>
                        <TabBigger label="Item One" />
                        <TabBigger label="Item Two" />
                    </Tabs>
                </AppBar>
                {value === 0 && <TabContainer>Item One</TabContainer>}
                {value === 1 && <TabContainer>Item Two</TabContainer>}
                {value === 2 && <TabContainer>Item Three</TabContainer>}
            </div>
        );
    }
}

export default withStyles(styles)(SimpleTabs);

2. 或者,您可能希望使用 MUI 规则 @987654324 设置 tab-label(而不是整个 Tab 组件)的样式@:

<Tab classes={{ root: { props.classes.tab }, labelContainer: props.classes.label }} />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2020-04-01
    • 2012-05-20
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    相关资源
    最近更新 更多