material-ui
Version:
Material Design UI components built with React
132 lines (112 loc) • 3.16 kB
JSX
const React = require('react');
const StylePropable = require('../mixins/style-propable');
const Tooltip = require('../tooltip');
const DefaultRawTheme = require('../styles/raw-themes/light-raw-theme');
const ThemeManager = require('../styles/theme-manager');
const TableHeaderColumn = React.createClass({
mixins: [StylePropable],
contextTypes: {
muiTheme: React.PropTypes.object,
},
propTypes: {
columnNumber: React.PropTypes.number,
onClick: React.PropTypes.func,
style: React.PropTypes.object,
tooltip: React.PropTypes.string,
tooltipStyle: React.PropTypes.object,
},
//for passing default theme context to children
childContextTypes: {
muiTheme: React.PropTypes.object,
},
getChildContext () {
return {
muiTheme: this.state.muiTheme,
};
},
getInitialState () {
return {
muiTheme: this.context.muiTheme ? this.context.muiTheme : ThemeManager.getMuiTheme(DefaultRawTheme),
hovered: false,
};
},
//to update theme inside state whenever a new theme is passed down
//from the parent / owner using context
componentWillReceiveProps (nextProps, nextContext) {
let newMuiTheme = nextContext.muiTheme ? nextContext.muiTheme : this.state.muiTheme;
this.setState({muiTheme: newMuiTheme});
},
getTheme() {
return this.state.muiTheme.tableHeaderColumn;
},
getStyles() {
let theme = this.getTheme();
let styles = {
root: {
fontWeight: 'normal',
fontSize: 12,
paddingLeft: theme.spacing,
paddingRight: theme.spacing,
height: theme.height,
textAlign: 'left',
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
color: this.getTheme().textColor,
position: 'relative',
},
tooltip: {
boxSizing: 'border-box',
marginTop: theme.height / 2,
},
};
return styles;
},
render() {
let styles = this.getStyles();
let handlers = {
onMouseEnter: this._onMouseEnter,
onMouseLeave: this._onMouseLeave,
onClick: this._onClick,
};
let {
className,
columnNumber,
onClick,
style,
tooltip,
tooltipStyle,
...other,
} = this.props;
let classes = 'mui-table-header-column';
if (className) classes += ' ' + className;
if (this.props.tooltip !== undefined) {
tooltip = (
<Tooltip
label={this.props.tooltip}
show={this.state.hovered}
style={this.mergeStyles(styles.tooltip, tooltipStyle)} />
);
}
return (
<th
key={this.props.key}
className={classes}
style={this.prepareStyles(styles.root, style)}
{...handlers}
{...other}>
{tooltip}
{this.props.children}
</th>
);
},
_onMouseEnter() {
if (this.props.tooltip !== undefined) this.setState({hovered: true});
},
_onMouseLeave() {
if (this.props.tooltip !== undefined) this.setState({hovered: false});
},
_onClick(e) {
if (this.props.onClick) this.props.onClick(e, this.props.columnNumber);
},
});
module.exports = TableHeaderColumn;