Skip to content

Commit 4481b14

Browse files
authored
Merge pull request #224 from gnidan/multiline-examples
Add `ValueComponent` to `viewerOptions` to allow customizing examples, enums, consts, and defaults
2 parents 54356c5 + d5307bd commit 4481b14

File tree

16 files changed

+307
-40
lines changed

16 files changed

+307
-40
lines changed

__tests__/JSONSchemaViewer/__snapshots__/array.test.tsx.snap

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,23 @@ exports[`JSONSchemaViewer - Array type test 5 1`] = `
540540
Possible values :
541541
</strong>
542542
 
543-
<code>
544-
["Street","Avenue","Boulevard"]
545-
</code>
543+
<ul>
544+
<li>
545+
<code>
546+
Street
547+
</code>
548+
</li>
549+
<li>
550+
<code>
551+
Avenue
552+
</code>
553+
</li>
554+
<li>
555+
<code>
556+
Boulevard
557+
</code>
558+
</li>
559+
</ul>
546560
</div>
547561
</div>
548562
<div
@@ -604,9 +618,28 @@ exports[`JSONSchemaViewer - Array type test 5 1`] = `
604618
Possible values :
605619
</strong>
606620
 
607-
<code>
608-
["NW","NE","SW","SE"]
609-
</code>
621+
<ul>
622+
<li>
623+
<code>
624+
NW
625+
</code>
626+
</li>
627+
<li>
628+
<code>
629+
NE
630+
</code>
631+
</li>
632+
<li>
633+
<code>
634+
SW
635+
</code>
636+
</li>
637+
<li>
638+
<code>
639+
SE
640+
</code>
641+
</li>
642+
</ul>
610643
</div>
611644
</div>
612645
<div

__tests__/JSONSchemaViewer/__snapshots__/boolean.test.tsx.snap

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,18 @@ exports[`JSONSchemaViewer - Boolean type test 1 1`] = `
7272
Possible values :
7373
</strong>
7474
 
75-
<code>
76-
[true,false]
77-
</code>
75+
<ul>
76+
<li>
77+
<code>
78+
true
79+
</code>
80+
</li>
81+
<li>
82+
<code>
83+
false
84+
</code>
85+
</li>
86+
</ul>
7887
</div>
7988
<div>
8089
<strong>

__tests__/JSONSchemaViewer/__snapshots__/generic_keywords.test.tsx.snap

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,23 @@ exports[`JSONSchemaViewer - Generic keywords test enum 1`] = `
257257
Possible values :
258258
</strong>
259259
 
260-
<code>
261-
["red","amber","green"]
262-
</code>
260+
<ul>
261+
<li>
262+
<code>
263+
red
264+
</code>
265+
</li>
266+
<li>
267+
<code>
268+
amber
269+
</code>
270+
</li>
271+
<li>
272+
<code>
273+
green
274+
</code>
275+
</li>
276+
</ul>
263277
</div>
264278
<div>
265279
<strong>

__tests__/JSONSchemaViewer/__snapshots__/null.test.tsx.snap

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,18 @@ exports[`JSONSchemaViewer - null type test 1 1`] = `
8585
Possible values :
8686
</strong>
8787
 
88-
<code>
89-
[null,"Some constant here ..."]
90-
</code>
88+
<ul>
89+
<li>
90+
<code>
91+
null
92+
</code>
93+
</li>
94+
<li>
95+
<code>
96+
Some constant here ...
97+
</code>
98+
</li>
99+
</ul>
91100
</div>
92101
</div>
93102
</div>

__tests__/JSONSchemaViewer/__snapshots__/object.test.tsx.snap

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,23 @@ exports[`JSONSchemaViewer - object test additionalProperties (JSON Schema) 1`] =
347347
Possible values :
348348
</strong>
349349
 
350-
<code>
351-
["Street","Avenue","Boulevard"]
352-
</code>
350+
<ul>
351+
<li>
352+
<code>
353+
Street
354+
</code>
355+
</li>
356+
<li>
357+
<code>
358+
Avenue
359+
</code>
360+
</li>
361+
<li>
362+
<code>
363+
Boulevard
364+
</code>
365+
</li>
366+
</ul>
353367
</div>
354368
</div>
355369
</div>
@@ -577,9 +591,23 @@ exports[`JSONSchemaViewer - object test additionalProperties (false) 1`] = `
577591
Possible values :
578592
</strong>
579593
 
