Skip to content

Commit d1e528f

Browse files
Juha Karttunensapegin
Juha Karttunen
authored andcommitted
Fix: Highlighting currently selected page on sidebar broken with sections (styleguidist#1405)
Fixes styleguidist#1375
1 parent c3731c7 commit d1e528f

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/client/rsg-components/ComponentsList/ComponentsList.spec.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import React from 'react';
22
import ComponentsList from './ComponentsList';
33
import { ComponentsListRenderer } from './ComponentsListRenderer';
44

5+
const context = {
6+
config: {
7+
pagePerSection: true,
8+
},
9+
};
10+
511
it('should set the correct href for items', () => {
612
const components = [
713
{
@@ -16,7 +22,7 @@ it('should set the correct href for items', () => {
1622
},
1723
];
1824

19-
const actual = shallow(<ComponentsList items={components} classes={{}} />);
25+
const actual = shallow(<ComponentsList items={components} classes={{}} />, { context });
2026
expect(actual).toMatchSnapshot();
2127
});
2228

@@ -34,7 +40,7 @@ it('if a custom href is provided, should use it instead of generating internal l
3440
},
3541
];
3642

37-
const actual = shallow(<ComponentsList items={components} classes={{}} />);
43+
const actual = shallow(<ComponentsList items={components} classes={{}} />, { context });
3844
expect(actual).toMatchSnapshot();
3945
});
4046

@@ -51,15 +57,15 @@ it('should set a parameter on link when useHashId is activated', () => {
5157
slug: 'input',
5258
},
5359
];
54-
5560
const actual = shallow(
5661
<ComponentsList
5762
items={components}
5863
classes={{}}
5964
useRouterLinks
6065
hashPath={['Components']}
6166
useHashId
62-
/>
67+
/>,
68+
{ context }
6369
);
6470
expect(actual).toMatchSnapshot();
6571
});
@@ -83,7 +89,8 @@ it('should set a sub route on link when useHashId is deactivated', () => {
8389
useRouterLinks
8490
hashPath={['Components']}
8591
useHashId={false}
86-
/>
92+
/>,
93+
{ context }
8794
);
8895
expect(actual).toMatchSnapshot();
8996
});
@@ -101,13 +108,13 @@ it('should render sections with nested components', () => {
101108
href: '#input',
102109
},
103110
];
104-
const actual = shallow(<ComponentsListRenderer items={components} classes={{}} />);
111+
const actual = shallow(<ComponentsListRenderer items={components} classes={{}} />, { context });
105112

106113
expect(actual).toMatchSnapshot();
107114
});
108115

109116
it('should return null when the list is empty', () => {
110-
const actual = shallow(<ComponentsListRenderer items={[]} classes={{}} />);
117+
const actual = shallow(<ComponentsListRenderer items={[]} classes={{}} />, { context });
111118

112119
expect(actual.getElement()).toBe(null);
113120
});
@@ -124,7 +131,7 @@ it('should ignore items without visibleName', () => {
124131
href: '#input',
125132
},
126133
];
127-
const actual = shallow(<ComponentsListRenderer items={components} classes={{}} />);
134+
const actual = shallow(<ComponentsListRenderer items={components} classes={{}} />, { context });
128135

129136
expect(actual).toMatchSnapshot();
130137
});

src/client/rsg-components/ComponentsList/ComponentsListRenderer.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ const styles = ({ color, fontFamily, fontSize, space, mq }) => ({
3737
},
3838
});
3939

40-
export function ComponentsListRenderer({ classes, items }) {
40+
export function ComponentsListRenderer({ classes, items }, { config }) {
41+
const { pagePerSection } = config;
4142
items = items.filter(item => item.visibleName);
4243

4344
if (!items.length) {
4445
return null;
4546
}
4647

47-
const windowHash = window.location.pathname + getHash(window.location.hash);
48+
// Match selected component in both basic routing and pagePerSection routing.
49+
const { hash, pathname } = window.location;
50+
const windowHash = pathname + (pagePerSection ? hash : getHash(hash));
4851
return (
4952
<ul className={classes.list}>
5053
{items.map(({ heading, visibleName, href, content, shouldOpenInNewTab }) => {
@@ -77,4 +80,8 @@ ComponentsListRenderer.propTypes = {
7780
classes: PropTypes.object.isRequired,
7881
};
7982

83+
ComponentsListRenderer.contextTypes = {
84+
config: PropTypes.object.isRequired,
85+
};
86+
8087
export default Styled(styles)(ComponentsListRenderer);

0 commit comments

Comments
 (0)