2

代码沙箱在这里:

https://codesandbox.io/s/ypx4qpjvpx

相关位:

const styles = theme => ({
  root: {
    flexGrow: 1,
    backgroundColor: theme.palette.background.paper
  },

  label: {
    fontWeight: "normal"
  },

  selected: {
    fontWeight: "bold"
  }
});



  <Tabs value={value} onChange={this.handleChange}>
    <Tab
      label="Item One"
      classes={{
        label: classes.label,
        selected: classes.selected
      }}
    />
    <Tab
      label="Item Two"
      classes={{
        label: classes.label,
        selected: classes.selected
      }}
    />
    <Tab
      label="Item Three"
      href="#basic-tabs"
      classes={{
        label: classes.label,
        selected: classes.selected
      }}
    />
  </Tabs>

我在这里要做的是我需要覆盖默认的字体粗细样式,但是在选择时,我希望它是粗体的。

问题是 - 这些具有相同级别的特异性,并且标签在选择后出现,因此它会覆盖它。

在不使用 !important 的情况下,我将如何使选择更具体/实现我想要的。

4

1 回答 1

1

我认为最简单的方法是使用root类而不是label(对于Tab组件)。

演示:https ://codesandbox.io/s/q3pmn9o7m4
(我添加了颜色以使更改更容易看到。)

<Tab
    label="Item One"
    classes={{
        root: classes.tabRoot,
        selected: classes.selected,
    }}
/>

const styles = theme => ({
    root: {
        flexGrow: 1,
        backgroundColor: theme.palette.background.paper,
    },

    tabRoot: {
        fontWeight: "normal",
        color: "#fff",
    },

    selected: {
        fontWeight: "bold",
        color: "#0ff",
    }
});

另一种方式:https ://codesandbox.io/s/8op0kwxpj

const styles = theme => ({
  root: {
    flexGrow: 1,
    backgroundColor: theme.palette.background.paper,
  },

  tabRoot: {
    fontWeight: "normal",
    color: "#fff",
    '&$selected': {
      fontWeight: "bold",
      color: "#0ff",
    },
  },

  selected: {},
});
于 2018-10-01T13:21:44.567 回答