forked from MrBlenny/react-flow-chart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomLink.tsx
89 lines (84 loc) · 2.4 KB
/
CustomLink.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { cloneDeep, mapValues } from 'lodash'
import * as React from 'react'
import styled from 'styled-components'
import { FlowChart, LinkDefault } from '../src'
import * as actions from '../src/container/actions'
import { Page } from './components'
import { chartSimple } from './misc/exampleChartState'
const Label = styled.div`
position: absolute;
`
const Button = styled.div`
position: absolute;
top: 0px;
right: 0px;
padding: 5px;
height: 15px;
width: 15px;
transform: translate(50%, -50%);
background: red;
color: white;
border-radius: 50%;
transition: 0.3s ease all;
display: flex;
align-items: center;
justify-content: center;
font-size: 10px;
cursor: pointer;
&:hover {
box-shadow: 0 10px 20px rgba(0,0,0,.1);
}
`
const LabelContent = styled.div`
padding: 5px 10px;
background: cornflowerblue;
color: white;
border-radius: 5px;
display: flex;
align-items: center;
justify-content: center;
font-size: 10px;
cursor: pointer;
`
export class CustomLinkDemo extends React.Component {
public state = cloneDeep(chartSimple)
public render () {
const chart = this.state
const stateActions = mapValues(actions, (func: any) =>
(...args: any) => this.setState(func(...args))) as typeof actions
return (
<Page>
<FlowChart
chart={chart}
callbacks={stateActions}
Components={{
Link: (props) => {
const { startPos, endPos, onLinkClick, link } = props
const centerX = startPos.x + (endPos.x - startPos.x) / 2
const centerY = startPos.y + (endPos.y - startPos.y) / 2
return (
<>
<LinkDefault {...props} />
<Label style={{ left: centerX, top: centerY }}>
{ props.link.properties && props.link.properties.label && (
<LabelContent>{props.link.properties && props.link.properties.label}</LabelContent>
)}
<Button
onClick={(e) => {
onLinkClick({ linkId: link.id })
stateActions.onDeleteKey({})
e.stopPropagation()
}}
>
x
</Button>
</Label>
</>
)
},
}}
/>
</Page>
)
}
}