Skip to content

Commit 829e7ed

Browse files
committed
fix: show caret for top-level $refs
fixes https://github.com/stoplightio/platform-internal/issues/1568
1 parent 467812a commit 829e7ed

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/components/SchemaRow.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { IRowRendererOptions, Tree } from '@stoplight/tree-list';
1+
import { IRowRendererOptions, isParentNode, Tree } from '@stoplight/tree-list';
22
import cn from 'classnames';
33
import * as React from 'react';
44

55
import { getNodeMetadata, getSchemaNodeMetadata } from '../tree/metadata';
66
import { GoToRefHandler, SchemaKind, SchemaTreeListNode } from '../types';
77
import { getPrimaryType } from '../utils/getPrimaryType';
8+
import { isRefNode } from '../utils/guards';
89
import { Caret, Description, Divider, Property, Validations } from './shared';
910

1011
export interface ISchemaRow {
@@ -48,17 +49,23 @@ export const SchemaPropertyRow: typeof SchemaRow = ({ node, onGoToRef, rowOption
4849

4950
return (
5051
<>
51-
{'children' in node && Tree.getLevel(node) > 0 && (
52+
{isRefNode(schemaNode) || (isParentNode(node) && Tree.getLevel(node) > 0) ? (
5253
<Caret
5354
isExpanded={!!rowOptions.isExpanded}
5455
style={{
55-
left: ICON_DIMENSION * -1 + ROW_OFFSET / -2,
5656
width: ICON_DIMENSION,
5757
height: ICON_DIMENSION,
58+
...(isRefNode(schemaNode)
59+
? {
60+
position: 'relative',
61+
}
62+
: {
63+
left: ICON_DIMENSION * -1 + ROW_OFFSET / -2,
64+
}),
5865
}}
5966
size={ICON_SIZE}
6067
/>
61-
)}
68+
) : null}
6269

6370
{node.parent !== null &&
6471
node.parent.children.length > 0 &&

src/components/__tests__/SchemaRow.spec.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { SchemaTree } from '../../tree';
88
import { metadataStore } from '../../tree/metadata';
99
import { SchemaKind, SchemaTreeListNode } from '../../types';
1010
import { SchemaErrorRow, SchemaPropertyRow, SchemaRow } from '../SchemaRow';
11+
import { Caret } from '../shared';
1112
import { Validations } from '../shared/Validations';
1213

1314
describe('SchemaRow component', () => {
@@ -51,6 +52,36 @@ describe('SchemaRow component', () => {
5152
expect(wrapper).toHaveText('enum:null,0,false');
5253
});
5354

55+
test('should render caret for top-level $ref', () => {
56+
const schema: JSONSchema4 = {
57+
$ref: '#/definitions/foo',
58+
definitions: {
59+
foo: {
60+
type: 'string',
61+
},
62+
},
63+
};
64+
65+
const tree = new SchemaTree(schema, new TreeState(), {
66+
expandedDepth: Infinity,
67+
mergeAllOf: false,
68+
resolveRef: void 0,
69+
});
70+
71+
tree.populate();
72+
73+
const wrapper = shallow(<SchemaRow node={tree.itemAt(0)!} rowOptions={{}} />)
74+
.find(SchemaPropertyRow)
75+
.shallow();
76+
77+
expect(wrapper.find(Caret)).toExist();
78+
expect(wrapper.find(Caret)).toHaveProp('style', {
79+
height: 20,
80+
position: 'relative',
81+
width: 20,
82+
});
83+
});
84+
5485
describe('expanding errors', () => {
5586
describe('$refs', () => {
5687
let tree: SchemaTree;

0 commit comments

Comments
 (0)