Skip to content

Commit

Permalink
Add toggle component
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Sep 20, 2017
1 parent 1b89efb commit 290a510
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 139 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ README.md
circle.yml
coverage
tests
test-setup.js
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export default QueryState(example);

## Toggle

**Coming Soon**
Toggle the display of data with three simple props!

- [source](/src/toggle.js)
- [tests](/tests/toggle.js)
- [example](/example/src/example/toggle.js)

```js
import { Toggle } from 'flashbang'
Expand Down
39 changes: 32 additions & 7 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"react-scripts": "1.0.13"
},
"dependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react": "^16.0.0-rc.2",
"react-dom": "^16.0.0-rc.2",
"react-router-dom": "^4.2.2"
},
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions example/src/App.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React, { Component } from 'react';
import { BrowserRouter, Switch, Route } from 'react-router-dom';

import Toggle from './example/toggle';
import QueryState from './example/query-state';

class App extends Component {
render() {
return (
<BrowserRouter>
<Switch>
<Route path="/toggle" component={Toggle} />
<Route path="/query-state" component={QueryState} />
</Switch>
</BrowserRouter>
Expand Down
11 changes: 11 additions & 0 deletions example/src/example/toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
import { Toggle } from '../flashbang';

export default ({ state, setState }) => (
<Toggle>
<div toggle>Click this to swap the content</div>

<div on>Toggle is now on!</div>
<div off>Click the toggle above to turn it on!</div>
</Toggle>
);
1 change: 1 addition & 0 deletions example/src/flashbang/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as QueryState } from './query-state';
export { default as Toggle } from './toggle';
37 changes: 37 additions & 0 deletions example/src/flashbang/toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';

export default class Toggle extends React.Component {
constructor() {
super();
this.state = {};
}

children(children) {
let { toggled } = this.state;

return React.Children.map(children, child => {
let { toggle, on, off, ...props } = child.props;

if (toggle)
return React.createElement(child.type, {
...props,
onClick: () => this.setState({ toggled: !toggled }),
});

if (on) {
if (!toggled) return null;
return React.createElement(child.type, props);
}

if (off) {
if (toggled) return null;
return React.createElement(child.type, props);
}

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

0 comments on commit 290a510

Please sign in to comment.