Skip to content

Commit a8771e7

Browse files
authored
fix(oas31): resolve schemas in 'Schemas section' only if expanded (#8616)
Refs #8606
1 parent 7bfee4e commit a8771e7

File tree

2 files changed

+46
-29
lines changed

2 files changed

+46
-29
lines changed

src/core/plugins/json-schema-2020-12/components/JSONSchema/JSONSchema.jsx

+24-14
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import {
2121
JSONSchemaCyclesContext,
2222
} from "../../context"
2323

24-
const JSONSchema = forwardRef(({ schema, name }, ref) => {
24+
const JSONSchema = forwardRef(({ schema, name, onExpand }, ref) => {
2525
const fn = useFn()
2626
const isExpandedDeeply = useIsExpandedDeeply()
2727
const [expanded, setExpanded] = useState(isExpandedDeeply)
28-
const [expandedDeeply, setExpandedDeeply] = useState(false)
28+
const [expandedDeeply, setExpandedDeeply] = useState(isExpandedDeeply)
2929
const [level, nextLevel] = useLevel()
3030
const isEmbedded = useIsEmbedded()
3131
const isExpandable = fn.isExpandable(schema)
@@ -68,18 +68,6 @@ const JSONSchema = forwardRef(({ schema, name }, ref) => {
6868
const KeywordDescription = useComponent("KeywordDescription")
6969
const ExpandDeepButton = useComponent("ExpandDeepButton")
7070

71-
/**
72-
* Event handlers.
73-
*/
74-
const handleExpansion = useCallback((e, expandedNew) => {
75-
setExpanded(expandedNew)
76-
!expandedNew && setExpandedDeeply(false)
77-
}, [])
78-
const handleExpansionDeep = useCallback((e, expandedDeepNew) => {
79-
setExpanded(expandedDeepNew)
80-
setExpandedDeeply(expandedDeepNew)
81-
}, [])
82-
8371
/**
8472
* Effects handlers.
8573
*/
@@ -91,6 +79,26 @@ const JSONSchema = forwardRef(({ schema, name }, ref) => {
9179
setExpandedDeeply(expandedDeeply)
9280
}, [expandedDeeply])
9381

82+
/**
83+
* Event handlers.
84+
*/
85+
const handleExpansion = useCallback(
86+
(e, expandedNew) => {
87+
setExpanded(expandedNew)
88+
!expandedNew && setExpandedDeeply(false)
89+
onExpand(e, expandedNew, false)
90+
},
91+
[onExpand]
92+
)
93+
const handleExpansionDeep = useCallback(
94+
(e, expandedDeepNew) => {
95+
setExpanded(expandedDeepNew)
96+
setExpandedDeeply(expandedDeepNew)
97+
onExpand(e, expandedDeepNew, true)
98+
},
99+
[onExpand]
100+
)
101+
94102
return (
95103
<JSONSchemaLevelContext.Provider value={nextLevel}>
96104
<JSONSchemaDeepExpansionContext.Provider value={expandedDeeply}>
@@ -173,10 +181,12 @@ const JSONSchema = forwardRef(({ schema, name }, ref) => {
173181
JSONSchema.propTypes = {
174182
name: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
175183
schema: propTypes.schema.isRequired,
184+
onExpand: PropTypes.func,
176185
}
177186

178187
JSONSchema.defaultProps = {
179188
name: "",
189+
onExpand: () => {},
180190
}
181191

182192
export default JSONSchema

src/core/plugins/oas31/components/models.jsx

+22-15
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,39 @@ const Models = ({
2727
* Effects.
2828
*/
2929
useEffect(() => {
30-
if (isOpen && specSelectors.specResolvedSubtree(schemasPath) == null) {
30+
const isOpenAndExpanded = isOpen && defaultModelsExpandDepth > 1
31+
const isResolved = specSelectors.specResolvedSubtree(schemasPath) != null
32+
if (isOpenAndExpanded && !isResolved) {
3133
specActions.requestResolvedSubtree(schemasPath)
3234
}
33-
}, [isOpen])
35+
}, [isOpen, defaultModelsExpandDepth])
3436

3537
/**
3638
* Event handlers.
3739
*/
3840

39-
const handleCollapse = useCallback(() => {
41+
const handleModelsExpand = useCallback(() => {
4042
layoutActions.show(schemasPath, !isOpen)
41-
}, [isOpen, layoutActions])
42-
43-
const handleModelsRef = useCallback(
44-
(node) => {
45-
if (node !== null) {
46-
layoutActions.readyToScroll(schemasPath, node)
47-
}
48-
},
49-
[layoutActions]
50-
)
51-
43+
}, [isOpen])
44+
const handleModelsRef = useCallback((node) => {
45+
if (node !== null) {
46+
layoutActions.readyToScroll(schemasPath, node)
47+
}
48+
}, [])
5249
const handleJSONSchema202012Ref = (schemaName) => (node) => {
5350
if (node !== null) {
5451
layoutActions.readyToScroll([...schemasPath, schemaName], node)
5552
}
5653
}
54+
const handleJSONSchema202012Expand = (schemaName) => (e, expanded) => {
55+
if (expanded) {
56+
const schemaPath = [...schemasPath, schemaName]
57+
const isResolved = specSelectors.specResolvedSubtree(schemaPath) != null
58+
if (!isResolved) {
59+
specActions.requestResolvedSubtree([...schemasPath, schemaName])
60+
}
61+
}
62+
}
5763

5864
/**
5965
* Rendering.
@@ -72,7 +78,7 @@ const Models = ({
7278
<button
7379
aria-expanded={isOpen}
7480
className="models-control"
75-
onClick={handleCollapse}
81+
onClick={handleModelsExpand}
7682
>
7783
<span>Schemas</span>
7884
<svg width="20" height="20" aria-hidden="true" focusable="false">
@@ -87,6 +93,7 @@ const Models = ({
8793
ref={handleJSONSchema202012Ref(schemaName)}
8894
schema={schema}
8995
name={fn.upperFirst(schemaName)}
96+
onExpand={handleJSONSchema202012Expand(schemaName)}
9097
/>
9198
))}
9299
</Collapse>

0 commit comments

Comments
 (0)