Skip to content

[added] wraptag property for Link #816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions modules/components/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ function isModifiedEvent(event) {

/**
* <Link> components are used to create an <a> element that links to a route.
* When that route is active, the link gets an "active" class name (or the
* You can also ask it to be wrapped in a tag by supplying a `wraptag` prop.
* When the route is active, the link gets an "active" class name (or the
* value of its `activeClassName` prop).
*
* For example, assuming you have the following route:
Expand All @@ -42,7 +43,13 @@ var Link = React.createClass({
to: PropTypes.string.isRequired,
params: PropTypes.object,
query: PropTypes.object,
onClick: PropTypes.func
onClick: PropTypes.func,
wraptag: function(props){
var tag = props.wraptag;
if (tag && !React.DOM[tag]){
return new Error('Wraptag must be a valid DOM tag, but was "'+tag+'"!');
}
}
},

getDefaultProps: function () {
Expand Down Expand Up @@ -94,13 +101,14 @@ var Link = React.createClass({
},

render: function () {
var wraptag = this.props.wraptag;
var props = assign({}, this.props, {
href: this.getHref(),
className: this.getClassName(),
className: wraptag ? "" : this.getClassName(),
onClick: this.handleClick
});

return React.DOM.a(props, this.props.children);
var link = React.DOM.a(props, this.props.children);
return wraptag ? React.DOM[wraptag]({className: this.getClassName()},link) : link;
}

});
Expand Down
39 changes: 39 additions & 0 deletions modules/components/__tests__/Link-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var { Foo, Bar } = require('../../utils/TestHandlers');
var { click } = React.addons.TestUtils.Simulate;

describe('A Link', function () {

describe('with params and a query', function () {
it('knows how to make its href', function () {
var LinkHandler = React.createClass({
Expand Down Expand Up @@ -96,6 +97,44 @@ describe('A Link', function () {
});
});

describe('when given a wraptag', function () {
it('renders inside that tag, and wraptag gets the classnames', function (done) {
var LinkHandler = React.createClass({
render: function () {
return (
<div>
<Link
to="foo"
className="dontKillMe"
activeClassName="highlight"
wraptag="li"
>Link</Link>
<RouteHandler/>
</div>
);
}
});

var routes = (
<Route path="/" handler={LinkHandler}>
<Route name="foo" handler={Foo} />
</Route>
);

var div = document.createElement('div');

TestLocation.history = ['/foo'];

Router.run(routes, TestLocation, function (Handler) {
React.render(<Handler/>, div, function(){
var li = div.querySelector('li');
expect(li.className.split(' ').sort().join(' ')).toEqual('dontKillMe highlight');
done();
});
});
});
});

describe('when clicked', function () {
it('calls a user defined click handler', function (done) {
var LinkHandler = React.createClass({
Expand Down