chowa
Version:
UI component library based on React
82 lines (81 loc) • 2.75 kB
JavaScript
/**
* @license chowa v1.1.3
*
* Copyright (c) Chowa Techonlogies Co.,Ltd.(http://www.chowa.cn).
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const ReactDom = require("react-dom");
const utils_1 = require("../utils");
class ModalManager {
constructor(c, props) {
this.data = {};
this.data.InstanceComponent = c;
this.data.props = this.compileProps(props);
this.createMountNode();
this.render();
this.data.props.visible = true;
this.render();
}
createMountNode() {
this.data.mountNode = document.createElement('div');
document.body.appendChild(this.data.mountNode);
}
compileProps(props) {
const { InstanceComponent } = this.data;
let componentProps = Object.assign({}, props, {
visible: false,
onHide: () => {
ReactDom.unmountComponentAtNode(this.data.mountNode);
document.body.removeChild(this.data.mountNode);
}
});
if (utils_1.hasProperty(InstanceComponent.propTypes, 'onClose')) {
componentProps = Object.assign(componentProps, {
onClose: () => {
this.destroy();
if (utils_1.hasProperty(props, 'onClose')) {
props.onClose();
}
}
});
}
if (utils_1.hasProperty(InstanceComponent.propTypes, 'onCancel')) {
componentProps = Object.assign(componentProps, {
onCancel: () => {
this.destroy();
if (utils_1.hasProperty(props, 'onCancel')) {
props.onCancel();
}
}
});
}
if (utils_1.hasProperty(InstanceComponent.propTypes, 'onConfirm')) {
componentProps = Object.assign(componentProps, {
onConfirm: () => {
this.destroy();
if (utils_1.hasProperty(props, 'onConfirm')) {
props.onConfirm();
}
}
});
}
return componentProps;
}
destroy() {
this.data.props.visible = false;
this.render();
}
render() {
const { InstanceComponent, props, mountNode } = this.data;
ReactDom.render(React.createElement(InstanceComponent, props), mountNode);
}
}
function $instanceManage(c, props) {
new ModalManager(c, props);
}
exports.default = $instanceManage;