diff --git a/example/App.js b/example/App.js index 3b8c3c4..79abba8 100644 --- a/example/App.js +++ b/example/App.js @@ -1,6 +1,7 @@ import React from 'react'; import FirstExample from './FirstExample'; import SecondExample from './SecondExample'; +import ThirdExample from './ThirdExample'; class App extends React.Component { state = { currentExample: 1 }; @@ -11,6 +12,8 @@ class App extends React.Component { return FirstExample; case 2: return SecondExample; + case 3: + return ThirdExample; default: return SecondExample; } @@ -29,6 +32,7 @@ class App extends React.Component {

Choose an example:

+
diff --git a/example/ThirdExample.js b/example/ThirdExample.js new file mode 100644 index 0000000..457bb1d --- /dev/null +++ b/example/ThirdExample.js @@ -0,0 +1,83 @@ +import React, { Component } from 'react'; +import ArcherContainer from '../src/ArcherContainer'; +import ArcherElement from '../src/ArcherElement'; + +const boxStyle = { padding: '10px', border: '1px solid black' }; +const elementStyle = { + width: '100px', + height: '30px', + position: 'absolute', + textAlign: 'center', +}; + +const initialState = { + elements: [{ + id: 'root', + label: 'Root', + position: { x: 400, y: 0 }, + relations: [{ + from: { anchor: 'bottom' }, + to: { anchor: 'top', id: 'element2' }, + }], + }, { + id: 'element2', + label: 'Element 2', + position: { x: 100, y: 200 }, + relations: [{ + from: { anchor: 'right' }, + to: { anchor: 'left', id: 'element3' }, + label:
Arrow 2
, + }], + }, { + id: 'element3', + label: 'Element 3', + position: { x: 600, y: 200 }, + relations: [], + }], +}; + +class ThirdExample extends Component { + constructor(props) { + super(props); + this.state = initialState; + this.onArrowClick = this.onArrowClick.bind(this) + } + deleteRelation(from, to) { + const elements = this.state.elements.map(el => { + if (el.id !== from) return el; + const relations = el.relations.filter(rel => { + return rel.to.id !== to; + }); + return { ...el, relations }; + }); + this.setState({ elements }); + } + renderElement(el) { + const { id, label, relations, position } = el; + return ( + +
{label}
+
+ ); + } + onArrowClick(evt, markerId, from, to) { + const { nativeEvent } = evt; + this.deleteRelation(from, to); + } + render() { + return ( +
+ + {this.state.elements.map(this.renderElement)} + +
+ ); + } +} + +export default ThirdExample; diff --git a/src/ArcherContainer.js b/src/ArcherContainer.js index a941da3..db4a559 100644 --- a/src/ArcherContainer.js +++ b/src/ArcherContainer.js @@ -14,6 +14,7 @@ type Props = { children: React$Node, style?: Object, className?: string, + onClick?: Function, }; type State = { @@ -252,6 +253,13 @@ export class ArcherContainer extends React.Component { to.id, parentCoordinates, ); + const markerId = this.getMarkerId(from, to); + const clickHandler = this.props.onClick + ? (evt) => { + if (this.props.onClick) { + this.props.onClick(evt, markerId, from.id, to.id); + } + } : null; const strokeColor = (style && style.strokeColor) || this.props.strokeColor; @@ -273,7 +281,8 @@ export class ArcherContainer extends React.Component { arrowLength={arrowLength} strokeWidth={strokeWidth} arrowLabel={label} - arrowMarkerId={this.getMarkerId(from, to)} + arrowMarkerId={markerId} + onClick={clickHandler} /> ); }); @@ -337,7 +346,7 @@ export class ArcherContainer extends React.Component { }} >
@@ -345,7 +354,9 @@ export class ArcherContainer extends React.Component { {SvgArrows} -
{this.props.children}
+
+ {this.props.children} +
); diff --git a/src/SvgArrow.js b/src/SvgArrow.js index 0e1263b..3b88e6f 100644 --- a/src/SvgArrow.js +++ b/src/SvgArrow.js @@ -13,6 +13,7 @@ type Props = { strokeWidth: number, arrowLabel?: ?React$Node, arrowMarkerId: string, + onClick?: ?Function, }; function computeEndingArrowDirectionVector(endingAnchor) { @@ -121,6 +122,7 @@ const SvgArrow = ({ strokeWidth, arrowLabel, arrowMarkerId, + onClick, }: Props) => { const actualArrowLength = arrowLength * 2; @@ -155,15 +157,18 @@ const SvgArrow = ({ const { xl, yl, wl, hl } = computeLabelDimensions(xs, ys, xe, ye); + const cursor = onClick && 'pointer' || ''; + return ( - + {arrowLabel && ( - +