import React from 'react';
import {StyleSheet, css} from 'aphrodite-simple';
import {shouldComponentUpdate} from 'react-addons-pure-render-mixin';
export default class Component extends React.Component {
constructor() {
super();
this.styles_ = this.createStyleSheet();
this.cssHelper = this.cssHelper.bind(this);
if (!this.shouldComponentUpdate) {
this.shouldComponentUpdate = shouldComponentUpdate.bind(this);
}
}
createStyleSheet() {
if (!this.styles) {
return {};
}
const styles = this.styles();
if (typeof styles !== 'object') {
throw new TypeError('Component `styles` returns a non-object');
}
return StyleSheet.create(this.styles());
}
cssHelper(...args) {
const classes = args
.map(c => {
if (c) {
const component = this.constructor.name
.toString()
.toLowerCase();
const globalName = `${component}_${c}`;
return [globalName, css(this.styles_[c])];
}
return null;
})
.filter(v => Boolean(v))
.reduce((a, b) => a.concat(b));
return classes.length ? classes.join(' ') : null;
}
render() {
if (!this.template) {
throw new TypeError('Component doesn\'t define `template`');
}
return this.template(this.cssHelper);
}
}