cosmo-ui
Version:
Common React components
53 lines • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var prop_types_1 = require("prop-types");
var BaseForm = (function (_super) {
tslib_1.__extends(BaseForm, _super);
function BaseForm() {
var _this = _super !== null && _super.apply(this, arguments) || this;
/**
* Prevent the native onSubmit event from triggering
* @param e
*/
_this.onSubmit = function (e) {
e.preventDefault();
var _a = _this.props, formData = _a.formData, name = _a.name, onSubmit = _a.onSubmit, submitFormValid = _a.submitFormValid, submitFormInvalid = _a.submitFormInvalid;
var keys = Object.keys(formData);
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
var field = keys_1[_i];
if (formData[field] && !formData[field].valid) {
// dispatch an action to say the form is submitted but invalid
// and end the submission process here
return submitFormInvalid(name, formData);
}
}
// dispatch an action to say the form is submitted and valid
submitFormValid(name, formData);
// now call the onsubmit handler with the data
if (onSubmit) {
onSubmit(formData);
}
};
return _this;
}
BaseForm.prototype.componentWillUnmount = function () {
this.props.destroyForm(this.props.name);
};
/**
* Make the form name available to child components
* without explicitly passing it down to every one
*/
BaseForm.prototype.getChildContext = function () {
return {
formName: this.props.name,
};
};
return BaseForm;
}(React.Component));
BaseForm.childContextTypes = {
formName: prop_types_1.string,
};
exports.BaseForm = BaseForm;
//# sourceMappingURL=form.js.map