react-form-controlled
Version:
Intuitive react forms for building powerful applications
66 lines (55 loc) • 1.91 kB
JavaScript
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
import PropTypes from 'prop-types';
import Element from './Element';
import wait from './utils/wait';
export default class Button extends Element {
constructor(...args) {
var _temp;
return _temp = super(...args), _initialiseProps.call(this), _temp;
}
process() {
return _asyncToGenerator(function* () {
throw new Error('You need to implement process');
})();
}
render() {
const { children, render } = this.props;
const newProps = {
onClick: this.onClick
};
if (typeof children === 'function') {
return this.replaceChildren(children(newProps));
}if (typeof render === 'function') {
return this.replaceChildren(render(newProps));
}
throw new Error('You need to set property render or children as function');
}
}
Button.propTypes = {
render: PropTypes.func,
text: PropTypes.string,
children: PropTypes.func
};
Button.defaultProps = {
text: 'Button'
};
var _initialiseProps = function () {
var _this = this;
this.onClick = (() => {
var _ref = _asyncToGenerator(function* (...args) {
const result = yield _this.process();
if (typeof result === 'undefined') {
return;
}
const { onClick } = _this.props;
if (onClick) {
yield wait(0);
onClick(...args, result);
}
});
return function () {
return _ref.apply(this, arguments);
};
})();
};
//# sourceMappingURL=Button.js.map