yarfl
Version:
Yet Another Redux Forms Library
87 lines • 3.83 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
// necessary import format
// see https://stackoverflow.com/questions/44547201/typescript-react-not-importing-correctly
var _equals = require("ramda/src/equals");
var React = require("react");
var connect_1 = require("./connect");
var utils_1 = require("./helpers/utils");
var init_1 = require("./init");
var component = /** @class */ (function (_super) {
__extends(class_1, _super);
function class_1(props) {
var _this = _super.call(this, props) || this;
_this.initialize = function (beforeMount) {
if (beforeMount === void 0) { beforeMount = false; }
var _a;
// create yarfl/redux assets with init function
var newFormName = _this.props.config.name;
var oldFormName = (_this.config || {}).name;
var _b = init_1.init(_this.props.config), initialState = _b.initialState, reducer = _b.reducer, config = _b.config;
_this.reducer = reducer;
_this.config = config[newFormName];
if (beforeMount) {
// if component has not been mounted set directly
_this.state = initialState;
}
else {
// if component is mounted use setState to set new form
// before updating config, reducer and setting the old
// form to undefined
_this.setState(__assign((_a = {}, _a[oldFormName] = undefined, _a), initialState));
}
};
// this dispatch takes the place of what would be Redux's dispatch
// if the store were being used
_this.dispatch = function (action) {
if (typeof action === 'function') {
// dispatch thunk like actions
return action(_this.dispatch, function () { return _this.state; });
}
var formName = action.formName;
var formDefined = !!(_this.state || {})[formName];
// skip dispatching action if the target form is missing
if (formDefined) {
return _this.setState(function (prevState) { return _this.reducer(prevState, action); });
}
utils_1.logWarning("Skipping dispatch in LocalForm instance '" + _this.config.name + "'.");
};
_this.initialize(true);
return _this;
}
class_1.prototype.componentDidUpdate = function (prevProps) {
var oldConfig = prevProps.config;
var newConfig = this.props.config;
// check if config prop name has changed
if (!_equals(oldConfig, newConfig)) {
this.initialize();
}
};
class_1.prototype.render = function () {
var render = this.props.render;
var props = connect_1.localFormMap(this.config)(this.state, this.dispatch);
return render(props);
};
return class_1;
}(React.Component));
// tslint:disable-next-line:variable-name
exports.LocalForm = component;
//# sourceMappingURL=LocalForm.js.map