Skip to content

Commit e1622f8

Browse files
committed
Refactor: Replace isolated* flags with displayMode enum
1 parent 32db130 commit e1622f8

File tree

12 files changed

+112
-65
lines changed

12 files changed

+112
-65
lines changed

src/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,32 @@ function renderStyleguide() {
3535
targetIndex,
3636
} = getInfoFromHash();
3737

38-
let isolatedComponent = false;
39-
let isolatedExample = false;
40-
let isolatedSection = false;
38+
// all: show all section and components (default)
39+
// section: show one section
40+
// component: show one component
41+
// example: show one example
42+
let displayMode = 'all';
4143

4244
// Filter the requested component if required
4345
if (targetName) {
4446
const filteredComponents = filterComponentsInSectionsByExactName(sections, targetName);
4547
if (filteredComponents.length) {
4648
sections = [{ components: filteredComponents }];
47-
isolatedComponent = true;
49+
displayMode = 'component';
4850
} else {
4951
const section = findSection(sections, targetName);
5052
sections = section ? [section] : [];
51-
isolatedSection = true;
53+
displayMode = 'section';
5254
}
5355

5456
// If a single component or section is filtered and a fenced block index is specified hide all other examples
5557
if (isFinite(targetIndex)) {
5658
if (filteredComponents.length === 1) {
5759
filteredComponents[0] = filterComponentExamples(filteredComponents[0], targetIndex);
58-
isolatedExample = true;
60+
displayMode = 'example';
5961
} else if (sections.length === 1) {
6062
sections[0] = filterSectionExamples(sections[0], targetIndex);
61-
isolatedExample = true;
63+
displayMode = 'example';
6264
}
6365
}
6466
}
@@ -68,9 +70,9 @@ function renderStyleguide() {
6870
sections = setSlugs(sections);
6971

7072
let documentTitle = styleguide.config.title;
71-
if (isolatedComponent || isolatedExample) {
73+
if (displayMode === 'component' || displayMode === 'example') {
7274
documentTitle = sections[0].components[0].name + ' — ' + documentTitle;
73-
} else if (isolatedSection) {
75+
} else if (displayMode === 'section') {
7476
documentTitle = sections[0].name + ' — ' + documentTitle;
7577
}
7678
document.title = documentTitle;
@@ -91,9 +93,7 @@ function renderStyleguide() {
9193
welcomeScreen={styleguide.welcomeScreen}
9294
patterns={styleguide.patterns}
9395
sections={sections}
94-
isolatedComponent={isolatedComponent}
95-
isolatedExample={isolatedExample}
96-
isolatedSection={isolatedSection}
96+
displayMode={displayMode}
9797
/>,
9898
document.getElementById('app')
9999
);

src/rsg-components/Examples/Examples.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ it('should render examples', () => {
1818
const actual = shallow(<Examples examples={examples} name="button" />, {
1919
context: {
2020
codeRevision: 1,
21-
isolatedExample: false,
21+
displayMode: 'example',
2222
},
2323
});
2424

src/rsg-components/Playground/Playground.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class Playground extends Component {
1818

1919
static contextTypes = {
2020
config: PropTypes.object.isRequired,
21-
isolatedExample: PropTypes.bool,
21+
displayMode: PropTypes.string,
2222
};
2323

2424
constructor(props, context) {
@@ -63,7 +63,7 @@ export default class Playground extends Component {
6363
render() {
6464
const { code, activeTab } = this.state;
6565
const { evalInContext, index, name, settings } = this.props;
66-
const { isolatedExample } = this.context;
66+
const { displayMode } = this.context;
6767
const preview = <Preview code={code} evalInContext={evalInContext} />;
6868
if (settings.noeditor) {
6969
return <Para>{preview}</Para>;
@@ -89,7 +89,10 @@ export default class Playground extends Component {
8989
/>
9090
}
9191
toolbar={
92-
<Slot name="exampleToolbar" props={{ name, isolated: isolatedExample, example: index }} />
92+
<Slot
93+
name="exampleToolbar"
94+
props={{ name, isolated: displayMode === 'example', example: index }}
95+
/>
9396
}
9497
/>
9598
);

src/rsg-components/Playground/__snapshots__/Playground.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ exports[`should render component renderer 1`] = `
7777
props={
7878
Object {
7979
"example": 0,
80-
"isolated": undefined,
80+
"isolated": false,
8181
"name": "name",
8282
}
8383
}

src/rsg-components/ReactComponent/ReactComponent.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default class ReactComponent extends Component {
2020
};
2121
static contextTypes = {
2222
config: PropTypes.object.isRequired,
23-
isolatedComponent: PropTypes.bool,
23+
displayMode: PropTypes.string,
2424
};
2525

2626
constructor(props, context) {
@@ -42,7 +42,7 @@ export default class ReactComponent extends Component {
4242

4343
render() {
4444
const { activeTab } = this.state;
45-
const { isolatedComponent } = this.context;
45+
const { displayMode } = this.context;
4646
const { component, depth } = this.props;
4747
const { name, slug, pathLine } = component;
4848
const { description, examples = [], tags = {} } = component.props;
@@ -64,7 +64,7 @@ export default class ReactComponent extends Component {
6464
slotName="componentToolbar"
6565
slotProps={{
6666
...component,
67-
isolated: isolatedComponent,
67+
isolated: displayMode !== 'all',
6868
}}
6969
depth={depth}
7070
>

src/rsg-components/ReactComponent/ReactComponent.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const options = {
88
config: {
99
showUsage: false,
1010
},
11+
displayMode: 'all',
1112
slots,
1213
},
1314
metadata: {},
@@ -133,7 +134,7 @@ describe('ReactComponent', () => {
133134
const actual = shallow(<ReactComponent component={component} depth={3} />, {
134135
context: {
135136
...options.context,
136-
isolatedComponent: true,
137+
displayMode: 'component',
137138
},
138139
});
139140

src/rsg-components/ReactComponent/__snapshots__/ReactComponent.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ exports[`ReactComponent should pass rendered description, usage, examples, etc.
3434
slotName="componentToolbar"
3535
slotProps={
3636
Object {
37-
"isolated": undefined,
37+
"isolated": false,
3838
"metadata": Object {
3939
"tags": Array [
4040
"one",

src/rsg-components/Section/Section.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Components from 'rsg-components/Components';
55
import Sections from 'rsg-components/Sections';
66
import SectionRenderer from 'rsg-components/Section/SectionRenderer';
77

8-
export default function Section({ section, depth }, { isolatedSection = false }) {
8+
export default function Section({ section, depth }, { displayMode }) {
99
const { name, slug, content, components, sections } = section;
1010

1111
const contentJsx = content && <Examples examples={content} name={name} />;
@@ -19,7 +19,7 @@ export default function Section({ section, depth }, { isolatedSection = false })
1919
content={contentJsx}
2020
components={componentsJsx}
2121
sections={sectionsJsx}
22-
isolated={isolatedSection}
22+
isolated={displayMode !== 'all'}
2323
depth={depth}
2424
/>
2525
);
@@ -31,5 +31,5 @@ Section.propTypes = {
3131
};
3232

3333
Section.contextTypes = {
34-
isolatedSection: PropTypes.bool,
34+
displayMode: PropTypes.string,
3535
};

src/rsg-components/Section/Section.spec.js

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import Sections from '../Sections';
66
import Section from './Section';
77
import { SectionRenderer } from './SectionRenderer';
88

9+
const options = {
10+
context: {
11+
displayMode: 'all',
12+
},
13+
};
14+
915
const section = {
1016
name: 'Foo',
1117
slug: 'foo',
@@ -24,8 +30,8 @@ const section = {
2430
sections: [],
2531
};
2632

27-
it('should render component renderer', () => {
28-
const actual = shallow(<Section section={section} depth={3} />);
33+
it('should render section renderer', () => {
34+
const actual = shallow(<Section section={section} depth={3} />, options);
2935

3036
expect(actual).toMatchSnapshot();
3137
});
@@ -39,7 +45,8 @@ it('should render components list', () => {
3945
components: [],
4046
}}
4147
depth={3}
42-
/>
48+
/>,
49+
options
4350
);
4451

4552
expect(actual).toMatchSnapshot();
@@ -53,7 +60,8 @@ it('should not render components list if not defined', () => {
5360
slug: 'no-components',
5461
}}
5562
depth={3}
56-
/>
63+
/>,
64+
options
5765
);
5866

5967
expect(actual).toMatchSnapshot();
@@ -68,7 +76,8 @@ it('should render sections if defined', () => {
6876
sections: [],
6977
}}
7078
depth={3}
71-
/>
79+
/>,
80+
options
7281
);
7382

7483
expect(actual).toMatchSnapshot();
@@ -82,13 +91,49 @@ it('should not render sections if not defined', () => {
8291
slug: 'no-sections',
8392
}}
8493
depth={3}
85-
/>
94+
/>,
95+
options
8696
);
8797

8898
expect(actual).toMatchSnapshot();
8999
});
90100

91-
it('render should render component', () => {
101+
test('should not render section in isolation mode by default', () => {
102+
const actual = shallow(
103+
<Section
104+
section={{
105+
name: 'A',
106+
slug: 'a',
107+
}}
108+
depth={3}
109+
/>,
110+
options
111+
);
112+
113+
expect(actual.prop('isolated')).toBeFalsy();
114+
});
115+
116+
test('should render section in isolation mode', () => {
117+
const actual = shallow(
118+
<Section
119+
section={{
120+
name: 'A',
121+
slug: 'a',
122+
}}
123+
depth={3}
124+
/>,
125+
{
126+
context: {
127+
...options.context,
128+
displayMode: 'section',
129+
},
130+
}
131+
);
132+
133+
expect(actual.prop('isolated')).toBeTruthy();
134+
});
135+
136+
it('render should render section', () => {
92137
const actual = shallow(
93138
<SectionRenderer
94139
classes={{}}
@@ -98,20 +143,24 @@ it('render should render component', () => {
98143
components={<Components components={[]} depth={3} />}
99144
sections={<Sections sections={[]} depth={3} />}
100145
depth={3}
101-
/>
146+
/>,
147+
options
102148
);
103149

104150
expect(actual).toMatchSnapshot();
105151
});
106152

107153
it('render should not render title if name is not set', () => {
108-
const actual = shallow(<SectionRenderer classes={{}} depth={3} />);
154+
const actual = shallow(<SectionRenderer classes={{}} depth={3} />, options);
109155

110156
expect(actual).toMatchSnapshot();
111157
});
112158

113159
it('render should render title if name is set', () => {
114-
const actual = shallow(<SectionRenderer classes={{}} name="test" slug="test" depth={3} />);
160+
const actual = shallow(
161+
<SectionRenderer classes={{}} name="test" slug="test" depth={3} />,
162+
options
163+
);
115164

116165
expect(actual).toMatchSnapshot();
117166
});

src/rsg-components/Section/__snapshots__/Section.spec.js.snap

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`render should not render title if name is not set 1`] = `<section />`;
44

5-
exports[`render should render component 1`] = `
5+
exports[`render should render section 1`] = `
66
<section>
77
<SectionHeading
88
depth={3}
@@ -108,7 +108,22 @@ exports[`should not render sections if not defined 1`] = `
108108
/>
109109
`;
110110

111-
exports[`should render component renderer 1`] = `
111+
exports[`should render components list 1`] = `
112+
<Styled(Section)
113+
components={
114+
<Components
115+
components={Array []}
116+
depth={4}
117+
/>
118+
}
119+
depth={3}
120+
isolated={false}
121+
name="Components"
122+
slug="components"
123+
/>
124+
`;
125+
126+
exports[`should render section renderer 1`] = `
112127
<Styled(Section)
113128
components={
114129
<Components
@@ -147,21 +162,6 @@ exports[`should render component renderer 1`] = `
147162
/>
148163
`;
149164

150-
exports[`should render components list 1`] = `
151-
<Styled(Section)
152-
components={
153-
<Components
154-
components={Array []}
155-
depth={4}
156-
/>
157-
}
158-
depth={3}
159-
isolated={false}
160-
name="Components"
161-
slug="components"
162-
/>
163-
`;
164-
165165
exports[`should render sections if defined 1`] = `
166166
<Styled(Section)
167167
depth={3}

0 commit comments

Comments
 (0)