Skip to content

Commit

Permalink
feat: improve CustomizeType to render basic constant values
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed Jan 8, 2024
1 parent 3a40253 commit 09922f3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2221,7 +2221,7 @@ exports[`JSONSchemaViewer - generateFriendlyName cases test Prefer title 1`] = `
</strong>
 
<code>
Hello World
"Hello World"
</code>
 
<strong>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ exports[`JSONSchemaViewer - Generic keywords test const 1`] = `
</strong>
 
<code>
United States of America
"United States of America"
</code>
 
<strong>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
</strong>
 
<code>
United States of America
"United States of America"
</code>
 
<strong>
Expand Down Expand Up @@ -588,7 +588,7 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
</strong>
 
<code>
Canada
"Canada"
</code>
 
<strong>
Expand Down Expand Up @@ -849,7 +849,7 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
</strong>
 
<code>
Netherlands
"Netherlands"
</code>
 
<strong>
Expand Down Expand Up @@ -1248,7 +1248,7 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else 1`] = `
</strong>
 
<code>
United States of America
"United States of America"
</code>
 
<strong>
Expand Down
16 changes: 13 additions & 3 deletions src/theme/JSONSchemaViewer/utils/generateFriendlyName.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from "react"

import CodeBlock from "@theme-original/CodeBlock"

// Utility functions to know which case we have
import { detectedTypes } from "@theme/JSONSchemaViewer/utils"

Expand All @@ -17,7 +19,6 @@ import type {
JSONSchemaNS,
TypeValues,
} from "@theme/JSONSchemaViewer/types"
import { printSchemaType } from "@theme/JSONSchemaViewer/utils/QualifierMessages"

// common function I need below
function shouldAddSeparator(idx: number, length: number): boolean {
Expand Down Expand Up @@ -139,12 +140,21 @@ function CustomizeType({ schema, type }: CustomizeProps): JSX.Element {
}

// For constant values
// I used Docusaurus Codeblock (instead of printSchemaType) as they put quotes whereas normal <code> don't
if (!["array", "object"].includes(type)) {
if (schema.const !== undefined) {
return printSchemaType(schema.const)
return (
<CodeBlock language="json">{`${JSON.stringify(
schema.const,
)}`}</CodeBlock>
)
}
if (schema.enum !== undefined && schema.enum.length === 1) {
return printSchemaType(schema.enum[0])
return (
<CodeBlock language="json">{`${JSON.stringify(
schema.enum[0],
)}`}</CodeBlock>
)
}
}

Expand Down

0 comments on commit 09922f3

Please sign in to comment.