-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
150 lines (134 loc) · 4.19 KB
/
index.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* A smart anchor tag view for react-native apps
* https://github.com/ljunb/react-native-anchor-tag/
* Released under the MIT license
* Copyright (c) 2017 ljunb <cookiejlim@gmail.com>
*/
import React, {Component} from 'react';
import {
View,
Text,
StyleSheet,
Dimensions
} from 'react-native';
const {width: screenW} = Dimensions.get('window');
// 45° line length
const LINE_LENGTH = 10;
// anchor label max width
const MAX_LABEL_WIDTH = 70;
// translucent dot width and height
const OUTER_DOT_WIDTH = 8;
// white dot width and height
const INNER_DOT_WIDTH = 4;
// default anchors wrapper width
const DEFAULT_BACKGROUND_WIDTH = screenW - 15 * 2;
// defualt anchor label padding
const DEFAULT_LABEL_PADDING = 4;
export default class AnchorView extends Component {
static propTypes = {
anchor: React.PropTypes.object,
backgroundWidth: React.PropTypes.number,
};
static defaultProps = {
backgroundWidth: DEFAULT_BACKGROUND_WIDTH,
};
state = {width: 0};
shouldComponentUpdate(nextProps, nextState) {
const {anchor: {name: nextName}} = nextProps;
const {width: nextWidth} = nextState;
return this.props.anchor.name !== nextName || this.state.width !== nextWidth;
}
handleLabelWidth = evt => {
const {width} = evt.nativeEvent.layout;
if (this.state.width !== width) {
this.setState({width});
}
};
render() {
const {width} = this.state;
const {anchor: {name, x, y}, backgroundWidth} = this.props;
// anchor label max width
const maxLabelWidth = (width > MAX_LABEL_WIDTH ? MAX_LABEL_WIDTH : width) + DEFAULT_LABEL_PADDING * 2;
// anchor position
const left = x || 0, top = y || 0;
const isLeftDirection = (left + maxLabelWidth) >= backgroundWidth;
// line style
const line45Style = isLeftDirection ?
{transform: [{rotate: '45deg'}], right: (INNER_DOT_WIDTH + OUTER_DOT_WIDTH) / 2} :
{transform: [{rotate: '-45deg'}], left: (INNER_DOT_WIDTH + OUTER_DOT_WIDTH) / 2};
const line180Style = isLeftDirection ? {right: LINE_LENGTH + 1} : {left: LINE_LENGTH + 1};
return (
<View style={[styles.container, {top, left}]}>
<Dot style={styles.realDot}/>
<View style={[styles.line45, line45Style]}/>
<View style={[styles.line180, line180Style]} onLayout={this.handleLabelWidth}>
<Text numberOfLines={2} style={styles.text}>{name || 'Error key!'}</Text>
</View>
</View>
)
}
}
const Dot = () => {
return (
<View style={[styles.dotWrapper]}>
<View style={[styles.dot]}/>
</View>
)
};
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
position: 'absolute'
},
dotWrapper: {
backgroundColor: 'rgba(0,0,0,0.15)',
width: OUTER_DOT_WIDTH,
height: OUTER_DOT_WIDTH,
borderRadius: OUTER_DOT_WIDTH / 2,
justifyContent: 'center',
alignItems: 'center'
},
dot: {
width: INNER_DOT_WIDTH,
height: INNER_DOT_WIDTH,
backgroundColor: '#ffffff',
borderRadius: INNER_DOT_WIDTH / 2
},
text: {
backgroundColor: 'transparent',
color: '#fff',
fontSize: 12,
lineHeight: 14,
maxWidth: MAX_LABEL_WIDTH
},
realDot: {
position: 'absolute',
bottom: 0
},
line45: {
height: LINE_LENGTH,
width: LINE_LENGTH,
transform: [{rotate: '45deg'}],
borderTopWidth: 1,
borderColor: '#ffffff',
position: 'absolute',
bottom: 0,
shadowOffset: {width: 0, height: 0},
shadowColor: 'black',
shadowOpacity: 0.8,
shadowRadius: 2,
backgroundColor: 'transparent'
},
line180: {
padding: DEFAULT_LABEL_PADDING,
borderBottomWidth: 1,
borderColor: '#ffffff',
position: 'absolute',
bottom: LINE_LENGTH + 1,
shadowOffset: {width: 0, height: 0},
shadowColor: 'black',
shadowOpacity: 0.8,
shadowRadius: 2,
backgroundColor: 'transparent'
},
});