UNPKG

3.86 kBJavaScriptView Raw
1import React from 'react';
2import isRequiredForA11y from 'tinper-bee-core/lib/isRequiredForA11y';
3import classnames from 'classnames';
4import PropTypes from 'prop-types';
5
6
7const propTypes = {
8 /**
9 * An html id attribute, necessary for accessibility
10 * @type {string}
11 * @required
12 */
13 id: isRequiredForA11y(PropTypes.oneOfType([
14 PropTypes.string, PropTypes.number,
15 ])),
16
17 /**
18 * Sets the direction the Popover is positioned towards.
19 */
20 placement: PropTypes.oneOf(['top', 'right', 'bottom', 'left','topLeft', 'rightTop', 'bottomLeft', 'leftTop','topRight', 'rightBottom', 'bottomRight', 'leftBottom']),
21
22 /**
23 * The "top" position value for the Popover.
24 */
25 positionTop: PropTypes.oneOfType([
26 PropTypes.number, PropTypes.string,
27 ]),
28 /**
29 * The "left" position value for the Popover.
30 */
31 positionLeft: PropTypes.oneOfType([
32 PropTypes.number, PropTypes.string,
33 ]),
34
35 /**
36 * The "top" position value for the Popover arrow.
37 */
38 arrowOffsetTop: PropTypes.oneOfType([
39 PropTypes.number, PropTypes.string,
40 ]),
41 /**
42 * The "left" position value for the Popover arrow.
43 */
44 arrowOffsetLeft: PropTypes.oneOfType([
45 PropTypes.number, PropTypes.string,
46 ]),
47};
48
49const defaultProps = {
50 placement: 'right',
51 clsPrefix: 'u-popover'
52};
53
54const PLACECLASS = {
55 right: 'right',
56 top: 'top',
57 bottom: 'bottom',
58 left: 'left',
59 rightTop: 'right-top',
60 rightBottom: 'right-bottom',
61 leftTop: 'left-top',
62 leftBottom: 'left-bottom',
63 topRight: 'top-right',
64 topLeft: 'top-left',
65 bottomLeft: 'bottom-left',
66 bottomRight: 'bottom-right'
67};
68
69class Content extends React.Component {
70 getInversePlacement(className, placement) {
71 if (placement && className && className.includes('inverse-arrow')) {
72 let inversePlacement = null;
73 if (placement.startsWith('left')) {
74 placement = placement.replace('left', 'right');
75
76 } else if (placement.startsWith('right')) {
77 placement = placement.replace('right', 'left');
78
79 } else if (placement.startsWith('top')) {
80 placement = placement.replace('top', 'bottom');
81
82 } else if (placement.startsWith('bottom')) {
83 placement = placement.replace('bottom', 'top');
84
85 }
86 return inversePlacement ? inversePlacement : placement;
87 } else {
88 return placement
89 }
90 }
91 render() {
92 const {
93 placement,
94 positionTop,
95 positionLeft,
96 arrowOffsetTop,
97 arrowOffsetLeft,
98 clsPrefix,
99 className,
100 style,
101 id,
102 children,
103 trigger,
104 ...others
105 } = this.props;
106
107 let resPlacement = this.getInversePlacement(className, placement)
108 const classes = {
109 [`${clsPrefix}`]: true,
110 [PLACECLASS[resPlacement]]: true,
111 };
112
113 const outerStyle = {
114 display: 'block',
115 top: positionTop,
116 left: positionLeft,
117 ...style,
118 };
119
120 /* const arrowStyle = {
121 top: arrowOffsetTop,
122 left: arrowOffsetLeft,
123 }; */
124
125 return (
126 <div
127 role="tooltip"
128 id={id}
129 className={classnames(className, classes)}
130 style={outerStyle}
131 {...others}
132 >
133 <div className="arrow"/>
134
135 <div className={classnames(`${clsPrefix}-content`)}>
136 { children }
137 </div>
138 </div>
139 );
140 }
141}
142
143Content.propTypes = propTypes;
144Content.defaultProps = defaultProps;
145
146export default Content;