Skip to content

Commit d5307bd

Browse files
committed
Update "Custom value component" docs page
- Move to new home in "Customizations" section - Switch to using tabs - Define example schema that features enum, default, and const - Include example ValueComponent that references schema in logic
1 parent c2b6228 commit d5307bd

File tree

3 files changed

+93
-59
lines changed

3 files changed

+93
-59
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
description: customize display of values
3+
title: "🎨 Custom value Component"
4+
---
5+
6+
import CodeBlock from "@theme/CodeBlock"
7+
import Schema from "@site/static/schemas/examples/custom/values.json"
8+
import JSONSchemaViewer from "@theme/JSONSchemaViewer"
9+
import ReactMarkdown from "react-markdown"
10+
import Tabs from "@theme/Tabs"
11+
import TabItem from "@theme/TabItem"
12+
13+
# Custom value component
14+
15+
<Tabs>
16+
<TabItem value="Viewer" label="Viewer" default>
17+
<JSONSchemaViewer
18+
schema={Schema}
19+
viewerOptions={{
20+
ValueComponent: ({ value, schema }) => {
21+
// render complex values as multiline JSON with 2 space indentation
22+
if (!(["string", "number", "undefined"].includes(typeof value))) {
23+
return <CodeBlock language="json">{`${
24+
JSON.stringify(value, null, 2)
25+
}`}</CodeBlock>;
26+
}
27+
28+
// display elementary values inline.
29+
const component = <code>{`${value}`}</code>;
30+
31+
// if schema defines a default value, ensure it is bold wherever it
32+
// appears (e.g. in an enum)
33+
if (value === schema.default) {
34+
return <strong>{component}</strong>
35+
}
36+
37+
return component;
38+
},
39+
}}
40+
/>
41+
42+
</TabItem>
43+
<TabItem value="viewerOptions" label="viewerOptions">
44+
```js
45+
{
46+
ValueComponent: ({ value, schema }) => {
47+
// render complex values as multiline JSON with 2 space indentation
48+
if (!(["string", "number", "undefined"].includes(typeof value))) {
49+
return <CodeBlock language="json">{`${
50+
JSON.stringify(value, null, 2)
51+
}`}</CodeBlock>;
52+
}
53+
54+
// display elementary values inline.
55+
const component = <code>{`${value}`}</code>;
56+
57+
// if schema defines a default value, ensure it is bold wherever it
58+
// appears (e.g. in an enum)
59+
if (value === schema.default) {
60+
return <strong>{component}</strong>
61+
}
62+
63+
return component;
64+
}
65+
}
66+
```
67+
68+
</TabItem>
69+
<TabItem value="JSON Schema" label="JSON Schema">
70+
<CodeBlock language="json">{JSON.stringify(Schema, null, 2)}</CodeBlock>
71+
</TabItem>
72+
</Tabs>

testsite/docs/demo-viewer/generic_keywords/customize_value_display.mdx

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "CustomizationOptions",
4+
"description": "JSON schema for customized options",
5+
"type": "object",
6+
"properties": {
7+
"customField": {
8+
"type": "string",
9+
"description": "A customized or personalized field",
10+
"enum": ["palette", "teddyBear", "tools", "laptop", "thread", "phone", "puzzle", "scissors", "hammer", "note"],
11+
"default": "palette",
12+
"examples": ["tools", "note"]
13+
},
14+
"customConstObject": {
15+
"type": "object",
16+
"const": { "version": 5 }
17+
}
18+
},
19+
"required": ["customField"],
20+
"additionalProperties": false
21+
}

0 commit comments

Comments
 (0)