react-diagrams icon indicating copy to clipboard operation
react-diagrams copied to clipboard

getting error "this.props.node.getInPorts is not a function" while mimicking Demo custom node.

Open attakhan opened this issue 7 years ago • 2 comments

I have mimic the Demo of custom node. Initially i was facing import issue due to SSR. but i have solved it with dynamic importing . the custom node code is creating node , port and links code as expected . But when i am adding the node into diagramModel . i am getting "this.props.node.getInPorts is not a function" error. any help and suggestion .?

attakhan avatar Jun 25 '19 16:06 attakhan

If your custom node model didn't extend DefaultNodeModel, then you'd need to add that in yourself. I'd imagine it's easier to just extend it though. Here's what the DefaultNodeModel.ts file looks like:

import { DefaultPortModel } from "./DefaultPortModel";
import * as _ from "lodash";

import { NodeModel, NodeModelListener } from "../../models/NodeModel";
import { Toolkit } from "../../Toolkit";
import { DiagramEngine } from "../../DiagramEngine";

/**
 * @author Dylan Vorster
 */
export class DefaultNodeModel extends NodeModel<NodeModelListener> {
	name: string;
	color: string;
	ports: { [s: string]: DefaultPortModel };

	constructor(name: string = "Untitled", color: string = "rgb(0,192,255)") {
		super("default");
		this.name = name;
		this.color = color;
	}

	addInPort(label: string): DefaultPortModel {
		return this.addPort(new DefaultPortModel(true, Toolkit.UID(), label));
	}

	addOutPort(label: string): DefaultPortModel {
		return this.addPort(new DefaultPortModel(false, Toolkit.UID(), label));
	}

	deSerialize(object, engine: DiagramEngine) {
		super.deSerialize(object, engine);
		this.name = object.name;
		this.color = object.color;
	}

	serialize() {
		return _.merge(super.serialize(), {
			name: this.name,
			color: this.color
		});
	}

	getInPorts(): DefaultPortModel[] {
		return _.filter(this.ports, portModel => {
			return portModel.in;
		});
	}

	getOutPorts(): DefaultPortModel[] {
		return _.filter(this.ports, portModel => {
			return !portModel.in;
		});
	}
}

jackietieu avatar Jun 25 '19 18:06 jackietieu

solved

attakhan avatar Jul 06 '19 14:07 attakhan