UNPKG

uniforms

Version:
59 lines (58 loc) 2.34 kB
import clone from 'lodash/clone'; import get from 'lodash/get'; import isEqual from 'lodash/isEqual'; import omit from 'lodash/omit'; import setWith from 'lodash/setWith'; import { ValidatedQuickForm, } from './ValidatedQuickForm'; export 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 (!isEqual(model, prevProps.model)) { this.setState({ model: this.mergeSchemaAndPropsModel(schema, model) }); } super.componentDidUpdate(prevProps, prevState, snapshot); } getNativeFormProps() { const props = super.getNativeFormProps(); return omit(props, ['onChangeModel']); } // eslint-disable-next-line @typescript-eslint/no-unused-vars getModel(mode) { return this.state.model; } onChange(key, value) { const previousValue = get(this.state.model, key); super.onChange(key, value); this.setState(state => ({ model: setWith(clone(state.model), key, value, clone) }), () => { 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; } export const AutoForm = Auto(ValidatedQuickForm);