UNPKG

dh-c

Version:

The front-end development engineers jimberton gulp react component

96 lines (73 loc) 3.17 kB
'use strict'; exports.__esModule = true; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _button = require('../button'); var _button2 = _interopRequireDefault(_button); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 ActionButton = function (_React$Component) { _inherits(ActionButton, _React$Component); function ActionButton(props) { _classCallCheck(this, ActionButton); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.onClick = function () { var _this$props = _this.props, actionFn = _this$props.actionFn, closeModal = _this$props.closeModal; if (actionFn) { var ret = void 0; if (actionFn.length) { ret = actionFn(closeModal); } else { ret = actionFn(); if (!ret) { closeModal(); } } if (ret && ret.then) { _this.setState({ loading: true }); ret.then(function () { // It's unnecessary to set loading=false, for the Modal will be unmounted after close. // this.setState({ loading: false }); closeModal.apply(undefined, arguments); }); } } else { closeModal(); } }; _this.state = { loading: false }; return _this; } ActionButton.prototype.componentDidMount = function componentDidMount() { if (this.props.autoFocus) { var $this = _reactDom2.default.findDOMNode(this); this.timeoutId = setTimeout(function () { return $this.focus(); }); } }; ActionButton.prototype.componentWillUnmount = function componentWillUnmount() { clearTimeout(this.timeoutId); }; ActionButton.prototype.render = function render() { var children = this.props.children; var loading = this.state.loading; return _react2.default.createElement( 'button', { onClick: this.onClick, className: 'dh-modal-footer_btn' }, children ); }; return ActionButton; }(_react2.default.Component); exports.default = ActionButton;