-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathConnectedStep.tsx
More file actions
124 lines (111 loc) 路 2.67 KB
/
Copy pathConnectedStep.tsx
File metadata and controls
124 lines (111 loc) 路 2.67 KB
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
import * as React from 'react'
import { BorderRadiusObject, Shape } from '../types'
import { ITourGuideContext } from './TourGuideContext'
declare var __TEST__: boolean
interface Props {
name: string
text: string
order: number
active?: boolean
shape?: Shape
context: ITourGuideContext
children?: any
maskOffset?: number
borderRadiusObject?: BorderRadiusObject
borderRadius?: number
keepTooltipPosition?: boolean
tooltipBottomOffset?: number
}
export class ConnectedStep extends React.Component<Props> {
static defaultProps = {
active: true,
}
wrapper: any
componentDidMount() {
if (this.props.active) {
this.register()
}
}
componentDidUpdate(prevProps: Props) {
if (this.props.active !== prevProps.active) {
if (this.props.active) {
this.register()
} else {
this.unregister()
}
}
}
componentWillUnmount() {
this.unregister()
}
setNativeProps(obj: any) {
this.wrapper.setNativeProps(obj)
}
register() {
if (this.props.context && this.props.context.registerStep) {
this.props.context.registerStep({
target: this,
wrapper: this.wrapper,
...this.props,
})
} else {
console.warn('context undefined')
}
}
unregister() {
if (this.props.context.unregisterStep) {
this.props.context.unregisterStep(this.props.name)
} else {
console.warn('unregisterStep undefined')
}
}
measure() {
if (typeof __TEST__ !== 'undefined' && __TEST__) {
return new Promise((resolve) =>
resolve({
x: 0,
y: 0,
width: 0,
height: 0,
}),
)
}
return new Promise((resolve, reject) => {
const measure = () => {
// Wait until the wrapper element appears
if (this.wrapper && this.wrapper.measure) {
const {borderRadius} = this.props;
this.wrapper.measure(
(
_ox: number,
_oy: number,
width: number,
height: number,
x: number,
y: number,
) =>
resolve({
x: borderRadius ? x + borderRadius : x,
y,
width: borderRadius ? width - borderRadius * 2 : width,
height,
}),
reject,
)
} else {
requestAnimationFrame(measure)
}
}
requestAnimationFrame(measure)
})
}
render() {
const copilot = {
ref: (wrapper: any) => {
this.wrapper = wrapper
},
onLayout: () => {}, // Android hack
}
return React.cloneElement(this.props.children, { copilot })
}
}