Skip to content

Commit

Permalink
feat(json-schema-2020-12): add support for defs deep extend
Browse files Browse the repository at this point in the history
Refs #8513
  • Loading branch information
char0n committed Apr 26, 2023
1 parent d404bbd commit 6bc26b9
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions src/core/plugins/json-schema-2020-12/components/keywords/$defs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, { useCallback, useState } from "react"

import { schema } from "../../prop-types"
import { useComponent, useIsExpandedDeeply } from "../../hooks"
import { JSONSchemaDeepExpansionContext } from "../../context"

const $defs = ({ schema }) => {
const $defs = schema?.$defs || {}
Expand All @@ -15,31 +16,43 @@ const $defs = ({ schema }) => {

const isExpandedDeeply = useIsExpandedDeeply()
const [expanded, setExpanded] = useState(isExpandedDeeply)
const [expandedDeeply, setExpandedDeeply] = useState(false)
const Accordion = useComponent("Accordion")
const ExpandDeepButton = useComponent("ExpandDeepButton")
const JSONSchema = useComponent("JSONSchema")

/**
* Event handlers.
*/
const handleExpansion = useCallback(() => {
setExpanded((prev) => !prev)
}, [])
const handleExpansionDeep = useCallback((e, expandedDeepNew) => {
setExpanded(expandedDeepNew)
setExpandedDeeply(expandedDeepNew)
}, [])

return (
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--$defs">
<Accordion expanded={expanded} onChange={handleExpansion}>
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--secondary">
$defs
</span>
</Accordion>
<span className="json-schema-2020-12__type">object</span>
{expanded && (
<ul>
{Object.entries($defs).map(([schemaName, schema]) => (
<li key={schemaName} className="json-schema-2020-12-property">
<JSONSchema name={schemaName} schema={schema} />
</li>
))}
</ul>
)}
</div>
<JSONSchemaDeepExpansionContext.Provider value={expandedDeeply}>
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--$defs">
<Accordion expanded={expanded} onChange={handleExpansion}>
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--secondary">
$defs
</span>
</Accordion>
<ExpandDeepButton expanded={expanded} onClick={handleExpansionDeep} />
<span className="json-schema-2020-12__type">object</span>
{expanded && (
<ul>
{Object.entries($defs).map(([schemaName, schema]) => (
<li key={schemaName} className="json-schema-2020-12-property">
<JSONSchema name={schemaName} schema={schema} />
</li>
))}
</ul>
)}
</div>
</JSONSchemaDeepExpansionContext.Provider>
)
}

Expand Down

0 comments on commit 6bc26b9

Please sign in to comment.