Skip to content

Commit

Permalink
Merge pull request #75 from blueberryapps/cl-57-variants
Browse files Browse the repository at this point in the history
CL-57 Render only variants that affect component's look
  • Loading branch information
imtoo authored Nov 2, 2016
2 parents 69e705e + 5844690 commit 901672b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/app/atoms/Input.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as colors from '../styles/Colors';
export default class Input extends Component {
static propTypes = {
inheritedStyles: RPT.oneOfType([RPT.array, RPT.object]),
key: RPT.string,
kind: RPT.oneOf([
'inputDefault',
'inputSearch'
Expand Down
35 changes: 29 additions & 6 deletions src/app/component/Variants.react.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import _ from 'lodash';
import AtomPreview from '../atoms/AtomPreview.react';
import Component from 'react-pure-render/component';
import {fromJS} from 'immutable';
import headingStyles from '../styles/Headings';
import Radium from 'radium';
import React, {PropTypes as RPT} from 'react';
import renderProp from '../../libraries/renderProp';
import SourceCode from './SourceCode.react';
import styles from '../styles/Sources';
import variantChecksum from '../../libraries/variantChecksum';
import {fromJS} from 'immutable';

@Radium
export default class Variants extends Component {
Expand Down Expand Up @@ -53,13 +55,36 @@ export default class Variants extends Component {

renderEnumVariant(key, name, value) {
if (typeof value === 'object')
return this.renderVariants(key, name, value.map(text => text.get('value').replace(/\'/g, '')))
return this.renderVariants(key, name, value.map(text => text.get('value').replace(/\'/g, '')).toJS())
return null
}

collectVariants(key, type, variants) {
const {atom, componentProps} = this.props

return variants.map(variant => {
const variantProps = componentProps.set(key, variant)

return {
atom,
key,
type,
variant,
variantProps,
checksum: variantChecksum({atom, variantProps, context: this.context}),
}
})
}

renderVariants(key, type, variants) {
const {headingColor} = this.props

const collection = this.collectVariants(key, type, variants);
const uniqCollection = _.uniqBy(collection, i => i.checksum);

if (uniqCollection.length < 2)
return null;

return (
<div>
<div style={styles.panel}>
Expand All @@ -69,15 +94,13 @@ export default class Variants extends Component {
>
Prop variant: <b>{key}</b>
</h2>
{variants.map(variant => this.renderVariant(key, type, variant))}
{collection.map(item => this.renderVariant(item))}
</div>
</div>
)
}

renderVariant(key, type, variant) {
const {atom, componentProps} = this.props
const variantProps = componentProps.set(key, variant)
renderVariant({atom, key, type, variant, variantProps}) {
const source = `<${atom.get('componentName')} ${renderProp(key, type, variant)} />`

return (
Expand Down
34 changes: 34 additions & 0 deletions src/libraries/variantChecksum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import AtomPreview from '../app/atoms/AtomPreview.react';
import React, {PropTypes as RPT} from 'react';
import ReactDOM from 'react-dom/server';

const variantChecksum = ({atom, variantProps, context}) => {
const Context = contextCreator(context);
const html = ReactDOM.renderToStaticMarkup(
<Context context={context}>
<AtomPreview atom={atom} variantProps={variantProps} />
</Context>
)
const pureHtml = html.replace(/<([a-zA-Z]+)\s?([^>])+>/ig, '<$1>');
const style = (html.match(/style="[^"]+"/ig) || []).toString();
const classes = (html.match(/class="[^"]+"/ig) || []).toString();
return JSON.stringify([pureHtml, style, classes]);
}

const contextCreator = (context) => class ContextProvider extends React.Component {
static childContextTypes = Object.keys(context).reduce((out, key)=> ({...out, [key]: RPT.any}), {});

static propTypes = {
children: RPT.node
}

getChildContext() {
return context;
}

render() {
return this.props.children;
}
}

export default variantChecksum;

0 comments on commit 901672b

Please sign in to comment.