/** * @prettier */ import React, { forwardRef, useState, useCallback, useEffect } from "react" import PropTypes from "prop-types" import classNames from "classnames" import * as propTypes from "../../prop-types" import { useComponent, useLevel, useFn, useIsEmbedded, useIsExpanded, useIsExpandedDeeply, useIsCircular, useRenderedSchemas, } from "../../hooks" import { JSONSchemaLevelContext, JSONSchemaDeepExpansionContext, JSONSchemaCyclesContext, } from "../../context" const JSONSchema = forwardRef( ({ schema, name = "", dependentRequired = [], onExpand = () => {} }, ref) => { const fn = useFn() const isExpanded = useIsExpanded() const isExpandedDeeply = useIsExpandedDeeply() const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply) const [expandedDeeply, setExpandedDeeply] = useState(isExpandedDeeply) const [level, nextLevel] = useLevel() const isEmbedded = useIsEmbedded() const isExpandable = fn.isExpandable(schema) || dependentRequired.length > 0 const isCircular = useIsCircular(schema) const renderedSchemas = useRenderedSchemas(schema) const constraints = fn.stringifyConstraints(schema) const Accordion = useComponent("Accordion") const Keyword$schema = useComponent("Keyword$schema") const Keyword$vocabulary = useComponent("Keyword$vocabulary") const Keyword$id = useComponent("Keyword$id") const Keyword$anchor = useComponent("Keyword$anchor") const Keyword$dynamicAnchor = useComponent("Keyword$dynamicAnchor") const Keyword$ref = useComponent("Keyword$ref") const Keyword$dynamicRef = useComponent("Keyword$dynamicRef") const Keyword$defs = useComponent("Keyword$defs") const Keyword$comment = useComponent("Keyword$comment") const KeywordAllOf = useComponent("KeywordAllOf") const KeywordAnyOf = useComponent("KeywordAnyOf") const KeywordOneOf = useComponent("KeywordOneOf") const KeywordNot = useComponent("KeywordNot") const KeywordIf = useComponent("KeywordIf") const KeywordThen = useComponent("KeywordThen") const KeywordElse = useComponent("KeywordElse") const KeywordDependentSchemas = useComponent("KeywordDependentSchemas") const KeywordPrefixItems = useComponent("KeywordPrefixItems") const KeywordItems = useComponent("KeywordItems") const KeywordContains = useComponent("KeywordContains") const KeywordProperties = useComponent("KeywordProperties") const KeywordPatternProperties = useComponent("KeywordPatternProperties") const KeywordAdditionalProperties = useComponent( "KeywordAdditionalProperties" ) const KeywordPropertyNames = useComponent("KeywordPropertyNames") const KeywordUnevaluatedItems = useComponent("KeywordUnevaluatedItems") const KeywordUnevaluatedProperties = useComponent( "KeywordUnevaluatedProperties" ) const KeywordType = useComponent("KeywordType") const KeywordEnum = useComponent("KeywordEnum") const KeywordConst = useComponent("KeywordConst") const KeywordConstraint = useComponent("KeywordConstraint") const KeywordDependentRequired = useComponent("KeywordDependentRequired") const KeywordContentSchema = useComponent("KeywordContentSchema") const KeywordTitle = useComponent("KeywordTitle") const KeywordDescription = useComponent("KeywordDescription") const KeywordDefault = useComponent("KeywordDefault") const KeywordDeprecated = useComponent("KeywordDeprecated") const KeywordReadOnly = useComponent("KeywordReadOnly") const KeywordWriteOnly = useComponent("KeywordWriteOnly") const ExpandDeepButton = useComponent("ExpandDeepButton") /** * Effects handlers. */ useEffect(() => { setExpandedDeeply(isExpandedDeeply) }, [isExpandedDeeply]) useEffect(() => { setExpandedDeeply(expandedDeeply) }, [expandedDeeply]) /** * Event handlers. */ const handleExpansion = useCallback( (e, expandedNew) => { setExpanded(expandedNew) !expandedNew && setExpandedDeeply(false) onExpand(e, expandedNew, false) }, [onExpand] ) const handleExpansionDeep = useCallback( (e, expandedDeepNew) => { setExpanded(expandedDeepNew) setExpandedDeeply(expandedDeepNew) onExpand(e, expandedDeepNew, true) }, [onExpand] ) return (
{isExpandable && !isCircular ? ( <> ) : ( )} {constraints.length > 0 && constraints.map((constraint) => ( ))}
{expanded && ( <> {!isCircular && isExpandable && ( <> )} {!isCircular && isExpandable && ( )} )}
) } ) JSONSchema.propTypes = { name: PropTypes.oneOfType([PropTypes.string, PropTypes.element]), schema: propTypes.schema.isRequired, dependentRequired: PropTypes.arrayOf(PropTypes.string), onExpand: PropTypes.func, } export default JSONSchema