diff --git a/src/core/plugins/json-schema-2020-12/components/JSONSchema/JSONSchema.jsx b/src/core/plugins/json-schema-2020-12/components/JSONSchema/JSONSchema.jsx index b4533b62..02f28353 100644 --- a/src/core/plugins/json-schema-2020-12/components/JSONSchema/JSONSchema.jsx +++ b/src/core/plugins/json-schema-2020-12/components/JSONSchema/JSONSchema.jsx @@ -21,11 +21,11 @@ import { JSONSchemaCyclesContext, } from "../../context" -const JSONSchema = forwardRef(({ schema, name }, ref) => { +const JSONSchema = forwardRef(({ schema, name, onExpand }, ref) => { const fn = useFn() const isExpandedDeeply = useIsExpandedDeeply() const [expanded, setExpanded] = useState(isExpandedDeeply) - const [expandedDeeply, setExpandedDeeply] = useState(false) + const [expandedDeeply, setExpandedDeeply] = useState(isExpandedDeeply) const [level, nextLevel] = useLevel() const isEmbedded = useIsEmbedded() const isExpandable = fn.isExpandable(schema) @@ -68,18 +68,6 @@ const JSONSchema = forwardRef(({ schema, name }, ref) => { const KeywordDescription = useComponent("KeywordDescription") const ExpandDeepButton = useComponent("ExpandDeepButton") - /** - * Event handlers. - */ - const handleExpansion = useCallback((e, expandedNew) => { - setExpanded(expandedNew) - !expandedNew && setExpandedDeeply(false) - }, []) - const handleExpansionDeep = useCallback((e, expandedDeepNew) => { - setExpanded(expandedDeepNew) - setExpandedDeeply(expandedDeepNew) - }, []) - /** * Effects handlers. */ @@ -91,6 +79,26 @@ const JSONSchema = forwardRef(({ schema, name }, ref) => { 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 ( @@ -173,10 +181,12 @@ const JSONSchema = forwardRef(({ schema, name }, ref) => { JSONSchema.propTypes = { name: PropTypes.oneOfType([PropTypes.string, PropTypes.element]), schema: propTypes.schema.isRequired, + onExpand: PropTypes.func, } JSONSchema.defaultProps = { name: "", + onExpand: () => {}, } export default JSONSchema diff --git a/src/core/plugins/oas31/components/models.jsx b/src/core/plugins/oas31/components/models.jsx index 8129cd71..d3017f1d 100644 --- a/src/core/plugins/oas31/components/models.jsx +++ b/src/core/plugins/oas31/components/models.jsx @@ -27,33 +27,39 @@ const Models = ({ * Effects. */ useEffect(() => { - if (isOpen && specSelectors.specResolvedSubtree(schemasPath) == null) { + const isOpenAndExpanded = isOpen && defaultModelsExpandDepth > 1 + const isResolved = specSelectors.specResolvedSubtree(schemasPath) != null + if (isOpenAndExpanded && !isResolved) { specActions.requestResolvedSubtree(schemasPath) } - }, [isOpen]) + }, [isOpen, defaultModelsExpandDepth]) /** * Event handlers. */ - const handleCollapse = useCallback(() => { + const handleModelsExpand = useCallback(() => { layoutActions.show(schemasPath, !isOpen) - }, [isOpen, layoutActions]) - - const handleModelsRef = useCallback( - (node) => { - if (node !== null) { - layoutActions.readyToScroll(schemasPath, node) - } - }, - [layoutActions] - ) - + }, [isOpen]) + const handleModelsRef = useCallback((node) => { + if (node !== null) { + layoutActions.readyToScroll(schemasPath, node) + } + }, []) const handleJSONSchema202012Ref = (schemaName) => (node) => { if (node !== null) { layoutActions.readyToScroll([...schemasPath, schemaName], node) } } + const handleJSONSchema202012Expand = (schemaName) => (e, expanded) => { + if (expanded) { + const schemaPath = [...schemasPath, schemaName] + const isResolved = specSelectors.specResolvedSubtree(schemaPath) != null + if (!isResolved) { + specActions.requestResolvedSubtree([...schemasPath, schemaName]) + } + } + } /** * Rendering. @@ -72,7 +78,7 @@ const Models = ({