fix(oas31): resolve schemas in 'Schemas section' only if expanded (#8616)

Refs #8606
This commit is contained in:
Vladimír Gorej
2023-05-04 19:28:51 +02:00
committed by GitHub
parent 7bfee4e492
commit a8771e744f
2 changed files with 46 additions and 29 deletions

View File

@@ -21,11 +21,11 @@ import {
JSONSchemaCyclesContext, JSONSchemaCyclesContext,
} from "../../context" } from "../../context"
const JSONSchema = forwardRef(({ schema, name }, ref) => { const JSONSchema = forwardRef(({ schema, name, onExpand }, ref) => {
const fn = useFn() const fn = useFn()
const isExpandedDeeply = useIsExpandedDeeply() const isExpandedDeeply = useIsExpandedDeeply()
const [expanded, setExpanded] = useState(isExpandedDeeply) const [expanded, setExpanded] = useState(isExpandedDeeply)
const [expandedDeeply, setExpandedDeeply] = useState(false) const [expandedDeeply, setExpandedDeeply] = useState(isExpandedDeeply)
const [level, nextLevel] = useLevel() const [level, nextLevel] = useLevel()
const isEmbedded = useIsEmbedded() const isEmbedded = useIsEmbedded()
const isExpandable = fn.isExpandable(schema) const isExpandable = fn.isExpandable(schema)
@@ -68,18 +68,6 @@ const JSONSchema = forwardRef(({ schema, name }, ref) => {
const KeywordDescription = useComponent("KeywordDescription") const KeywordDescription = useComponent("KeywordDescription")
const ExpandDeepButton = useComponent("ExpandDeepButton") 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. * Effects handlers.
*/ */
@@ -91,6 +79,26 @@ const JSONSchema = forwardRef(({ schema, name }, ref) => {
setExpandedDeeply(expandedDeeply) setExpandedDeeply(expandedDeeply)
}, [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 ( return (
<JSONSchemaLevelContext.Provider value={nextLevel}> <JSONSchemaLevelContext.Provider value={nextLevel}>
<JSONSchemaDeepExpansionContext.Provider value={expandedDeeply}> <JSONSchemaDeepExpansionContext.Provider value={expandedDeeply}>
@@ -173,10 +181,12 @@ const JSONSchema = forwardRef(({ schema, name }, ref) => {
JSONSchema.propTypes = { JSONSchema.propTypes = {
name: PropTypes.oneOfType([PropTypes.string, PropTypes.element]), name: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
schema: propTypes.schema.isRequired, schema: propTypes.schema.isRequired,
onExpand: PropTypes.func,
} }
JSONSchema.defaultProps = { JSONSchema.defaultProps = {
name: "", name: "",
onExpand: () => {},
} }
export default JSONSchema export default JSONSchema

View File

@@ -27,33 +27,39 @@ const Models = ({
* Effects. * Effects.
*/ */
useEffect(() => { useEffect(() => {
if (isOpen && specSelectors.specResolvedSubtree(schemasPath) == null) { const isOpenAndExpanded = isOpen && defaultModelsExpandDepth > 1
const isResolved = specSelectors.specResolvedSubtree(schemasPath) != null
if (isOpenAndExpanded && !isResolved) {
specActions.requestResolvedSubtree(schemasPath) specActions.requestResolvedSubtree(schemasPath)
} }
}, [isOpen]) }, [isOpen, defaultModelsExpandDepth])
/** /**
* Event handlers. * Event handlers.
*/ */
const handleCollapse = useCallback(() => { const handleModelsExpand = useCallback(() => {
layoutActions.show(schemasPath, !isOpen) layoutActions.show(schemasPath, !isOpen)
}, [isOpen, layoutActions]) }, [isOpen])
const handleModelsRef = useCallback((node) => {
const handleModelsRef = useCallback(
(node) => {
if (node !== null) { if (node !== null) {
layoutActions.readyToScroll(schemasPath, node) layoutActions.readyToScroll(schemasPath, node)
} }
}, }, [])
[layoutActions]
)
const handleJSONSchema202012Ref = (schemaName) => (node) => { const handleJSONSchema202012Ref = (schemaName) => (node) => {
if (node !== null) { if (node !== null) {
layoutActions.readyToScroll([...schemasPath, schemaName], node) 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. * Rendering.
@@ -72,7 +78,7 @@ const Models = ({
<button <button
aria-expanded={isOpen} aria-expanded={isOpen}
className="models-control" className="models-control"
onClick={handleCollapse} onClick={handleModelsExpand}
> >
<span>Schemas</span> <span>Schemas</span>
<svg width="20" height="20" aria-hidden="true" focusable="false"> <svg width="20" height="20" aria-hidden="true" focusable="false">
@@ -87,6 +93,7 @@ const Models = ({
ref={handleJSONSchema202012Ref(schemaName)} ref={handleJSONSchema202012Ref(schemaName)}
schema={schema} schema={schema}
name={fn.upperFirst(schemaName)} name={fn.upperFirst(schemaName)}
onExpand={handleJSONSchema202012Expand(schemaName)}
/> />
))} ))}
</Collapse> </Collapse>