UNPKG

3.29 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
3import _createClass from 'babel-runtime/helpers/createClass';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6var __rest = this && this.__rest || function (s, e) {
7 var t = {};
8 for (var p in s) {
9 if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
10 }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
11 if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]];
12 }return t;
13};
14import React from 'react';
15import Tooltip from 'rmc-tooltip';
16import Item from './Item';
17function recursiveCloneChildren(children) {
18 var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (ch, _) {
19 return ch;
20 };
21
22 return React.Children.map(children, function (child, index) {
23 var newChild = cb(child, index);
24 if (typeof newChild !== 'string' && typeof newChild !== 'number' && newChild && newChild.props && newChild.props.children) {
25 return React.cloneElement(newChild, {}, recursiveCloneChildren(newChild.props.children, cb));
26 }
27 return newChild;
28 });
29}
30
31var Popover = function (_React$Component) {
32 _inherits(Popover, _React$Component);
33
34 function Popover() {
35 _classCallCheck(this, Popover);
36
37 return _possibleConstructorReturn(this, (Popover.__proto__ || Object.getPrototypeOf(Popover)).apply(this, arguments));
38 }
39
40 _createClass(Popover, [{
41 key: 'render',
42 value: function render() {
43 var _a = this.props,
44 overlay = _a.overlay,
45 _a$onSelect = _a.onSelect,
46 onSelect = _a$onSelect === undefined ? function () {} : _a$onSelect,
47 restProps = __rest(_a, ["overlay", "onSelect"]);
48 var overlayNode = recursiveCloneChildren(overlay, function (child, index) {
49 var extraProps = { firstItem: false };
50 if (child && typeof child !== 'string' && typeof child !== 'number' && child.type &&
51 // Fixme: not sure where the `myName` came from.
52 child.type.myName === 'PopoverItem' && !child.props.disabled) {
53 extraProps.onClick = function () {
54 return onSelect(child, index);
55 };
56 extraProps.firstItem = index === 0;
57 return React.cloneElement(child, extraProps);
58 }
59 return child;
60 });
61 var wrapperNode = React.createElement(
62 'div',
63 { className: this.props.prefixCls + '-inner-wrapper' },
64 overlayNode
65 );
66 return React.createElement(Tooltip, _extends({}, restProps, { overlay: wrapperNode }));
67 }
68 }]);
69
70 return Popover;
71}(React.Component);
72
73export default Popover;
74
75Popover.defaultProps = {
76 prefixCls: 'am-popover',
77 placement: 'bottomRight',
78 align: { overflow: { adjustY: 0, adjustX: 0 } },
79 trigger: ['click']
80};
81Popover.Item = Item;
\No newline at end of file