/
Arrow.jsx
50 lines (42 loc) 路 1.13 KB
/
Arrow.jsx
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
import React, { Component, createElement } from 'react'
import PropTypes from 'prop-types'
const Arrow = (props, context) => {
const { component = 'span', innerRef, children, ...restProps } = props
const { popper } = context
const arrowRef = node => {
popper.setArrowNode(node)
if (typeof innerRef === 'function') {
innerRef(node)
}
}
const arrowStyle = popper.getArrowStyle()
if (typeof children === 'function') {
const arrowProps = {
ref: arrowRef,
style: arrowStyle,
}
return children({ arrowProps, restProps })
}
const componentProps = {
...restProps,
style: {
...arrowStyle,
...restProps.style,
},
}
if (typeof component === 'string') {
componentProps.ref = arrowRef
} else {
componentProps.innerRef = arrowRef
}
return createElement(component, componentProps, children)
}
Arrow.contextTypes = {
popper: PropTypes.object.isRequired,
}
Arrow.propTypes = {
component: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
innerRef: PropTypes.func,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
}
export default Arrow