feat(json-schema-2020-12): add support for expandable behavior
Refs #8513
This commit is contained in:
committed by
Vladimír Gorej
parent
9ed1c1b3a0
commit
ddedb57dc0
@@ -20,14 +20,14 @@ const Accordion = ({ expanded, children, onChange }) => {
|
|||||||
return (
|
return (
|
||||||
<button className="json-schema-2020-12-accordion" onClick={handleExpansion}>
|
<button className="json-schema-2020-12-accordion" onClick={handleExpansion}>
|
||||||
<div className="json-schema-2020-12-accordion__children">{children}</div>
|
<div className="json-schema-2020-12-accordion__children">{children}</div>
|
||||||
<div
|
<span
|
||||||
className={classNames("json-schema-2020-12-accordion__icon", {
|
className={classNames("json-schema-2020-12-accordion__icon", {
|
||||||
"json-schema-2020-12-accordion__icon--expanded": expanded,
|
"json-schema-2020-12-accordion__icon--expanded": expanded,
|
||||||
"json-schema-2020-12-accordion__icon--collapsed": !expanded,
|
"json-schema-2020-12-accordion__icon--collapsed": !expanded,
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
<ChevronRightIcon />
|
<ChevronRightIcon />
|
||||||
</div>
|
</span>
|
||||||
</button>
|
</button>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -35,7 +35,7 @@ const Accordion = ({ expanded, children, onChange }) => {
|
|||||||
Accordion.propTypes = {
|
Accordion.propTypes = {
|
||||||
expanded: PropTypes.bool,
|
expanded: PropTypes.bool,
|
||||||
children: PropTypes.node.isRequired,
|
children: PropTypes.node.isRequired,
|
||||||
onClick: PropTypes.func.isRequired,
|
onChange: PropTypes.func.isRequired,
|
||||||
}
|
}
|
||||||
|
|
||||||
Accordion.defaultProps = {
|
Accordion.defaultProps = {
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
.json-schema-2020-12-accordion {
|
.json-schema-2020-12-accordion {
|
||||||
outline: none;
|
outline: none;
|
||||||
border: none;
|
border: none;
|
||||||
display: flex;
|
padding-left: 0;
|
||||||
align-items: center;
|
|
||||||
|
|
||||||
&__children {
|
&__children {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
}
|
}
|
||||||
|
|
||||||
&__icon {
|
&__icon {
|
||||||
width: 20px;
|
width: 18px;
|
||||||
height: 20px;
|
height: 18px;
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
vertical-align: center;
|
vertical-align: bottom;
|
||||||
|
|
||||||
&--expanded {
|
&--expanded {
|
||||||
transition: transform .15s ease-in;
|
transition: transform .15s ease-in;
|
||||||
|
|||||||
@@ -3,16 +3,21 @@
|
|||||||
*/
|
*/
|
||||||
import React, { useState, useCallback } from "react"
|
import React, { useState, useCallback } from "react"
|
||||||
import PropTypes from "prop-types"
|
import PropTypes from "prop-types"
|
||||||
|
import classNames from "classnames"
|
||||||
|
|
||||||
import * as propTypes from "../../prop-types"
|
import * as propTypes from "../../prop-types"
|
||||||
import { useComponent, useFn } from "../../hooks"
|
import { useComponent, useFn, useLevel, useIsEmbedded } from "../../hooks"
|
||||||
|
import { JSONSchemaLevelContext } from "../../context"
|
||||||
|
|
||||||
const JSONSchema = ({ schema, name }) => {
|
const JSONSchema = ({ schema, name }) => {
|
||||||
const [expanded, setExpanded] = useState(false)
|
const [expanded, setExpanded] = useState(false)
|
||||||
|
|
||||||
const fn = useFn()
|
const fn = useFn()
|
||||||
|
const [level, nextLevel] = useLevel()
|
||||||
|
const isEmbedded = useIsEmbedded()
|
||||||
const BooleanJSONSchema = useComponent("BooleanJSONSchema")
|
const BooleanJSONSchema = useComponent("BooleanJSONSchema")
|
||||||
const Accordion = useComponent("Accordion")
|
const Accordion = useComponent("Accordion")
|
||||||
|
const KeywordProperties = useComponent("KeywordProperties")
|
||||||
|
|
||||||
const handleExpansion = useCallback(() => {
|
const handleExpansion = useCallback(() => {
|
||||||
setExpanded((prev) => !prev)
|
setExpanded((prev) => !prev)
|
||||||
@@ -23,76 +28,27 @@ const JSONSchema = ({ schema, name }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<article className="json-schema-2020-12">
|
<JSONSchemaLevelContext.Provider value={nextLevel}>
|
||||||
<div className="json-schema-2020-12-head">
|
<article
|
||||||
<Accordion expanded={expanded} onChange={handleExpansion}>
|
data-json-schema-level={level}
|
||||||
<div className="json-schema-2020-12__title">
|
className={classNames("json-schema-2020-12", {
|
||||||
{name || fn.getTitle(schema)}
|
"json-schema-2020-12--embedded": isEmbedded,
|
||||||
</div>
|
})}
|
||||||
</Accordion>
|
>
|
||||||
</div>
|
<div className="json-schema-2020-12-head">
|
||||||
{expanded && (
|
<Accordion expanded={expanded} onChange={handleExpansion}>
|
||||||
<div className="json-schema-2020-12-body">
|
<div className="json-schema-2020-12__title">
|
||||||
<div className="json-schema-2020-12-property">
|
{name || fn.getTitle(schema)}
|
||||||
<article className="json-schema-2020-12 json-schema-2020-12--embedded">
|
</div>
|
||||||
<div className="json-schema-2020-12-head">
|
</Accordion>
|
||||||
<span className="json-schema-2020-12__title">property1</span>
|
|
||||||
<span className="json-schema-2020-12__type">Object</span>
|
|
||||||
<span className="json-schema-2020-12__format">int64</span>
|
|
||||||
<span className="json-schema-2020-12__limit">[0...100]</span>
|
|
||||||
<div className="json-schema-2020-12__description">
|
|
||||||
Whether to turn on or off the light.
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="json-schema-2020-12-body">
|
|
||||||
<div className="json-schema-2020-12-property">
|
|
||||||
<article className="json-schema-2020-12 json-schema-2020-12--embedded">
|
|
||||||
<div className="json-schema-2020-12-head">
|
|
||||||
<span className="json-schema-2020-12__title">
|
|
||||||
property11
|
|
||||||
</span>
|
|
||||||
<span className="json-schema-2020-12__type">Object</span>
|
|
||||||
</div>
|
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
<div className="json-schema-2020-12-property">
|
|
||||||
<article className="json-schema-2020-12 json-schema-2020-12--embedded">
|
|
||||||
<div className="json-schema-2020-12-head">
|
|
||||||
<span className="json-schema-2020-12__title">
|
|
||||||
property22
|
|
||||||
</span>
|
|
||||||
<span className="json-schema-2020-12__type">Object</span>
|
|
||||||
</div>
|
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
<div className="json-schema-2020-12-note">
|
|
||||||
Additional properties are allowed.
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
<div className="json-schema-2020-12-property">
|
|
||||||
<article className="json-schema-2020-12 json-schema-2020-12--embedded">
|
|
||||||
<div className="json-schema-2020-12-head">
|
|
||||||
<span className="json-schema-2020-12__title">property2</span>
|
|
||||||
<span className="json-schema-2020-12__type">Object</span>
|
|
||||||
</div>
|
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
<div className="json-schema-2020-12-property">
|
|
||||||
<article className="json-schema-2020-12 json-schema-2020-12--embedded">
|
|
||||||
<div className="json-schema-2020-12-head">
|
|
||||||
<span className="json-schema-2020-12__title">property3</span>
|
|
||||||
<span className="json-schema-2020-12__type">Object</span>
|
|
||||||
</div>
|
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
<div className="json-schema-2020-12-note">
|
|
||||||
Additional properties are allowed.
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
{expanded && (
|
||||||
</article>
|
<div className="json-schema-2020-12-body">
|
||||||
|
<KeywordProperties schema={schema} />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</article>
|
||||||
|
</JSONSchemaLevelContext.Provider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
&__title {
|
&__title {
|
||||||
@include text_headline($section-models-model-title-font-color);
|
@include text_headline($section-models-model-title-font-color);
|
||||||
|
display: inline-block;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,8 +32,9 @@
|
|||||||
margin: 7px 0;
|
margin: 7px 0;
|
||||||
|
|
||||||
.json-schema-2020-12__title {
|
.json-schema-2020-12__title {
|
||||||
font-size: 12px;
|
|
||||||
@include text_code();
|
@include text_code();
|
||||||
|
font-size: 12px;
|
||||||
|
vertical-align: bottom;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +44,6 @@
|
|||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
text-transform: capitalize;
|
text-transform: capitalize;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
margin-left: 10px;
|
|
||||||
|
|
||||||
&:before {
|
&:before {
|
||||||
content: "{";
|
content: "{";
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* @prettier
|
||||||
|
*/
|
||||||
|
import React from "react"
|
||||||
|
|
||||||
|
import { schema } from "../../prop-types"
|
||||||
|
import { useFn, useComponent } from "../../hooks"
|
||||||
|
|
||||||
|
const Properties = ({ schema }) => {
|
||||||
|
const fn = useFn()
|
||||||
|
const JSONSchema = useComponent("JSONSchema")
|
||||||
|
|
||||||
|
if (fn.isBooleanJSONSchema(schema)) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const properties = schema.properties || {}
|
||||||
|
|
||||||
|
if (Object.keys(properties).length === 0) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{Object.entries(properties).map(([propertyName, propertyValue]) => (
|
||||||
|
<div key={propertyName} className="json-schema-2020-12-property">
|
||||||
|
<JSONSchema name={propertyName} schema={propertyValue} />
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
Properties.propTypes = {
|
||||||
|
schema: schema.isRequired,
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Properties
|
||||||
@@ -6,4 +6,5 @@ import { createContext } from "react"
|
|||||||
export const JSONSchemaContext = createContext(null)
|
export const JSONSchemaContext = createContext(null)
|
||||||
JSONSchemaContext.displayName = "JSONSchemaContext"
|
JSONSchemaContext.displayName = "JSONSchemaContext"
|
||||||
|
|
||||||
export default JSONSchemaContext
|
export const JSONSchemaLevelContext = createContext(0)
|
||||||
|
JSONSchemaLevelContext.displayName = "JSONSchemaLevelContext"
|
||||||
|
|||||||
@@ -5,9 +5,10 @@ import React from "react"
|
|||||||
|
|
||||||
import JSONSchema from "./components/JSONSchema/JSONSchema"
|
import JSONSchema from "./components/JSONSchema/JSONSchema"
|
||||||
import BooleanJSONSchema from "./components/BooleanJSONSchema/BooleanJSONSchema"
|
import BooleanJSONSchema from "./components/BooleanJSONSchema/BooleanJSONSchema"
|
||||||
|
import KeywordProperties from "./components/keywords/Properties"
|
||||||
import Accordion from "./components/Accordion/Accordion"
|
import Accordion from "./components/Accordion/Accordion"
|
||||||
import ChevronRightIcon from "./components/icons/ChevronRight"
|
import ChevronRightIcon from "./components/icons/ChevronRight"
|
||||||
import JSONSchemaContext from "./context"
|
import { JSONSchemaContext } from "./context"
|
||||||
import { getTitle, isBooleanJSONSchema, upperFirst } from "./fn"
|
import { getTitle, isBooleanJSONSchema, upperFirst } from "./fn"
|
||||||
|
|
||||||
export const withJSONSchemaContext = (Component, overrides = {}) => {
|
export const withJSONSchemaContext = (Component, overrides = {}) => {
|
||||||
@@ -15,6 +16,7 @@ export const withJSONSchemaContext = (Component, overrides = {}) => {
|
|||||||
components: {
|
components: {
|
||||||
JSONSchema,
|
JSONSchema,
|
||||||
BooleanJSONSchema,
|
BooleanJSONSchema,
|
||||||
|
KeywordProperties,
|
||||||
Accordion,
|
Accordion,
|
||||||
ChevronRightIcon,
|
ChevronRightIcon,
|
||||||
...overrides.components,
|
...overrides.components,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
import { useContext } from "react"
|
import { useContext } from "react"
|
||||||
|
|
||||||
import JSONSchemaContext from "./context"
|
import { JSONSchemaContext, JSONSchemaLevelContext } from "./context"
|
||||||
|
|
||||||
export const useConfig = () => {
|
export const useConfig = () => {
|
||||||
const { config } = useContext(JSONSchemaContext)
|
const { config } = useContext(JSONSchemaContext)
|
||||||
@@ -20,3 +20,15 @@ export const useFn = (fnName = undefined) => {
|
|||||||
|
|
||||||
return typeof fnName !== "undefined" ? fn[fnName] : fn
|
return typeof fnName !== "undefined" ? fn[fnName] : fn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const useLevel = () => {
|
||||||
|
const level = useContext(JSONSchemaLevelContext)
|
||||||
|
|
||||||
|
return [level, level + 1]
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useIsEmbedded = () => {
|
||||||
|
const [level] = useLevel()
|
||||||
|
|
||||||
|
return level > 0
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
import JSONSchema from "./components/JSONSchema/JSONSchema"
|
import JSONSchema from "./components/JSONSchema/JSONSchema"
|
||||||
import BooleanJSONSchema from "./components/BooleanJSONSchema/BooleanJSONSchema"
|
import BooleanJSONSchema from "./components/BooleanJSONSchema/BooleanJSONSchema"
|
||||||
|
import JSONSchemaKeywordProperties from "./components/keywords/Properties"
|
||||||
import Accordion from "./components/Accordion/Accordion"
|
import Accordion from "./components/Accordion/Accordion"
|
||||||
import ChevronRightIcon from "./components/icons/ChevronRight"
|
import ChevronRightIcon from "./components/icons/ChevronRight"
|
||||||
import { upperFirst } from "./fn"
|
import { upperFirst } from "./fn"
|
||||||
@@ -12,6 +13,7 @@ const JSONSchema202012Plugin = () => ({
|
|||||||
components: {
|
components: {
|
||||||
JSONSchema202012: JSONSchema,
|
JSONSchema202012: JSONSchema,
|
||||||
BooleanJSONSchema202012: BooleanJSONSchema,
|
BooleanJSONSchema202012: BooleanJSONSchema,
|
||||||
|
JSONSchema202012KeywordProperties: JSONSchemaKeywordProperties,
|
||||||
JSONSchema202012Accordion: Accordion,
|
JSONSchema202012Accordion: Accordion,
|
||||||
JSONSchema202012ChevronRightIcon: ChevronRightIcon,
|
JSONSchema202012ChevronRightIcon: ChevronRightIcon,
|
||||||
withJSONSchema202012Context: withJSONSchemaContext,
|
withJSONSchema202012Context: withJSONSchemaContext,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
|
|||||||
const Models = getComponent("OAS31Models", true)
|
const Models = getComponent("OAS31Models", true)
|
||||||
const JSONSchema = getComponent("JSONSchema202012")
|
const JSONSchema = getComponent("JSONSchema202012")
|
||||||
const BooleanJSONSchema = getComponent("BooleanJSONSchema202012")
|
const BooleanJSONSchema = getComponent("BooleanJSONSchema202012")
|
||||||
|
const KeywordProperties = getComponent("JSONSchema202012KeywordProperties")
|
||||||
const Accordion = getComponent("JSONSchema202012Accordion")
|
const Accordion = getComponent("JSONSchema202012Accordion")
|
||||||
const ChevronRightIcon = getComponent("JSONSchema202012ChevronRightIcon")
|
const ChevronRightIcon = getComponent("JSONSchema202012ChevronRightIcon")
|
||||||
const withSchemaContext = getComponent("withJSONSchema202012Context")
|
const withSchemaContext = getComponent("withJSONSchema202012Context")
|
||||||
@@ -20,6 +21,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
|
|||||||
components: {
|
components: {
|
||||||
JSONSchema,
|
JSONSchema,
|
||||||
BooleanJSONSchema,
|
BooleanJSONSchema,
|
||||||
|
KeywordProperties,
|
||||||
Accordion,
|
Accordion,
|
||||||
ChevronRightIcon,
|
ChevronRightIcon,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user