Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit c890d8b

Browse files
committed
Collapsible JSON viewer for example code
If example code language is JSON, it now renders with collapsible JSON viewer
1 parent e56872d commit c890d8b

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

packages/api-explorer/__tests__/Example.test.jsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,27 @@ test('should show each example', () => {
4343
expect(example.find('pre').length).toEqual(2);
4444
});
4545

46-
test('should correctly highlight syntax', () => {
46+
test('should display json viewer', () => {
4747
const exampleOas = new Oas(exampleResults);
4848
const example = shallow(
4949
<Example {...props} oas={exampleOas} operation={exampleOas.operation('/results', 'get')} />,
5050
);
5151

52-
// Asserting all JSON keys from the example oas.json
52+
// Asserting all JSON examples are displayed with JSON viewer from the example oas.json
5353
expect(
5454
example
5555
.find('pre')
5656
.at(0)
5757
.render()
58-
.find('.cm-property').length,
59-
).toBe(3);
58+
.find('.react-json-view').length,
59+
).toBe(1);
60+
});
61+
62+
test('should correctly highlight XML syntax', () => {
63+
const exampleOas = new Oas(exampleResults);
64+
const example = shallow(
65+
<Example {...props} oas={exampleOas} operation={exampleOas.operation('/results', 'get')} />,
66+
);
6067

6168
// Asserting that there are XML tags
6269
expect(

packages/api-explorer/src/Example.jsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const React = require('react');
22
const PropTypes = require('prop-types');
3+
const ReactJson = require('react-json-view').default;
34
const showCodeResults = require('./lib/show-code-results');
45

56
// const { replaceVars } = require('./lib/replace-vars');
@@ -24,13 +25,32 @@ function Example({ operation, result, oas, selected, setExampleTab, exampleRespo
2425
<ExampleTabs examples={examples} selected={selected} setExampleTab={setExampleTab} />
2526
<div className="code-sample-body">
2627
{examples.map((example, index) => {
28+
const isJson = example.language && example.language === 'application/json';
2729
return (
2830
<pre
2931
className={`tomorrow-night tabber-body tabber-body-${index}`}
3032
style={{ display: index === selected ? 'block' : '' }}
3133
key={index} // eslint-disable-line react/no-array-index-key
3234
>
33-
{syntaxHighlighter(example.code, example.language, { dark: true })}
35+
{isJson ? (
36+
<ReactJson
37+
src={JSON.parse(example.code)}
38+
collapsed={1}
39+
collapseStringsAfterLength={100}
40+
enableClipboard={false}
41+
theme="tomorrow"
42+
name={null}
43+
displayDataTypes={false}
44+
displayObjectSize={false}
45+
style={{
46+
padding: '20px 10px',
47+
backgroundColor: 'transparent',
48+
fontSize: '12px',
49+
}}
50+
/>
51+
) : (
52+
<div>{syntaxHighlighter(example.code, example.language, { dark: true })}</div>
53+
)}
3454
</pre>
3555
);
3656
})}

0 commit comments

Comments
 (0)