Skip to content

Commit

Permalink
Merge pull request #18 from cloudflare/multiple-children
Browse files Browse the repository at this point in the history
Support multiple children, expose gateway registry
  • Loading branch information
marksteyn authored Dec 13, 2016
2 parents 354d970 + 4fb199b commit 8e45827
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export default class Gateway extends React.Component {
}

componentWillReceiveProps(props) {
this.gatewayRegistry.clearChild(this.props.into);
this.gatewayRegistry.clearChild(this.props.into, this.props.children);
this.renderIntoGatewayNode(props);
}

componentWillUnmount() {
this.gatewayRegistry.removeChild(this.props.into);
this.gatewayRegistry.removeChild(this.props.into, this.props.children);
}

renderIntoGatewayNode(props) {
Expand Down
2 changes: 1 addition & 1 deletion src/GatewayDest.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ export default class GatewayDest extends React.Component {
render() {
const { component, tagName, ...attrs } = this.props;
delete attrs.name;
return React.createElement(component || tagName || 'div', attrs, this.state.child);
return React.createElement(component || tagName || 'div', attrs, this.state.children);
}
}
19 changes: 7 additions & 12 deletions src/GatewayRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class GatewayRegistry {
}

this._containers[name].setState({
child: this._children[name]
children: this._children[name]
});
}

Expand All @@ -24,22 +24,17 @@ export default class GatewayRegistry {
}

addChild(name, child) {
if (this._children[name]) {
console.warn(
'Only a single Gateway can be rendered at a time into a GatewayDest.' +
`You rendered multiple into "${name}"`
);
}
this._children[name] = child;
this._children[name] = this._children[name] || [];
this._children[name].push(child);
this._renderContainer(name);
}

clearChild(name) {
this._children[name] = null;
clearChild(name, child) {
this._children[name] = this._children[name].filter(item => item !== child);
}

removeChild(name) {
this.clearChild(name);
removeChild(name, child) {
this.clearChild(name, child);
this._renderContainer(name);
}
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export Gateway from './Gateway';
export GatewayDest from './GatewayDest';
export GatewayProvider from './GatewayProvider';
export GatewayRegistry from './GatewayRegistry';

0 comments on commit 8e45827

Please sign in to comment.