Skip to content

Commit 3ab6f8d

Browse files
authored
feat: Add expand option for sections and allow custom root section options (#1689)
1 parent 43e19df commit 3ab6f8d

File tree

7 files changed

+41
-20
lines changed

7 files changed

+41
-20
lines changed

docs/Components.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Each section consists of (all fields are optional):
106106
- `ignore` — string/array of globs that should not be included in the section.
107107
- `href` - an URL to navigate to instead of navigating to the section content
108108
- `external` - if set, the link will open in a new window
109+
- `expand` - Determines if the section should be expanded by default even when `tocMode` is set to `collapse` in general settings
109110

110111
Configuring a style guide with textual documentation section and a list of components would look like:
111112

src/client/rsg-components/TableOfContents/TableOfContents.spec.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,31 @@ it('should detect sections containing current selection when tocMode is collapse
302302

303303
expect(getByText('1.1')).not.toBeEmpty();
304304
});
305+
306+
it('should show sections with expand: true when tocMode is collapse', () => {
307+
const { getByText } = render(
308+
<TableOfContents
309+
tocMode="collapse"
310+
sections={[
311+
{
312+
sections: [
313+
{
314+
visibleName: '1',
315+
expand: true,
316+
href: '#/components',
317+
sections: [{ visibleName: '1.1', href: '#/button' }],
318+
},
319+
{
320+
visibleName: '2',
321+
href: '#/chap',
322+
content: 'chapter.md',
323+
sections: [{ visibleName: '2.1', href: '#/chapter-1' }],
324+
},
325+
{ visibleName: '3', href: 'http://react-styleguidist.com' },
326+
],
327+
},
328+
]}
329+
/>
330+
);
331+
expect(getByText('1.1')).toBeVisible();
332+
});

src/client/rsg-components/TableOfContents/TableOfContents.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default class TableOfContents extends Component<TableOfContentsProps> {
6666
content,
6767
selected,
6868
shouldOpenInNewTab: !!section.external && !!section.externalLink,
69-
initialOpen: this.props.tocMode !== 'collapse' || containsSelected,
69+
initialOpen: this.props.tocMode !== 'collapse' || containsSelected || section.expand,
7070
forcedOpen: !!this.state.searchTerm.length,
7171
};
7272
});

src/loaders/utils/__tests__/__snapshots__/getSections.spec.ts.snap

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ Array [
77
"content": Object {
88
"require": "!!~/src/loaders/examples-loader.js!~/test/components/Button/Readme.md",
99
},
10-
"description": undefined,
1110
"exampleMode": "collapse",
12-
"external": undefined,
1311
"href": undefined,
1412
"name": "Readme",
1513
"sectionDepth": 0,
@@ -88,9 +86,7 @@ Array [
8886
},
8987
],
9088
"content": undefined,
91-
"description": undefined,
9289
"exampleMode": "collapse",
93-
"external": undefined,
9490
"href": undefined,
9591
"name": "Components",
9692
"sectionDepth": 0,
@@ -169,10 +165,9 @@ Array [
169165
},
170166
],
171167
"content": undefined,
172-
"description": undefined,
173168
"exampleMode": "collapse",
174-
"external": undefined,
175169
"href": undefined,
170+
"ignore": "**/components/Annotation/*",
176171
"name": "Ignore",
177172
"sectionDepth": 0,
178173
"sections": Array [],
@@ -185,9 +180,7 @@ Array [
185180
"content": "Hello World",
186181
"type": "markdown",
187182
},
188-
"description": undefined,
189183
"exampleMode": "collapse",
190-
"external": undefined,
191184
"href": undefined,
192185
"name": "Ignore",
193186
"sectionDepth": 0,
@@ -270,9 +263,7 @@ Object {
270263
},
271264
],
272265
"content": undefined,
273-
"description": undefined,
274266
"exampleMode": "collapse",
275-
"external": undefined,
276267
"href": undefined,
277268
"name": "Components",
278269
"sectionDepth": 0,
@@ -288,9 +279,7 @@ Object {
288279
"content": Object {
289280
"require": "!!~/src/loaders/examples-loader.js!~/test/components/Button/Readme.md",
290281
},
291-
"description": undefined,
292282
"exampleMode": "collapse",
293-
"external": undefined,
294283
"href": undefined,
295284
"name": "Readme",
296285
"sectionDepth": 0,
@@ -307,9 +296,7 @@ Object {
307296
"content": "Hello World",
308297
"type": "markdown",
309298
},
310-
"description": undefined,
311299
"exampleMode": "collapse",
312-
"external": undefined,
313300
"href": undefined,
314301
"name": "Ignore",
315302
"sectionDepth": 0,
@@ -391,10 +378,9 @@ Object {
391378
},
392379
],
393380
"content": undefined,
394-
"description": undefined,
395381
"exampleMode": "collapse",
396-
"external": undefined,
397382
"href": undefined,
383+
"ignore": "**/components/Annotation/*",
398384
"name": "Ignore",
399385
"sectionDepth": 0,
400386
"sections": Array [],

src/loaders/utils/__tests__/getSections.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const sectionsWithDepth = [
4747
},
4848
{
4949
name: 'Components',
50+
expand: true,
5051
sections: [
5152
{
5253
name: 'Buttons',
@@ -148,6 +149,12 @@ it('getSections() should return an array of sectionsWithDepth with sectionDepth
148149
]);
149150
});
150151

152+
it('getSections() should make custom options by user available', () => {
153+
const result = getSections(sectionsWithDepth, config);
154+
const expandSection = result.find(section => section.name === 'Components');
155+
expect(expandSection).toHaveProperty('expand');
156+
});
157+
151158
it('getSections() should return an array of sectionsWithBadDepth taking the sectionDepth of the first depth of the sections', () => {
152159
const result = getSections(sectionsWithBadDepth, config);
153160

src/loaders/utils/getSections.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,14 @@ export function processSection(
8888
}
8989

9090
return {
91-
name: section.name,
91+
...section,
9292
exampleMode: section.exampleMode || config.exampleMode,
9393
usageMode: section.usageMode || config.usageMode,
9494
sectionDepth,
95-
description: section.description,
9695
slug: `section-${slugger.slug(section.name || 'untitled')}`,
9796
sections: getSections(section.sections || [], config, sectionDepth),
9897
href: section.href,
9998
components: getSectionComponents(section, config),
10099
content,
101-
external: section.external,
102100
};
103101
}

src/typings/RsgSection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface BaseSection {
1212
href?: string;
1313
sectionDepth?: number;
1414
external?: boolean;
15+
expand?: boolean;
1516
}
1617

1718
export interface ProcessedSection extends BaseSection {

0 commit comments

Comments
 (0)