【问题标题】:Styling reacts material-ui tabs样式反应材料-ui选项卡
【发布时间】:2015-08-05 17:33:45
【问题描述】:

我刚开始使用 reacts material-ui,我想自定义一些样式。例如更改标签的背景颜色。

尝试使用 inlineStyle

喜欢

<Tabs style={this.getStyles().tabs} > </Tabs>

    getStyles(){
        return {

          tabs: {
            backgroundColor: Colors.teal200
          },

          headline: {
            fontSize: '24px',
            lineHeight: '32px',
            paddingTop: '16px',
            marginBottom: '12px',
            letterSpacing: '0',
            fontWeight: Typography.fontWeightNormal,
            color: Typography.textDarkBlack,

          }
        }
    }

更改选项卡内容区域,但不更改标题。

here我们有一些颜色属性我们怎么用呢?在这种情况下,文档没有给出示例。

我该怎么做?

【问题讨论】:

标签: css reactjs material-ui


【解决方案1】:

我这样做的方式是覆盖&lt;Tab&gt; 样式(如果您有受控的选项卡)

render: function() {

  var styles = {
    default_tab:{
      color: Colors.grey500,
      backgroundColor: Colors.grey50,
      fontWeight: 400,
    },
    active_tab:{
      color: Colors.deepOrange700,
    }
  }

  styles.tab = []
  styles.tab[0] = styles.default_tab;
  styles.tab[1] = styles.default_tab;
  styles.tab[2] = styles.default_tab;
  styles.tab[this.state.slideIndex] = objectAssign({},   styles.tab[this.state.slideIndex], styles.active_tab);

  return (
    <Tabs>
      <Tab style={styles.tab[0]} label="Tab 0" value="0" />
      <Tab style={styles.tab[1]} label="Tab 1" value="1" />
      <Tab style={styles.tab[2]} label="Tab 2" value="2" />
    </Tabs>
  )

虽然我认为更好的方法是为 Tabs/Tab 提供更多的道具,以便我们可以自定义它。

【讨论】:

  • 不错!只需更新:objectAssign 现在是 Object.assign
【解决方案2】:

所以如果有人会面临同样的问题,这就是我发现的

使用 ThemeManager 我们可以更改样式输出

例如

ThemeManager.setTheme(ThemeManager.types.DARK);

使用setPalette更改特定样式变量

componentWillMount() {
        ThemeManager.setPalette({
          accent1Color: Colors.indigo50,
            primary1Color: "#474B4E",
            primary2Color: "#2173B3",
            primary3Color: "#A9D2EB",
            accent1Color: "#ED3B3B",
            accent2Color: "#ED2B2B",
            accent3Color: "#F58C8C"
        });
     }

【讨论】:

    【解决方案3】:

    自定义组件最方便的方法是使用 className 属性简单地应用普通的旧 CSS,因为提供的 style 属性的功能相当有限。

    让我们考虑一个简单的例子:

    const MyAwesomeTabContainer = () => (
        <Tabs className="tabs-container">
            <Tab icon={<Person />} className="tab" />
            <Tab icon={<Content />} className="tab" />
        </Tabs>
    );
    

    以下 LESS 代码(不是 CSS!)可让您根据需要自定义样式:

    .tabs-container {
      >div:first-child { // modifies the background color
        background-color: #f6f6f6 !important;
      }
    
      >div:last-child { // changes the distance between tabs and content
        margin-top: 10px;
      }
    
      .tab {
        >div>div { // modifies the font size of the tab labels 
          font-size: 10px;
    
          svg { // modifies the color of SVG icons
            fill: #626262 !important;
          }
        }
      }
    }
    

    不幸的是,由于组件的样式信息是在代码中内联设置的,因此需要应用一些!important 语句。

    【讨论】:

      【解决方案4】:

      我想在活动选项卡上开设一个课程,所以这里有一个快速解决方案:

      <Tabs className="pcd-tabs" onChange={this.handleChange}>
          <Tab className="pcd-tab pcd-tab0 pcd-tab-active" label="Chart" value={0} />
          <Tab className="pcd-tab pcd-tab1" label="Series" value={1} />
      </Tabs>
      

      handleChange = (value) => {
        document.getElementsByClassName('pcd-tab-active')[0].classList.remove('pcd-tab-active');
        document.getElementsByClassName('pcd-tab' + value)[0].classList.add('pcd-tab-active');
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-27
        • 1970-01-01
        • 1970-01-01
        • 2019-02-05
        • 1970-01-01
        • 2021-12-01
        • 2018-06-10
        相关资源
        最近更新 更多