Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

uniforms

Package Overview
Dependencies
Maintainers
1
Versions
190
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uniforms - npm Package Compare versions

Comparing version 3.6.0 to 3.6.1

cjs/AutoForm.d.ts

59

esm/AutoForm.js

@@ -1,2 +0,1 @@

import { __assign, __extends } from "tslib";
import clone from 'lodash/clone';

@@ -9,41 +8,37 @@ import isEqual from 'lodash/isEqual';

// @ts-expect-error: Mixin class problem.
var AutoForm = /** @class */ (function (_super) {
__extends(AutoForm, _super);
function AutoForm(props) {
var _this = _super.call(this, props) || this;
_this.state = __assign(__assign({}, _this.state), { model: props.model });
return _this;
class AutoForm extends Base {
constructor(props) {
super(props);
this.state = Object.assign(Object.assign({}, this.state), { model: props.model });
}
AutoForm.prototype.componentDidUpdate = function (prevProps, prevState, snapshot) {
var model = this.props.model;
componentDidUpdate(prevProps, prevState, snapshot) {
const { model } = this.props;
if (!isEqual(model, prevProps.model)) {
this.setState({ model: model });
this.setState({ model });
}
_super.prototype.componentDidUpdate.call(this, prevProps, prevState, snapshot);
};
AutoForm.prototype.getNativeFormProps = function () {
return omit(_super.prototype.getNativeFormProps.call(this), ['onChangeModel']);
};
super.componentDidUpdate(prevProps, prevState, snapshot);
}
getNativeFormProps() {
return omit(super.getNativeFormProps(), ['onChangeModel']);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
AutoForm.prototype.getModel = function (mode) {
getModel(mode) {
return this.state.model;
};
AutoForm.prototype.onChange = function (key, value) {
var _this = this;
_super.prototype.onChange.call(this, key, value);
this.setState(function (state) { return ({ model: setWith(clone(state.model), key, value, clone) }); }, function () {
if (_this.props.onChangeModel) {
_this.props.onChangeModel(_this.state.model);
}
onChange(key, value) {
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);
}
});
};
AutoForm.prototype.__reset = function (state) {
return __assign(__assign({}, _super.prototype.__reset.call(this, state)), { model: this.props.model });
};
AutoForm.Auto = Auto;
AutoForm.displayName = "Auto" + Base.displayName;
return AutoForm;
}(Base));
}
__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 var AutoForm = Auto(ValidatedQuickForm);
export const AutoForm = Auto(ValidatedQuickForm);

@@ -1,2 +0,1 @@

import { __assign, __extends } from "tslib";
import clone from 'lodash/clone';

@@ -10,8 +9,7 @@ import get from 'lodash/get';

import { randomIds } from './randomIds';
var BaseForm = /** @class */ (function (_super) {
__extends(BaseForm, _super);
function BaseForm(props) {
var _this = _super.call(this, props) || this;
export class BaseForm extends Component {
constructor(props) {
super(props);
// @ts-expect-error: State may be bigger, but it'll be covered by the subclasses.
_this.state = {
this.state = {
changed: false,

@@ -23,23 +21,19 @@ changedMap: Object.create(null),

};
_this.mounted = false;
_this.randomId = randomIds(_this.props.id);
_this.onReset = _this.reset = _this.onReset.bind(_this);
_this.onChange = _this.change = _this.onChange.bind(_this);
_this.onSubmit = _this.submit = _this.onSubmit.bind(_this);
this.mounted = false;
this.randomId = randomIds(this.props.id);
this.onReset = this.reset = this.onReset.bind(this);
this.onChange = this.change = this.onChange.bind(this);
this.onSubmit = this.submit = this.onSubmit.bind(this);
// TODO: It shouldn't be here
var getModel = _this.getModel.bind(_this);
_this.getModel = function (mode, model) {
if (model === void 0) { model = getModel(mode); }
return mode !== undefined && _this.props.modelTransform
? _this.props.modelTransform(mode, model)
: model;
};
return _this;
const getModel = this.getModel.bind(this);
this.getModel = (mode, model = getModel(mode)) => mode !== undefined && this.props.modelTransform
? this.props.modelTransform(mode, model)
: model;
}
BaseForm.prototype.componentDidMount = function () {
componentDidMount() {
this.mounted = true;
};
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
BaseForm.prototype.componentDidUpdate = function (prevProps, prevState, snapshot) { };
BaseForm.prototype.componentWillUnmount = function () {
componentDidUpdate(prevProps, prevState, snapshot) { }
componentWillUnmount() {
this.mounted = false;

@@ -49,4 +43,4 @@ if (this.delayId) {

}
};
BaseForm.prototype.getContext = function () {
}
getContext() {
return {

@@ -67,13 +61,13 @@ changed: this.state.changed,

};
};
BaseForm.prototype.getContextName = function () {
}
getContextName() {
return [];
};
BaseForm.prototype.getContextError = function () {
}
getContextError() {
return this.props.error;
};
BaseForm.prototype.getContextModel = function () {
}
getContextModel() {
return this.getModel('form');
};
BaseForm.prototype.getContextState = function () {
}
getContextState() {
return {

@@ -86,22 +80,21 @@ disabled: !!this.props.disabled,

};
};
BaseForm.prototype.getContextSchema = function () {
}
getContextSchema() {
return this.props.schema;
};
BaseForm.prototype.getContextOnChange = function () {
}
getContextOnChange() {
// It's bound in constructor.
// eslint-disable-next-line @typescript-eslint/unbound-method
return this.onChange;
};
BaseForm.prototype.getContextOnSubmit = function () {
}
getContextOnSubmit() {
// It's bound in constructor.
// eslint-disable-next-line @typescript-eslint/unbound-method
return this.onSubmit;
};
BaseForm.prototype.getModel = function (mode, model) {
if (model === void 0) { model = this.props.model; }
}
getModel(mode, model = this.props.model) {
return model;
};
BaseForm.prototype.getNativeFormProps = function () {
var props = omit(this.props, [
}
getNativeFormProps() {
const props = omit(this.props, [
'autosave',

@@ -121,22 +114,20 @@ 'autosaveDelay',

]);
return __assign(__assign({}, props), {
return Object.assign(Object.assign({}, props), {
// It's bound in constructor.
// eslint-disable-next-line @typescript-eslint/unbound-method
onSubmit: this.onSubmit, key: "reset-" + this.state.resetCount });
};
BaseForm.prototype.onChange = function (key, value) {
var _this = this;
onSubmit: this.onSubmit, key: `reset-${this.state.resetCount}` });
}
onChange(key, value) {
// Do not set `changed` before componentDidMount
if (this.mounted) {
var keys_1 = changedKeys(key, value, get(this.getModel(), key));
if (keys_1.length !== 0) {
this.setState(function (state) {
// If all are already marked, we can skip the update completely.
return state.changed && keys_1.every(function (key) { return !!get(state.changedMap, key); })
? null
: {
changed: true,
changedMap: keys_1.reduce(function (changedMap, key) { return setWith(changedMap, key, {}, clone); }, clone(state.changedMap)),
};
});
const keys = changedKeys(key, value, get(this.getModel(), key));
if (keys.length !== 0) {
this.setState(state =>
// If all are already marked, we can skip the update completely.
state.changed && keys.every(key => !!get(state.changedMap, key))
? null
: {
changed: true,
changedMap: keys.reduce((changedMap, key) => setWith(changedMap, key, {}, clone), clone(state.changedMap)),
});
}

@@ -153,12 +144,12 @@ }

// Delay autosave by `autosaveDelay` milliseconds...
this.delayId = setTimeout(function () {
this.delayId = setTimeout(() => {
// ...and wait for all scheduled `setState`s to commit. This is required
// for AutoForm to validate correct model, waiting in `onChange`.
_this.setState(function () { return null; }, function () {
_this.onSubmit();
this.setState(() => null, () => {
this.onSubmit();
});
}, this.props.autosaveDelay);
}
};
BaseForm.prototype.__reset = function (state) {
}
__reset(state) {
return {

@@ -171,4 +162,4 @@ changed: false,

};
};
BaseForm.prototype.onReset = function () {
}
onReset() {
// @ts-expect-error

@@ -178,5 +169,4 @@ // It's bound in constructor.

this.setState(this.__reset);
};
BaseForm.prototype.onSubmit = function (event) {
var _this = this;
}
onSubmit(event) {
if (event) {

@@ -186,4 +176,4 @@ event.preventDefault();

}
this.setState(function (state) { return (state.submitted ? null : { submitted: true }); });
var result = this.props.onSubmit(this.getModel('submit'));
this.setState(state => (state.submitted ? null : { submitted: true }));
const result = this.props.onSubmit(this.getModel('submit'));
if (!(result instanceof Promise)) {

@@ -193,22 +183,20 @@ return Promise.resolve();

this.setState({ submitting: true });
return result.finally(function () {
_this.setState({ submitting: false });
return result.finally(() => {
this.setState({ submitting: false });
});
};
BaseForm.prototype.render = function () {
}
render() {
return (React.createElement(context.Provider, { value: this.getContext() },
React.createElement("form", __assign({}, this.getNativeFormProps()))));
};
BaseForm.displayName = 'Form';
BaseForm.defaultProps = {
autosave: false,
autosaveDelay: 0,
error: null,
label: true,
model: Object.create(null),
noValidate: true,
onSubmit: function () { },
};
return BaseForm;
}(Component));
export { BaseForm };
React.createElement("form", Object.assign({}, this.getNativeFormProps()))));
}
}
BaseForm.displayName = 'Form';
BaseForm.defaultProps = {
autosave: false,
autosaveDelay: 0,
error: null,
label: true,
model: Object.create(null),
noValidate: true,
onSubmit() { },
};
import invariant from 'invariant';
var Bridge = /** @class */ (function () {
export class Bridge {
// Each bridge can have a different set of parameters.
function Bridge() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
invariant(this.constructor !== Bridge, 'Bridge cannot be instantiated (args=%o).', { args: args });
constructor(...args) {
invariant(this.constructor !== Bridge, 'Bridge cannot be instantiated (args=%o).', { args });
}

@@ -14,21 +10,21 @@ // Get an error for field `name` out of `error`. There is no standarized

// receive this as a `error` guaranteed prop.
Bridge.prototype.getError = function (name, error) {
return invariant(false, '%s have not implemented `getError` method (args=%o).', this.constructor.name, { name: name, error: error });
};
getError(name, error) {
return invariant(false, '%s have not implemented `getError` method (args=%o).', this.constructor.name, { name, error });
}
// Get an error message for field `name` out of `error`. If there is no error,
// return an empty string. Fields receive this as a `errorMessage` guaranteed
// prop.
Bridge.prototype.getErrorMessage = function (name, error) {
return invariant(false, '%s have not implemented `getErrorMessage` method (args=%o).', this.constructor.name, { name: name, error: error });
};
getErrorMessage(name, error) {
return invariant(false, '%s have not implemented `getErrorMessage` method (args=%o).', this.constructor.name, { name, error });
}
// Get all error messages from `error`. Only `ErrorsField` make use of that
// (in builtin themes).
Bridge.prototype.getErrorMessages = function (error) {
return invariant(false, '%s have not implemented `getErrorMessages` method (args=%o).', this.constructor.name, { error: error });
};
getErrorMessages(error) {
return invariant(false, '%s have not implemented `getErrorMessages` method (args=%o).', this.constructor.name, { error });
}
// Get internal field definition for field `name`. Fields receive this as a
// `field` guaranteed prop.
Bridge.prototype.getField = function (name) {
return invariant(false, '%s have not implemented `getField` method (args=%o).', this.constructor.name, { name: name });
};
getField(name) {
return invariant(false, '%s have not implemented `getField` method (args=%o).', this.constructor.name, { name });
}
// Get initial value of field `name`. It is used as a default when no value is

@@ -39,5 +35,5 @@ // set (e.g. the form is rendered with an empty `model`). Additionally,

// `props`.
Bridge.prototype.getInitialValue = function (name, props) {
return invariant(false, '%s have not implemented `getInitialValue` method (args=%o).', this.constructor.name, { name: name, props: props });
};
getInitialValue(name, props) {
return invariant(false, '%s have not implemented `getInitialValue` method (args=%o).', this.constructor.name, { name, props });
}
// Get props defined in schema for a field `name`. There are no required nor

@@ -48,14 +44,14 @@ // banned fields, however properties like `required` are often available.

// different `props`.
Bridge.prototype.getProps = function (name, props) {
return invariant(false, '%s have not implemented `getProps` method (args=%o).', this.constructor.name, { name: name, props: props });
};
getProps(name, props) {
return invariant(false, '%s have not implemented `getProps` method (args=%o).', this.constructor.name, { name, props });
}
// Get a list of subfields of field `name` or top-level fields, if no `name`
// is passed.
Bridge.prototype.getSubfields = function (name) {
return invariant(false, '%s have not implemented `getSubfields` method (args=%o).', this.constructor.name, { name: name });
};
getSubfields(name) {
return invariant(false, '%s have not implemented `getSubfields` method (args=%o).', this.constructor.name, { name });
}
// Get a type of field `name`. See `FieldTypeType` for details.
Bridge.prototype.getType = function (name) {
return invariant(false, '%s have not implemented `getType` method (args=%o).', this.constructor.name, { name: name });
};
getType(name) {
return invariant(false, '%s have not implemented `getType` method (args=%o).', this.constructor.name, { name });
}
// Get a validator function. The `options` here are from the `validator` prop

@@ -66,7 +62,5 @@ // of the form. A validator function receives a model and returns an error or

// eslint-disable-next-line prettier/prettier
Bridge.prototype.getValidator = function (options) {
return invariant(false, '%s have not implemented `getValidator` method (args=%o).', this.constructor.name, { options: options });
};
return Bridge;
}());
export { Bridge };
getValidator(options) {
return invariant(false, '%s have not implemented `getValidator` method (args=%o).', this.constructor.name, { options });
}
}

@@ -11,5 +11,5 @@ import isEqual from 'lodash/isEqual';

}
var changed = [root];
const changed = [root];
if (isObject(valueB)) {
for (var key in valueA) {
for (const key in valueA) {
if (!(key in valueB) || !isEqual(valueA[key], valueB[key])) {

@@ -19,3 +19,3 @@ changed.push(joinName(root, key));

}
for (var key in valueB) {
for (const key in valueB) {
if (!(key in valueA)) {

@@ -31,3 +31,3 @@ changed.push(joinName(root, key));

// eslint-disable-next-line guard-for-in
for (var key in valueA) {
for (const key in valueA) {
changed.push(joinName(root, key));

@@ -34,0 +34,0 @@ }

@@ -1,2 +0,1 @@

import { __assign } from "tslib";
import mapValues from 'lodash/mapValues';

@@ -8,15 +7,15 @@ import React from 'react';

function Field(props) {
var _a = useField(props.name, props, options), fieldProps = _a[0], context = _a[1];
var hasChainName = props.name !== '';
var anyFlowingPropertySet = Object.keys(context.state).some(function (key) {
var next = props[key];
const [fieldProps, context] = useField(props.name, props, options);
const hasChainName = props.name !== '';
const anyFlowingPropertySet = Object.keys(context.state).some(key => {
const next = props[key];
return next !== null && next !== undefined;
});
if (!anyFlowingPropertySet && !hasChainName) {
return React.createElement(Component, __assign({}, props, fieldProps));
return React.createElement(Component, Object.assign({}, props, fieldProps));
}
var nextContext = __assign({}, context);
const nextContext = Object.assign({}, context);
if (anyFlowingPropertySet) {
nextContext.state = mapValues(nextContext.state, function (prev, key) {
var next = props[key];
nextContext.state = mapValues(nextContext.state, (prev, key) => {
const next = props[key];
return next !== null && next !== undefined ? !!next : prev;

@@ -29,6 +28,6 @@ });

return (React.createElement(contextReference.Provider, { value: nextContext },
React.createElement(Component, __assign({}, props, fieldProps))));
React.createElement(Component, Object.assign({}, props, fieldProps))));
}
Field.displayName = (Component.displayName || Component.name) + "Field";
return Object.assign(Field, { Component: Component, options: options });
Field.displayName = `${Component.displayName || Component.name}Field`;
return Object.assign(Field, { Component, options });
}
import { createContext } from 'react';
export var context = createContext(null);
export const context = createContext(null);

@@ -5,8 +5,8 @@ import invariant from 'invariant';

export function createAutoField(defaultComponentDetector) {
var context = createContext(defaultComponentDetector);
const context = createContext(defaultComponentDetector);
function AutoField(rawProps) {
var _a, _b;
var _c = useField(rawProps.name, rawProps), props = _c[0], uniforms = _c[1];
var componentDetector = useContext(context);
var component = (_a = props.component) !== null && _a !== void 0 ? _a : componentDetector(props, uniforms);
const [props, uniforms] = useField(rawProps.name, rawProps);
const componentDetector = useContext(context);
const component = (_a = props.component) !== null && _a !== void 0 ? _a : componentDetector(props, uniforms);
invariant(component, 'AutoField received no component for: %s', props.name);

@@ -19,4 +19,4 @@ return 'options' in component && ((_b = component.options) === null || _b === void 0 ? void 0 : _b.kind) === 'leaf'

componentDetectorContext: context,
defaultComponentDetector: defaultComponentDetector,
defaultComponentDetector,
});
}

@@ -1,7 +0,6 @@

import { __assign } from "tslib";
var registered = [];
var registeredCache = new Set();
export var filterDOMProps = Object.assign(function filterDOMProps(props) {
var filteredProps = __assign({}, props);
for (var prop in props) {
const registered = [];
const registeredCache = new Set();
export const filterDOMProps = Object.assign(function filterDOMProps(props) {
const filteredProps = Object.assign({}, props);
for (const prop in props) {
if (registeredCache.has(prop)) {

@@ -13,8 +12,4 @@ delete filteredProps[prop];

}, {
register: function () {
var props = [];
for (var _i = 0; _i < arguments.length; _i++) {
props[_i] = arguments[_i];
}
props.forEach(function (prop) {
register(...props) {
props.forEach(prop => {
if (!registeredCache.has(prop)) {

@@ -21,0 +16,0 @@ registered.push(prop);

@@ -1,14 +0,9 @@

import { __spreadArray } from "tslib";
export function joinName() {
var parts = [];
for (var _i = 0; _i < arguments.length; _i++) {
parts[_i] = arguments[_i];
}
var name = [];
for (var index = 0; index !== parts.length; ++index) {
var part = parts[index];
export function joinName(...parts) {
const name = [];
for (let index = 0; index !== parts.length; ++index) {
const part = parts[index];
if (part || part === 0) {
if (typeof part === 'string') {
if (part.indexOf('.') !== -1) {
name.push.apply(name, part.split('.'));
name.push(...part.split('.'));
}

@@ -20,3 +15,3 @@ else {

else if (Array.isArray(part)) {
parts.splice.apply(parts, __spreadArray([index--, 1], part));
parts.splice(index--, 1, ...part);
}

@@ -23,0 +18,0 @@ else {

@@ -1,2 +0,2 @@

import { __extends, __rest } from "tslib";
import { __rest } from "tslib";
import React from 'react';

@@ -6,13 +6,9 @@ import { BaseForm } from './BaseForm';

// @ts-expect-error: Mixin class problem.
var QuickForm = /** @class */ (function (_super) {
__extends(QuickForm, _super);
function QuickForm() {
return _super !== null && _super.apply(this, arguments) || this;
}
QuickForm.prototype.getNativeFormProps = function () {
var _a = _super.prototype.getNativeFormProps.call(this), _b = _a.autoField, AutoField = _b === void 0 ? this.getAutoField() : _b, _c = _a.errorsField, ErrorsField = _c === void 0 ? this.getErrorsField() : _c, _d = _a.submitField, SubmitField = _d === void 0 ? this.getSubmitField() : _d, props = __rest(_a, ["autoField", "errorsField", "submitField"]);
class QuickForm extends Base {
getNativeFormProps() {
const _a = super.getNativeFormProps(), { autoField: AutoField = this.getAutoField(), errorsField: ErrorsField = this.getErrorsField(), submitField: SubmitField = this.getSubmitField() } = _a, props = __rest(_a, ["autoField", "errorsField", "submitField"]);
if (!props.children) {
props.children = this.getContextSchema()
.getSubfields()
.map(function (key) { return React.createElement(AutoField, { key: key, name: key }); })
.map(key => React.createElement(AutoField, { key: key, name: key }))
.concat([

@@ -24,18 +20,17 @@ React.createElement(ErrorsField, { key: "$ErrorsField" }),

return props;
};
QuickForm.prototype.getAutoField = function () {
return function () { return null; };
};
QuickForm.prototype.getErrorsField = function () {
return function () { return null; };
};
QuickForm.prototype.getSubmitField = function () {
return function () { return null; };
};
QuickForm.Quick = Quick;
QuickForm.displayName = "Quick" + Base.displayName;
return QuickForm;
}(Base));
}
getAutoField() {
return () => null;
}
getErrorsField() {
return () => null;
}
getSubmitField() {
return () => null;
}
}
QuickForm.Quick = Quick;
QuickForm.displayName = `Quick${Base.displayName}`;
return QuickForm;
}
export var QuickForm = Quick(BaseForm);
export const QuickForm = Quick(BaseForm);

@@ -5,9 +5,8 @@ // Workaround for SSR

function randomIdsGenerator(prefix) {
var counter = 0;
return function () { return prefix + "-" + ('000' + (counter++).toString(36)).slice(-4); };
let counter = 0;
return () => `${prefix}-${('000' + (counter++).toString(36)).slice(-4)}`;
}
var randomIdPrefix = randomIdsGenerator('uniforms');
export function randomIds(prefix) {
if (prefix === void 0) { prefix = randomIdPrefix(); }
const randomIdPrefix = randomIdsGenerator('uniforms');
export function randomIds(prefix = randomIdPrefix()) {
return randomIdsGenerator(prefix);
}

@@ -1,2 +0,1 @@

import { __assign } from "tslib";
import get from 'lodash/get';

@@ -8,6 +7,6 @@ import mapValues from 'lodash/mapValues';

function propagate(prop, schema, state, fallback) {
var forcedFallbackInProp = prop === true || prop === undefined;
var forcedFallbackInSchema = schema === true || schema === undefined;
var schemaValue = forcedFallbackInSchema ? fallback : schema;
var value = prop === '' ||
const forcedFallbackInProp = prop === true || prop === undefined;
const forcedFallbackInSchema = schema === true || schema === undefined;
const schemaValue = forcedFallbackInSchema ? fallback : schema;
const value = prop === '' ||
prop === false ||

@@ -24,21 +23,20 @@ prop === null ||

var _a;
var context = useForm();
var name = joinName((options === null || options === void 0 ? void 0 : options.absoluteName) ? '' : context.name, fieldName);
var state = mapValues(context.state, function (prev, key) {
var next = props[key];
const context = useForm();
const name = joinName((options === null || options === void 0 ? void 0 : options.absoluteName) ? '' : context.name, fieldName);
const state = mapValues(context.state, (prev, key) => {
const next = props[key];
return next !== null && next !== undefined ? !!next : prev;
});
var changed = !!get(context.changedMap, name);
var error = context.schema.getError(name, context.error);
var errorMessage = context.schema.getErrorMessage(name, context.error);
var field = context.schema.getField(name);
var fieldType = context.schema.getType(name);
var fields = context.schema.getSubfields(name);
var schemaProps = context.schema.getProps(name, __assign(__assign({}, state), props));
var _b = propagate(props.label, schemaProps.label, state.label, ''), label = _b[0], labelFallback = _b[1];
var placeholder = propagate(props.placeholder, schemaProps.placeholder, state.placeholder, label || labelFallback)[0];
const changed = !!get(context.changedMap, name);
const error = context.schema.getError(name, context.error);
const errorMessage = context.schema.getErrorMessage(name, context.error);
const field = context.schema.getField(name);
const fieldType = context.schema.getType(name);
const fields = context.schema.getSubfields(name);
const schemaProps = context.schema.getProps(name, Object.assign(Object.assign({}, state), props));
const [label, labelFallback] = propagate(props.label, schemaProps.label, state.label, '');
const [placeholder] = propagate(props.placeholder, schemaProps.placeholder, state.placeholder, label || labelFallback);
// eslint-disable-next-line react-hooks/exhaustive-deps
var id = useMemo(function () { return context.randomId(); }, []);
var onChange = useCallback(function (value, key) {
if (key === void 0) { key = name; }
const id = useMemo(() => context.randomId(), []);
const onChange = useCallback((value, key = name) => {
context.onChange(key, value);

@@ -48,5 +46,5 @@ },

[context.onChange, name]);
var valueFromModel = get(context.model, name);
var initialValue;
var value = (_a = props.value) !== null && _a !== void 0 ? _a : valueFromModel;
const valueFromModel = get(context.model, name);
let initialValue;
let value = (_a = props.value) !== null && _a !== void 0 ? _a : valueFromModel;
if (value === undefined) {

@@ -61,5 +59,5 @@ value = context.schema.getInitialValue(name, props);

// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(function () {
useEffect(() => {
var _a;
var required = (_a = props.required) !== null && _a !== void 0 ? _a : schemaProps.required;
const required = (_a = props.required) !== null && _a !== void 0 ? _a : schemaProps.required;
if (required && initialValue !== undefined) {

@@ -71,11 +69,11 @@ onChange(initialValue);

}
var fieldProps = __assign(__assign(__assign(__assign(__assign({ id: id }, state), { changed: changed,
error: error,
errorMessage: errorMessage,
field: field,
fieldType: fieldType,
fields: fields,
onChange: onChange,
value: value }), schemaProps), props), { label: label,
name: name,
const fieldProps = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ id }, state), { changed,
error,
errorMessage,
field,
fieldType,
fields,
onChange,
value }), schemaProps), props), { label,
name,
// TODO: Should we assert `typeof placeholder === 'string'`?

@@ -82,0 +80,0 @@ placeholder: placeholder });

@@ -5,5 +5,5 @@ import invariant from 'invariant';

export function useForm() {
var context = useContext(contextReference);
const context = useContext(contextReference);
invariant(context !== null, 'useForm must be used within a form.');
return context;
}

@@ -1,2 +0,1 @@

import { __assign, __extends } from "tslib";
import clone from 'lodash/clone';

@@ -11,21 +10,19 @@ import cloneDeep from 'lodash/cloneDeep';

// @ts-expect-error: Mixin class problem.
var ValidatedForm = /** @class */ (function (_super) {
__extends(ValidatedForm, _super);
function ValidatedForm(props) {
var _this = _super.call(this, props) || this;
_this.state = __assign(__assign({}, _this.state), { error: null, validate: false, validating: false, validator: _this.getContextSchema().getValidator(props.validator) });
_this.onValidate = _this.validate = _this.onValidate.bind(_this);
_this.onValidateModel = _this.validateModel =
_this.onValidateModel.bind(_this);
return _this;
class ValidatedForm extends Base {
constructor(props) {
super(props);
this.state = Object.assign(Object.assign({}, this.state), { error: null, validate: false, validating: false, validator: this.getContextSchema().getValidator(props.validator) });
this.onValidate = this.validate = this.onValidate.bind(this);
this.onValidateModel = this.validateModel =
this.onValidateModel.bind(this);
}
ValidatedForm.prototype.getContextError = function () {
getContextError() {
var _a;
return (_a = _super.prototype.getContextError.call(this)) !== null && _a !== void 0 ? _a : this.state.error;
};
ValidatedForm.prototype.getContext = function () {
return __assign(__assign({}, _super.prototype.getContext.call(this)), { validating: this.state.validating });
};
ValidatedForm.prototype.getNativeFormProps = function () {
return omit(_super.prototype.getNativeFormProps.call(this), [
return (_a = super.getContextError()) !== null && _a !== void 0 ? _a : this.state.error;
}
getContext() {
return Object.assign(Object.assign({}, super.getContext()), { validating: this.state.validating });
}
getNativeFormProps() {
return omit(super.getNativeFormProps(), [
'onValidate',

@@ -35,11 +32,10 @@ 'validate',

]);
};
ValidatedForm.prototype.componentDidUpdate = function (prevProps, prevState, snapshot) {
var _this = this;
_super.prototype.componentDidUpdate.call(this, prevProps, prevState, snapshot);
var _a = this.props, model = _a.model, schema = _a.schema, validate = _a.validate, validator = _a.validator;
}
componentDidUpdate(prevProps, prevState, snapshot) {
super.componentDidUpdate(prevProps, prevState, snapshot);
const { model, schema, validate, validator } = this.props;
if (schema !== prevProps.schema || validator !== prevProps.validator) {
this.setState({ validator: schema.getValidator(validator) }, function () {
if (shouldRevalidate(validate, _this.state.validate)) {
_this.onValidate();
this.setState({ validator: schema.getValidator(validator) }, () => {
if (shouldRevalidate(validate, this.state.validate)) {
this.onValidate();
}

@@ -52,14 +48,13 @@ });

}
};
ValidatedForm.prototype.onChange = function (key, value) {
}
onChange(key, value) {
if (shouldRevalidate(this.props.validate, this.state.validate)) {
this.onValidate(key, value);
}
_super.prototype.onChange.call(this, key, value);
};
ValidatedForm.prototype.__reset = function (state) {
return __assign(__assign({}, _super.prototype.__reset.call(this, state)), { error: null, validate: false, validating: false });
};
ValidatedForm.prototype.onSubmit = function (event) {
var _this = this;
super.onChange(key, value);
}
__reset(state) {
return Object.assign(Object.assign({}, super.__reset(state)), { error: null, validate: false, validating: false });
}
onSubmit(event) {
if (event) {

@@ -70,3 +65,3 @@ event.preventDefault();

this.setState({ submitted: true, validate: true });
var result = this.onValidate().then(function (error) {
const result = this.onValidate().then(error => {
if (error !== null) {

@@ -77,4 +72,4 @@ return Promise.reject(error);

// both in the props nor the state.
return _super.prototype.onSubmit.call(_this).catch(function (error) {
_this.setState({ error: error });
return super.onSubmit().catch(error => {
this.setState({ error });
throw error;

@@ -85,5 +80,5 @@ });

return result;
};
ValidatedForm.prototype.onValidate = function (key, value) {
var model = this.getContextModel();
}
onValidate(key, value) {
let model = this.getContextModel();
if (model && key) {

@@ -93,38 +88,30 @@ model = setWith(clone(model), key, cloneDeep(value), clone);

return this.onValidateModel(model);
};
ValidatedForm.prototype.onValidateModel = function (originalModel) {
var _this = this;
var model = this.getModel('validate', originalModel);
}
onValidateModel(originalModel) {
const model = this.getModel('validate', originalModel);
// Using `then` allows using the same code for both synchronous and
// asynchronous cases. We could use `await` here, but it would make all
// calls asynchronous, unnecessary delaying synchronous validation.
var then = makeThen(function () {
_this.setState({ validating: true });
const then = makeThen(() => {
this.setState({ validating: true });
});
return then(this.state.validator(model), function (error) {
if (error === void 0) { error = null; }
return then(_this.props.onValidate(model, error), function (error) {
var _a;
if (error === void 0) { error = null; }
// Do not copy the error from props to the state.
error = _this.props.error === error ? null : error;
// If the whole operation was synchronous and resulted in the same
// error, we can skip the re-render.
_this.setState(function (state) {
return state.error === error && !state.validating
? null
: { error: error, validating: false };
});
// A predefined error takes precedence over the validation one.
return Promise.resolve((_a = _this.props.error) !== null && _a !== void 0 ? _a : error);
});
});
};
ValidatedForm.Validated = Validated;
ValidatedForm.displayName = "Validated" + Base.displayName;
ValidatedForm.defaultProps = __assign(__assign({}, Base.defaultProps), { onValidate: function (model, error) {
return error;
}, validate: 'onChangeAfterSubmit' });
return ValidatedForm;
}(Base));
return then(this.state.validator(model), (error = null) => then(this.props.onValidate(model, error), (error = null) => {
var _a;
// Do not copy the error from props to the state.
error = this.props.error === error ? null : error;
// If the whole operation was synchronous and resulted in the same
// error, we can skip the re-render.
this.setState(state => state.error === error && !state.validating
? null
: { error, validating: false });
// A predefined error takes precedence over the validation one.
return Promise.resolve((_a = this.props.error) !== null && _a !== void 0 ? _a : error);
}));
}
}
ValidatedForm.Validated = Validated;
ValidatedForm.displayName = `Validated${Base.displayName}`;
ValidatedForm.defaultProps = Object.assign(Object.assign({}, Base.defaultProps), { onValidate(model, error) {
return error;
}, validate: 'onChangeAfterSubmit' });
return ValidatedForm;

@@ -145,2 +132,2 @@ }

}
export var ValidatedForm = Validated(BaseForm);
export const ValidatedForm = Validated(BaseForm);
import { BaseForm } from './BaseForm';
import { Quick } from './QuickForm';
import { Validated, } from './ValidatedForm';
export var ValidatedQuickForm = Validated(Quick(BaseForm));
export const ValidatedQuickForm = Validated(Quick(BaseForm));
{
"name": "uniforms",
"version": "3.6.0",
"version": "3.6.1",
"license": "MIT",
"main": "./es5/index.js",
"main": "./cjs/index.js",
"module": "./esm/index.js",
"sideEffects": [
"es5/filterDOMProps.js",
"es5/randomIds.js",
"cjs/filterDOMProps.js",
"cjs/randomIds.js",
"esm/filterDOMProps.js",
"esm/randomIds.js",
"es6/filterDOMProps.js",
"es6/randomIds.js",
"src/filterDOMProps.ts",

@@ -31,8 +29,6 @@ "src/randomIds.ts"

"files": [
"es5/*.d.ts",
"es5/*.js",
"cjs/*.d.ts",
"cjs/*.js",
"esm/*.d.ts",
"esm/*.js",
"es6/*.d.ts",
"es6/*.js",
"src/*.ts",

@@ -49,3 +45,3 @@ "src/*.tsx"

},
"gitHead": "8ec8ba7751b6ce4b205c49d2aca1ab460bd09e0a"
"gitHead": "a36891aa6aa9bb10c40bf5709085fbe82ac4ae92"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc