Skip to content

Commit 9152242

Browse files
committed
feat: onTreePopulate handler
1 parent 0317064 commit 9152242

File tree

9 files changed

+232
-39
lines changed

9 files changed

+232
-39
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"build": "sl-scripts bundle --sourcemap",
2424
"build.docs": "build-storybook -c .storybook -o docs-auto",
2525
"commit": "git-cz",
26-
"lint": "tslint -c tslint.json -p tsconfig.json 'src/**/*.ts?'",
26+
"lint": "tslint -c tslint.json -p tsconfig.json 'src/**/*.{ts,tsx}'",
2727
"lint.fix": "yarn lint --fix",
2828
"release": "sl-scripts release",
2929
"release.docs": "sl-scripts release:docs",

src/components/JsonSchemaViewer.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { action } from 'mobx';
55
import * as React from 'react';
66

77
import { JSONSchema4 } from 'json-schema';
8-
import { SchemaTree, SchemaTreeOptions, SchemaTreeRefDereferenceFn } from '../tree/tree';
8+
import { SchemaTree, SchemaTreeOptions, SchemaTreePopulateHandler, SchemaTreeRefDereferenceFn } from '../tree/tree';
99
import { GoToRefHandler, RowRenderer } from '../types';
1010
import { isSchemaViewerEmpty } from '../utils/isSchemaViewerEmpty';
1111
import { SchemaTree as SchemaTreeComponent } from './SchemaTree';
@@ -24,6 +24,7 @@ export interface IJsonSchemaViewer {
2424
mergeAllOf?: boolean;
2525
FallbackComponent?: typeof FallbackComponent;
2626
rowRenderer?: RowRenderer;
27+
onTreePopulate?: SchemaTreePopulateHandler;
2728
resolveRef?: SchemaTreeRefDereferenceFn;
2829
shouldResolveEagerly?: boolean;
2930
}
@@ -49,6 +50,7 @@ export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaVi
4950
mergeAllOf: this.mergeAllOf,
5051
resolveRef: this.props.resolveRef,
5152
shouldResolveEagerly: !!this.props.shouldResolveEagerly,
53+
onPopulate: this.props.onTreePopulate,
5254
};
5355
}
5456

@@ -86,6 +88,10 @@ export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaVi
8688
this.tree.treeOptions.resolveRef = this.props.resolveRef;
8789
}
8890

91+
if (prevProps.onTreePopulate !== this.props.onTreePopulate) {
92+
this.tree.treeOptions.onPopulate = this.props.onTreePopulate;
93+
}
94+
8995
if (
9096
this.treeStore.defaultExpandedDepth !== this.expandedDepth ||
9197
prevProps.schema !== this.props.schema ||

src/components/__tests__/Property.spec.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ describe('Property component', () => {
161161
mergeAllOf: false,
162162
resolveRef: void 0,
163163
shouldResolveEagerly: false,
164+
onPopulate: void 0,
164165
});
165166

166167
tree.populate();
@@ -186,6 +187,7 @@ describe('Property component', () => {
186187
mergeAllOf: false,
187188
resolveRef: void 0,
188189
shouldResolveEagerly: false,
190+
onPopulate: void 0,
189191
});
190192

191193
tree.populate();
@@ -211,6 +213,7 @@ describe('Property component', () => {
211213
mergeAllOf: false,
212214
resolveRef: void 0,
213215
shouldResolveEagerly: false,
216+
onPopulate: void 0,
214217
});
215218

216219
tree.populate();
@@ -237,6 +240,7 @@ describe('Property component', () => {
237240
mergeAllOf: false,
238241
resolveRef: void 0,
239242
shouldResolveEagerly: false,
243+
onPopulate: void 0,
240244
});
241245

242246
tree.populate();

src/components/__tests__/SchemaRow.spec.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ describe('SchemaRow component', () => {
6767
mergeAllOf: false,
6868
resolveRef: void 0,
6969
shouldResolveEagerly: false,
70+
onPopulate: void 0,
7071
});
7172

7273
tree.populate();
@@ -96,6 +97,7 @@ describe('SchemaRow component', () => {
9697
mergeAllOf: false,
9798
resolveRef: void 0,
9899
shouldResolveEagerly: false,
100+
onPopulate: void 0,
99101
});
100102

101103
tree.populate();
@@ -131,6 +133,7 @@ describe('SchemaRow component', () => {
131133
mergeAllOf: false,
132134
resolveRef: void 0,
133135
shouldResolveEagerly: false,
136+
onPopulate: void 0,
134137
});
135138

136139
tree.populate();
@@ -189,6 +192,7 @@ describe('SchemaRow component', () => {
189192
mergeAllOf: false,
190193
resolveRef: void 0,
191194
shouldResolveEagerly: false,
195+
onPopulate: void 0,
192196
});
193197

194198
tree.populate();

src/components/shared/Property.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ function count(obj: Optional<JSONSchema4 | null>): number | null {
2626
function shouldShowPropertyName(treeNode: SchemaTreeListNode) {
2727
if (treeNode.parent === null) return false;
2828
try {
29-
const { schema } = getSchemaNodeMetadata(treeNode.parent);
30-
let type = getPrimaryType(schema);
29+
const { schemaNode } = getSchemaNodeMetadata(treeNode.parent);
30+
if (!('type' in schemaNode)) {
31+
return false;
32+
}
33+
34+
let type = schemaNode.type;
3135

32-
if (type === SchemaKind.Array && schema.items) {
33-
type = getPrimaryType(schema.items);
36+
if (type === SchemaKind.Array && (schemaNode as IArrayNode).items) {
37+
type = getPrimaryType((schemaNode as IArrayNode).items!);
3438
}
3539

3640
return type === SchemaKind.Object;

0 commit comments

Comments
 (0)