2016-08-04 4 views
0

AppBarは、特定のタイプの子どもたちにいくつかのスタイルを適用します。残念ながら、それが直接の子 素材uiコンポーネントからスタイルを抽出する

jsfiddle

<AppBar title="first" iconElementRight={ 
    <FlatButton label="first" /> 
}/> 
<AppBar title="second" iconElementRight={ 
    <div> 
     <FlatButton label="second" /> <!-- styles are not applied --> 
    </div> 
}/> 

にのみ発生コンポーネントがgetStylesメソッドを公開するアプリケーションバー のようにうまくいけば、それはそう。

exports.getStyles = getStyles; 

残念ながら、私はそれを消費する方法を見つけることができません。自分ですべてのスタイルを再定義するのではなく、方法はありますか?あなたはそこからすべてのスタイルを得ることができるので、

PS 私はimportディレクティブ

import AppBar from 'material-ui/AppBar'; 
+0

は何を達成したいですか? AppBarからスタイルを取得し、 ' QoP

+0

@QoP:FlatButtonとiconElementRightの直接の子として指定されているので、そこに配置しているIconMenuを作成したいと思います。これを行うために、私はAppBarからスタイルを取得し、それを私の要素(色と位置に関するべきである)のためにオーバーライドすることを計画しています。 –

答えて

1

MuiThemeProviderで部品を輸入していますが、コンテキストにmuiThemeオブジェクトを追加します。

Comp.contextTypes = { 
    muiTheme: React.PropTypes.object 
} 

render: function() { 
     let {appBar} = this.context.muiTheme; // appBar styles here 
     ... 
} 

jsfiddle

+0

ありがとうございます。私は、これが自分のコンポーネントのハードコーディングカラーよりも優れていると思います。しかし、https://github.com/callemall/material-ui/blob/master/src/AppBar/AppBar.js#L55から正確なスタイルを取得するのは素晴らしい方法です。それで、私は同じことをする必要はありません他のプロパティ+ AppBarの変更時に自動更新されます。また、getStyles関数がエクスポートされているようです。何とかそれを呼び出す方法があると思いますか? –

+0

私はそれが可能であるとは思わない、 "内部"機能のように思える。 'import {getStyles}'を 'material-ui/AppBar/AppBar'からインポートし、getStyles(this.props、this.context)を使って呼び出すことはできますが、うまくいくかどうかはわかりません。 – QoP

1

ご希望の子供にスタイルを再ルーティングするためのカスタム・コンポーネントを必要とするような音。このバージョンでは子にstyleが転送され、残りはラッピングコンポーネント(デフォルトで<div>)に残りますが、要件に合わせてカスタマイズできます。

 const StyleDelegate = function (props) { 
 
    const {component: Component, children, style, ...rest} = props; 
 
    // pass style to the only child and everything else to the component 
 
    // could be further customized for other props 
 
    return (
 
     <Component {...rest}> 
 
      {React.cloneElement(children, {style})} 
 
     </Component> 
 
); 
 
}; 
 

 
StyleDelegate.defaultProps = { 
 
    component: 'div' 
 
}; 
 

 
const AppBar = function (props) { 
 
    return (
 
    <div> 
 
     {props.title} 
 
     {React.cloneElement(props.iconElementRight, {style: {color: 'blue'}})} 
 
    </div> 
 
); 
 
} 
 
    
 
ReactDOM.render(<AppBar 
 
        iconElementRight={<StyleDelegate><span>Icon</span></StyleDelegate>} 
 
        title="title" />, 
 
       document.querySelector('#app') 
 
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 
<div id="app"></div>

関連する問題