-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add ability to Expand/Collapse All #260
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import { isMirroredNode, isReferenceNode, isRegularNode, SchemaNode } from '@stoplight/json-schema-tree'; | ||
import { Box, Flex, NodeAnnotation, Select, SpaceVals, VStack } from '@stoplight/mosaic'; | ||
import type { ChangeType } from '@stoplight/types'; | ||
import { Atom } from 'jotai'; | ||
import { useAtomValue, useUpdateAtom } from 'jotai/utils'; | ||
import { Atom, useAtomValue, useSetAtom } from 'jotai'; | ||
import last from 'lodash/last.js'; | ||
import * as React from 'react'; | ||
|
||
|
@@ -15,7 +14,7 @@ import { Caret, Description, getValidationsFromSchema, Types, Validations } from | |
import { ChildStack } from '../shared/ChildStack'; | ||
import { Error } from '../shared/Error'; | ||
import { Properties, useHasProperties } from '../shared/Properties'; | ||
import { hoveredNodeAtom, isNodeHoveredAtom } from './state'; | ||
import { expansionModeAtom, hoveredNodeAtom, isNodeHoveredAtom } from './state'; | ||
import { useChoices } from './useChoices'; | ||
|
||
export interface SchemaRowProps { | ||
|
@@ -30,6 +29,7 @@ export const SchemaRow: React.FunctionComponent<SchemaRowProps> = React.memo( | |
({ schemaNode, nestingLevel, pl, parentNodeId, parentChangeType }) => { | ||
const { | ||
defaultExpandedDepth, | ||
maxRefDepth, | ||
renderRowAddon, | ||
renderExtensionAddon, | ||
onGoToRef, | ||
|
@@ -39,7 +39,9 @@ export const SchemaRow: React.FunctionComponent<SchemaRowProps> = React.memo( | |
viewMode, | ||
} = useJSVOptionsContext(); | ||
|
||
const setHoveredNode = useUpdateAtom(hoveredNodeAtom); | ||
const setHoveredNode = useSetAtom(hoveredNodeAtom); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to get right of |
||
|
||
const expansionMode = useAtomValue(expansionModeAtom); | ||
|
||
const nodeId = getNodeId(schemaNode, parentNodeId); | ||
|
||
|
@@ -49,7 +51,9 @@ export const SchemaRow: React.FunctionComponent<SchemaRowProps> = React.memo( | |
const hasChanged = nodeHasChanged?.({ nodeId: originalNodeId, mode }); | ||
|
||
const [isExpanded, setExpanded] = React.useState<boolean>( | ||
!isMirroredNode(schemaNode) && nestingLevel <= defaultExpandedDepth, | ||
expansionMode === 'expand_all' | ||
? !isMirroredNode(schemaNode) | ||
: !isMirroredNode(schemaNode) && nestingLevel <= defaultExpandedDepth, | ||
); | ||
|
||
const { selectedChoice, setSelectedChoice, choices } = useChoices(schemaNode); | ||
|
@@ -67,6 +71,17 @@ export const SchemaRow: React.FunctionComponent<SchemaRowProps> = React.memo( | |
const validations = isRegularNode(schemaNode) ? schemaNode.validations : {}; | ||
const hasProperties = useHasProperties({ required, deprecated, validations }); | ||
|
||
React.useEffect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't think of a better way to ensure the internal state of the row is in sync |
||
if (expansionMode === 'expand_all' && !isExpanded) { | ||
const canBeExpanded = maxRefDepth && maxRefDepth > 0 ? nestingLevel < maxRefDepth : true; | ||
if (canBeExpanded) { | ||
setExpanded(true); | ||
} | ||
} else if (expansionMode === 'collapse_all' && isExpanded) { | ||
setExpanded(false); | ||
} | ||
}, [isExpanded, expansionMode, nestingLevel, maxRefDepth]); | ||
|
||
const [totalVendorExtensions, vendorExtensions] = React.useMemo( | ||
() => extractVendorExtensions(schemaNode.fragment), | ||
[schemaNode.fragment], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { isPlainObject } from '@stoplight/json'; | ||
import { isRegularNode, RegularNode } from '@stoplight/json-schema-tree'; | ||
import { Box, Flex, HStack, Icon, Menu, Pressable } from '@stoplight/mosaic'; | ||
import { useUpdateAtom } from 'jotai/utils'; | ||
import { useSetAtom } from 'jotai'; | ||
import { isEmpty } from 'lodash'; | ||
import * as React from 'react'; | ||
|
||
|
@@ -22,7 +22,6 @@ export const TopLevelSchemaRow = ({ | |
skipDescription, | ||
}: Pick<SchemaRowProps, 'schemaNode'> & { skipDescription?: boolean }) => { | ||
const { renderExtensionAddon } = useJSVOptionsContext(); | ||
|
||
const { selectedChoice, setSelectedChoice, choices } = useChoices(schemaNode); | ||
const childNodes = React.useMemo(() => visibleChildren(selectedChoice.type), [selectedChoice.type]); | ||
const nestingLevel = 0; | ||
|
@@ -150,7 +149,7 @@ function ScrollCheck() { | |
const elementRef = React.useRef<HTMLDivElement>(null); | ||
|
||
const isOnScreen = useIsOnScreen(elementRef); | ||
const setShowPathCrumbs = useUpdateAtom(showPathCrumbsAtom); | ||
const setShowPathCrumbs = useSetAtom(showPathCrumbsAtom); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to get right of |
||
React.useEffect(() => { | ||
setShowPathCrumbs(!isOnScreen); | ||
}, [isOnScreen, setShowPathCrumbs]); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to only show this button when the schema as any properties with schema definition that can be expanded/collapsed.
How can I detect this?