Skip to content

Commit bf45f06

Browse files
committed
Treeview fixes (very un-optimised)
1 parent 628412a commit bf45f06

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33

4+
import memoize from 'memoizerific';
5+
46
import { TreeState } from '../treeview/treeview';
57
import Placeholder from '../placeholder/placeholder';
8+
import { getParents } from '../treeview/utils';
9+
10+
const toSelected = memoize(50)((id, stories) =>
11+
Object.keys(stories).reduce((acc, k) => Object.assign(acc, { [k]: k === id }), {})
12+
);
13+
const toExpanded = memoize(50)((selectedIds, stories) => {
14+
const parents = selectedIds.reduce(
15+
(acc, id) => acc.concat(getParents({ path: id, dataset: stories }).map(i => i.path)),
16+
[]
17+
);
18+
return Object.keys(stories).reduce(
19+
(acc, k) => Object.assign(acc, { [k]: parents.includes(k) }),
20+
{}
21+
);
22+
});
623

724
// This component gets a ref so it needs to be a class
825
// eslint-disable-next-line react/prefer-stateless-function
926
class Explorer extends Component {
1027
render() {
11-
const { stories, ...rest } = this.props;
28+
const { stories, componentId, ...rest } = this.props;
1229
const list = Object.entries(stories);
30+
const selected = toSelected(componentId, stories);
31+
const expanded = toExpanded(
32+
Object.entries(selected).reduce((acc, [k, v]) => (v ? acc.concat(k) : acc), []),
33+
stories
34+
);
1335

1436
return list.length ? (
1537
<TreeState
1638
dataset={stories}
1739
prefix="explorer"
1840
{...rest}
19-
selected={{}}
20-
expanded={{}}
41+
selected={selected}
42+
expanded={expanded}
2143
filter=""
2244
/>
2345
) : (
@@ -28,6 +50,7 @@ class Explorer extends Component {
2850

2951
Explorer.propTypes = {
3052
stories: PropTypes.shape({}).isRequired,
53+
componentId: PropTypes.string,
3154
};
3255

3356
export { Explorer };

lib/components/src/treeview/treeview.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,16 @@ class TreeState extends PureComponent {
273273

274274
render() {
275275
const { Head, Leaf, Branch, Filter, RootTitle, events } = this;
276-
const { dataset, selected, expanded } = this.state;
276+
const { dataset } = this.state;
277+
278+
const expanded = Object.keys(dataset).reduce(
279+
(acc, k) => Object.assign(acc, { [k]: this.props.expanded[k] || this.state.expanded[k] }),
280+
{}
281+
);
282+
const selected = Object.keys(dataset).reduce(
283+
(acc, k) => Object.assign(acc, { [k]: this.props.selected[k] || this.state.selected[k] }),
284+
{}
285+
);
277286

278287
const mains = getMains({ dataset });
279288
const { roots, others } = mains.reduce(

lib/ui/src/components/nav/explorer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const LeafLink = ({ id, href, children, className, ...rest }) => (
2222
</Router.Location>
2323
);
2424

25-
const StoriesPanel = ({ stories }) => <Explorer allowClick stories={stories} Link={LeafLink} />;
25+
const StoriesPanel = props => <Explorer allowClick {...props} Link={LeafLink} />;
2626
StoriesPanel.propTypes = {
2727
stories: PropTypes.shape({}).isRequired,
2828
};

lib/ui/src/components/nav/nav.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ const Brand = withCSSContext(({ title, url }, { theme: { brand: UserBrand } }) =
7878
));
7979
Brand.displayName = 'Brand';
8080

81-
const Nav = ({ title, url, notifications = [], stories, menu: items }) => (
81+
const Nav = ({ title, url, componentId, notifications = [], stories, menu: items }) => (
8282
<Container>
8383
<Inner>
8484
<Main>
8585
<Head>
8686
<Brand title={title} url={url} />
8787
<MenuToggle id="storybook-explorer-menu">{createMenu(items)}</MenuToggle>
8888
</Head>
89-
<Explorer stories={stories} />
89+
<Explorer stories={stories} componentId={componentId} />
9090
</Main>
9191
<Foot>
9292
{notifications.map(({ id }) => (
@@ -130,6 +130,7 @@ Nav.propTypes = {
130130
title: PropTypes.string.isRequired,
131131
url: PropTypes.string.isRequired,
132132
stories: PropTypes.shape({}).isRequired,
133+
componentId: PropTypes.string,
133134
notifications: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
134135
menu: PropTypes.arrayOf(
135136
PropTypes.shape({

lib/ui/src/containers/nav.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export const mapper = ({ state, api }) => {
1212
const {
1313
uiOptions: { name, url },
1414
notifications,
15+
viewMode,
16+
componentId,
1517
ui: { isFullscreen, showPanel, showNav, panelPosition },
1618
} = state;
1719
const shortcutKeys = get('shortcutKeys') || defaultKeyboardShortcuts;
@@ -21,6 +23,8 @@ export const mapper = ({ state, api }) => {
2123
url,
2224
notifications,
2325
stories: state.storiesHash,
26+
componentId,
27+
viewMode,
2428
menu: [
2529
{
2630
id: 'about',

0 commit comments

Comments
 (0)