Skip to content

Commit

Permalink
Add docs to Boomerang and connect file
Browse files Browse the repository at this point in the history
  • Loading branch information
matuzalemsteles committed Sep 11, 2018
1 parent 47bd560 commit 464b828
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
21 changes: 20 additions & 1 deletion src/Boomerang.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,27 @@ import Component from 'metal-jsx';

let Boomerang = {};

/**
* A Provider receives the `events` that he wishes to
* hear from his children`s.
* @extends Component
*/
class Provider extends Component {
static PROPS = {
events: Config.object(),
/**
* The events you want to hear from children
* components.
* @default undefined
* @instance
* @memberof Provider
* @type {!object}
*/
events: Config.object().required(),
};

/**
* @inheritDoc
*/
getChildContext() {
return {
boomerang: {
Expand All @@ -23,6 +39,9 @@ class Provider extends Component {
};
}

/**
* @inheritDoc
*/
render() {
const {children} = this.props;

Expand Down
22 changes: 17 additions & 5 deletions src/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,28 @@

import JSXComponent from 'metal-jsx';

/**
* connect is a higher-order component (HOC) that connects the
* component to the providers, it passes the events written in
* `context.boomerang` which are registered from <Boomerang.Provider />
* @param {class} Component
* @public
* @return {class}
*/
function connect(Component) {
class WithConnect extends JSXComponent {
render() {
const {events: Events} = this.context.boomerang;
const events = Object.assign({}, this.props.events, Events);
const Props = Object.assign({}, this.props, {events});
const {events: eventsFromProvider} = this.context.boomerang;
const events = Object.assign(
{},
this.props.events,
eventsFromProvider
);
const props = Object.assign({}, this.props, {events});

return (
<Component ref="connect" {...Props}>
{Props.children}
<Component ref="connect" {...props}>
{props.children}
</Component>
);
}
Expand Down

0 comments on commit 464b828

Please sign in to comment.