final-form
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -307,3 +307,3 @@ 'use strict'; | ||
// | ||
var formSubscriptionItems = ['active', 'dirty', 'error', 'initialValues', 'invalid', 'pristine', 'submitting', 'submitError', 'submitFailed', 'submitSucceeded', 'valid', 'validating', 'values']; | ||
var formSubscriptionItems = ['active', 'dirty', 'error', 'errors', 'initialValues', 'invalid', 'pristine', 'submitting', 'submitError', 'submitErrors', 'submitFailed', 'submitSucceeded', 'valid', 'validating', 'values']; | ||
@@ -368,9 +368,8 @@ // | ||
var FORM_ERROR = Symbol('form-error'); | ||
var version = '0.0.2'; | ||
var version = '1.0.0'; | ||
var safeFormStateCast = function safeFormStateCast(_ref) { | ||
var convertToExternalFormState = function convertToExternalFormState(_ref) { | ||
var active = _ref.active, | ||
dirty = _ref.dirty, | ||
error = _ref.error, | ||
invalid = _ref.invalid, | ||
errors = _ref.errors, | ||
initialValues = _ref.initialValues, | ||
@@ -382,2 +381,3 @@ pristine = _ref.pristine, | ||
submitError = _ref.submitError, | ||
submitErrors = _ref.submitErrors, | ||
valid = _ref.valid, | ||
@@ -388,5 +388,6 @@ validating = _ref.validating, | ||
active: active, | ||
dirty: dirty, | ||
dirty: !pristine, | ||
error: error, | ||
invalid: invalid, | ||
errors: errors, | ||
invalid: !valid, | ||
initialValues: initialValues, | ||
@@ -398,4 +399,5 @@ pristine: pristine, | ||
submitError: submitError, | ||
submitErrors: submitErrors, | ||
valid: valid, | ||
validating: validating, | ||
validating: validating > 0, | ||
values: values | ||
@@ -476,2 +478,3 @@ }; | ||
dirty: false, | ||
errors: {}, | ||
initialValues: initialValues && _extends({}, initialValues), | ||
@@ -484,3 +487,3 @@ invalid: false, | ||
valid: true, | ||
validating: false, | ||
validating: 0, | ||
values: initialValues ? _extends({}, initialValues) : {} | ||
@@ -493,23 +496,14 @@ }; | ||
formState: formState, | ||
lastFormState: undefined, | ||
validating: 0 | ||
lastFormState: undefined | ||
}; | ||
var inBatch = false; | ||
var runRecordLevelValidation = function runRecordLevelValidation(setError) { | ||
var runRecordLevelValidation = function runRecordLevelValidation(setErrors) { | ||
var promises = []; | ||
if (validate) { | ||
var processErrors = function processErrors() { | ||
var errors = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
state.error = errors[FORM_ERROR]; | ||
Object.keys(state.fields).forEach(function (key) { | ||
setError(key, getIn(errors, key)); | ||
}); | ||
}; | ||
var errorsOrPromise = validate(_extends({}, state.formState.values)); // clone to avoid writing | ||
if (isPromise(errorsOrPromise)) { | ||
promises.push(errorsOrPromise.then(processErrors)); | ||
promises.push(errorsOrPromise.then(setErrors)); | ||
} else { | ||
processErrors(errorsOrPromise); | ||
setErrors(errorsOrPromise); | ||
} | ||
@@ -543,3 +537,4 @@ } | ||
var runValidation = function runValidation(callback) { | ||
var fields = state.fields; | ||
var fields = state.fields, | ||
formState = state.formState; | ||
@@ -556,29 +551,32 @@ var fieldKeys = Object.keys(fields); | ||
// sync version of setError | ||
var recordLevelErrors = {}; | ||
var fieldLevelErrors = {}; | ||
var setRecordLevelError = function setRecordLevelError(name, error) { | ||
recordLevelErrors[name] = error; | ||
}; | ||
var setFieldLevelError = function setFieldLevelError(name, error) { | ||
fieldLevelErrors[name] = error; | ||
}; | ||
var promises = [].concat(toConsumableArray(runRecordLevelValidation(function (name, error) { | ||
return setRecordLevelError(name, error); | ||
var promises = [].concat(toConsumableArray(runRecordLevelValidation(function (errors) { | ||
recordLevelErrors = errors || {}; | ||
})), toConsumableArray(fieldKeys.reduce(function (result, name) { | ||
return [].concat(toConsumableArray(result), toConsumableArray(runFieldLevelValidation(fields[name], function (error) { | ||
return setFieldLevelError(name, error); | ||
}))); | ||
return result.concat(runFieldLevelValidation(fields[name], function (error) { | ||
fieldLevelErrors[name] = error; | ||
})); | ||
}, []))); | ||
var processErrors = function processErrors() { | ||
var merged = _extends({}, recordLevelErrors); | ||
fieldKeys.forEach(function (name) { | ||
// field-level errors take precedent over record-level errors | ||
var error = fieldLevelErrors[name] || getIn(recordLevelErrors, name); | ||
merged = setIn(merged, name, error) || {}; | ||
fields[name].error = error; | ||
}); | ||
if (!shallowEqual(formState.errors, merged)) { | ||
formState.errors = merged; | ||
} | ||
formState.error = recordLevelErrors[FORM_ERROR]; | ||
}; | ||
// process sync errors | ||
fieldKeys.forEach(function (name) { | ||
// field-level errors take precedent over record-level errors | ||
fields[name].error = fieldLevelErrors[name] || recordLevelErrors[name]; | ||
}); | ||
processErrors(); | ||
if (promises.length) { | ||
// sync errors have been set. notify listeners while we wait for others | ||
state.validating++; | ||
state.formState.validating++; | ||
if (callback) { | ||
@@ -588,9 +586,5 @@ callback(); | ||
// reassign setError functions for async responses | ||
setRecordLevelError = setFieldLevelError = function setFieldLevelError(name, error) { | ||
fields[name].error = error; | ||
}; | ||
Promise.all(promises).then(function () { | ||
state.validating--; | ||
state.formState.validating--; | ||
processErrors(); | ||
if (callback) { | ||
@@ -625,10 +619,4 @@ callback(); | ||
var isValid = function isValid() { | ||
return !state.error && Object.keys(state.fields).every(function (key) { | ||
return !state.fields[key].error && !state.fields[key].submitError; | ||
}); | ||
}; | ||
var hasSyncErrors = function hasSyncErrors() { | ||
return state.error || Object.keys(state.fields).some(function (key) { | ||
return formState.error || Object.keys(state.fields).some(function (key) { | ||
return state.fields[key].error; | ||
@@ -640,3 +628,4 @@ }); | ||
var fields = state.fields, | ||
formState = state.formState; | ||
formState = state.formState, | ||
lastFormState = state.lastFormState; | ||
@@ -646,41 +635,12 @@ var fieldKeys = Object.keys(fields); | ||
// calculate dirty/pristine | ||
var pristine = fieldKeys.every(function (key) { | ||
formState.pristine = fieldKeys.every(function (key) { | ||
return fields[key].value === fields[key].initial; | ||
}); | ||
// calculate valid/invalid | ||
var valid = isValid(); | ||
var validating = state.validating > 0; | ||
if (pristine === formState.pristine && valid === formState.valid && state.error === formState.error && state.lastFormState && state.lastFormState.validating === validating && state.lastFormState.values === formState.values && state.lastFormState.active === formState.active) { | ||
return state.lastFormState; | ||
} | ||
var active = formState.active, | ||
initialValues = formState.initialValues, | ||
submitting = formState.submitting, | ||
submitError = formState.submitError, | ||
submitFailed = formState.submitFailed, | ||
submitSucceeded = formState.submitSucceeded, | ||
values = formState.values; | ||
return { | ||
active: active, | ||
dirty: !pristine, | ||
error: state.error, | ||
initialValues: initialValues, | ||
invalid: !valid, | ||
pristine: pristine, | ||
submitting: submitting, | ||
submitError: submitError, | ||
submitFailed: submitFailed, | ||
submitSucceeded: submitSucceeded, | ||
valid: valid, | ||
validating: validating, | ||
values: values | ||
}; | ||
formState.valid = !formState.error && !formState.submitError && !Object.keys(formState.errors).length && !(formState.submitErrors && Object.keys(formState.submitErrors).length); | ||
var nextFormState = convertToExternalFormState(formState); | ||
return lastFormState && shallowEqual(lastFormState, nextFormState) ? lastFormState : nextFormState; | ||
}; | ||
var callDebug = function callDebug() { | ||
return debug && process.env.NODE_ENV !== 'production' && debug(safeFormStateCast(state.formState), Object.keys(state.fields).reduce(function (result, key) { | ||
return debug && process.env.NODE_ENV !== 'production' && debug(convertToExternalFormState(state.formState), Object.keys(state.fields).reduce(function (result, key) { | ||
result[key] = safeFieldStateCast(state.fields[key]); | ||
@@ -705,8 +665,4 @@ return result; | ||
// generate initial error (even with no fields yet) if we need to | ||
// generate initial errors | ||
runValidation(); | ||
// runValidation(() => { | ||
// notifyFieldListeners() | ||
// notifyFormListeners() | ||
// }) | ||
@@ -752,4 +708,2 @@ var api = { | ||
state.formState.values = setIn(state.formState.values, name, value) || {}; | ||
notifyFieldListeners(); | ||
notifyFormListeners(); | ||
if (validateOnBlur) { | ||
@@ -779,3 +733,3 @@ notifyFieldListeners(); | ||
getState: function getState() { | ||
return safeFormStateCast(state.formState); | ||
return convertToExternalFormState(state.formState); | ||
}, | ||
@@ -898,2 +852,3 @@ | ||
formState.submitSucceeded = false; | ||
formState.submitErrors = errors; | ||
Object.keys(fields).forEach(function (key) { | ||
@@ -907,2 +862,3 @@ fields[key].submitError = errors && getIn(errors, key); | ||
}); | ||
delete formState.submitErrors; | ||
delete formState.submitError; | ||
@@ -954,4 +910,4 @@ formState.submitFailed = false; | ||
var memoized = memoize(subscriber); | ||
var formState = state.formState, | ||
subscribers = state.subscribers; | ||
var subscribers = state.subscribers, | ||
lastFormState = state.lastFormState; | ||
@@ -963,9 +919,7 @@ var index = subscribers.index++; | ||
}; | ||
var valid = !state.error; | ||
var stateWithError = _extends({}, formState, { | ||
error: state.error, | ||
invalid: !valid, | ||
valid: valid | ||
}); | ||
notifySubscriber(memoized, subscription, stateWithError, stateWithError, filterFormState, true); | ||
var nextFormState = calculateNextFormState(); | ||
if (nextFormState !== lastFormState) { | ||
state.lastFormState = nextFormState; | ||
} | ||
notifySubscriber(memoized, subscription, nextFormState, nextFormState, filterFormState, true); | ||
return function () { | ||
@@ -972,0 +926,0 @@ delete subscribers.entries[index]; |
@@ -303,3 +303,3 @@ // | ||
// | ||
var formSubscriptionItems = ['active', 'dirty', 'error', 'initialValues', 'invalid', 'pristine', 'submitting', 'submitError', 'submitFailed', 'submitSucceeded', 'valid', 'validating', 'values']; | ||
var formSubscriptionItems = ['active', 'dirty', 'error', 'errors', 'initialValues', 'invalid', 'pristine', 'submitting', 'submitError', 'submitErrors', 'submitFailed', 'submitSucceeded', 'valid', 'validating', 'values']; | ||
@@ -364,9 +364,8 @@ // | ||
var FORM_ERROR = Symbol('form-error'); | ||
var version = '0.0.2'; | ||
var version = '1.0.0'; | ||
var safeFormStateCast = function safeFormStateCast(_ref) { | ||
var convertToExternalFormState = function convertToExternalFormState(_ref) { | ||
var active = _ref.active, | ||
dirty = _ref.dirty, | ||
error = _ref.error, | ||
invalid = _ref.invalid, | ||
errors = _ref.errors, | ||
initialValues = _ref.initialValues, | ||
@@ -378,2 +377,3 @@ pristine = _ref.pristine, | ||
submitError = _ref.submitError, | ||
submitErrors = _ref.submitErrors, | ||
valid = _ref.valid, | ||
@@ -384,5 +384,6 @@ validating = _ref.validating, | ||
active: active, | ||
dirty: dirty, | ||
dirty: !pristine, | ||
error: error, | ||
invalid: invalid, | ||
errors: errors, | ||
invalid: !valid, | ||
initialValues: initialValues, | ||
@@ -394,4 +395,5 @@ pristine: pristine, | ||
submitError: submitError, | ||
submitErrors: submitErrors, | ||
valid: valid, | ||
validating: validating, | ||
validating: validating > 0, | ||
values: values | ||
@@ -472,2 +474,3 @@ }; | ||
dirty: false, | ||
errors: {}, | ||
initialValues: initialValues && _extends({}, initialValues), | ||
@@ -480,3 +483,3 @@ invalid: false, | ||
valid: true, | ||
validating: false, | ||
validating: 0, | ||
values: initialValues ? _extends({}, initialValues) : {} | ||
@@ -489,23 +492,14 @@ }; | ||
formState: formState, | ||
lastFormState: undefined, | ||
validating: 0 | ||
lastFormState: undefined | ||
}; | ||
var inBatch = false; | ||
var runRecordLevelValidation = function runRecordLevelValidation(setError) { | ||
var runRecordLevelValidation = function runRecordLevelValidation(setErrors) { | ||
var promises = []; | ||
if (validate) { | ||
var processErrors = function processErrors() { | ||
var errors = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
state.error = errors[FORM_ERROR]; | ||
Object.keys(state.fields).forEach(function (key) { | ||
setError(key, getIn(errors, key)); | ||
}); | ||
}; | ||
var errorsOrPromise = validate(_extends({}, state.formState.values)); // clone to avoid writing | ||
if (isPromise(errorsOrPromise)) { | ||
promises.push(errorsOrPromise.then(processErrors)); | ||
promises.push(errorsOrPromise.then(setErrors)); | ||
} else { | ||
processErrors(errorsOrPromise); | ||
setErrors(errorsOrPromise); | ||
} | ||
@@ -539,3 +533,4 @@ } | ||
var runValidation = function runValidation(callback) { | ||
var fields = state.fields; | ||
var fields = state.fields, | ||
formState = state.formState; | ||
@@ -552,29 +547,32 @@ var fieldKeys = Object.keys(fields); | ||
// sync version of setError | ||
var recordLevelErrors = {}; | ||
var fieldLevelErrors = {}; | ||
var setRecordLevelError = function setRecordLevelError(name, error) { | ||
recordLevelErrors[name] = error; | ||
}; | ||
var setFieldLevelError = function setFieldLevelError(name, error) { | ||
fieldLevelErrors[name] = error; | ||
}; | ||
var promises = [].concat(toConsumableArray(runRecordLevelValidation(function (name, error) { | ||
return setRecordLevelError(name, error); | ||
var promises = [].concat(toConsumableArray(runRecordLevelValidation(function (errors) { | ||
recordLevelErrors = errors || {}; | ||
})), toConsumableArray(fieldKeys.reduce(function (result, name) { | ||
return [].concat(toConsumableArray(result), toConsumableArray(runFieldLevelValidation(fields[name], function (error) { | ||
return setFieldLevelError(name, error); | ||
}))); | ||
return result.concat(runFieldLevelValidation(fields[name], function (error) { | ||
fieldLevelErrors[name] = error; | ||
})); | ||
}, []))); | ||
var processErrors = function processErrors() { | ||
var merged = _extends({}, recordLevelErrors); | ||
fieldKeys.forEach(function (name) { | ||
// field-level errors take precedent over record-level errors | ||
var error = fieldLevelErrors[name] || getIn(recordLevelErrors, name); | ||
merged = setIn(merged, name, error) || {}; | ||
fields[name].error = error; | ||
}); | ||
if (!shallowEqual(formState.errors, merged)) { | ||
formState.errors = merged; | ||
} | ||
formState.error = recordLevelErrors[FORM_ERROR]; | ||
}; | ||
// process sync errors | ||
fieldKeys.forEach(function (name) { | ||
// field-level errors take precedent over record-level errors | ||
fields[name].error = fieldLevelErrors[name] || recordLevelErrors[name]; | ||
}); | ||
processErrors(); | ||
if (promises.length) { | ||
// sync errors have been set. notify listeners while we wait for others | ||
state.validating++; | ||
state.formState.validating++; | ||
if (callback) { | ||
@@ -584,9 +582,5 @@ callback(); | ||
// reassign setError functions for async responses | ||
setRecordLevelError = setFieldLevelError = function setFieldLevelError(name, error) { | ||
fields[name].error = error; | ||
}; | ||
Promise.all(promises).then(function () { | ||
state.validating--; | ||
state.formState.validating--; | ||
processErrors(); | ||
if (callback) { | ||
@@ -621,10 +615,4 @@ callback(); | ||
var isValid = function isValid() { | ||
return !state.error && Object.keys(state.fields).every(function (key) { | ||
return !state.fields[key].error && !state.fields[key].submitError; | ||
}); | ||
}; | ||
var hasSyncErrors = function hasSyncErrors() { | ||
return state.error || Object.keys(state.fields).some(function (key) { | ||
return formState.error || Object.keys(state.fields).some(function (key) { | ||
return state.fields[key].error; | ||
@@ -636,3 +624,4 @@ }); | ||
var fields = state.fields, | ||
formState = state.formState; | ||
formState = state.formState, | ||
lastFormState = state.lastFormState; | ||
@@ -642,41 +631,12 @@ var fieldKeys = Object.keys(fields); | ||
// calculate dirty/pristine | ||
var pristine = fieldKeys.every(function (key) { | ||
formState.pristine = fieldKeys.every(function (key) { | ||
return fields[key].value === fields[key].initial; | ||
}); | ||
// calculate valid/invalid | ||
var valid = isValid(); | ||
var validating = state.validating > 0; | ||
if (pristine === formState.pristine && valid === formState.valid && state.error === formState.error && state.lastFormState && state.lastFormState.validating === validating && state.lastFormState.values === formState.values && state.lastFormState.active === formState.active) { | ||
return state.lastFormState; | ||
} | ||
var active = formState.active, | ||
initialValues = formState.initialValues, | ||
submitting = formState.submitting, | ||
submitError = formState.submitError, | ||
submitFailed = formState.submitFailed, | ||
submitSucceeded = formState.submitSucceeded, | ||
values = formState.values; | ||
return { | ||
active: active, | ||
dirty: !pristine, | ||
error: state.error, | ||
initialValues: initialValues, | ||
invalid: !valid, | ||
pristine: pristine, | ||
submitting: submitting, | ||
submitError: submitError, | ||
submitFailed: submitFailed, | ||
submitSucceeded: submitSucceeded, | ||
valid: valid, | ||
validating: validating, | ||
values: values | ||
}; | ||
formState.valid = !formState.error && !formState.submitError && !Object.keys(formState.errors).length && !(formState.submitErrors && Object.keys(formState.submitErrors).length); | ||
var nextFormState = convertToExternalFormState(formState); | ||
return lastFormState && shallowEqual(lastFormState, nextFormState) ? lastFormState : nextFormState; | ||
}; | ||
var callDebug = function callDebug() { | ||
return debug && process.env.NODE_ENV !== 'production' && debug(safeFormStateCast(state.formState), Object.keys(state.fields).reduce(function (result, key) { | ||
return debug && process.env.NODE_ENV !== 'production' && debug(convertToExternalFormState(state.formState), Object.keys(state.fields).reduce(function (result, key) { | ||
result[key] = safeFieldStateCast(state.fields[key]); | ||
@@ -701,8 +661,4 @@ return result; | ||
// generate initial error (even with no fields yet) if we need to | ||
// generate initial errors | ||
runValidation(); | ||
// runValidation(() => { | ||
// notifyFieldListeners() | ||
// notifyFormListeners() | ||
// }) | ||
@@ -748,4 +704,2 @@ var api = { | ||
state.formState.values = setIn(state.formState.values, name, value) || {}; | ||
notifyFieldListeners(); | ||
notifyFormListeners(); | ||
if (validateOnBlur) { | ||
@@ -775,3 +729,3 @@ notifyFieldListeners(); | ||
getState: function getState() { | ||
return safeFormStateCast(state.formState); | ||
return convertToExternalFormState(state.formState); | ||
}, | ||
@@ -894,2 +848,3 @@ | ||
formState.submitSucceeded = false; | ||
formState.submitErrors = errors; | ||
Object.keys(fields).forEach(function (key) { | ||
@@ -903,2 +858,3 @@ fields[key].submitError = errors && getIn(errors, key); | ||
}); | ||
delete formState.submitErrors; | ||
delete formState.submitError; | ||
@@ -950,4 +906,4 @@ formState.submitFailed = false; | ||
var memoized = memoize(subscriber); | ||
var formState = state.formState, | ||
subscribers = state.subscribers; | ||
var subscribers = state.subscribers, | ||
lastFormState = state.lastFormState; | ||
@@ -959,9 +915,7 @@ var index = subscribers.index++; | ||
}; | ||
var valid = !state.error; | ||
var stateWithError = _extends({}, formState, { | ||
error: state.error, | ||
invalid: !valid, | ||
valid: valid | ||
}); | ||
notifySubscriber(memoized, subscription, stateWithError, stateWithError, filterFormState, true); | ||
var nextFormState = calculateNextFormState(); | ||
if (nextFormState !== lastFormState) { | ||
state.lastFormState = nextFormState; | ||
} | ||
notifySubscriber(memoized, subscription, nextFormState, nextFormState, filterFormState, true); | ||
return function () { | ||
@@ -968,0 +922,0 @@ delete subscribers.entries[index]; |
@@ -309,3 +309,3 @@ (function (global, factory) { | ||
// | ||
var formSubscriptionItems = ['active', 'dirty', 'error', 'initialValues', 'invalid', 'pristine', 'submitting', 'submitError', 'submitFailed', 'submitSucceeded', 'valid', 'validating', 'values']; | ||
var formSubscriptionItems = ['active', 'dirty', 'error', 'errors', 'initialValues', 'invalid', 'pristine', 'submitting', 'submitError', 'submitErrors', 'submitFailed', 'submitSucceeded', 'valid', 'validating', 'values']; | ||
@@ -370,9 +370,8 @@ // | ||
var FORM_ERROR = Symbol('form-error'); | ||
var version = '0.0.2'; | ||
var version = '1.0.0'; | ||
var safeFormStateCast = function safeFormStateCast(_ref) { | ||
var convertToExternalFormState = function convertToExternalFormState(_ref) { | ||
var active = _ref.active, | ||
dirty = _ref.dirty, | ||
error = _ref.error, | ||
invalid = _ref.invalid, | ||
errors = _ref.errors, | ||
initialValues = _ref.initialValues, | ||
@@ -384,2 +383,3 @@ pristine = _ref.pristine, | ||
submitError = _ref.submitError, | ||
submitErrors = _ref.submitErrors, | ||
valid = _ref.valid, | ||
@@ -390,5 +390,6 @@ validating = _ref.validating, | ||
active: active, | ||
dirty: dirty, | ||
dirty: !pristine, | ||
error: error, | ||
invalid: invalid, | ||
errors: errors, | ||
invalid: !valid, | ||
initialValues: initialValues, | ||
@@ -400,4 +401,5 @@ pristine: pristine, | ||
submitError: submitError, | ||
submitErrors: submitErrors, | ||
valid: valid, | ||
validating: validating, | ||
validating: validating > 0, | ||
values: values | ||
@@ -478,2 +480,3 @@ }; | ||
dirty: false, | ||
errors: {}, | ||
initialValues: initialValues && _extends({}, initialValues), | ||
@@ -486,3 +489,3 @@ invalid: false, | ||
valid: true, | ||
validating: false, | ||
validating: 0, | ||
values: initialValues ? _extends({}, initialValues) : {} | ||
@@ -495,23 +498,14 @@ }; | ||
formState: formState, | ||
lastFormState: undefined, | ||
validating: 0 | ||
lastFormState: undefined | ||
}; | ||
var inBatch = false; | ||
var runRecordLevelValidation = function runRecordLevelValidation(setError) { | ||
var runRecordLevelValidation = function runRecordLevelValidation(setErrors) { | ||
var promises = []; | ||
if (validate) { | ||
var processErrors = function processErrors() { | ||
var errors = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
state.error = errors[FORM_ERROR]; | ||
Object.keys(state.fields).forEach(function (key) { | ||
setError(key, getIn(errors, key)); | ||
}); | ||
}; | ||
var errorsOrPromise = validate(_extends({}, state.formState.values)); // clone to avoid writing | ||
if (isPromise(errorsOrPromise)) { | ||
promises.push(errorsOrPromise.then(processErrors)); | ||
promises.push(errorsOrPromise.then(setErrors)); | ||
} else { | ||
processErrors(errorsOrPromise); | ||
setErrors(errorsOrPromise); | ||
} | ||
@@ -545,3 +539,4 @@ } | ||
var runValidation = function runValidation(callback) { | ||
var fields = state.fields; | ||
var fields = state.fields, | ||
formState = state.formState; | ||
@@ -558,29 +553,32 @@ var fieldKeys = Object.keys(fields); | ||
// sync version of setError | ||
var recordLevelErrors = {}; | ||
var fieldLevelErrors = {}; | ||
var setRecordLevelError = function setRecordLevelError(name, error) { | ||
recordLevelErrors[name] = error; | ||
}; | ||
var setFieldLevelError = function setFieldLevelError(name, error) { | ||
fieldLevelErrors[name] = error; | ||
}; | ||
var promises = [].concat(toConsumableArray(runRecordLevelValidation(function (name, error) { | ||
return setRecordLevelError(name, error); | ||
var promises = [].concat(toConsumableArray(runRecordLevelValidation(function (errors) { | ||
recordLevelErrors = errors || {}; | ||
})), toConsumableArray(fieldKeys.reduce(function (result, name) { | ||
return [].concat(toConsumableArray(result), toConsumableArray(runFieldLevelValidation(fields[name], function (error) { | ||
return setFieldLevelError(name, error); | ||
}))); | ||
return result.concat(runFieldLevelValidation(fields[name], function (error) { | ||
fieldLevelErrors[name] = error; | ||
})); | ||
}, []))); | ||
var processErrors = function processErrors() { | ||
var merged = _extends({}, recordLevelErrors); | ||
fieldKeys.forEach(function (name) { | ||
// field-level errors take precedent over record-level errors | ||
var error = fieldLevelErrors[name] || getIn(recordLevelErrors, name); | ||
merged = setIn(merged, name, error) || {}; | ||
fields[name].error = error; | ||
}); | ||
if (!shallowEqual(formState.errors, merged)) { | ||
formState.errors = merged; | ||
} | ||
formState.error = recordLevelErrors[FORM_ERROR]; | ||
}; | ||
// process sync errors | ||
fieldKeys.forEach(function (name) { | ||
// field-level errors take precedent over record-level errors | ||
fields[name].error = fieldLevelErrors[name] || recordLevelErrors[name]; | ||
}); | ||
processErrors(); | ||
if (promises.length) { | ||
// sync errors have been set. notify listeners while we wait for others | ||
state.validating++; | ||
state.formState.validating++; | ||
if (callback) { | ||
@@ -590,9 +588,5 @@ callback(); | ||
// reassign setError functions for async responses | ||
setRecordLevelError = setFieldLevelError = function setFieldLevelError(name, error) { | ||
fields[name].error = error; | ||
}; | ||
Promise.all(promises).then(function () { | ||
state.validating--; | ||
state.formState.validating--; | ||
processErrors(); | ||
if (callback) { | ||
@@ -627,10 +621,4 @@ callback(); | ||
var isValid = function isValid() { | ||
return !state.error && Object.keys(state.fields).every(function (key) { | ||
return !state.fields[key].error && !state.fields[key].submitError; | ||
}); | ||
}; | ||
var hasSyncErrors = function hasSyncErrors() { | ||
return state.error || Object.keys(state.fields).some(function (key) { | ||
return formState.error || Object.keys(state.fields).some(function (key) { | ||
return state.fields[key].error; | ||
@@ -642,3 +630,4 @@ }); | ||
var fields = state.fields, | ||
formState = state.formState; | ||
formState = state.formState, | ||
lastFormState = state.lastFormState; | ||
@@ -648,41 +637,12 @@ var fieldKeys = Object.keys(fields); | ||
// calculate dirty/pristine | ||
var pristine = fieldKeys.every(function (key) { | ||
formState.pristine = fieldKeys.every(function (key) { | ||
return fields[key].value === fields[key].initial; | ||
}); | ||
// calculate valid/invalid | ||
var valid = isValid(); | ||
var validating = state.validating > 0; | ||
if (pristine === formState.pristine && valid === formState.valid && state.error === formState.error && state.lastFormState && state.lastFormState.validating === validating && state.lastFormState.values === formState.values && state.lastFormState.active === formState.active) { | ||
return state.lastFormState; | ||
} | ||
var active = formState.active, | ||
initialValues = formState.initialValues, | ||
submitting = formState.submitting, | ||
submitError = formState.submitError, | ||
submitFailed = formState.submitFailed, | ||
submitSucceeded = formState.submitSucceeded, | ||
values = formState.values; | ||
return { | ||
active: active, | ||
dirty: !pristine, | ||
error: state.error, | ||
initialValues: initialValues, | ||
invalid: !valid, | ||
pristine: pristine, | ||
submitting: submitting, | ||
submitError: submitError, | ||
submitFailed: submitFailed, | ||
submitSucceeded: submitSucceeded, | ||
valid: valid, | ||
validating: validating, | ||
values: values | ||
}; | ||
formState.valid = !formState.error && !formState.submitError && !Object.keys(formState.errors).length && !(formState.submitErrors && Object.keys(formState.submitErrors).length); | ||
var nextFormState = convertToExternalFormState(formState); | ||
return lastFormState && shallowEqual(lastFormState, nextFormState) ? lastFormState : nextFormState; | ||
}; | ||
var callDebug = function callDebug() { | ||
return debug && "development" !== 'production' && debug(safeFormStateCast(state.formState), Object.keys(state.fields).reduce(function (result, key) { | ||
return debug && "development" !== 'production' && debug(convertToExternalFormState(state.formState), Object.keys(state.fields).reduce(function (result, key) { | ||
result[key] = safeFieldStateCast(state.fields[key]); | ||
@@ -707,8 +667,4 @@ return result; | ||
// generate initial error (even with no fields yet) if we need to | ||
// generate initial errors | ||
runValidation(); | ||
// runValidation(() => { | ||
// notifyFieldListeners() | ||
// notifyFormListeners() | ||
// }) | ||
@@ -754,4 +710,2 @@ var api = { | ||
state.formState.values = setIn(state.formState.values, name, value) || {}; | ||
notifyFieldListeners(); | ||
notifyFormListeners(); | ||
if (validateOnBlur) { | ||
@@ -781,3 +735,3 @@ notifyFieldListeners(); | ||
getState: function getState() { | ||
return safeFormStateCast(state.formState); | ||
return convertToExternalFormState(state.formState); | ||
}, | ||
@@ -900,2 +854,3 @@ | ||
formState.submitSucceeded = false; | ||
formState.submitErrors = errors; | ||
Object.keys(fields).forEach(function (key) { | ||
@@ -909,2 +864,3 @@ fields[key].submitError = errors && getIn(errors, key); | ||
}); | ||
delete formState.submitErrors; | ||
delete formState.submitError; | ||
@@ -956,4 +912,4 @@ formState.submitFailed = false; | ||
var memoized = memoize(subscriber); | ||
var formState = state.formState, | ||
subscribers = state.subscribers; | ||
var subscribers = state.subscribers, | ||
lastFormState = state.lastFormState; | ||
@@ -965,9 +921,7 @@ var index = subscribers.index++; | ||
}; | ||
var valid = !state.error; | ||
var stateWithError = _extends({}, formState, { | ||
error: state.error, | ||
invalid: !valid, | ||
valid: valid | ||
}); | ||
notifySubscriber(memoized, subscription, stateWithError, stateWithError, filterFormState, true); | ||
var nextFormState = calculateNextFormState(); | ||
if (nextFormState !== lastFormState) { | ||
state.lastFormState = nextFormState; | ||
} | ||
notifySubscriber(memoized, subscription, nextFormState, nextFormState, filterFormState, true); | ||
return function () { | ||
@@ -974,0 +928,0 @@ delete subscribers.entries[index]; |
@@ -1,2 +0,2 @@ | ||
!function(e,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports):"function"==typeof define&&define.amd?define(["exports"],i):i(e["final-form"]={})}(this,function(e){"use strict";function i(e,i,t,r,n){var o=n(t,r,i,arguments.length>5&&void 0!==arguments[5]&&arguments[5]);o&&e(o)}function t(e,t,r,n){var o=e.entries;Object.keys(o).forEach(function(e){var a=o[Number(e)],u=a.subscription;i(a.subscriber,u,t,r,n)})}var r=function(e){if(null===e||void 0===e)return[];if("string"!=typeof e)throw new Error("toPath() expects a string");return e.length?e.split(/[.[\]]+/).filter(Boolean):[]},n=function(e,i){var t=r(i),n=e,o=!0,a=!1,u=void 0;try{for(var s,l=t[Symbol.iterator]();!(o=(s=l.next()).done);o=!0){var c=s.value;if(void 0===n||null===n||!isNaN(n))return;if(Array.isArray(n)&&isNaN(c))return;n=n[c]}}catch(e){a=!0,u=e}finally{try{!o&&l.return&&l.return()}finally{if(a)throw u}}return n},o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},a=function(e,i,t){return i in e?Object.defineProperty(e,i,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[i]=t,e},u=Object.assign||function(e){for(var i=1;i<arguments.length;i++){var t=arguments[i];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},s=function(e){if(Array.isArray(e)){for(var i=0,t=Array(e.length);i<e.length;i++)t[i]=e[i];return t}return Array.from(e)},l=function e(i,t,r,n){if(t>=r.length)return n;var o=r[t];if(isNaN(o)){if(void 0===i||null===i){var l=e(void 0,t+1,r,n);return void 0===l?void 0:a({},o,l)}if(Array.isArray(i))throw new Error("Cannot set a non-numeric property on an array");var c=e(i[o],t+1,r,n),f=Object.keys(i).length;return void 0===c&&(void 0===i[o]&&0===f||void 0!==i[o]&&f<=1)?void 0:u({},i,a({},o,c))}var d=Number(o);if(void 0===i||null===i){var v=e(void 0,t+1,r,n);if(void 0===v)return;var b=[];return b[d]=v,b}if(!Array.isArray(i))throw new Error("Cannot set a numeric property on an object");var m=i[d],y=e(m,t+1,r,n);if(void 0!==y||!(void 0===m&&0===i.length||void 0!==m&&1===i.length)){var h=[].concat(s(i));return void 0===y?h.splice(d,1):h[d]=y,h}},c=function(e,i,t){if(void 0===e||null===e)throw new Error("Cannot call setIn() with "+String(e)+" state");if(void 0===i||null===i)throw new Error("Cannot call setIn() with "+String(i)+" key");return l(e,0,r(i),t)},f=function(e,i){var t=e.submitFailed,r=e.submitSucceeded,n=i.active,o=i.blur,a=i.change,u=i.error,s=i.focus,l=i.initial,c=i.name,f=i.submitError,d=i.touched,v=i.value,b=i.visited,m=l===v,y=!u&&!f;return{active:n,blur:o,change:a,dirty:!m,error:u,focus:s,initial:l,invalid:!y,name:c,pristine:m,submitError:f,submitFailed:t,submitSucceeded:r,touched:d,valid:y,value:v,visited:b}},d=["active","dirty","error","initial","invalid","pristine","submitError","submitFailed","submitSucceeded","touched","valid","value","visited"],v=function(e,i,t,r,n){var o=!1;return n.forEach(function(n){r[n]&&(e[n]=i[n],t&&i[n]===t[n]||(o=!0))}),o},b=function(e,i,t,r){var n={blur:e.blur,change:e.change,focus:e.focus,name:e.name};return v(n,e,i,t,d)||!i||r?n:void 0},m=["active","dirty","error","initialValues","invalid","pristine","submitting","submitError","submitFailed","submitSucceeded","valid","validating","values"],y=function(e,i,t,r){var n={};return v(n,e,i,t,m)||!i||r?n:void 0},h=function(e,i){if(e===i)return!0;if("object"!==(void 0===e?"undefined":o(e))||!e||"object"!==(void 0===i?"undefined":o(i))||!i)return!1;var t=Object.keys(e),r=Object.keys(i);if(t.length!==r.length)return!1;for(var n=Object.prototype.hasOwnProperty.bind(i),a=0;a<t.length;a++){var u=t[a];if(!n(u)||e[u]!==i[u])return!1}return!0},S=function(e){var i=void 0,t=void 0;return function(){for(var r=arguments.length,n=Array(r),o=0;o<r;o++)n[o]=arguments[o];return i&&n.length===i.length&&!n.some(function(e,t){return!h(i[t],e)})||(i=n,t=e.apply(void 0,n)),t}},g=function(e){return!!e&&("object"===(void 0===e?"undefined":o(e))||"function"==typeof e)&&"function"==typeof e.then},p=Symbol("form-error"),E=function(e){return{active:e.active,dirty:e.dirty,error:e.error,invalid:e.invalid,initialValues:e.initialValues,pristine:e.pristine,submitting:e.submitting,submitFailed:e.submitFailed,submitSucceeded:e.submitSucceeded,submitError:e.submitError,valid:e.valid,validating:e.validating,values:e.values}},O=function(e){var i=e.active,t=e.blur,r=e.change,n=e.error,o=e.focus,a=e.initial,u=(e.lastFieldState,e.name),s=e.submitError,l=e.touched,c=e.value,f=e.visited,d=c===a,v=n||s;return{active:i,blur:t,change:r,dirty:!d,error:n,focus:o,initial:a,invalid:v,name:u,pristine:d,submitError:s,touched:l,valid:!v,value:c,visited:f}};e.createForm=function(e){if(!e)throw new Error("No config specified");var r=e.debug,o=e.initialValues,a=e.onSubmit,l=e.validate,d=e.validateOnBlur;if(!a)throw new Error("No onSubmit function specified");var v={subscribers:{index:0,entries:{}},fieldSubscribers:{},fields:{},formState:{dirty:!1,initialValues:o&&u({},o),invalid:!1,pristine:!0,submitting:!1,submitFailed:!1,submitSucceeded:!1,valid:!0,validating:!1,values:o?u({},o):{}},lastFormState:void 0,validating:0},m=!1,j=function(e){var i=[];if(l){var t=function(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};v.error=i[p],Object.keys(v.fields).forEach(function(t){e(t,n(i,t))})},r=l(u({},v.formState.values));g(r)?i.push(r.then(t)):t(r)}return i},F=function(e,i){var t=e.validators,r=[];if(Object.keys(t).length){var n=void 0;Object.keys(t).forEach(function(o){var a=(0,t[Number(o)])(e.value,v.formState.values);a&&g(a)?r.push(a.then(i)):n||(n=a)}),i(n)}return r},w=function(e){var i=v.fields,t=Object.keys(i);if(l||t.some(function(e){return i[e].validators&&Object.keys(i[e].validators).length})){var r={},n={},o=function(e,i){r[e]=i},a=function(e,i){n[e]=i},u=[].concat(s(j(function(e,i){return o(e,i)})),s(t.reduce(function(e,t){return[].concat(s(e),s(F(i[t],function(e){return a(t,e)})))},[])));t.forEach(function(e){i[e].error=n[e]||r[e]}),u.length?(v.validating++,e&&e(),o=a=function(e,t){i[e].error=t},Promise.all(u).then(function(){v.validating--,e&&e()})):e&&e()}else e&&e()},k=function(e){if(!m){var i=v.fields,r=v.fieldSubscribers,n=v.formState;Object.keys(i).forEach(function(e){var o=i[e],a=f(n,o),u=o.lastFieldState;h(a,u)||(o.lastFieldState=a,t(r[e],a,u,b))})}},N=function(){return!v.error&&Object.keys(v.fields).every(function(e){return!v.fields[e].error&&!v.fields[e].submitError})},A=function(){return v.error||Object.keys(v.fields).some(function(e){return v.fields[e].error})},V=function(){var e=v.fields,i=v.formState,t=Object.keys(e).every(function(i){return e[i].value===e[i].initial}),r=N(),n=v.validating>0;if(t===i.pristine&&r===i.valid&&v.error===i.error&&v.lastFormState&&v.lastFormState.validating===n&&v.lastFormState.values===i.values&&v.lastFormState.active===i.active)return v.lastFormState;var o=i.active,a=i.initialValues,u=i.submitting,s=i.submitError,l=i.submitFailed,c=i.submitSucceeded,f=i.values;return{active:o,dirty:!t,error:v.error,initialValues:a,invalid:!r,pristine:t,submitting:u,submitError:s,submitFailed:l,submitSucceeded:c,valid:r,validating:n,values:f}},x=function(){return r&&!1&&r(E(v.formState),Object.keys(v.fields).reduce(function(e,i){return e[i]=O(v.fields[i]),e},{}))},P=function(){if(x(),!m){var e=v.lastFormState,i=V();i!==e&&(v.lastFormState=i,t(v.subscribers,i,e,y))}};w();var C={batch:function(e){m=!0,e(),m=!1,k(),P()},blur:function(e){var i=v.fields,t=v.formState,r=i[e];r&&r.active&&(delete t.active,i[e]=u({},r,{active:!1,touched:!0}),d?w(function(){k(),P()}):(k(),P()))},change:function(e,i){var t=v.fields;t[e]&&t[e].value!==i&&(t[e].value=i,v.formState.values=c(v.formState.values,e,i)||{},k(),P(),d?(k(),P()):w(function(){k(),P()}))},focus:function(e){var i=v.fields[e];i&&!i.active&&(v.formState.active=e,i.active=!0,i.visited=!0,k(),P())},getState:function(){return E(v.formState)},initialize:function(e){var i=v.fields,t=v.formState;t.initialValues=e,t.values=e,Object.keys(i).forEach(function(t){var r=i[t],o=n(e,t);r.value=o,r.initial=o,r.touched=!1,r.visited=!1}),w(function(){k(),P()})},registerField:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},o=arguments[3];v.fieldSubscribers[e]||(v.fieldSubscribers[e]={index:0,entries:{}});var a=v.fieldSubscribers[e].index++;if(v.fieldSubscribers[e].entries[a]={subscriber:S(t),subscription:r},!v.fields[e]){var u=v.formState.initialValues?n(v.formState.initialValues,e):void 0;v.fields[e]={active:!1,blur:function(){return C.blur(e)},change:function(i){return C.change(e,i)},focus:function(){return C.focus(e)},initial:u,lastFieldState:void 0,name:e,touched:!1,value:u,validators:{},visited:!1}}o&&(v.fields[e].validators[a]=o);var s=!1,l=function(){var n=f(v.formState,v.fields[e]);i(t,r,n,void 0,b,!0),v.fields[e].lastFieldState=n,s=!0};return w(function(){P(),s||l(),k()}),function(){delete v.fields[e].validators[a],delete v.fieldSubscribers[e].entries[a],Object.keys(v.fieldSubscribers[e].entries).length||(delete v.fieldSubscribers[e],delete v.fields[e]),w(function(){k(),P()})}},reset:function(){C.initialize(v.formState.initialValues||{})},submit:function(){var e=v.formState,i=v.fields;if(A())return Object.keys(i).forEach(function(e){i[e].touched=!0}),void k();var t=void 0,r=!1,o=function(o){e.submitting=!1,o&&(Object.keys(o).length||Object.getOwnPropertySymbols(o).length)?(e.submitFailed=!0,e.submitSucceeded=!1,Object.keys(i).forEach(function(e){i[e].submitError=o&&n(o,e)}),e.submitError=o[p]):(Object.keys(i).forEach(function(e){delete i[e].submitError}),delete e.submitError,e.submitFailed=!1,e.submitSucceeded=!0),P(),k(),r=!0,t&&t()};if(e.submitting=!0,e.submitFailed=!1,e.submitSucceeded=!1,2===a.length){if(a(e.values,o),!r)return P(),new Promise(function(e){t=e})}else{var u=a(e.values);if(u&&g(u))return P(),u.then(o);o(u)}},subscribe:function(e,t){if(!e)throw new Error("No callback given.");if(!t)throw new Error("No subscription provided. What values do you want to listen to?");var r=S(e),n=v.formState,o=v.subscribers,a=o.index++;o.entries[a]={subscriber:r,subscription:t};var s=!v.error,l=u({},n,{error:v.error,invalid:!s,valid:s});return i(r,t,l,l,y,!0),function(){delete o.entries[a]}}};return C},e.FORM_ERROR=p,e.version="0.0.2",e.formSubscriptionItems=m,e.fieldSubscriptionItems=d,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(e["final-form"]={})}(this,function(e){"use strict";function r(e,r,t,i,n){var o=n(t,i,r,arguments.length>5&&void 0!==arguments[5]&&arguments[5]);o&&e(o)}function t(e,t,i,n){var o=e.entries;Object.keys(o).forEach(function(e){var a=o[Number(e)],u=a.subscription;r(a.subscriber,u,t,i,n)})}var i=function(e){if(null===e||void 0===e)return[];if("string"!=typeof e)throw new Error("toPath() expects a string");return e.length?e.split(/[.[\]]+/).filter(Boolean):[]},n=function(e,r){var t=i(r),n=e,o=!0,a=!1,u=void 0;try{for(var s,l=t[Symbol.iterator]();!(o=(s=l.next()).done);o=!0){var c=s.value;if(void 0===n||null===n||!isNaN(n))return;if(Array.isArray(n)&&isNaN(c))return;n=n[c]}}catch(e){a=!0,u=e}finally{try{!o&&l.return&&l.return()}finally{if(a)throw u}}return n},o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},a=function(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e},u=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i])}return e},s=function(e){if(Array.isArray(e)){for(var r=0,t=Array(e.length);r<e.length;r++)t[r]=e[r];return t}return Array.from(e)},l=function e(r,t,i,n){if(t>=i.length)return n;var o=i[t];if(isNaN(o)){if(void 0===r||null===r){var l=e(void 0,t+1,i,n);return void 0===l?void 0:a({},o,l)}if(Array.isArray(r))throw new Error("Cannot set a non-numeric property on an array");var c=e(r[o],t+1,i,n),f=Object.keys(r).length;return void 0===c&&(void 0===r[o]&&0===f||void 0!==r[o]&&f<=1)?void 0:u({},r,a({},o,c))}var d=Number(o);if(void 0===r||null===r){var v=e(void 0,t+1,i,n);if(void 0===v)return;var b=[];return b[d]=v,b}if(!Array.isArray(r))throw new Error("Cannot set a numeric property on an object");var m=r[d],h=e(m,t+1,i,n);if(void 0!==h||!(void 0===m&&0===r.length||void 0!==m&&1===r.length)){var y=[].concat(s(r));return void 0===h?y.splice(d,1):y[d]=h,y}},c=function(e,r,t){if(void 0===e||null===e)throw new Error("Cannot call setIn() with "+String(e)+" state");if(void 0===r||null===r)throw new Error("Cannot call setIn() with "+String(r)+" key");return l(e,0,i(r),t)},f=function(e,r){var t=e.submitFailed,i=e.submitSucceeded,n=r.active,o=r.blur,a=r.change,u=r.error,s=r.focus,l=r.initial,c=r.name,f=r.submitError,d=r.touched,v=r.value,b=r.visited,m=l===v,h=!u&&!f;return{active:n,blur:o,change:a,dirty:!m,error:u,focus:s,initial:l,invalid:!h,name:c,pristine:m,submitError:f,submitFailed:t,submitSucceeded:i,touched:d,valid:h,value:v,visited:b}},d=["active","dirty","error","initial","invalid","pristine","submitError","submitFailed","submitSucceeded","touched","valid","value","visited"],v=function(e,r,t,i,n){var o=!1;return n.forEach(function(n){i[n]&&(e[n]=r[n],t&&r[n]===t[n]||(o=!0))}),o},b=function(e,r,t,i){var n={blur:e.blur,change:e.change,focus:e.focus,name:e.name};return v(n,e,r,t,d)||!r||i?n:void 0},m=["active","dirty","error","errors","initialValues","invalid","pristine","submitting","submitError","submitErrors","submitFailed","submitSucceeded","valid","validating","values"],h=function(e,r,t,i){var n={};return v(n,e,r,t,m)||!r||i?n:void 0},y=function(e,r){if(e===r)return!0;if("object"!==(void 0===e?"undefined":o(e))||!e||"object"!==(void 0===r?"undefined":o(r))||!r)return!1;var t=Object.keys(e),i=Object.keys(r);if(t.length!==i.length)return!1;for(var n=Object.prototype.hasOwnProperty.bind(r),a=0;a<t.length;a++){var u=t[a];if(!n(u)||e[u]!==r[u])return!1}return!0},S=function(e){var r=void 0,t=void 0;return function(){for(var i=arguments.length,n=Array(i),o=0;o<i;o++)n[o]=arguments[o];return r&&n.length===r.length&&!n.some(function(e,t){return!y(r[t],e)})||(r=n,t=e.apply(void 0,n)),t}},g=function(e){return!!e&&("object"===(void 0===e?"undefined":o(e))||"function"==typeof e)&&"function"==typeof e.then},p=Symbol("form-error"),E=function(e){var r=e.active,t=e.error,i=e.errors,n=e.initialValues,o=e.pristine,a=e.submitting,u=e.submitFailed,s=e.submitSucceeded,l=e.submitError,c=e.submitErrors,f=e.valid;return{active:r,dirty:!o,error:t,errors:i,invalid:!f,initialValues:n,pristine:o,submitting:a,submitFailed:u,submitSucceeded:s,submitError:l,submitErrors:c,valid:f,validating:e.validating>0,values:e.values}},O=function(e){var r=e.active,t=e.blur,i=e.change,n=e.error,o=e.focus,a=e.initial,u=(e.lastFieldState,e.name),s=e.submitError,l=e.touched,c=e.value,f=e.visited,d=c===a,v=n||s;return{active:r,blur:t,change:i,dirty:!d,error:n,focus:o,initial:a,invalid:v,name:u,pristine:d,submitError:s,touched:l,valid:!v,value:c,visited:f}};e.createForm=function(e){if(!e)throw new Error("No config specified");var i=e.debug,o=e.initialValues,a=e.onSubmit,l=e.validate,d=e.validateOnBlur;if(!a)throw new Error("No onSubmit function specified");var v={dirty:!1,errors:{},initialValues:o&&u({},o),invalid:!1,pristine:!0,submitting:!1,submitFailed:!1,submitSucceeded:!1,valid:!0,validating:0,values:o?u({},o):{}},m={subscribers:{index:0,entries:{}},fieldSubscribers:{},fields:{},formState:v,lastFormState:void 0},j=!1,w=function(e){var r=[];if(l){var t=l(u({},m.formState.values));g(t)?r.push(t.then(e)):e(t)}return r},F=function(e,r){var t=e.validators,i=[];if(Object.keys(t).length){var n=void 0;Object.keys(t).forEach(function(o){var a=(0,t[Number(o)])(e.value,m.formState.values);a&&g(a)?i.push(a.then(r)):n||(n=a)}),r(n)}return i},k=function(e){var r=m.fields,t=m.formState,i=Object.keys(r);if(l||i.some(function(e){return r[e].validators&&Object.keys(r[e].validators).length})){var o={},a={},f=[].concat(s(w(function(e){o=e||{}})),s(i.reduce(function(e,t){return e.concat(F(r[t],function(e){a[t]=e}))},[]))),d=function(){var e=u({},o);i.forEach(function(t){var i=a[t]||n(o,t);e=c(e,t,i)||{},r[t].error=i}),y(t.errors,e)||(t.errors=e),t.error=o[p]};d(),f.length?(m.formState.validating++,e&&e(),Promise.all(f).then(function(){m.formState.validating--,d(),e&&e()})):e&&e()}else e&&e()},N=function(e){if(!j){var r=m.fields,i=m.fieldSubscribers,n=m.formState;Object.keys(r).forEach(function(e){var o=r[e],a=f(n,o),u=o.lastFieldState;y(a,u)||(o.lastFieldState=a,t(i[e],a,u,b))})}},A=function(){return v.error||Object.keys(m.fields).some(function(e){return m.fields[e].error})},x=function(){var e=m.fields,r=m.formState,t=m.lastFormState,i=Object.keys(e);r.pristine=i.every(function(r){return e[r].value===e[r].initial}),r.valid=!(r.error||r.submitError||Object.keys(r.errors).length||r.submitErrors&&Object.keys(r.submitErrors).length);var n=E(r);return t&&y(t,n)?t:n},V=function(){return i&&!1&&i(E(m.formState),Object.keys(m.fields).reduce(function(e,r){return e[r]=O(m.fields[r]),e},{}))},P=function(){if(V(),!j){var e=m.lastFormState,r=x();r!==e&&(m.lastFormState=r,t(m.subscribers,r,e,h))}};k();var C={batch:function(e){j=!0,e(),j=!1,N(),P()},blur:function(e){var r=m.fields,t=m.formState,i=r[e];i&&i.active&&(delete t.active,r[e]=u({},i,{active:!1,touched:!0}),d?k(function(){N(),P()}):(N(),P()))},change:function(e,r){var t=m.fields;t[e]&&t[e].value!==r&&(t[e].value=r,m.formState.values=c(m.formState.values,e,r)||{},d?(N(),P()):k(function(){N(),P()}))},focus:function(e){var r=m.fields[e];r&&!r.active&&(m.formState.active=e,r.active=!0,r.visited=!0,N(),P())},getState:function(){return E(m.formState)},initialize:function(e){var r=m.fields,t=m.formState;t.initialValues=e,t.values=e,Object.keys(r).forEach(function(t){var i=r[t],o=n(e,t);i.value=o,i.initial=o,i.touched=!1,i.visited=!1}),k(function(){N(),P()})},registerField:function(e,t){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},o=arguments[3];m.fieldSubscribers[e]||(m.fieldSubscribers[e]={index:0,entries:{}});var a=m.fieldSubscribers[e].index++;if(m.fieldSubscribers[e].entries[a]={subscriber:S(t),subscription:i},!m.fields[e]){var u=m.formState.initialValues?n(m.formState.initialValues,e):void 0;m.fields[e]={active:!1,blur:function(){return C.blur(e)},change:function(r){return C.change(e,r)},focus:function(){return C.focus(e)},initial:u,lastFieldState:void 0,name:e,touched:!1,value:u,validators:{},visited:!1}}o&&(m.fields[e].validators[a]=o);var s=!1,l=function(){var n=f(m.formState,m.fields[e]);r(t,i,n,void 0,b,!0),m.fields[e].lastFieldState=n,s=!0};return k(function(){P(),s||l(),N()}),function(){delete m.fields[e].validators[a],delete m.fieldSubscribers[e].entries[a],Object.keys(m.fieldSubscribers[e].entries).length||(delete m.fieldSubscribers[e],delete m.fields[e]),k(function(){N(),P()})}},reset:function(){C.initialize(m.formState.initialValues||{})},submit:function(){var e=m.formState,r=m.fields;if(A())return Object.keys(r).forEach(function(e){r[e].touched=!0}),void N();var t=void 0,i=!1,o=function(o){e.submitting=!1,o&&(Object.keys(o).length||Object.getOwnPropertySymbols(o).length)?(e.submitFailed=!0,e.submitSucceeded=!1,e.submitErrors=o,Object.keys(r).forEach(function(e){r[e].submitError=o&&n(o,e)}),e.submitError=o[p]):(Object.keys(r).forEach(function(e){delete r[e].submitError}),delete e.submitErrors,delete e.submitError,e.submitFailed=!1,e.submitSucceeded=!0),P(),N(),i=!0,t&&t()};if(e.submitting=!0,e.submitFailed=!1,e.submitSucceeded=!1,2===a.length){if(a(e.values,o),!i)return P(),new Promise(function(e){t=e})}else{var u=a(e.values);if(u&&g(u))return P(),u.then(o);o(u)}},subscribe:function(e,t){if(!e)throw new Error("No callback given.");if(!t)throw new Error("No subscription provided. What values do you want to listen to?");var i=S(e),n=m.subscribers,o=m.lastFormState,a=n.index++;n.entries[a]={subscriber:i,subscription:t};var u=x();return u!==o&&(m.lastFormState=u),r(i,t,u,u,h,!0),function(){delete n.entries[a]}}};return C},e.FORM_ERROR=p,e.version="1.0.0",e.formSubscriptionItems=m,e.fieldSubscriptionItems=d,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=final-form.umd.min.js.map |
{ | ||
"name": "final-form", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": | ||
@@ -5,0 +5,0 @@ "🏁 Framework agnostic, high performance, subscription-based form state management", |
179
README.md
@@ -5,2 +5,8 @@ # 🏁 Final Form | ||
[![NPM Version](https://img.shields.io/npm/v/final-form.svg?style=flat)](https://www.npmjs.com/package/final-form) | ||
[![NPM Downloads](https://img.shields.io/npm/dm/final-form.svg?style=flat)](https://www.npmjs.com/package/final-form) | ||
[![Build Status](https://img.shields.io/travis/erikras/final-form/v6.svg?style=flat)](https://travis-ci.org/erikras/final-form) | ||
[![codecov.io](https://codecov.io/gh/erikras/final-form/branch/master/graph/badge.svg)](https://codecov.io/gh/erikras/final-form) | ||
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier) | ||
✅ **Zero** dependencies | ||
@@ -85,4 +91,6 @@ | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
* [Examples](#examples) | ||
* [Simple React Example](#simple-react-example) | ||
* [Libraries](#libraries) | ||
* [🏁 React Final Form](#-react-final-form) | ||
* [API](#api) | ||
@@ -93,9 +101,11 @@ * [`createForm: (config: Config) => FormApi`](#createform-config-config--formapi) | ||
* [`FORM_ERROR: Symbol`](#form_error-symbol) | ||
* [`version: string`](#version-string) | ||
* [Types](#types) | ||
* [`Config`](#config) | ||
* [`debug?: DebugFunction`](#debug-debugfunction) | ||
* [`initialValues?: Object`](#initialvalues-object) | ||
* [`onSubmit: (values: Object, callback: ?(errors: ?Object) => void) => ?Object | Promise<?Object>`](#onsubmit-values-object-callback-errors-object--void--object--promiseobject) | ||
* [`validate?: (values: Object) => void) => Object | Promise<Object>`](#validate-values-object--void--object--promiseobject) | ||
* [`debug?: (state: FormState, fieldStates: { [string]: FieldState }) => void`](#debug-state-formstate-fieldstates--string-fieldstate---void) | ||
* [`onSubmit: (values: Object, callback: ?(errors: ?Object) => void) => ?Object | Promise<?Object> | void`](#onsubmit-values-object-callback-errors-object--void--object--promiseobject--void) | ||
* [`validate?: (values: Object) => Object | Promise<Object>`](#validate-values-object--object--promiseobject) | ||
* [`validateOnBlur?: boolean`](#validateonblur-boolean) | ||
* [`DebugFunction: (state: FormState, fieldStates: { [string]: FieldState }) => void`](#debugfunction-state-formstate-fieldstates--string-fieldstate---void) | ||
* [`FieldState`](#fieldstate) | ||
@@ -141,3 +151,3 @@ * [`active?: boolean`](#active-boolean) | ||
* [`subscribe: (subscriber: FormSubscriber, subscription: FormSubscription) => Unsubscribe`](#subscribe-subscriber-formsubscriber-subscription-formsubscription--unsubscribe) | ||
* [`registerField: (name: string, subscriber: FieldSubscriber, subscription: FieldSubscription, validate?: (value: ?any, allValues: Object) => any | Promise<any>) => Unsubscribe`](#registerfield-name-string-subscriber-fieldsubscriber-subscription-fieldsubscription-validate-value-any-allvalues-object--any--promiseany--unsubscribe) | ||
* [`registerField: RegisterField`](#registerfield-registerfield) | ||
* [`reset: () => void`](#reset---void) | ||
@@ -148,9 +158,11 @@ * [`FormState`](#formstate) | ||
* [`error?: any`](#error-any-1) | ||
* [`errors?: Object`](#errors-object) | ||
* [`initialValues?: Object`](#initialvalues-object-1) | ||
* [`invalid?: boolean`](#invalid-boolean-2) | ||
* [`initialValues?: Object`](#initialvalues-object-1) | ||
* [`pristine?: boolean`](#pristine-boolean-2) | ||
* [`submitting?: boolean`](#submitting-boolean-1) | ||
* [`submitError?: any`](#submiterror-any-1) | ||
* [`submitErrors?: Object`](#submiterrors-object) | ||
* [`submitFailed?: boolean`](#submitfailed-boolean-2) | ||
* [`submitSucceeded?: boolean`](#submitsucceeded-boolean-2) | ||
* [`submitError?: any`](#submiterror-any-1) | ||
* [`submitting?: boolean`](#submitting-boolean-1) | ||
* [`valid?: boolean`](#valid-boolean-2) | ||
@@ -161,20 +173,40 @@ * [`validating?: boolean`](#validating-boolean-1) | ||
* [`FormSubscription: { [string]: boolean }`](#formsubscription--string-boolean-) | ||
* [`active`](#active) | ||
* [`dirty`](#dirty) | ||
* [`error`](#error) | ||
* [`initialValues`](#initialvalues) | ||
* [`invalid`](#invalid) | ||
* [`pristine`](#pristine) | ||
* [`submitting`](#submitting) | ||
* [`submitFailed`](#submitfailed) | ||
* [`submitSucceeded`](#submitsucceeded) | ||
* [`valid`](#valid) | ||
* [`validating`](#validating) | ||
* [`values`](#values) | ||
* [`active?: boolean`](#active-boolean-2) | ||
* [`dirty?: boolean`](#dirty-boolean-3) | ||
* [`error?: boolean`](#error-boolean-1) | ||
* [`errors?: boolean`](#errors-boolean) | ||
* [`initialValues?: boolean`](#initialvalues-boolean-1) | ||
* [`invalid?: boolean`](#invalid-boolean-3) | ||
* [`pristine?: boolean`](#pristine-boolean-3) | ||
* [`submitError?: boolean`](#submiterror-boolean) | ||
* [`submitErrors?: boolean`](#submiterrors-boolean) | ||
* [`submitFailed?: boolean`](#submitfailed-boolean-3) | ||
* [`submitSucceeded?: boolean`](#submitsucceeded-boolean-3) | ||
* [`submitting?: boolean`](#submitting-boolean-2) | ||
* [`valid?: boolean`](#valid-boolean-3) | ||
* [`validating?: boolean`](#validating-boolean-2) | ||
* [`values?: boolean`](#values-boolean-1) | ||
* [`RegisterField: (name: string, subscriber: FieldSubscriber, subscription: FieldSubscription, validate?: (value: ?any, allValues: Object) => ?any) => Unsubscribe`](#registerfield-name-string-subscriber-fieldsubscriber-subscription-fieldsubscription-validate-value-any-allvalues-object--any--unsubscribe) | ||
* [`Unsubscribe : () => void`](#unsubscribe----void) | ||
* [Libraries](#libraries) | ||
* [🏁 React Final Form](#-react-final-form) | ||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
## Examples | ||
### [Simple React Example](https://codesandbox.io/s/q78r2oqq96) | ||
Demonstrates how 🏁 Final Form can be used inside a React component to manage | ||
form state. It also shows just how much | ||
[🏁 React Final Form](https://github.com/erikras/react-final-form#-react-final-form) | ||
does for you out of the box. | ||
For more examples using React, see | ||
[🏁 React Final Form Examples](https://github.com/erikras/react-final-form#examples). | ||
## Libraries | ||
### [🏁 React Final Form](https://github.com/erikras/react-final-form#-react-final-form) | ||
A form state management system for React that uses 🏁 Final Form under the hood. | ||
## API | ||
@@ -204,2 +236,6 @@ | ||
### `version: string` | ||
The current used version of 🏁 Final Form. | ||
--- | ||
@@ -211,8 +247,10 @@ | ||
#### `debug?: DebugFunction` | ||
#### `initialValues?: Object` | ||
The initial values of your form. These will be used to compare against the | ||
The initial values of your form. These will also be used to compare against the | ||
current values to calculate `pristine` and `dirty`. | ||
#### `onSubmit: (values: Object, callback: ?(errors: ?Object) => void) => ?Object | Promise<?Object>` | ||
#### `onSubmit: (values: Object, callback: ?(errors: ?Object) => void) => ?Object | Promise<?Object> | void` | ||
@@ -235,3 +273,3 @@ Function to call when the form is submitted. There are three possible ways to | ||
#### `validate?: (values: Object) => void) => Object | Promise<Object>` | ||
#### `validate?: (values: Object) => Object | Promise<Object>` | ||
@@ -253,4 +291,2 @@ A whole-record validation function that takes all the values of the form and | ||
#### `debug?: (state: FormState, fieldStates: { [string]: FieldState }) => void` | ||
An optional callback for debugging that returns the form state and the states of | ||
@@ -265,2 +301,4 @@ all the fields. It's called _on every state change_. A typical thing to pass in | ||
### `DebugFunction: (state: FormState, fieldStates: { [string]: FieldState }) => void` | ||
### `FieldState` | ||
@@ -452,3 +490,3 @@ | ||
#### `registerField: (name: string, subscriber: FieldSubscriber, subscription: FieldSubscription, validate?: (value: ?any, allValues: Object) => any | Promise<any>) => Unsubscribe` | ||
#### `registerField: RegisterField` | ||
@@ -485,6 +523,6 @@ Registers a new field and subscribes to changes to it. **The `subscriber` will | ||
#### `invalid?: boolean` | ||
#### `errors?: Object` | ||
`true` if any of the fields or the form has a validation or submission error. | ||
`false` otherwise. | ||
An object containing all the current validation errors. The shape will match the | ||
shape of the form's values. | ||
@@ -496,2 +534,8 @@ #### `initialValues?: Object` | ||
#### `invalid?: boolean` | ||
`true` if any of the fields or the form has a validation or submission error. | ||
`false` otherwise. Note that a form can be invalid even if the errors do not | ||
belong to any currently registered fields. | ||
#### `pristine?: boolean` | ||
@@ -502,7 +546,12 @@ | ||
#### `submitting?: boolean` | ||
#### `submitError?: any` | ||
`true` if the form is currently being submitted asynchronously. `false` | ||
otherwise. | ||
The whole-form submission error returned by `onSubmit` under the `FORM_ERROR` | ||
key. | ||
#### `submitErrors?: Object` | ||
An object containing all the current submission errors. The shape will match the | ||
shape of the form's values. | ||
#### `submitFailed?: boolean` | ||
@@ -517,6 +566,6 @@ | ||
#### `submitError?: any` | ||
#### `submitting?: boolean` | ||
The whole-form submission error returned by `onSubmit` under the `FORM_ERROR` | ||
key. | ||
`true` if the form is currently being submitted asynchronously. `false` | ||
otherwise. | ||
@@ -526,3 +575,4 @@ #### `valid?: boolean` | ||
`true` if neither the form nor any of its fields has a validation or submission | ||
error. `false` otherwise. | ||
error. `false` otherwise. Note that a form can be invalid even if the errors do | ||
not belong to any currently registered fields. | ||
@@ -544,3 +594,3 @@ #### `validating?: boolean` | ||
#### `active` | ||
#### `active?: boolean` | ||
@@ -550,3 +600,3 @@ When `true` the `FormSubscriber` will be notified of changes to the `active` | ||
#### `dirty` | ||
#### `dirty?: boolean` | ||
@@ -556,3 +606,3 @@ When `true` the `FormSubscriber` will be notified of changes to the `dirty` | ||
#### `error` | ||
#### `error?: boolean` | ||
@@ -562,8 +612,13 @@ When `true` the `FormSubscriber` will be notified of changes to the `error` | ||
#### `initialValues` | ||
#### `errors?: boolean` | ||
When `true` the `FormSubscriber` will be notified of changes to the `errors` | ||
value in `FormState`. | ||
#### `initialValues?: boolean` | ||
When `true` the `FormSubscriber` will be notified of changes to the | ||
`initialValues` value in `FormState`. | ||
#### `invalid` | ||
#### `invalid?: boolean` | ||
@@ -573,3 +628,3 @@ When `true` the `FormSubscriber` will be notified of changes to the `invalid` | ||
#### `pristine` | ||
#### `pristine?: boolean` | ||
@@ -579,13 +634,18 @@ When `true` the `FormSubscriber` will be notified of changes to the `pristine` | ||
#### `submitting` | ||
#### `submitError?: boolean` | ||
When `true` the `FormSubscriber` will be notified of changes to the `submitting` | ||
value in `FormState`. | ||
When `true` the `FormSubscriber` will be notified of changes to the | ||
`submitError` value in `FormState`. | ||
#### `submitFailed` | ||
#### `submitErrors?: boolean` | ||
When `true` the `FormSubscriber` will be notified of changes to the | ||
`submitErrors` value in `FormState`. | ||
#### `submitFailed?: boolean` | ||
When `true` the `FormSubscriber` will be notified of changes to the | ||
`submitFailed` value in `FormState`. | ||
#### `submitSucceeded` | ||
#### `submitSucceeded?: boolean` | ||
@@ -595,8 +655,13 @@ When `true` the `FormSubscriber` will be notified of changes to the | ||
#### `valid` | ||
#### `submitting?: boolean` | ||
When `true` the `FormSubscriber` will be notified of changes to the `submitting` | ||
value in `FormState`. | ||
#### `valid?: boolean` | ||
When `true` the `FormSubscriber` will be notified of changes to the `valid` | ||
value in `FormState`. | ||
#### `validating` | ||
#### `validating?: boolean` | ||
@@ -606,3 +671,3 @@ When `true` the `FormSubscriber` will be notified of changes to the `validating` | ||
#### `values` | ||
#### `values?: boolean` | ||
@@ -612,10 +677,6 @@ When `true` the `FormSubscriber` will be notified of changes to the `values` | ||
### `RegisterField: (name: string, subscriber: FieldSubscriber, subscription: FieldSubscription, validate?: (value: ?any, allValues: Object) => ?any) => Unsubscribe` | ||
### `Unsubscribe : () => void` | ||
Unsubscribes a listener. | ||
## Libraries | ||
### [🏁 React Final Form](https://github.com/erikras/react-final-form#-react-final-form) | ||
A form state management system for React that uses 🏁 Final Form under the hood. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11
657
117535
2368