UNPKG

7.74 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = PopupMixin;
7
8var React = _interopRequireWildcard(require("react"));
9
10function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
11
12function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
13
14function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
16function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
17
18function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
19
20function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
21
22function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
23
24function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
25
26function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
27
28function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
29
30function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
31
32function PopupMixin(getModal, platformProps) {
33 var _a;
34
35 return _a =
36 /*#__PURE__*/
37 function (_React$Component) {
38 _inherits(_a, _React$Component);
39
40 function _a(props) {
41 var _this;
42
43 _classCallCheck(this, _a);
44
45 _this = _possibleConstructorReturn(this, _getPrototypeOf(_a).call(this, props));
46
47 _this.onPickerChange = function (pickerValue) {
48 if (_this.state.pickerValue !== pickerValue) {
49 _this.setState({
50 pickerValue: pickerValue
51 });
52
53 var _this$props = _this.props,
54 picker = _this$props.picker,
55 pickerValueChangeProp = _this$props.pickerValueChangeProp;
56
57 if (picker && picker.props[pickerValueChangeProp]) {
58 picker.props[pickerValueChangeProp](pickerValue);
59 }
60 }
61 };
62
63 _this.saveRef = function (picker) {
64 _this.picker = picker;
65 };
66
67 _this.onTriggerClick = function (e) {
68 var child = _this.props.children;
69 var childProps = child.props || {};
70
71 if (childProps[_this.props.triggerType]) {
72 childProps[_this.props.triggerType](e);
73 }
74
75 _this.fireVisibleChange(!_this.state.visible);
76 };
77
78 _this.onOk = function () {
79 _this.props.onOk(_this.picker && _this.picker.getValue());
80
81 _this.fireVisibleChange(false);
82 };
83
84 _this.getContent = function () {
85 if (_this.props.picker) {
86 var _React$cloneElement;
87
88 var pickerValue = _this.state.pickerValue;
89
90 if (pickerValue === null) {
91 pickerValue = _this.props.value;
92 }
93
94 return React.cloneElement(_this.props.picker, (_React$cloneElement = {}, _defineProperty(_React$cloneElement, _this.props.pickerValueProp, pickerValue), _defineProperty(_React$cloneElement, _this.props.pickerValueChangeProp, _this.onPickerChange), _defineProperty(_React$cloneElement, "ref", _this.saveRef), _React$cloneElement));
95 } else {
96 return _this.props.content;
97 }
98 };
99
100 _this.onDismiss = function () {
101 _this.props.onDismiss();
102
103 _this.fireVisibleChange(false);
104 };
105
106 _this.hide = function () {
107 _this.fireVisibleChange(false);
108 };
109
110 _this.state = {
111 pickerValue: 'value' in _this.props ? _this.props.value : null,
112 visible: _this.props.visible || false
113 };
114 return _this;
115 }
116
117 _createClass(_a, [{
118 key: "componentWillReceiveProps",
119 value: function componentWillReceiveProps(nextProps) {
120 if ('value' in nextProps) {
121 this.setState({
122 pickerValue: nextProps.value
123 });
124 }
125
126 if ('visible' in nextProps) {
127 this.setVisibleState(nextProps.visible);
128 }
129 }
130 }, {
131 key: "setVisibleState",
132 value: function setVisibleState(visible) {
133 this.setState({
134 visible: visible
135 });
136
137 if (!visible) {
138 this.setState({
139 pickerValue: null
140 });
141 }
142 }
143 }, {
144 key: "fireVisibleChange",
145 value: function fireVisibleChange(visible) {
146 if (this.state.visible !== visible) {
147 if (!('visible' in this.props)) {
148 this.setVisibleState(visible);
149 }
150
151 this.props.onVisibleChange(visible);
152 }
153 }
154 }, {
155 key: "getRender",
156 value: function getRender() {
157 var props = this.props;
158 var children = props.children;
159
160 if (!children) {
161 return getModal(props, this.state.visible, {
162 getContent: this.getContent,
163 onOk: this.onOk,
164 hide: this.hide,
165 onDismiss: this.onDismiss
166 });
167 }
168
169 var _this$props2 = this.props,
170 WrapComponent = _this$props2.WrapComponent,
171 disabled = _this$props2.disabled;
172 var child = children;
173 var newChildProps = {};
174
175 if (!disabled) {
176 newChildProps[props.triggerType] = this.onTriggerClick;
177 }
178
179 return React.createElement(WrapComponent, {
180 style: props.wrapStyle
181 }, React.cloneElement(child, newChildProps), getModal(props, this.state.visible, {
182 getContent: this.getContent,
183 onOk: this.onOk,
184 hide: this.hide,
185 onDismiss: this.onDismiss
186 }));
187 }
188 }, {
189 key: "render",
190 value: function render() {
191 return this.getRender();
192 }
193 }]);
194
195 return _a;
196 }(React.Component), _a.defaultProps = _extends({
197 onVisibleChange: function onVisibleChange(_) {},
198 okText: 'Ok',
199 dismissText: 'Dismiss',
200 title: '',
201 onOk: function onOk(_) {},
202 onDismiss: function onDismiss() {}
203 }, platformProps), _a;
204}
\No newline at end of file