|
| 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> |
0 commit comments