uniforms
Version:
Core package of uniforms.
64 lines (63 loc) • 2.75 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoForm = void 0;
exports.Auto = Auto;
const tslib_1 = require("tslib");
const clone_1 = tslib_1.__importDefault(require("lodash/clone"));
const get_1 = tslib_1.__importDefault(require("lodash/get"));
const isEqual_1 = tslib_1.__importDefault(require("lodash/isEqual"));
const omit_1 = tslib_1.__importDefault(require("lodash/omit"));
const setWith_1 = tslib_1.__importDefault(require("lodash/setWith"));
const ValidatedQuickForm_1 = require("./ValidatedQuickForm");
function Auto(Base) {
// @ts-expect-error: Mixin class problem.
class AutoForm extends Base {
constructor(props) {
super(props);
this.state = Object.assign(Object.assign({}, this.state), { model: this.mergeSchemaAndPropsModel(this.props.schema, this.props.model) });
}
/**
* Returns model value based on the `schema` model and `props.model`.
* Latter one takes precedence. Does shallow copy.
*/
mergeSchemaAndPropsModel(schema, model) {
const initialModel = schema.getInitialModel();
return Object.assign(initialModel, model);
}
componentDidUpdate(prevProps, prevState, snapshot) {
const { model, schema } = this.props;
if (!(0, isEqual_1.default)(model, prevProps.model)) {
this.setState({ model: this.mergeSchemaAndPropsModel(schema, model) });
}
super.componentDidUpdate(prevProps, prevState, snapshot);
}
getNativeFormProps() {
const props = super.getNativeFormProps();
return (0, omit_1.default)(props, ['onChangeModel']);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getModel(mode) {
return this.state.model;
}
onChange(key, value) {
const previousValue = (0, get_1.default)(this.state.model, key);
super.onChange(key, value);
this.setState(state => ({ model: (0, setWith_1.default)((0, clone_1.default)(state.model), key, value, clone_1.default) }), () => {
if (this.props.onChangeModel) {
this.props.onChangeModel(this.state.model, {
key,
value,
previousValue,
});
}
});
}
__reset(state) {
return Object.assign(Object.assign({}, super.__reset(state)), { model: this.props.model });
}
}
AutoForm.Auto = Auto;
AutoForm.displayName = `Auto${Base.displayName}`;
return AutoForm;
}
exports.AutoForm = Auto(ValidatedQuickForm_1.ValidatedQuickForm);
;