https://github.com/xcomponent/react-gojs
GoJS React integration
https://github.com/xcomponent/react-gojs
diagram gojs gojs-diagram gojs-react javascript library react redux typescript
Last synced: 4 months ago
JSON representation
GoJS React integration
- Host: GitHub
- URL: https://github.com/xcomponent/react-gojs
- Owner: xcomponent
- License: apache-2.0
- Created: 2018-01-31T10:43:19.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-08-26T15:02:08.000Z (over 5 years ago)
- Last Synced: 2025-08-30T06:36:07.836Z (5 months ago)
- Topics: diagram, gojs, gojs-diagram, gojs-react, javascript, library, react, redux, typescript
- Language: TypeScript
- Size: 495 KB
- Stars: 61
- Watchers: 8
- Forks: 23
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
:warning: **react-gojs is no longer under active development**. Now, Northwoods provides an official React integration for GoJS: [https://github.com/NorthwoodsSoftware/gojs-react](https://github.com/NorthwoodsSoftware/gojs-react). I will not add new features to this lib, only bug fixes. The migration to the official library is pretty easy. I migrated my example [here](https://github.com/nicolaserny/gojs-react-example).
[](http://slack.xcomponent.com/)
[](https://www.npmjs.com/package/react-gojs)
[](https://www.npmjs.com/package/react-gojs)
[](https://travis-ci.org/xcomponent/react-gojs)
[](https://coveralls.io/github/xcomponent/react-gojs?branch=master)
[](https://github.com/ellerbrock/typescript-badges/)
# react-gojs
_react-gojs_ is a [GoJS](https://gojs.net/latest/index.html) React integration.
## Install
Install it from npm. It has peer dependencies of react and react-dom, which will have to be installed as well.
```bash
npm install --save react-gojs
```
or:
```bash
yarn add react-gojs
```
## Usage
Import `GojsDiagram` in your React component:
```javascript static
import GojsDiagram from 'react-gojs';
```
To create a GoJS diagram, just use the _GojsDiagram_ React component:
```tsx
```
_GojsDiagram_ is a generic React component which is responsible for rendering and updating (when the model changes) the diagram. The render step is based on the model and the go.Diagram object provided as props. It acts as a go.Diagram wrapper.
_GojsDiagram_ props:
- diagramId: _id_ required by GoJS.
- model: generic model containing nodes and links.
Model type: _DiagramModel_
Example (_Typescript / Javascript_):
```ts
const model = {
nodeDataArray: [
{ key: 'Alpha', color: 'lightblue' },
{ key: 'Beta', color: 'orange' },
{ key: 'Gamma', color: 'lightgreen' },
{ key: 'Delta', color: 'pink' },
{ key: 'Omega', color: 'grey' }
],
linkDataArray: [
{ from: 'Alpha', to: 'Beta' },
{ from: 'Alpha', to: 'Gamma' },
{ from: 'Beta', to: 'Delta' },
{ from: 'Gamma', to: 'Omega' }
]
};
```
- createDiagram: method called by the React component to create the customized GoJS diagram object. It enables you to customize the look and feel.
_Typescript_ example:
```tsx
const createDiagram = (diagramId: string): Diagram => {
const $ = go.GraphObject.make;
const myDiagram: Diagram = $(go.Diagram, diagramId, {
initialContentAlignment: go.Spot.LeftCenter
});
myDiagram.nodeTemplate = $(
go.Node,
'Auto',
$(go.Shape, 'RoundedRectangle', { strokeWidth: 0 }, new go.Binding('fill', 'color')),
$(go.TextBlock, { margin: 8 }, new go.Binding('text', 'key'))
);
return myDiagram;
};
```
_Javascript (ES6)_ example:
```jsx
const createDiagram = diagramId => {
const $ = go.GraphObject.make;
const myDiagram = $(go.Diagram, diagramId, {
initialContentAlignment: go.Spot.LeftCenter
});
myDiagram.nodeTemplate = $(
go.Node,
'Auto',
$(go.Shape, 'RoundedRectangle', { strokeWidth: 0 }, new go.Binding('fill', 'color')),
$(go.TextBlock, { margin: 8 }, new go.Binding('text', 'key'))
);
return myDiagram;
};
```
- className: css applied to the _div_ containing our diagram. You should define at least width and height.
Example:
```css
.myDiagram {
width: 70%;
height: 400px;
}
```
- onModelChange: the _onModelChange_ event occurs when the diagram model has changed (add/remove nodes/links from the UI). This event is very useful to keep your model (provided as prop) in sync with the diagram.
For example, in a Redux environment, the diagram model should be immutable (and stored in the redux store). The _onModelChange_ handler can dispatch actions to update the model.
- updateDiagramProps (optional parameter): Method allows to update/modify Diagram properties dynamically once the diagram has been rendered. It gives more control to the user, as it is a user-defined.
Basic implementation of the method.
Example 1:
```
const updateDiagramProps = (myDiagram: Diagram): void => {
myDiagram.layout = go.GraphObject.make(go.LayeredDigraphLayout, { direction: 90 });
// User can add more properties here.
};
```
Example 2:
```
const updateDiagramProps = (myDiagram: Diagram): void => {
// Empty method.
};
```
## Examples
- _Typescript_: You can find a react / redux / react-gojs example + live demo [here](https://github.com/nicolaserny/react-gojs-example).
- _Javascript (ES6)_: You can find a react / react-gojs example + live demo [here](https://github.com/nicolaserny/react-gojs-example-es6).
## Contributing
### Build and Test
```
yarn install
```
```
yarn build
```
```
yarn test
```
### Submit a Pull Request
1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Fix lint errors: `yarn lint`
4. Commit your changes: `git commit -am 'Add some feature'`
5. Push to the branch: `git push origin my-new-feature`
6. Submit a pull request
## License
[Apache License V2](https://raw.githubusercontent.com/xcomponent/react-gojs/master/LICENSE)