Skip to content

Commit

Permalink
feat(RenderHandler): working version of RenderHandler
Browse files Browse the repository at this point in the history
affects: @tao.js/react, patois.web

able to work with cartesian permutations
fixed render when missing context prop
works with router-based signals
SwitchHandler not working so RenderHandlers don't disappear
  • Loading branch information
eudaimos committed Oct 30, 2018
1 parent c768b0e commit 8a0eaee
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
11 changes: 5 additions & 6 deletions examples/patois.web/src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import React, { Component } from 'react';
import TAO from '@tao.js/core';
import { Provider } from '@tao.js/react';
import logo from './logo.svg';
import './App.css';
import Space from './components/Space';

const TAOContext = React.createContext({ TAO });
import Space, { SpaceAltContainer } from './components/Space';

class App extends Component {
render() {
return (
<TAOContext.Provider TAO={TAO}>
<Provider TAO={TAO}>
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<Space />
<SpaceAltContainer />
</div>
</TAOContext.Provider>
</Provider>
);
}
}
Expand Down
8 changes: 3 additions & 5 deletions examples/patois.web/src/components/Space/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ class SpaceForm extends Component {
onChange={this.handleChange}
/>
<br />
<input type="submit" value="Save" />&nbsp;<button
onClick={this.handleCancel}
>
Cancel
</button>
<input type="submit" value="Save" />
&nbsp;
<button onClick={this.handleCancel}>Cancel</button>
</form>
</div>
);
Expand Down
25 changes: 23 additions & 2 deletions examples/patois.web/src/components/Space/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import React, { Fragment } from 'react';
import TAO, { AppCtx } from '@tao.js/core';
import { Adapter, Reactor } from '@tao.js/react';
import { Adapter, Reactor, SwitchHandler, RenderHandler } from '@tao.js/react';
import List from './List';
import View from './View';
import Form from './Form';
Expand Down Expand Up @@ -50,3 +50,24 @@ const SpaceContainer = () => (
// };

export default SpaceContainer;

const SpaceAltContainer = () => (
<Fragment>
<RenderHandler action="Fail">
{(tao, data) => <ErrorMessage Space={data.Space} Fail={data.Fail} />}
</RenderHandler>
{/*<SwitchHandler term="Space" orient="Portal">*/}
<RenderHandler term="Space" action="List" orient="Portal">
{(tao, data) => <List Space={data.Space} />}
</RenderHandler>
<RenderHandler term="Space" action="View" orient="Portal">
{(tao, data) => <View Space={data.Space} />}
</RenderHandler>
<RenderHandler term="Space" action={['New', 'Edit']} orient="Portal">
{(tao, data) => <Form Space={data.Space} a={tao.a} />}
</RenderHandler>
{/*</SwitchHandler>*/}
</Fragment>
);

export { SpaceAltContainer };
2 changes: 1 addition & 1 deletion packages/react-tao/src/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class Provider extends Component {
}

render() {
const { TAO } = this.props;
const { TAO, children } = this.props;
const dataContextFunctions = this.state;
return (
<Context.Provider value={{ TAO, ...dataContextFunctions }}>
Expand Down
30 changes: 27 additions & 3 deletions packages/react-tao/src/RenderHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { Component } from 'react';
import React, { Fragment, Component } from 'react';
import PropTypes from 'prop-types';
import { Context } from './Provider';
import cartesian from 'cartesian';

function recursiveContextGenerator(
ctxList,
Expand Down Expand Up @@ -43,6 +45,13 @@ function recursiveContextGenerator(
export default class RenderHandler extends Component {
static contextType = Context;

static propTypes = {
term: PropTypes.any,
action: PropTypes.any,
orient: PropTypes.any,
children: PropTypes.func.isRequired
};

constructor(props) {
super(props);
let { shouldRender } = props;
Expand All @@ -51,15 +60,27 @@ export default class RenderHandler extends Component {
}

componentWillMount() {
console.log('RenderHandler::props:', this.props);
const { term, action, orient } = this.props;
console.log('RenderHandler::context:', this.context);
const { TAO } = this.context;
TAO.addInlineHandler({ term, action, orient }, this.handleRender);
const permutations = cartesian({ term, action, orient });
if (permutations.length) {
permutations.forEach(({ term, action, orient }) =>
TAO.addInlineHandler({ term, action, orient }, this.handleRender)
);
}
}

componentWillUnmount() {
const { term, action, orient } = this.props;
const { TAO } = this.context;
TAO.removeInlineHandler({ term, action, orient }, this.handleRender);
const permutations = cartesian({ term, action, orient });
if (permutations.length) {
permutations.forEach(({ term, action, orient }) =>
TAO.removeInlineHandler({ term, action, orient }, this.handleRender)
);
}
}

handleRender = (tao, data) => {
Expand All @@ -74,6 +95,9 @@ export default class RenderHandler extends Component {
if (!shouldRender) {
return null;
}
if (!context) {
return <Fragment>{children(tao, data)}</Fragment>;
}
const ctxList = Array.isArray(context) ? context : [context];
return recursiveContextGenerator(
ctxList,
Expand Down

0 comments on commit 8a0eaee

Please sign in to comment.