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

Add left panel hierarchy support #1329

Merged
merged 16 commits into from
Jun 25, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
48 changes: 38 additions & 10 deletions lib/ui/example/client/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,53 @@ export default class ReactProvider extends Provider {
this.api = api;
this.api.setOptions({
name: 'REACT-STORYBOOK',
leftPanelHierarchy: true
});

// set stories
this.api.setStories([
this.api.setStories(this.createStories());

// listen to the story change and update the preview.
this.api.onStory((kind, story) => {
this.globalState.emit('change', kind, story);
});
}

createStories() {
return [
{
kind: 'Component 1',
kind: 'some.name.Component 1',
stories: ['State 1', 'State 2'],
},

{
kind: 'Component 2',
kind: 'some.name.Component 2',
stories: ['State a', 'State b'],
},
]);

// listen to the story change and update the preview.
this.api.onStory((kind, story) => {
this.globalState.emit('change', kind, story);
});
{
kind: 'some.name2.Component 3',
stories: ['State a', 'State b'],
},
{
kind: 'some.name2.Component 4',
stories: ['State a', 'State b'],
},
{
kind: 'some2.name3.Component 5',
stories: ['State a', 'State b'],
},
{
kind: 'some2.name3.Component 6',
stories: ['State a', 'State b'],
},
{
kind: 'Bla 1',
stories: ['State 1', 'State 2'],
},
{
kind: 'Bla 2',
stories: ['State 1', 'State 2'],
}
]
}

