UNPKG

chowa

Version:

UI component library based on React

59 lines (58 loc) 1.85 kB
/** * @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 message_1 = require("./message"); const utils_1 = require("../utils"); class MessageInstances extends React.Component { constructor() { super(...arguments); this.state = { notices: [] }; } add(props) { const { notices } = this.state; this.setState({ notices: notices.concat(props) }); } remove(index) { const notices = [].concat(this.state.notices); notices.splice(index, 1); this.setState({ notices }); } render() { const { notices } = this.state; return notices.map((props, index) => { return (React.createElement(message_1.default, Object.assign({}, props, { key: props.key, index: index }))); }); } } let instance = null; let instanceKey = 0; function getInstance() { if (instance === null) { const rootEle = document.createElement('div'); document.body.appendChild(rootEle); ReactDom.render(React.createElement(MessageInstances, { ref: (component) => instance = component }), rootEle); } instanceKey++; return instance; } function $message(props) { const ins = getInstance(); ins.add(Object.assign(Object.assign({}, props), { key: instanceKey, onHide(index) { ins.remove(index); if (typeof props === 'object' && utils_1.hasProperty(props, 'onHide')) { props.onHide(index); } } })); } exports.default = $message;