Skip to content

Commit 2d89dcf

Browse files
authored
Merge pull request #228 from v12/child-class-name
Join className from container with the one from child element
2 parents a9bae58 + 8cecff7 commit 2d89dcf

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/LinkContainer.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ export default class LinkContainer extends Component {
8888
typeof to === 'string' ? { pathname: to } : to
8989
);
9090

91+
const child = React.Children.only(children);
92+
9193
return (
9294
<Route
9395
path={typeof to === 'object' ? to.pathname : to}
@@ -97,10 +99,10 @@ export default class LinkContainer extends Component {
9799
const isActive = !!(getIsActive ? getIsActive(match, location) : match);
98100

99101
return React.cloneElement(
100-
React.Children.only(children),
102+
child,
101103
{
102104
...props,
103-
className: isActive ? [className, activeClassName].join(' ') : className,
105+
className: [className, child.props.className, isActive && activeClassName].join(' '),
104106
style: isActive ? { ...style, ...activeStyle } : style,
105107
href,
106108
onClick: this.handleClick,

test/LinkContainer.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,32 @@ describe('LinkContainer', () => {
7272
.to.equal(findDOMNode(component));
7373
});
7474

75+
it('should join child element className with the one from container', () => {
76+
function renderComponent(location) {
77+
const router = ReactTestUtils.renderIntoDocument(
78+
<Router initialEntries={[location]}>
79+
<Route
80+
path="/"
81+
render={() => (
82+
<LinkContainer to="/" className="container-css">
83+
<Component className="foo-css">Foo</Component>
84+
</LinkContainer>
85+
)}
86+
/>
87+
</Router>
88+
);
89+
90+
const component = ReactTestUtils.findRenderedComponentWithType(
91+
router, Component
92+
);
93+
return findDOMNode(component);
94+
}
95+
96+
const { className } = renderComponent('/test');
97+
98+
expect(className.trim()).to.match(/\bcontainer-css foo-css\b/);
99+
});
100+
75101
describe('when clicked', () => {
76102
it('should transition to the correct route', () => {
77103
const router = ReactTestUtils.renderIntoDocument(

0 commit comments

Comments
 (0)