Skip to content

Commit

Permalink
Merge pull request #4 from whatknight/nextprops
Browse files Browse the repository at this point in the history
Use next props when component will receive props.
  • Loading branch information
jdlehman committed Jan 22, 2016
2 parents 3b94a36 + 0a9673f commit dc94afc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/switcheroo.min.js

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

15 changes: 7 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class Switcher extends Component {
super(props);

var currentPath = this.getLocation();
var switchElement = this.getSwitch(currentPath);
var switchElement = this.getSwitch(currentPath, props);
this.state = {
visibleSwitch: switchElement
};
Expand All @@ -50,8 +50,7 @@ export default class Switcher extends Component {

componentWillReceiveProps(nextProps) {
var currentPath = this.getLocation();
var switchElement = this.getSwitch(currentPath);

var switchElement = this.getSwitch(currentPath, nextProps);
this.setState({
visibleSwitch: switchElement
});
Expand All @@ -78,21 +77,21 @@ export default class Switcher extends Component {
}
};

getSwitch = (path) => {
var children = [].concat(this.props.children);
getSwitch(path, props) {
var children = [].concat(props.children);
var consistentPath = removeTrailingSlash(path);
return children.filter(child => {
var childPaths = [].concat(child.props.path).map(childPath => {
return `${removeTrailingSlash(this.props.basePath + childPath)}/?`;
return `${removeTrailingSlash(props.basePath + childPath)}/?`;
});
var regex = new RegExp(`^${childPaths.join('|')}$`);
return regex.test(consistentPath);
})[0] || null;
};
}

handleRouteChange = (ev) => {
var currentPath = this.getLocation();
var switchElement = this.getSwitch(currentPath);
var switchElement = this.getSwitch(currentPath, this.props);

if (typeof this.props.onChange === 'function') {
this.props.onChange(!!switchElement, currentPath);
Expand Down
22 changes: 11 additions & 11 deletions test/Switcher_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,42 +97,42 @@ describe('Switcher', function() {
});

it('gets component with matching path', function() {
var swtch = this.switcher.getSwitch('/another');
var swtch = this.switcher.getSwitch('/another', this.switcher.props);
assert.equal(swtch.props.children, 'Another');
});

it('handles trailing /', function() {
var swtch = this.switcher.getSwitch('/another/');
var swtch = this.switcher.getSwitch('/another/', this.switcher.props);
assert.equal(swtch.props.children, 'Another');
});

it('returns null if there is no matching switch', function() {
var swtch = this.switcher.getSwitch('/notHere');
var swtch = this.switcher.getSwitch('/notHere', this.switcher.props);
assert.isNull(swtch);
});

it('gets first match if duplicate paths', function() {
var swtch = this.switcher.getSwitch('/duplicate');
var swtch = this.switcher.getSwitch('/duplicate', this.switcher.props);
assert.equal(swtch.props.children, 'Dup 1');
});

it('handles paths with wild cards', function() {
var swtch = this.switcher.getSwitch('/wildCardPath/something');
var swtch2 = this.switcher.getSwitch('/wildCardPath/something/more');
var swtch = this.switcher.getSwitch('/wildCardPath/something', this.switcher.props);
var swtch2 = this.switcher.getSwitch('/wildCardPath/something/more', this.switcher.props);
assert.equal(swtch.props.children, 'Wild');
assert.equal(swtch2.props.children, 'Wild');
});

it('handles paths with dynamic segments', function() {
var swtch = this.switcher.getSwitch('/path/abc123/more');
var swtch2 = this.switcher.getSwitch('/path/somethingelse/more');
var swtch = this.switcher.getSwitch('/path/abc123/more', this.switcher.props);
var swtch2 = this.switcher.getSwitch('/path/somethingelse/more', this.switcher.props);
assert.equal(swtch.props.children, 'Dynamic');
assert.equal(swtch2.props.children, 'Dynamic');
});

it('handles array of paths', function() {
var swtch = this.switcher.getSwitch('/arr1');
var swtch2 = this.switcher.getSwitch('/arr2/more');
var swtch = this.switcher.getSwitch('/arr1', this.switcher.props);
var swtch2 = this.switcher.getSwitch('/arr2/more', this.switcher.props);
assert.equal(swtch.props.children, 'Array');
assert.equal(swtch2.props.children, 'Array');
});
Expand All @@ -156,7 +156,7 @@ describe('Switcher', function() {
});

it('gets component with matching path', function() {
var swtch = this.switcher.getSwitch('/base/another');
var swtch = this.switcher.getSwitch('/base/another', this.switcher.props);
assert.equal(swtch.props.children, 'Another');
});
});
Expand Down

0 comments on commit dc94afc

Please sign in to comment.