UNPKG

tuya-panel-kit

Version:

a functional component library for developing tuya device panels!

125 lines (91 loc) 4.6 kB
Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _TYNativeApi = require('../../TYNativeApi'); var _portalDirect = require('./portal-direct'); var _portalDirect2 = _interopRequireDefault(_portalDirect); var _list = require('../popup/list'); var _picker = require('../popup/picker'); var _countdown = require('../popup/countdown'); var _datePicker = require('../popup/date-picker'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var uuid = 0; var TYEvent = _TYNativeApi.TYSdk.event; var Portal = function (_React$Component) { _inherits(Portal, _React$Component); function Portal(props) { _classCallCheck(this, Portal); var _this = _possibleConstructorReturn(this, (Portal.__proto__ || Object.getPrototypeOf(Portal)).call(this, props)); _initialiseProps.call(_this); uuid += 1; _this.uuid = 'portal-' + uuid; return _this; } _createClass(Portal, [{ key: 'componentDidMount', value: function componentDidMount() { this.registerPortal(this.props, false); if (this.props.visible) { this.showPortal(); } } }, { key: 'componentWillUpdate', value: function componentWillUpdate(nextProps) { this.registerPortal(nextProps, true); if (nextProps.visible === this.props.visible) return; if (nextProps.visible) { this.showPortal(); } else { this.hidePortal(); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.removePortal(); } }, { key: 'render', value: function render() { return null; } }]); return Portal; }(_react2.default.Component); Portal.List = _list.ListModal; Portal.Picker = _picker.PickerModal; Portal.Countdown = _countdown.CountdownModal; Portal.DatePicker = _datePicker.DatePickerModal; Portal.render = _portalDirect2.default.show; Portal.close = _portalDirect2.default.hide; Portal.propTypes = { visible: _propTypes2.default.bool.isRequired }; var _initialiseProps = function _initialiseProps() { var _this2 = this; this.registerPortal = function (registerProps, isUpdate) { var children = registerProps.children, props = _objectWithoutProperties(registerProps, ['children']); TYEvent.emit('registerPortal', { node: children, uuid: _this2.uuid, props: props, isUpdate: isUpdate }); }; this.showPortal = function () { TYEvent.emit('showPortal', { uuid: _this2.uuid, show: true }); }; this.hidePortal = function () { TYEvent.emit('showPortal', { uuid: _this2.uuid, show: false }); }; this.removePortal = function () { TYEvent.emit('removePortal', _this2.uuid); }; }; exports.default = Portal;