Skip to content

Commit a67bccf

Browse files
authored
feat: add skipTopLevelDescription option (#206)
1 parent e86fbde commit a67bccf

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

src/__tests__/__snapshots__/index.spec.tsx.snap

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,40 @@ exports[`HTML Output given write mode, should populate proper nodes 1`] = `
659659
"
660660
`;
661661
662-
exports[`HTML Output should render top-level description on allOf 1`] = `
662+
exports[`HTML Output top level descriptions should not render top-level description when skipTopLevelDescription=true 1`] = `
663+
"<div class=\\"\\" id=\\"mosaic-provider-react-aria-0-1\\">
664+
<div data-overlay-container=\\"true\\">
665+
<div class=\\"JsonSchemaViewer\\">
666+
<div></div>
667+
<div data-level=\\"0\\">
668+
<div data-id=\\"862ab7c3a6663\\">
669+
<div>
670+
<div>
671+
<div>
672+
<div>foo</div>
673+
<span>string</span>
674+
</div>
675+
</div>
676+
</div>
677+
</div>
678+
<div data-id=\\"3d3ab69efb5e7\\">
679+
<div>
680+
<div>
681+
<div>
682+
<div>baz</div>
683+
<span>string</span>
684+
</div>
685+
</div>
686+
</div>
687+
</div>
688+
</div>
689+
</div>
690+
</div>
691+
</div>
692+
"
693+
`;
694+
695+
exports[`HTML Output top level descriptions should render top-level description on allOf 1`] = `
663696
"<div class=\\"\\" id=\\"mosaic-provider-react-aria-0-1\\">
664697
<div data-overlay-container=\\"true\\">
665698
<div class=\\"JsonSchemaViewer\\">

src/__tests__/index.spec.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe('HTML Output', () => {
165165
expect(dumpDom(<JsonSchemaViewer schema={schema} />)).toMatchSnapshot();
166166
});
167167

168-
it('should render top-level description on allOf', () => {
168+
describe('top level descriptions', () => {
169169
const schema: JSONSchema4 = {
170170
description: 'This is a description that should be rendered',
171171
allOf: [
@@ -188,7 +188,15 @@ describe('HTML Output', () => {
188188
],
189189
};
190190

191-
expect(dumpDom(<JsonSchemaViewer schema={schema} defaultExpandedDepth={Infinity} />)).toMatchSnapshot();
191+
it('should render top-level description on allOf', () => {
192+
expect(dumpDom(<JsonSchemaViewer schema={schema} defaultExpandedDepth={Infinity} />)).toMatchSnapshot();
193+
});
194+
195+
it('should not render top-level description when skipTopLevelDescription=true', () => {
196+
expect(
197+
dumpDom(<JsonSchemaViewer schema={schema} defaultExpandedDepth={Infinity} skipTopLevelDescription />),
198+
).toMatchSnapshot();
199+
});
192200
});
193201
});
194202

src/components/JsonSchemaViewer.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type JsonSchemaProps = Partial<JSVOptions> & {
2626
onTreePopulated?: (props: { rootNode: RootNode; nodeCount: number }) => void;
2727
maxHeight?: number;
2828
parentCrumbs?: string[];
29+
skipTopLevelDescription?: boolean;
2930
};
3031

3132
const JsonSchemaViewerComponent = ({
@@ -37,6 +38,7 @@ const JsonSchemaViewerComponent = ({
3738
renderRootTreeLines,
3839
disableCrumbs,
3940
nodeHasChanged,
41+
skipTopLevelDescription,
4042
...rest
4143
}: JsonSchemaProps & ErrorBoundaryForwardedProps) => {
4244
const options = React.useMemo(
@@ -66,7 +68,7 @@ const JsonSchemaViewerComponent = ({
6668
<MosaicProvider>
6769
<JSVOptionsContextProvider value={options}>
6870
<Provider>
69-
<JsonSchemaViewerInner viewMode={viewMode} {...rest} />
71+
<JsonSchemaViewerInner viewMode={viewMode} skipTopLevelDescription={skipTopLevelDescription} {...rest} />
7072
</Provider>
7173
</JSVOptionsContextProvider>
7274
</MosaicProvider>
@@ -82,9 +84,18 @@ const JsonSchemaViewerInner = ({
8284
onTreePopulated,
8385
maxHeight,
8486
parentCrumbs,
87+
skipTopLevelDescription,
8588
}: Pick<
8689
JsonSchemaProps,
87-
'schema' | 'viewMode' | 'className' | 'resolveRef' | 'emptyText' | 'onTreePopulated' | 'maxHeight' | 'parentCrumbs'
90+
| 'schema'
91+
| 'viewMode'
92+
| 'className'
93+
| 'resolveRef'
94+
| 'emptyText'
95+
| 'onTreePopulated'
96+
| 'maxHeight'
97+
| 'parentCrumbs'
98+
| 'skipTopLevelDescription'
8899
>) => {
89100
const setHoveredNode = useUpdateAtom(hoveredNodeAtom);
90101
const onMouseLeave = React.useCallback(() => {
@@ -153,7 +164,7 @@ const JsonSchemaViewerInner = ({
153164
style={{ maxHeight }}
154165
>
155166
<PathCrumbs parentCrumbs={parentCrumbs} />
156-
<TopLevelSchemaRow schemaNode={jsonSchemaTreeRoot.children[0]} />
167+
<TopLevelSchemaRow schemaNode={jsonSchemaTreeRoot.children[0]} skipDescription={skipTopLevelDescription} />
157168
</Box>
158169
);
159170
};

src/components/SchemaRow/TopLevelSchemaRow.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import { getInternalSchemaError } from '../shared/Validations';
1313
import { SchemaRow, SchemaRowProps } from './SchemaRow';
1414
import { useChoices } from './useChoices';
1515

16-
export const TopLevelSchemaRow = ({ schemaNode }: Pick<SchemaRowProps, 'schemaNode'>) => {
16+
export const TopLevelSchemaRow = ({
17+
schemaNode,
18+
skipDescription,
19+
}: Pick<SchemaRowProps, 'schemaNode'> & { skipDescription?: boolean }) => {
1720
const { selectedChoice, setSelectedChoice, choices } = useChoices(schemaNode);
1821
const childNodes = React.useMemo(() => calculateChildrenToShow(selectedChoice.type), [selectedChoice.type]);
1922
const nestingLevel = 0;
@@ -27,7 +30,7 @@ export const TopLevelSchemaRow = ({ schemaNode }: Pick<SchemaRowProps, 'schemaNo
2730
return (
2831
<>
2932
<ScrollCheck />
30-
<Description value={schemaNode.annotations.description} />
33+
{!skipDescription ? <Description value={schemaNode.annotations.description} /> : null}
3134
<ChildStack
3235
schemaNode={schemaNode}
3336
childNodes={childNodes}

0 commit comments

Comments
 (0)