Skip to content

Commit 10dd2de

Browse files
committedApr 24, 2015
Allow optional "component" argument to Switcher
- This is the React component that will wrap the currently rendered Switch (if defined). Otherwise the Switch is rendered by itself.
1 parent f6c5c99 commit 10dd2de

File tree

6 files changed

+27
-35
lines changed

6 files changed

+27
-35
lines changed
 

‎dist/switcheroo.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/components/Switcher.md

+4
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ The `defaultHandler` is the handler that is used when there are no `Switch` elem
4141
```js
4242
onChange(match, pathname) { ... }
4343
```
44+
45+
### wrapper
46+
47+
If the `wrapper` prop is defined, the rendered `Switch` will be wrapped in the specified React component. Any additional props passed to the `Switcher` will also be properties of this component.

‎modules/components/NullComponent.js

-7
This file was deleted.

‎modules/components/Switcher.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, {Component} from 'react';
2-
import NullComponent from './NullComponent';
32

43
export default class Switcher extends Component {
54
constructor(props) {
@@ -11,10 +10,10 @@ export default class Switcher extends Component {
1110
this.handleRouteChange = this.handleRouteChange.bind(this);
1211
this.getSwitch = this.getSwitch.bind(this);
1312

14-
this.defaultComponent = React.createElement(this.props.defaultHandler || NullComponent, this.props.defaultHandlerProps);
13+
this.defaultSwitch = this.props.defaultHandler ? React.createElement(this.props.defaultHandler, this.props.defaultHandlerProps) : null;
1514
// set initial state
1615
this.state = {
17-
visibleComponent: null
16+
visibleSwitch: null
1817
};
1918
}
2019

@@ -71,12 +70,21 @@ export default class Switcher extends Component {
7170
}
7271

7372
this.setState({
74-
visibleComponent: switchElement
73+
visibleSwitch: switchElement
7574
});
7675
}
7776

7877
render() {
79-
return this.state.visibleComponent || this.defaultComponent;
78+
if(this.props.wrapper) {
79+
return React.createElement(
80+
this.props.wrapper,
81+
this.props,
82+
this.state.visibleSwitch || this.defaultSwitch
83+
);
84+
}
85+
else {
86+
return this.state.visibleSwitch || this.defaultSwitch;
87+
}
8088
}
8189
}
8290

@@ -90,7 +98,8 @@ Switcher.propTypes = {
9098
pushState: React.PropTypes.bool,
9199
defaultHandler: React.PropTypes.func,
92100
defaultHandlerProps: React.PropTypes.object,
93-
onChange: React.PropTypes.func
101+
onChange: React.PropTypes.func,
102+
wrapper: React.PropTypes.any
94103
};
95104

96105
Switcher.defaultProps = {

‎test/components/NullComponent_test.js

-19
This file was deleted.

‎test/components/Switch_test.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import React from 'react/addons';
1+
import React, {Component} from 'react/addons';
22
import {assert} from 'chai';
33
import sinon from 'sinon';
44
import Switch from 'components/Switch';
5-
import NullComponent from 'components/NullComponent';
5+
6+
class NullComponent extends Component {
7+
render() {
8+
return false;
9+
}
10+
}
611

712
describe('Switch', function() {
813

0 commit comments

Comments
 (0)
Please sign in to comment.