Skip to content

Commit b4967b9

Browse files
authored
Merge pull request #13089 from storybookjs/13082-fix-no-parent
UI: Fix single story hoisting at the root
2 parents efa66e3 + 1a4efe1 commit b4967b9

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

lib/ui/src/components/sidebar/Tree.stories.tsx

+31-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,33 @@ export const Full = () => {
3434
);
3535
};
3636

37+
const singleStoryComponent = {
38+
single: {
39+
name: 'Single',
40+
id: 'single',
41+
parent: false,
42+
depth: 0,
43+
children: ['single--single'],
44+
isComponent: true,
45+
isLeaf: false,
46+
isRoot: false,
47+
},
48+
'single--single': {
49+
id: 'single--single',
50+
kind: 'Single',
51+
name: 'Single',
52+
story: 'Single',
53+
args: {},
54+
argTypes: {},
55+
initialArgs: {},
56+
depth: 1,
57+
parent: 'single',
58+
isLeaf: true,
59+
isComponent: false,
60+
isRoot: false,
61+
},
62+
};
63+
3764
const tooltipStories = Object.keys(stories).reduce((acc, key) => {
3865
if (key === 'tooltip-tooltipselect--default') {
3966
acc['tooltip-tooltipselect--tooltipselect'] = {
@@ -51,15 +78,15 @@ const tooltipStories = Object.keys(stories).reduce((acc, key) => {
5178
return acc;
5279
}, {} as StoriesHash);
5380

54-
export const SingleStoryComponent = () => {
55-
const [selectedId, setSelectedId] = React.useState('tooltip-tooltipselect--tooltipselect');
81+
export const SingleStoryComponents = () => {
82+
const [selectedId, setSelectedId] = React.useState('tooltip-tooltipbuildlist--default');
5683
return (
5784
<Tree
5885
isBrowsing
5986
isMain
6087
refId={refId}
61-
data={tooltipStories}
62-
highlightedItemId="tooltip-tooltipselect--tooltipselect"
88+
data={{ ...singleStoryComponent, ...tooltipStories } as StoriesHash}
89+
highlightedItemId="tooltip-tooltipbuildlist--default"
6390
setHighlightedItemId={log}
6491
selectedStoryId={selectedId}
6592
onSelectStoryId={setSelectedId}

lib/ui/src/components/sidebar/Tree.tsx

+10-9
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export const Tree = React.memo<{
225225
}, [data, rootIds, orphanIds]);
226226

227227
// Create a list of component IDs which have exactly one story, which name exactly matches the component name.
228-
const singleStoryComponents = useMemo(() => {
228+
const singleStoryComponentIds = useMemo(() => {
229229
return orphansFirst.filter((nodeId) => {
230230
const { children = [], isComponent, isLeaf, name } = data[nodeId];
231231
return (
@@ -239,20 +239,21 @@ export const Tree = React.memo<{
239239
}, [data, orphansFirst]);
240240

241241
// Omit single-story components from the list of nodes.
242-
const collapsedItems = useMemo(
243-
() => orphansFirst.filter((id) => !singleStoryComponents.includes(id)),
244-
[orphansFirst, singleStoryComponents]
245-
);
242+
const collapsedItems = useMemo(() => {
243+
return orphansFirst.filter((id) => !singleStoryComponentIds.includes(id));
244+
}, [orphanIds, orphansFirst, singleStoryComponentIds]);
246245

247246
// Rewrite the dataset to place the child story in place of the component.
248247
const collapsedData = useMemo(() => {
249-
return singleStoryComponents.reduce(
248+
return singleStoryComponentIds.reduce(
250249
(acc, id) => {
251250
const { children, parent } = data[id] as Group;
252251
const [childId] = children;
253-
const siblings = [...data[parent].children];
254-
siblings[siblings.indexOf(id)] = childId;
255-
acc[parent] = { ...data[parent], children: siblings };
252+
if (parent) {
253+
const siblings = [...data[parent].children];
254+
siblings[siblings.indexOf(id)] = childId;
255+
acc[parent] = { ...data[parent], children: siblings };
256+
}
256257
acc[childId] = { ...data[childId], parent, depth: data[childId].depth - 1 } as Story;
257258
return acc;
258259
},

0 commit comments

Comments
 (0)