Skip to content

Commit

Permalink
more interesting example
Browse files Browse the repository at this point in the history
  • Loading branch information
dlmanning committed Jun 23, 2015
1 parent 208c414 commit 556f14b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"optional": [
"es7.decorators",
"es7.classProperties",
"optimisation.react.constantElements",
"es7.objectRestSpread",
"runtime"
]
}
21 changes: 6 additions & 15 deletions src/flex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ export class FlexContext extends Component {

startNewStyleTree () {
this.stylesRoot = { children: [] };
const { setStyle: layoutFunc } = setStyle(undefined, this.stylesRoot);
this.styleTools.setStyle = layoutFunc;
({ setStyle: this.styleTools.setStyle } = setStyle(undefined, this.stylesRoot));
}

computeLayoutAndBroadcastResults () {
Expand Down Expand Up @@ -129,27 +128,19 @@ export const FlexBox = (Composed, componentStyles = {}) => class extends Compone
}

componentWillMount () {
const { setStyle: setStyleFunc, path} = this.context.styleTools.setStyle(this.flexStyles);

this.styleTools.setStyle = setStyleFunc;
this.pathToNode = path;
({ setStyle: this.styleTools.setStyle,
path: this.pathToNode } = this.context.styleTools.setStyle(this.flexStyles));

this.context.waitForLayoutCalculation(this.handleLayoutCalculation);
}

componentWillReceiveProps () {
console.log('FlexItem receiving props');
}

componentWillReceiveProps (nextProps) {
const { svgStyles, flexStyles } = partitionStyles(nextProps.style)
const { setStyle: setStyleFunc, path} = this.context.styleTools.setStyle(flexStyles);
const { flexStyles } = partitionStyles(nextProps.style);

this.styleTools.setStyle = setStyleFunc;
this.pathToNode = path;
({ setStyle: this.styleTools.setStyle,
path: this.pathToNode } = this.context.styleTools.setStyle(flexStyles));

this.context.waitForLayoutCalculation(this.handleLayoutCalculation);

}

getChildContext () {
Expand Down
2 changes: 2 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

import { default as React, Component } from 'react';
// import Chessboard from './chessboard.jsx';
import Thing from './test.js';
Expand Down
52 changes: 39 additions & 13 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

import { default as React, Component } from 'react';
import { FlexContext, FlexBox } from 'flex';
import { EventEmitter } from 'wolfy87-eventemitter';
Expand All @@ -10,21 +12,45 @@ class Container extends Component {
}

const circleStyle = {
height: 150,
width: 150,
fill: 'tomato'
margin: 10,
height: 100,
width: 100
};

@composeWith(FlexBox)
class Circle extends Component {
render () {
return <circle style={this.props.style} cx="50" cy="50" r="50" />;
return <circle style={this.props.style} cx="40" cy="40" r="40" />;
}
}

export default class Thing extends Component {
static colors = (function (n) {
const totalColors = 256**3;
const stepSize = Math.floor(totalColors / n);
const colorValues = []

for (let i = 0; i < n; i++) {
colorValues.push(i * stepSize);
}

return colorValues.map(value => rgb(value));

function rgb (value) {
const digits = [];

for (let i = 0; i < 3; i++) {
digits[i] = Math.floor(value / 256 ** (2 - i));
value %= 256 ** (2 - i);
}

return `rgb(${digits[0]},${digits[1]}, ${digits[2]})`;
}

})(18);

state = {
height: 900,
height: 700,
width: 700
};

Expand All @@ -38,22 +64,22 @@ export default class Thing extends Component {
}

render () {
console.log('Calling render');
const { height, width } = this.state;

const containerStyle = {
height,
width,
flexDirection: 'row',
flexWrap: 'wrap',
height: this.state.height,
width: this.state.width
flexWrap: 'wrap'
};

return (
<svg height={this.state.height} width={this.state.width}>
<svg height={height} width={width}>
<FlexContext>
<rect height={this.state.height - 1} width={this.state.width - 1} style={{fill: 'none', stroke: 'black'}} />
<rect height={height - 1} width={width - 1} style={{fill: 'lightgrey', stroke: 'black'}} />
<Container style={containerStyle} >
{[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].map(idx =>
<Circle style={circleStyle} key={idx} />)}
{Thing.colors.map((color, idx) =>
<Circle style={{fill: color, ...circleStyle}} key={idx} />)}
</Container>
</FlexContext>
</svg>
Expand Down
2 changes: 2 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const path = require('path');

module.exports = {
Expand Down

0 comments on commit 556f14b

Please sign in to comment.