From 38e843e9c7f0c0ad2a55e51e8159c003792b4c51 Mon Sep 17 00:00:00 2001 From: Jonathan Lehman Date: Tue, 20 Oct 2015 18:11:24 -0400 Subject: [PATCH] Remove `defaultHandler` and `defaultHandlerProps` - using `path="/.*"` as the last `Switch` does the same thing --- docs/components/Switcher.md | 9 +-------- examples/basic/components/LeftContent.js | 7 ++----- src/components/Switcher.js | 7 ++----- test/components/Switcher_test.js | 25 ++++++------------------ 4 files changed, 11 insertions(+), 37 deletions(-) diff --git a/docs/components/Switcher.md b/docs/components/Switcher.md index b5b68c3..784fe53 100644 --- a/docs/components/Switcher.md +++ b/docs/components/Switcher.md @@ -9,6 +9,7 @@ The `Switcher` is a container component that holds a list of React components. I
Hello World
+ ``` @@ -38,14 +39,6 @@ When true, `Switcher` listens to the `load` event and looks for path changes on By default `window.location.hash` is used to match paths. If `location` is set to 'pathname', then `window.location.pathname` is used instead. Under the hood, it is using `window.location[this.props.location]`, so you can use `search` or any other valid property on `window.location`. -### defaultHandler - -The `defaultHandler` is the handler that is used when there are no child elements with matching paths. If a default handler is not provided by the user, the component will render nothing when there isn't a match. - -### defaultHandlerProps - -`defaultHandlerProps` is passed directly as props to the `defaultHandler`. - ### onChange `onChange` enables a hook to call the provided function whenever the path changes. The function is provided 2 arguments, the first being a boolean of whether the path had a match, and the second being the path as a string. diff --git a/examples/basic/components/LeftContent.js b/examples/basic/components/LeftContent.js index f73a4ba..f71be8c 100644 --- a/examples/basic/components/LeftContent.js +++ b/examples/basic/components/LeftContent.js @@ -2,10 +2,6 @@ import React, {Component} from 'react'; import {Switcher} from 'switcheroo'; import Panel from './Panel'; -function defaultView() { - return
Default Content: No matching route
; -} - export default class LeftContent extends Component { render() { var panelItems = [ @@ -17,13 +13,14 @@ export default class LeftContent extends Component { return (

Left Content

- +
Route 1 rules!
Route 2 for life
+
Default content: No matching route
); diff --git a/src/components/Switcher.js b/src/components/Switcher.js index 5f7f182..f76270c 100644 --- a/src/components/Switcher.js +++ b/src/components/Switcher.js @@ -12,8 +12,6 @@ export default class Switcher extends Component { pushState: React.PropTypes.bool, hashChange: React.PropTypes.bool, load: React.PropTypes.bool, - defaultHandler: React.PropTypes.func, - defaultHandlerProps: React.PropTypes.object, onChange: React.PropTypes.func, wrapper: React.PropTypes.any, location: React.PropTypes.string, @@ -30,7 +28,6 @@ export default class Switcher extends Component { constructor(props) { super(props); - this.defaultSwitch = props.defaultHandler ? React.createElement(props.defaultHandler, props.defaultHandlerProps) : null; var currentPath = this.getLocation(); var switchElement = this.getSwitch(currentPath); @@ -111,10 +108,10 @@ export default class Switcher extends Component { return React.createElement( this.props.wrapper, this.props, - this.state.visibleSwitch || this.defaultSwitch + this.state.visibleSwitch ); } else { - return this.state.visibleSwitch || this.defaultSwitch; + return this.state.visibleSwitch; } } } diff --git a/test/components/Switcher_test.js b/test/components/Switcher_test.js index c873061..fb8ffbc 100644 --- a/test/components/Switcher_test.js +++ b/test/components/Switcher_test.js @@ -1,21 +1,10 @@ -import React, {Component, PropTypes} from 'react'; +import React from 'react'; import ReactDOM from 'react-dom'; import {assert} from 'chai'; import sinon from 'sinon'; import window from 'window'; import Switcher from 'components/Switcher'; -class Handler extends Component { - static displayName = 'Handler'; - static propTypes = { - text: PropTypes.string - }; - - render() { - return
{this.props.text}
; - } -} - describe('Switcher', function() { describe('#getLocation', function() { describe('using location.hash', function() { @@ -252,12 +241,10 @@ describe('Switcher', function() { describe('with default handler', function() { beforeEach(function() { - var props = {text: 'Hello'}; this.switcher = ReactDOM.render( - -
Home
+ +
Home
+
Default Handler
, document.body ); @@ -271,11 +258,11 @@ describe('Switcher', function() { window.location.hash = '/nomatch'; this.switcher.handleRouteChange(); var node = ReactDOM.findDOMNode(this.switcher); - assert.equal(node.innerHTML, 'Hello'); + assert.equal(node.innerHTML, 'Default Handler'); }); it('renders matching component', function() { - window.location.hash = '/'; + window.location.hash = '/home'; this.switcher.handleRouteChange(); var node = ReactDOM.findDOMNode(this.switcher); assert.equal(node.innerHTML, 'Home');