ukelli-ui
Version:
Base on React's UI lib. Make frontend's dev simpler and faster.
117 lines (116 loc) • 4.55 kB
JavaScript
/* eslint-disable max-classes-per-file */
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Popover from './popover';
import setDOMById, { destoryDOM } from '../set-dom';
var PopoverWrapper = /** @class */ (function (_super) {
__extends(PopoverWrapper, _super);
function PopoverWrapper(props) {
var _this = _super.call(this, props) || this;
_this.state = {
relativeElem: document.body,
open: false,
children: null
};
return _this;
}
PopoverWrapper.prototype.closePopover = function () {
console.warn('closePopover 要被废弃了,请使用 close');
this.close();
};
PopoverWrapper.prototype.close = function () {
this.setState({
open: false
});
};
PopoverWrapper.prototype.show = function (elem, isShow, children, props) {
var _isShow = typeof isShow !== 'undefined' ? isShow : !this.state.open;
this.setState(function (prevState) { return (__assign(__assign({}, props), { relativeElem: elem || prevState.relativeElem, open: _isShow, children: children || prevState.children })); });
};
PopoverWrapper.prototype.render = function () {
var _this = this;
return (React.createElement(Popover, __assign({}, this.props, this.state, { onClose: function (e) { return _this.close(); } })));
};
return PopoverWrapper;
}(Component));
export { PopoverWrapper };
var PopoverEntity = /** @class */ (function () {
function PopoverEntity(options) {
var _this = this;
if (options === void 0) { options = {}; }
this.idPrefix = 'pop_';
this.prevOptions = {};
this.savePopWrapper = function (e) {
if (!e)
return;
_this.popoverEntity = e;
};
this.destroy = function () {
_this.close();
destoryDOM(_this.id);
};
var _a = options.id, id = _a === void 0 ? 'topPopover' : _a, _b = options.fixed, fixed = _b === void 0 ? false : _b;
this.id = this.idPrefix + id;
this.prevProps = { fixed: fixed };
this.initDOM({});
}
PopoverEntity.prototype.initDOM = function (props) {
var topPopoverDOM = setDOMById(this.id);
var popoverWrapper = (React.createElement(PopoverWrapper, __assign({}, props, { ref: this.savePopWrapper })));
ReactDOM.render(popoverWrapper, topPopoverDOM);
};
PopoverEntity.prototype.show = function (options) {
var setConfig = Object.assign({}, options, {
open: true
});
this.set(setConfig);
};
PopoverEntity.prototype.set = function (options) {
var _options = Object.assign({}, this.prevOptions, options);
var elem = _options.elem, children = _options.children, open = _options.open, _a = _options.props, props = _a === void 0 ? this.prevProps : _a;
this.prevProps = props;
this.prevOptions = _options;
this.popoverEntity.show(elem, open, children, props);
};
PopoverEntity.prototype.close = function () {
var nextState = {
open: false
};
this.set(nextState);
};
PopoverEntity.prototype.delayClose = function (timer) {
var _this = this;
if (timer === void 0) { timer = 5000; }
if (this.lifeTimer)
clearTimeout(this.lifeTimer);
this.lifeTimer = setTimeout(function () {
_this.close();
}, timer);
};
return PopoverEntity;
}());
export { PopoverEntity };