_handlePreviewEvents() {
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
{
test: /\.js$/,
loader: require.resolve('babel-loader'),
query: { presets: ['react', 'es2015', 'stage-0'] },
query: { presets: ['babel-preset-es2015', 'babel-preset-react', 'babel-preset-stage-0'].map(require.resolve) },
include: [path.join(__dirname, 'client'), path.resolve(__dirname, '../src')],
},
],
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@storybook/ui",
"version": "3.1.3",
"version": "3.1.4",
"description": "Core Storybook UI",
"license": "MIT",
"main": "dist/index.js",
Expand Down
1 change: 1 addition & 0 deletions lib/ui/src/modules/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default {
name: 'STORYBOOK',
url: 'https://github.com/storybooks/storybook',
sortStoriesByKind: false,
leftPanelHierarchy: false,
},
},
load({ clientStore, provider }, _actions) {
Expand Down
8 changes: 7 additions & 1 deletion lib/ui/src/modules/ui/components/left_panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ const mainStyle = {
padding: '10px 0 10px 10px',
};

const storyProps = ['stories', 'selectedKind', 'selectedStory', 'onSelectStory'];
const storyProps = [
'stories',
'selectedKind',
'leftPanelHierarchy',
'selectedStory',
'onSelectStory',
];

const LeftPanel = props =>
<div style={mainStyle}>
Expand Down
134 changes: 116 additions & 18 deletions lib/ui/src/modules/ui/components/left_panel/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ const listStyle = {
const listStyleType = {
listStyleType: 'none',
paddingLeft: 0,
margin: 0,
};

const kindStyle = {
fontSize: 15,
padding: '10px 0px',
cursor: 'pointer',
borderBottom: '1px solid #EEE',
};

const nestedListStyle = {
paddingLeft: '10px',
borderLeft: '1px solid #BBB',
};

const storyStyle = {
Expand All @@ -41,6 +46,61 @@ class Stories extends React.Component {
if (onSelectStory) onSelectStory(selectedKind, story);
}

fillHierarchy(namespaces, hierarchy, story) {
if (namespaces.length === 1) {
hierarchy.stories.push(story);
return;
}

const namespace = namespaces[0];
let childHierarchy = hierarchy.map.get(namespace);

if (!childHierarchy) {
childHierarchy = {
stories: [],
current: namespace,
namespace: hierarchy.namespace ? `${hierarchy.namespace}${namespace}.` : `${namespace}.`,
firstKind: story.kind,
map: new Map(),
};

hierarchy.map.set(namespace, childHierarchy);
}

this.fillHierarchy(namespaces.slice(1), childHierarchy, story);
}

createStoriesHierarchy(stories) {
const groupedStories = stories.map(story => {
const namespaces = story.kind.split('.');

return {
namespaces,
name: namespaces[namespaces.length - 1],
...story,
};
});

const hierarchyRoot = {
stories: [],
namespace: '',
current: '',
map: new Map(),
};

groupedStories.forEach(story => this.fillHierarchy(story.namespaces, hierarchyRoot, story));

return hierarchyRoot;
}

renderMenuItem(item, style, onClick, displayName) {
return (
<a title={`Open ${item}`} style={style} onClick={onClick} role="menuitem" tabIndex="0">
{displayName}
</a>
);
}

renderStory(story) {
const { selectedStory } = this.props;
const style = { display: 'block', ...storyStyle };
Expand All @@ -54,31 +114,22 @@ class Stories extends React.Component {

return (
<li key={story}>
<a
title={`Open ${story}`}
style={style}
onClick={props.onClick}
role="menuitem"
tabIndex="0"
>
{story}
</a>
{this.renderMenuItem(story, style, props.onClick, story)}
</li>
);
}

renderKind({ kind, stories }) {
renderKind({ kind, stories, name }) {
const { selectedKind } = this.props;
const style = { display: 'block', ...kindStyle };
const onClick = this.fireOnKind.bind(this, kind);
const displayName = name || kind;

if (kind === selectedKind) {
style.fontWeight = 'bold';
return (
<li key={kind}>
<a title={`Open ${kind}`} style={style} onClick={onClick} role="menuitem" tabIndex="0">
{kind}
</a>
{this.renderMenuItem(kind, style, onClick, displayName)}
<div>
<ul style={listStyleType} role="menu">
{stories.map(this.renderStory)}
Expand All @@ -90,15 +141,61 @@ class Stories extends React.Component {

return (
<li key={kind}>
<a title={`Open ${kind}`} style={style} onClick={onClick} role="menuitem" tabIndex="0">
{kind}
</a>
{this.renderMenuItem(kind, style, onClick, displayName)}
</li>
);
}

renderHierarchy({ stories, namespace, map }) {
const { selectedKind } = this.props;
const children = [];

if (stories.length) {
children.push(
<ul style={listStyleType} role="menu" key={namespace ? `${namespace}menu` : ''}>
{stories.map(this.renderKind)}
</ul>
);
}

map.forEach((value, key) => {
const style = { display: 'block', ...kindStyle };
const onClick = this.fireOnKind.bind(this, value.firstKind);
const isSelectedHierarchy = selectedKind.indexOf(value.namespace) >= 0;

if (isSelectedHierarchy) {
style.fontWeight = 'bold';
}

children.push(
<ul style={listStyleType} role="menu" key={`${value.current}_container`}>
<li key={value.current}>
{this.renderMenuItem(key, style, onClick, key)}
</li>
{isSelectedHierarchy &&
<li key={`${value.current}_children`} style={nestedListStyle}>
{this.renderHierarchy(value)}
</li>}
</ul>
);
});

return children;
}

render() {
const { stories } = this.props;
const { stories, leftPanelHierarchy } = this.props;

if (leftPanelHierarchy) {
const hierarchy = this.createStoriesHierarchy(stories);

return (
<div style={listStyle}>
{this.renderHierarchy(hierarchy)}
</div>
);
}

return (
<div style={listStyle}>
<ul style={listStyleType} role="menu">
Expand All @@ -123,6 +220,7 @@ Stories.propTypes = {
),
selectedKind: PropTypes.string.isRequired,
selectedStory: PropTypes.string.isRequired,
leftPanelHierarchy: PropTypes.bool.isRequired,
onSelectStory: PropTypes.func,
};

Expand Down
74 changes: 74 additions & 0 deletions lib/ui/src/modules/ui/components/left_panel/stories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,58 @@ describe('manager.ui.components.left_panel.stories', () => {
expect(output).toMatch(/20/);
expect(output).toMatch(/b2/);
});

test('should render stories with hierarchy - leftPanelHierarchy is true', () => {
const data = [
{ kind: 'some.name.item1', stories: ['a1', 'a2'] },
{ kind: 'another.space.20', stories: ['b1', 'b2'] },
];
const wrap = shallow(
<Stories
stories={data}
selectedKind="another.space.20"
selectedStory="b2"
leftPanelHierarchy
/>
);

const output = wrap.html();

expect(output).toMatch(/some/);
expect(output).not.toMatch(/name/);
expect(output).not.toMatch(/item1/);
expect(output).not.toMatch(/a1/);
expect(output).not.toMatch(/a2/);
expect(output).toMatch(/another/);
expect(output).toMatch(/space/);
expect(output).toMatch(/20/);
expect(output).toMatch(/b1/);
expect(output).toMatch(/b2/);
});

test('should render stories without hierarchy - leftPanelHierarchy is false', () => {
const data = [
{ kind: 'some.name.item1', stories: ['a1', 'a2'] },
{ kind: 'another.space.20', stories: ['b1', 'b2'] },
];
const wrap = shallow(
<Stories
stories={data}
selectedKind="another.space.20"
selectedStory="b2"
leftPanelHierarchy={false}
/>
);

const output = wrap.html();

expect(output).toMatch(/some.name.item1/);
expect(output).not.toMatch(/a1/);
expect(output).not.toMatch(/a2/);
expect(output).toMatch(/another.space.20/);
expect(output).toMatch(/b1/);
expect(output).toMatch(/b2/);
});
});

describe('events', () => {
Expand Down Expand Up @@ -50,5 +102,27 @@ describe('manager.ui.components.left_panel.stories', () => {

expect(onSelectStory).toHaveBeenCalledWith('b', 'b1');
});

test('should call the onSelectStory prop when a namespace is clicked - leftPanelHierarchy is true', () => {
const data = [
{ kind: 'some.name.item1', stories: ['a1', 'a2'] },
{ kind: 'another.space.20', stories: ['b1', 'b2'] },
];
const onSelectStory = jest.fn();
const wrap = shallow(
<Stories
stories={data}
selectedKind="some.name.item1"
selectedStory="a2"
onSelectStory={onSelectStory}
leftPanelHierarchy
/>
);

const kind = wrap.find('a').filterWhere(el => el.text() === 'another').last();
kind.simulate('click');

expect(onSelectStory).toHaveBeenCalledWith('another.space.20', null);
});
});
});
3 changes: 2 additions & 1 deletion lib/ui/src/modules/ui/containers/left_panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import compose from '../../../compose';
export const mapper = (state, props, { actions }) => {
const actionMap = actions();
const { stories, selectedKind, selectedStory, uiOptions, storyFilter } = state;
const { name, url, sortStoriesByKind } = uiOptions;
const { name, url, sortStoriesByKind, leftPanelHierarchy } = uiOptions;

const data = {
stories: filters.storyFilter(stories, storyFilter, selectedKind, sortStoriesByKind),
Expand All @@ -20,6 +20,7 @@ export const mapper = (state, props, { actions }) => {
openShortcutsHelp: actionMap.ui.toggleShortcutsHelp,
name,
url,
leftPanelHierarchy,
};

return data;
Expand Down