Skip to content

Commit 6015f02

Browse files
committed
fix: react to props changes & remove dereferencedSchema
1 parent 61bfb97 commit 6015f02

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

src/components/JsonSchemaViewer.tsx

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ErrorBoundaryForwardedProps, FallbackComponent, withErrorBoundary } fro
22
import { TreeState, TreeStore } from '@stoplight/tree-list';
33
import { Intent, Spinner } from '@stoplight/ui-kit';
44
import cn from 'classnames';
5-
import { action, runInAction } from 'mobx';
5+
import { action } from 'mobx';
66
import * as React from 'react';
77

88
import { JSONSchema4 } from 'json-schema';
@@ -13,7 +13,6 @@ import { SchemaTree as SchemaTreeComponent } from './SchemaTree';
1313

1414
export interface IJsonSchemaViewer {
1515
schema: JSONSchema4;
16-
dereferencedSchema?: JSONSchema4;
1716
style?: object;
1817
emptyText?: string;
1918
defaultExpandedDepth?: number;
@@ -33,10 +32,6 @@ export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaVi
3332
protected readonly tree: SchemaTree;
3433
protected readonly treeState: TreeState;
3534

36-
public state = {
37-
computing: false,
38-
};
39-
4035
constructor(props: IJsonSchemaViewer & ErrorBoundaryForwardedProps) {
4136
super(props);
4237

@@ -60,10 +55,16 @@ export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaVi
6055
}
6156

6257
protected get schema() {
63-
return this.props.dereferencedSchema || this.props.schema;
58+
return this.props.schema;
6459
}
6560

6661
protected renderSchema() {
62+
if (this.tree.count > 1) {
63+
for (const child of this.tree) {
64+
this.tree.removeNode(child);
65+
}
66+
}
67+
6768
this.tree.populate();
6869
}
6970

@@ -74,17 +75,11 @@ export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaVi
7475
@action
7576
public componentDidUpdate(prevProps: Readonly<IJsonSchemaViewer>) {
7677
if (this.treeStore.defaultExpandedDepth !== this.expandedDepth) {
77-
runInAction(() => {
78-
this.treeStore.defaultExpandedDepth = this.expandedDepth;
79-
});
80-
}
81-
82-
if (
83-
prevProps.schema !== this.props.schema ||
84-
prevProps.dereferencedSchema !== this.props.dereferencedSchema ||
85-
prevProps.mergeAllOf !== this.props.mergeAllOf
86-
) {
87-
// todo: this doesn't work yet
78+
this.treeStore.defaultExpandedDepth = this.expandedDepth;
79+
this.tree.expandedDepth = this.expandedDepth;
80+
this.renderSchema();
81+
} else if (prevProps.schema !== this.props.schema || prevProps.mergeAllOf !== this.props.mergeAllOf) {
82+
this.tree.schema = this.props.schema;
8883
this.renderSchema();
8984
}
9085
}
@@ -125,6 +120,6 @@ const JsonSchemaFallbackComponent: typeof FallbackComponent = ({ error }) => {
125120

126121
export const JsonSchemaViewer = withErrorBoundary<IJsonSchemaViewer>(JsonSchemaViewerComponent, {
127122
FallbackComponent: JsonSchemaFallbackComponent,
128-
recoverableProps: ['schema', 'dereferencedSchema'],
123+
recoverableProps: ['schema'],
129124
reportErrors: false,
130125
});

src/tree/tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getNodeMetadata, metadataStore } from './metadata';
99
import { populateTree } from './populateTree';
1010

1111
export class SchemaTree extends Tree {
12-
constructor(public schema: JSONSchema4, public state: TreeState, public defaultExpandedDepth: number) {
12+
constructor(public schema: JSONSchema4, public state: TreeState, public expandedDepth: number) {
1313
super(Tree.createArtificialRoot());
1414
}
1515

@@ -26,7 +26,7 @@ export class SchemaTree extends Tree {
2626
const metadata = metadataStore.get(parentTreeNode);
2727

2828
if (metadata !== void 0 && isRefNode(metadata.schema)) return false;
29-
return level <= this.defaultExpandedDepth + 1;
29+
return level <= this.expandedDepth + 1;
3030
},
3131
});
3232
this.state.expanded = expanded;

0 commit comments

Comments
 (0)