580-
<code>
581-
["Street","Avenue","Boulevard"]
582-
</code>
594+
<ul>
595+
<li>
596+
<code>
597+
Street
598+
</code>
599+
</li>
600+
<li>
601+
<code>
602+
Avenue
603+
</code>
604+
</li>
605+
<li>
606+
<code>
607+
Boulevard
608+
</code>
609+
</li>
610+
</ul>
583611
</div>
584612
</div>
585613
</div>
@@ -1261,9 +1289,23 @@ exports[`JSONSchemaViewer - object test properties 1`] = `
12611289
Possible values :
12621290
</strong>
12631291
 
1264-
<code>
1265-
["Street","Avenue","Boulevard"]
1266-
</code>
1292+
<ul>
1293+
<li>
1294+
<code>
1295+
Street
1296+
</code>
1297+
</li>
1298+
<li>
1299+
<code>
1300+
Avenue
1301+
</code>
1302+
</li>
1303+
<li>
1304+
<code>
1305+
Boulevard
1306+
</code>
1307+
</li>
1308+
</ul>
12671309
</div>
12681310
</div>
12691311
</div>

__tests__/JSONSchemaViewer/__snapshots__/schema_conditionally.test.tsx.snap

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,23 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
116116
Possible values :
117117
</strong>
118118
 
119-
<code>
120-
["United States of America","Canada","Netherlands"]
121-
</code>
119+
<ul>
120+
<li>
121+
<code>
122+
United States of America
123+
</code>
124+
</li>
125+
<li>
126+
<code>
127+
Canada
128+
</code>
129+
</li>
130+
<li>
131+
<code>
132+
Netherlands
133+
</code>
134+
</li>
135+
</ul>
122136
</div>
123137
<div>
124138
<strong>
@@ -1094,9 +1108,18 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else 1`] = `
10941108
Possible values :
10951109
</strong>
10961110
 
1097-
<code>
1098-
["United States of America","Canada"]
1099-
</code>
1111+
<ul>
1112+
<li>
1113+
<code>
1114+
United States of America
1115+
</code>
1116+
</li>
1117+
<li>
1118+
<code>
1119+
Canada
1120+
</code>
1121+
</li>
1122+
</ul>
11001123
</div>
11011124
<div>
11021125
<strong>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from "react"
2+
3+
import type { JSONSchema } from "@theme/JSONSchemaViewer/types"
4+
import { useJSVOptionsContext } from "@theme/JSONSchemaViewer/contexts"
5+
import { printSchemaType } from "@theme/JSONSchemaViewer/utils/QualifierMessages"
6+
7+
type Props = {
8+
value: unknown
9+
schema: JSONSchema
10+
}
11+
12+
export default function CreateValue(props: Props): JSX.Element {
13+
const { value, schema } = props
14+
const { ValueComponent } = useJSVOptionsContext()
15+
16+
if (ValueComponent) {
17+
return <ValueComponent value={value} schema={schema} />
18+
}
19+
20+
return printSchemaType(value)
21+
}

src/theme/JSONSchemaViewer/JSONSchemaElements/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@ export { default as CreateAlwaysInvalid } from "@theme/JSONSchemaViewer/JSONSche
3333

3434
// Component to deal with description
3535
export { default as CreateDescription } from "@theme/JSONSchemaViewer/JSONSchemaElements/CreateDescription"
36+
37+
// Component to deal with values (for examples, enums, default, const, etc.)
38+
export { default as CreateValue } from "@theme/JSONSchemaViewer/JSONSchemaElements/CreateValue"

src/theme/JSONSchemaViewer/contexts/jsvOptions.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ export type JSVOptions = {
2222
* @default undefined
2323
*/
2424
DescriptionComponent?: (params: { description: string }) => JSX.Element
25+
/**
26+
* To overwrite the printout of "examples", "default", "const", and "enum"
27+
* By default, print out as provided
28+
* @default undefined
29+
*/
30+
ValueComponent?: (params: {
31+
value: unknown
32+
schema: JSONSchema
33+
}) => JSX.Element
2534
/**
2635
* To overwrite the default handling of unresolved $refs
2736
* By default, print out as provided

src/theme/JSONSchemaViewer/utils/QualifierMessages/Constant.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react"
22

33
import Translate from "@docusaurus/Translate"
44

5-
import { printSchemaType } from "@theme/JSONSchemaViewer/utils/QualifierMessages"
5+
import { CreateValue } from "@theme/JSONSchemaViewer/JSONSchemaElements"
66

77
import type { JSONSchema } from "@theme/JSONSchemaViewer/types"
88

@@ -30,7 +30,7 @@ export default function Constant(props: Props): JSX.Element {
3030
<div key={"const"}>
3131
{constantLabel}
3232
&nbsp;
33-
{printSchemaType(schema.const!)}
33+
<CreateValue value={schema.const!} schema={schema} />
3434
</div>
3535
)
3636
}

0 commit comments

Comments
 (0)