Files
freeCodeCamp/common/app/components/Nav/NavItem.jsx

92 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-06-29 09:50:25 -07:00
import React from 'react';
import classNames from 'classnames';
2015-06-04 10:53:01 -07:00
export default React.createClass({
displayName: 'FCCNavItem',
2015-10-27 15:40:04 -07:00
propTypes: {
2015-06-04 10:53:01 -07:00
active: React.PropTypes.bool,
'aria-controls': React.PropTypes.string,
2015-10-27 15:40:04 -07:00
children: React.PropTypes.node,
className: React.PropTypes.string,
2015-06-04 10:53:01 -07:00
disabled: React.PropTypes.bool,
eventKey: React.PropTypes.any,
href: React.PropTypes.string,
linkId: React.PropTypes.string,
onSelect: React.PropTypes.func,
role: React.PropTypes.string,
target: React.PropTypes.string,
title: React.PropTypes.node
},
getDefaultProps() {
return {
href: '#'
};
},
2015-06-04 10:53:01 -07:00
2015-06-29 09:50:25 -07:00
handleClick(e) {
if (this.props.onSelect) {
e.preventDefault();
if (!this.props.disabled) {
2015-10-27 15:40:04 -07:00
this.props.onSelect(
this.props.eventKey,
this.props.href,
this.props.target
);
2015-06-29 09:50:25 -07:00
}
}
},
2015-06-04 10:53:01 -07:00
2015-06-29 09:50:25 -07:00
render() {
let {
role,
linkId,
2015-06-29 09:50:25 -07:00
disabled,
active,
href,
title,
target,
children,
'aria-controls': ariaControls, // eslint-disable-line react/prop-types
className,
...props
2015-06-29 09:50:25 -07:00
} = this.props;
2015-10-27 15:40:04 -07:00
const linkClassName = classNames(className, {
// 'active': active, we don't actually use the active class
// but it is used for a11y below
'disabled': disabled
});
let linkProps = {
role,
href,
title,
target,
id: linkId,
onClick: this.handleClick,
ref: 'anchor'
2015-06-29 09:50:25 -07:00
};
2015-06-04 10:53:01 -07:00
if (!role && href === '#') {
linkProps.role = 'button';
}
2015-06-04 10:53:01 -07:00
return (
2015-06-29 09:50:25 -07:00
<li
{...props}
role='presentation'>
2015-06-04 10:53:01 -07:00
<a
{ ...linkProps }
aria-controls={ ariaControls }
2015-10-27 15:40:04 -07:00
aria-selected={ active }
className={ linkClassName }>
2015-06-04 10:53:01 -07:00
{ children }
</a>
</li>
);
}
});