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 expand and allow custom root section options #1689

Merged
merged 6 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/Components.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Each section consists of (all fields are optional):
- `ignore` — string/array of globs that should not be included in the section.
- `href` - an URL to navigate to instead of navigating to the section content
- `external` - if set, the link will open in a new window
- `expand` - Determines if the section should be expanded by default even when `tocMode` is set to `collapse` in general settings

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

Expand Down
28 changes: 28 additions & 0 deletions src/client/rsg-components/TableOfContents/TableOfContents.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,31 @@ it('should detect sections containing current selection when tocMode is collapse

expect(getByText('1.1')).not.toBeEmpty();
});

it('should show sections with expand: true when tocMode is collapse', () => {
const { getByText } = render(
<TableOfContents
tocMode="collapse"
sections={[
{
sections: [
{
visibleName: '1',
expand: true,
href: '#/components',
sections: [{ visibleName: '1.1', href: '#/button' }],
},
{
visibleName: '2',
href: '#/chap',
content: 'chapter.md',
sections: [{ visibleName: '2.1', href: '#/chapter-1' }],
},
{ visibleName: '3', href: 'http://react-styleguidist.com' },
],
},
]}
/>
);
expect(getByText('1.1')).toBeVisible();
});
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class TableOfContents extends Component<TableOfContentsProps> {
content,
selected,
shouldOpenInNewTab: !!section.external && !!section.externalLink,
initialOpen: this.props.tocMode !== 'collapse' || containsSelected,
initialOpen: this.props.tocMode !== 'collapse' || containsSelected || section.expand,
forcedOpen: !!this.state.searchTerm.length,
};
});
Expand Down
18 changes: 2 additions & 16 deletions src/loaders/utils/__tests__/__snapshots__/getSections.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ Array [
"content": Object {
"require": "!!~/src/loaders/examples-loader.js!~/test/components/Button/Readme.md",
},
"description": undefined,
"exampleMode": "collapse",
"external": undefined,
"href": undefined,
"name": "Readme",
"sectionDepth": 0,
Expand Down Expand Up @@ -88,9 +86,7 @@ Array [
},
],
"content": undefined,
"description": undefined,
"exampleMode": "collapse",
"external": undefined,
"href": undefined,
"name": "Components",
"sectionDepth": 0,
Expand Down Expand Up @@ -169,10 +165,9 @@ Array [
},
],
"content": undefined,
"description": undefined,
"exampleMode": "collapse",
"external": undefined,
"href": undefined,
"ignore": "**/components/Annotation/*",
"name": "Ignore",
"sectionDepth": 0,
"sections": Array [],
Expand All @@ -185,9 +180,7 @@ Array [
"content": "Hello World",
"type": "markdown",
},
"description": undefined,
"exampleMode": "collapse",
"external": undefined,
"href": undefined,
"name": "Ignore",
"sectionDepth": 0,
Expand Down Expand Up @@ -270,9 +263,7 @@ Object {
},
],
"content": undefined,
"description": undefined,
"exampleMode": "collapse",
"external": undefined,
"href": undefined,
"name": "Components",
"sectionDepth": 0,
Expand All @@ -288,9 +279,7 @@ Object {
"content": Object {
"require": "!!~/src/loaders/examples-loader.js!~/test/components/Button/Readme.md",
},
"description": undefined,
"exampleMode": "collapse",
"external": undefined,
"href": undefined,
"name": "Readme",
"sectionDepth": 0,
Expand All @@ -307,9 +296,7 @@ Object {
"content": "Hello World",
"type": "markdown",
},
"description": undefined,
"exampleMode": "collapse",
"external": undefined,
"href": undefined,
"name": "Ignore",
"sectionDepth": 0,
Expand Down Expand Up @@ -391,10 +378,9 @@ Object {
},
],
"content": undefined,
"description": undefined,
"exampleMode": "collapse",
"external": undefined,
"href": undefined,
"ignore": "**/components/Annotation/*",
"name": "Ignore",
"sectionDepth": 0,
"sections": Array [],
Expand Down
7 changes: 7 additions & 0 deletions src/loaders/utils/__tests__/getSections.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const sectionsWithDepth = [
},
{
name: 'Components',
expand: true,
sections: [
{
name: 'Buttons',
Expand Down Expand Up @@ -148,6 +149,12 @@ it('getSections() should return an array of sectionsWithDepth with sectionDepth
]);
});

it('getSections() should make custom options by user available', () => {
const result = getSections(sectionsWithDepth, config);
const expandSection = result.find(section => section.name === 'Components');
expect(expandSection).toHaveProperty('expand');
});

it('getSections() should return an array of sectionsWithBadDepth taking the sectionDepth of the first depth of the sections', () => {
const result = getSections(sectionsWithBadDepth, config);

Expand Down
4 changes: 1 addition & 3 deletions src/loaders/utils/getSections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,14 @@ export function processSection(
}

return {
name: section.name,
...section,
exampleMode: section.exampleMode || config.exampleMode,
usageMode: section.usageMode || config.usageMode,
sectionDepth,
description: section.description,
slug: `section-${slugger.slug(section.name || 'untitled')}`,
sections: getSections(section.sections || [], config, sectionDepth),
href: section.href,
components: getSectionComponents(section, config),
content,
external: section.external,
};
}
1 change: 1 addition & 0 deletions src/typings/RsgSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface BaseSection {
href?: string;
sectionDepth?: number;
external?: boolean;
expand?: boolean;
}

export interface ProcessedSection extends BaseSection {
Expand Down