Skip to content

Commit a4c440d

Browse files
authored
export useChoices and other utilities for elements (#241)
1 parent 431fc71 commit a4c440d

File tree

7 files changed

+61
-12
lines changed

7 files changed

+61
-12
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"oneOf": [
3+
{
4+
"allOf": [
5+
{
6+
"type": "object",
7+
"properties": {
8+
"foo": {
9+
"type": "string"
10+
}
11+
}
12+
},
13+
{
14+
"type": "object",
15+
"properties": {
16+
"bar": {
17+
"type": "number"
18+
}
19+
}
20+
}
21+
]
22+
},
23+
{
24+
"allOf": [
25+
{
26+
"type": "object",
27+
"properties": {
28+
"baz": {
29+
"type": "string"
30+
}
31+
}
32+
},
33+
{
34+
"type": "object",
35+
"properties": {
36+
"boggle": {
37+
"type": "number"
38+
}
39+
}
40+
}
41+
]
42+
}
43+
]
44+
}

src/components/SchemaRow/SchemaRow.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as React from 'react';
99
import { COMBINER_NAME_MAP } from '../../consts';
1010
import { useJSVOptionsContext } from '../../contexts';
1111
import { getNodeId, getOriginalNodeId } from '../../hash';
12-
import { calculateChildrenToShow, isPropertyRequired } from '../../tree';
12+
import { isPropertyRequired, visibleChildren } from '../../tree';
1313
import { Caret, Description, getValidationsFromSchema, Types, Validations } from '../shared';
1414
import { ChildStack } from '../shared/ChildStack';
1515
import { Error } from '../shared/Error';
@@ -55,7 +55,7 @@ export const SchemaRow: React.FunctionComponent<SchemaRowProps> = React.memo(
5555
const description = isRegularNode(typeToShow) ? typeToShow.annotations.description : null;
5656

5757
const rootLevel = renderRootTreeLines ? 1 : 2;
58-
const childNodes = React.useMemo(() => calculateChildrenToShow(typeToShow), [typeToShow]);
58+
const childNodes = React.useMemo(() => visibleChildren(typeToShow), [typeToShow]);
5959
const combiner = isRegularNode(schemaNode) && schemaNode.combiners?.length ? schemaNode.combiners[0] : null;
6060
const isCollapsible = childNodes.length > 0;
6161
const isRootLevel = nestingLevel < rootLevel;

src/components/SchemaRow/TopLevelSchemaRow.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as React from 'react';
66

77
import { COMBINER_NAME_MAP } from '../../consts';
88
import { useIsOnScreen } from '../../hooks/useIsOnScreen';
9-
import { calculateChildrenToShow, isComplexArray } from '../../tree';
9+
import { isComplexArray, visibleChildren } from '../../tree';
1010
import { showPathCrumbsAtom } from '../PathCrumbs/state';
1111
import { Description, getValidationsFromSchema, Validations } from '../shared';
1212
import { ChildStack } from '../shared/ChildStack';
@@ -19,7 +19,7 @@ export const TopLevelSchemaRow = ({
1919
skipDescription,
2020
}: Pick<SchemaRowProps, 'schemaNode'> & { skipDescription?: boolean }) => {
2121
const { selectedChoice, setSelectedChoice, choices } = useChoices(schemaNode);
22-
const childNodes = React.useMemo(() => calculateChildrenToShow(selectedChoice.type), [selectedChoice.type]);
22+
const childNodes = React.useMemo(() => visibleChildren(selectedChoice.type), [selectedChoice.type]);
2323
const nestingLevel = 0;
2424

2525
const nodeId = schemaNode.fragment?.['x-stoplight']?.id;

src/components/SchemaRow/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './SchemaRow';
22
export * from './TopLevelSchemaRow';
3+
export { Choice, useChoices } from './useChoices';

src/components/SchemaRow/useChoices.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import * as React from 'react';
66
import { isComplexArray, isNonEmptyParentNode } from '../../tree';
77
import { printName } from '../../utils';
88

9-
type Choice = {
9+
/** one option among several mutually exclusive sub-schemas */
10+
export type Choice = {
1011
title: string;
1112
type: SchemaNode;
1213
};
@@ -53,10 +54,11 @@ function makeArrayChoice(node: SchemaNode, combiner?: string): Choice {
5354
}
5455

5556
/**
56-
* Calculates type choices for a given node.
57+
* Enumerates the sub-schema type for a given node.
5758
*
58-
* Usually a node has one choice - only one possible type -, itself.
59-
* If a node is an oneOf or anyOf combiner, the possible types are the sub-types of the combiner.
59+
* Usually a node has one choice, only one possible type: itself. If a node is
60+
* a oneOf or anyOf combiner, the possible types are the sub-types of the
61+
* combiner.
6062
*/
6163
export const useChoices = (schemaNode: SchemaNode) => {
6264
const choices: Choice[] = React.useMemo(() => {

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export { JsonSchemaViewer, Validations } from './components';
1+
export { Choice, JsonSchemaViewer, useChoices, Validations } from './components';
2+
export { visibleChildren } from './tree';
23
export * from './types';

src/tree/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ export function isComplexArray(node: SchemaNode): node is ComplexArrayNode {
4141
}
4242

4343
/**
44-
* Returns the children of `node` that should be displayed in the tree.
45-
* Defaults to `node.children`, except for Arrays that get special handling (flattening).
44+
* Returns the children of `node` that should be displayed in a viewer or
45+
* editor. Defaults to `node.children`, except for Arrays that get special
46+
* handling (flattening).
4647
*/
47-
export function calculateChildrenToShow(node: SchemaNode): SchemaNode[] {
48+
export function visibleChildren(node: SchemaNode): SchemaNode[] {
4849
if (!isRegularNode(node) || isPrimitiveArray(node)) {
4950
return [];
5051
}

0 commit comments

Comments
 (0)