Skip to content

Commit 2d352e2

Browse files
author
Nicolas ERNY
authored
Bug(#111): fix initial render (#112)
* Bug(#111): fix initial render * Fix lint
1 parent d054bc4 commit 2d352e2

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-gojs",
3-
"version": "4.5.0",
3+
"version": "4.6.0",
44
"description": "GoJS React integration",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/GojsDiagram.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ModelChangeEventType } from './modelChangeEvent';
77

88
const groupName = 'myGroup';
99
const singleNode = 'singleNode';
10-
10+
jest.useFakeTimers();
1111
describe('<GojsDiagram />', () => {
1212
const portFrom = 'R';
1313
const portTo = 'L';
@@ -96,6 +96,8 @@ describe('<GojsDiagram />', () => {
9696
let keyIndex = 0;
9797

9898
beforeEach(() => {
99+
Object.defineProperty(Element.prototype, 'clientWidth', { value: 100 });
100+
Object.defineProperty(Element.prototype, 'clientHeight', { value: 100 });
99101
keyIndex = 0;
100102
const dom = document.body;
101103
modelChangeCallback = jest.fn();
@@ -133,6 +135,7 @@ describe('<GojsDiagram />', () => {
133135
/>,
134136
{ attachTo: dom }
135137
);
138+
jest.runAllTimers();
136139
});
137140

138141
it('should default to "category" for nodeCategoryProperty', () => {
@@ -223,9 +226,7 @@ describe('<GojsDiagram />', () => {
223226

224227
testCases.forEach(test => {
225228
// tslint:disable-next-line:max-line-length
226-
it(`should update the render of the diagram (nodes and links) based on the new model provided as prop - case: ${
227-
test.description
228-
}`, () => {
229+
it(`should update the render of the diagram (nodes and links) based on the new model provided as prop - case: ${test.description}`, () => {
229230
checkIfDiagramRendersModel(model, diagram);
230231
wrapper.setProps({ model: test.updatedModel });
231232
checkIfDiagramRendersModel(test.updatedModel, diagram);

src/GojsDiagram.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export interface GojsModel extends go.Model {
4242
class GojsDiagram<N extends BaseNodeModel, L extends LinkModel> extends React.PureComponent<GojsDiagramProps<N, L>>
4343
implements DiagramNotificationDelegate<N, L> {
4444
private myDiagram: Diagram;
45+
private divRef: React.RefObject<HTMLDivElement>;
46+
47+
private mountInterval;
48+
4549
private eventsToDispatch: ModelChangeEvent<N, L>[];
4650
private modelChangedHandlers = [
4751
new AddNodeModelChangedHandler<N, L>(),
@@ -55,19 +59,36 @@ class GojsDiagram<N extends BaseNodeModel, L extends LinkModel> extends React.Pu
5559

5660
constructor(props: GojsDiagramProps<N, L>) {
5761
super(props);
62+
this.divRef = React.createRef();
5863
this.eventsToDispatch = [];
5964
this.modelChangedHandler = this.modelChangedHandler.bind(this);
6065
}
6166

6267
componentDidMount() {
63-
this.init();
68+
let intervalCount = 0;
69+
this.mountInterval = setInterval(() => {
70+
if (this.divRef.current && this.divRef.current.clientWidth && this.divRef.current.clientHeight) {
71+
this.init();
72+
clearInterval(this.mountInterval);
73+
} else {
74+
if (intervalCount > 10) {
75+
clearInterval(this.mountInterval);
76+
}
77+
intervalCount++;
78+
}
79+
}, 10);
6480
}
6581

6682
componentWillUnmount() {
67-
if (this.props.onModelChange) {
83+
if (this.myDiagram && this.props.onModelChange) {
6884
this.myDiagram.removeModelChangedListener(this.modelChangedHandler);
6985
}
70-
this.myDiagram.clear();
86+
if (this.myDiagram) {
87+
this.myDiagram.clear();
88+
}
89+
if (this.mountInterval) {
90+
clearInterval(this.mountInterval);
91+
}
7192
}
7293

7394
componentDidUpdate() {
@@ -109,7 +130,7 @@ class GojsDiagram<N extends BaseNodeModel, L extends LinkModel> extends React.Pu
109130
}
110131
}
111132
render() {
112-
return <div id={this.props.diagramId} className={this.props.className} />;
133+
return <div ref={this.divRef} id={this.props.diagramId} className={this.props.className} />;
113134
}
114135

115136
enqueueEvent(event: ModelChangeEvent<N, L>) {

0 commit comments

Comments
 (0)