From fd8e03eb99c6a9977b14e34e30e99cb87f490147 Mon Sep 17 00:00:00 2001 From: Eric Bednarz Date: Thu, 23 Jul 2015 15:34:36 +0200 Subject: [PATCH 1/3] Add activeElement property --- lib/NavLink.js | 11 +++++++++++ tests/unit/lib/NavLink-test.js | 11 ++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/NavLink.js b/lib/NavLink.js index 5dac755..a8cbb26 100644 --- a/lib/NavLink.js +++ b/lib/NavLink.js @@ -26,6 +26,7 @@ var NavLink = React.createClass({ executeAction: React.PropTypes.func }, propTypes: { + activeElement: React.PropTypes.string, currentRoute: React.PropTypes.object, currentNavigate: React.PropTypes.object, href: React.PropTypes.string, @@ -116,6 +117,16 @@ var NavLink = React.createClass({ className = className ? (className + ' ') : ''; className += this.props.activeClass || 'active'; style = objectAssign({}, style, this.props.activeStyle); + if (this.props.activeElement) { + return React.createElement( + this.props.activeElement, + objectAssign({}, this.props, { + className: className, + style: style + }), + this.props.children + ); + } } return React.createElement( diff --git a/tests/unit/lib/NavLink-test.js b/tests/unit/lib/NavLink-test.js index 4f6113d..4ce3ef0 100644 --- a/tests/unit/lib/NavLink-test.js +++ b/tests/unit/lib/NavLink-test.js @@ -92,7 +92,7 @@ describe('NavLink', function () { ); }).to.throw(); }); - it('should set active state if href matches current route', function () { + it('should set active state by class name if href matches current route', function () { var navParams = {a: 1, b: 2}; var link = ReactTestUtils.renderIntoDocument( @@ -101,6 +101,15 @@ describe('NavLink', function () { ); expect(link.getDOMNode().getAttribute('class')).to.equal('active'); }); + it('should set active state by tag name if the optional activeElement property is set', function () { + var navParams = {a: 1, b: 2}; + var link = ReactTestUtils.renderIntoDocument( + + + + ); + expect(link.getDOMNode().nodeName.toLowerCase()).to.equal('span'); + }); it('should set active state with custom class and style', function () { var link = ReactTestUtils.renderIntoDocument( From 9ba834f175b4cd4d7469539196b1c10f2249d90a Mon Sep 17 00:00:00 2001 From: Eric Bednarz Date: Wed, 2 Sep 2015 12:50:34 +0200 Subject: [PATCH 2/3] Add activeElement to createNavLinkComponent --- lib/createNavLinkComponent.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/createNavLinkComponent.js b/lib/createNavLinkComponent.js index 01ae341..dbea3fd 100644 --- a/lib/createNavLinkComponent.js +++ b/lib/createNavLinkComponent.js @@ -67,6 +67,7 @@ module.exports = function createNavLinkComponent (overwriteSpec) { }, _getState: function (props) { var routeStore = this.context.getStore(RouteStore); + var activeElement = props.activeElement; var href = this._getHrefFromProps(props); var className = props.className; var style = props.style; @@ -77,6 +78,7 @@ module.exports = function createNavLinkComponent (overwriteSpec) { style = objectAssign({}, style, props.activeStyle); } return { + activeElement: activeElement, href: href, isActive: isActive, className: className, @@ -157,6 +159,20 @@ module.exports = function createNavLinkComponent (overwriteSpec) { }, render: function () { this.receivedNewProps = false; + + if (this.state.isActive) { + if (this.state.activeElement) { + return React.createElement( + this.state.activeElement, + objectAssign({}, this.props, { + className: this.state.className, + style: this.state.style + }), + this.props.children + ); + } + } + return React.createElement( 'a', objectAssign({}, { From 2ea18fc47ee8c335502f3f7f7ea8a2efe03755e3 Mon Sep 17 00:00:00 2001 From: Eric Bednarz Date: Wed, 2 Sep 2015 13:35:04 +0200 Subject: [PATCH 3/3] Revert auto merge error --- tests/unit/lib/NavLink-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/lib/NavLink-test.js b/tests/unit/lib/NavLink-test.js index 0583886..26c235f 100644 --- a/tests/unit/lib/NavLink-test.js +++ b/tests/unit/lib/NavLink-test.js @@ -92,7 +92,7 @@ describe('NavLink', function () { ); }).to.throw(); }); - it('should set active state by class name if href matches current route', function () { + it('should set active state if href matches current route', function () { var navParams = {a: 1, b: 2}; var link = ReactTestUtils.renderIntoDocument(