Skip to content
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

Removed unnecessary className on Link #3288

Merged
merged 1 commit into from
Apr 13, 2016
Merged
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
3 changes: 1 addition & 2 deletions modules/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ const Link = React.createClass({
getDefaultProps() {
return {
onlyActiveOnIndex: false,
className: '',
style: {}
}
},
Expand Down Expand Up @@ -119,7 +118,7 @@ const Link = React.createClass({
if (activeClassName || (activeStyle != null && !isEmptyObject(activeStyle))) {
if (router.isActive(location, onlyActiveOnIndex)) {
if (activeClassName)
props.className += props.className === '' ? activeClassName : ` ${activeClassName}`
props.className = `${props.className || ''} ${activeClassName}`.trim()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any harm to just pulling in classnames here? I can't imagine anybody doesn't have classnames already installed, so this should dedupe trivially.

Otherwise IMO this is better written as:

if (activeClassName) {
  if (props.className) {
    props.className += ` ${activeClassName}`
  } else {
    props.className = activeClassName
  }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. We can do that after merge. For now, this gets the job done and adds a test so any further fixes are easier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't used a className in a while.


if (activeStyle)
props.style = { ...props.style, ...activeStyle }
Expand Down
9 changes: 9 additions & 0 deletions modules/__tests__/Link-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ describe('A <Link>', function () {
node = document.createElement('div')
})

it('should not render unnecessary class=""', function () {
render((
<Link to="/something" />
), node, function () {
const a = node.querySelector('a')
expect(a.hasAttribute('class')).toBe(false)
})
})

it('knows how to make its href', function () {
class LinkWrapper extends Component {
render() {
Expand Down