@shopify/react-form-state
Advanced tools
Comparing version 2.1.3 to 2.1.4
@@ -16,3 +16,2 @@ 'use strict'; | ||
this.changeHandlers = new Map(); | ||
this.handleChange = ({ | ||
@@ -23,7 +22,5 @@ index, | ||
const hashKey = `${index}:${key}`; | ||
if (this.changeHandlers.has(hashKey)) { | ||
return this.changeHandlers.get(hashKey); | ||
} | ||
const handler = newValue => { | ||
@@ -37,3 +34,4 @@ const { | ||
const existingItem = value[index]; | ||
const newItem = { ...existingItem, | ||
const newItem = { | ||
...existingItem, | ||
[key]: typeof newValue === 'function' ? newValue(value[index][key]) : newValue | ||
@@ -44,3 +42,2 @@ }; | ||
}; | ||
this.changeHandlers.set(hashKey, handler); | ||
@@ -50,3 +47,2 @@ return handler; | ||
} | ||
render() { | ||
@@ -86,5 +82,4 @@ const { | ||
} | ||
} | ||
exports["default"] = List; |
@@ -16,3 +16,2 @@ 'use strict'; | ||
this.changeHandlers = new Map(); | ||
this.handleChange = key => { | ||
@@ -22,3 +21,2 @@ if (this.changeHandlers.has(key)) { | ||
} | ||
const handler = newValue => { | ||
@@ -31,3 +29,4 @@ const { | ||
onChange(value => { | ||
return { ...value, | ||
return { | ||
...value, | ||
[key]: typeof newValue === 'function' ? newValue(value[key]) : newValue | ||
@@ -37,3 +36,2 @@ }; | ||
}; | ||
this.changeHandlers.set(key, handler); | ||
@@ -43,3 +41,2 @@ return handler; | ||
} | ||
render() { | ||
@@ -70,5 +67,4 @@ const { | ||
} | ||
} | ||
exports["default"] = Nested; |
@@ -23,3 +23,2 @@ 'use strict'; | ||
this.fieldsWithHandlers = new WeakMap(); | ||
this.reset = () => { | ||
@@ -30,3 +29,2 @@ return new Promise(resolve => { | ||
}; | ||
this.submit = async event => { | ||
@@ -40,23 +38,17 @@ const { | ||
} = this; | ||
if (!this.mounted) { | ||
return; | ||
} | ||
if (event && event.preventDefault && !event.defaultPrevented) { | ||
event.preventDefault(); | ||
} | ||
if (onSubmit == null) { | ||
return; | ||
} | ||
this.setState({ | ||
submitting: true | ||
}); | ||
if (validateOnSubmit) { | ||
await this.validateForm(); | ||
const clientErrors = this.clientErrors; | ||
if (clientErrors.length > 0) { | ||
@@ -70,9 +62,6 @@ this.setState({ | ||
} | ||
const errors = (await onSubmit(formData)) || []; | ||
if (!this.mounted) { | ||
return; | ||
} | ||
if (errors.length > 0) { | ||
@@ -90,3 +79,2 @@ this.updateRemoteErrors(errors); | ||
}; | ||
this.fieldWithHandlers = (field, fieldPath) => { | ||
@@ -96,4 +84,4 @@ if (this.fieldsWithHandlers.has(field)) { | ||
} | ||
const result = { ...field, | ||
const result = { | ||
...field, | ||
name: String(fieldPath), | ||
@@ -107,3 +95,2 @@ onChange: this.updateField.bind(this, fieldPath), | ||
} | ||
static getDerivedStateFromProps(newProps, oldState) { | ||
@@ -120,10 +107,7 @@ const { | ||
} : null; | ||
switch (onInitialValuesChange) { | ||
case 'ignore': | ||
return updatedExternalErrors; | ||
case 'reset-where-changed': | ||
return reconcileFormState(initialValues, oldState, externalErrors); | ||
case 'reset-all': | ||
@@ -133,19 +117,14 @@ default: | ||
const valuesMatch = isEqual__default["default"](oldInitialValues, initialValues); | ||
if (valuesMatch) { | ||
return updatedExternalErrors; | ||
} | ||
return createFormState(initialValues, externalErrors); | ||
} | ||
} | ||
componentDidMount() { | ||
this.mounted = true; | ||
} | ||
componentWillUnmount() { | ||
this.mounted = false; | ||
} | ||
render() { | ||
@@ -163,3 +142,4 @@ const { | ||
} = this; | ||
return children({ ...formData, | ||
return children({ | ||
...formData, | ||
submit, | ||
@@ -169,5 +149,5 @@ reset, | ||
}); | ||
} // eslint-disable-next-line @shopify/react-prefer-private-members | ||
} | ||
// eslint-disable-next-line @shopify/react-prefer-private-members | ||
validateForm() { | ||
@@ -177,4 +157,5 @@ return new Promise(resolve => { | ||
}); | ||
} // eslint-disable-next-line @shopify/react-prefer-private-members | ||
} | ||
// eslint-disable-next-line @shopify/react-prefer-private-members | ||
@@ -200,7 +181,5 @@ get formData() { | ||
} | ||
get dirty() { | ||
return this.state.dirtyFields.length > 0; | ||
} | ||
get valid() { | ||
@@ -213,3 +192,2 @@ const { | ||
} | ||
get hasClientErrors() { | ||
@@ -224,3 +202,2 @@ const { | ||
} | ||
get clientErrors() { | ||
@@ -234,3 +211,2 @@ const { | ||
} | ||
get fields() { | ||
@@ -243,3 +219,2 @@ const { | ||
} | ||
updateField(fieldPath, value) { | ||
@@ -265,3 +240,4 @@ this.setState(({ | ||
}), | ||
fields: updatedField === field ? fields : { // FieldStates<Fields> is not spreadable due to a TS bug | ||
fields: updatedField === field ? fields : { | ||
// FieldStates<Fields> is not spreadable due to a TS bug | ||
// https://github.com/Microsoft/TypeScript/issues/13557 | ||
@@ -274,3 +250,2 @@ ...fields, | ||
} | ||
getUpdatedDirtyFields({ | ||
@@ -282,3 +257,2 @@ fieldPath, | ||
const dirtyFieldsSet = new Set(dirtyFields); | ||
if (dirty) { | ||
@@ -289,7 +263,5 @@ dirtyFieldsSet.add(fieldPath); | ||
} | ||
const newDirtyFields = Array.from(dirtyFieldsSet); | ||
return dirtyFields.length === newDirtyFields.length ? dirtyFields : newDirtyFields; | ||
} | ||
getUpdatedField({ | ||
@@ -308,8 +280,7 @@ fieldPath, | ||
}); | ||
if (value === field.value && error === field.error) { | ||
return field; | ||
} | ||
return { ...field, | ||
return { | ||
...field, | ||
value, | ||
@@ -320,3 +291,2 @@ dirty, | ||
} | ||
blurField(fieldPath) { | ||
@@ -328,12 +298,12 @@ const { | ||
const error = this.validateFieldValue(fieldPath, field); | ||
if (error == null) { | ||
return; | ||
} | ||
this.setState(state => ({ | ||
fields: { // FieldStates<Fields> is not spreadable due to a TS bug | ||
fields: { | ||
// FieldStates<Fields> is not spreadable due to a TS bug | ||
// https://github.com/Microsoft/TypeScript/issues/13557 | ||
...state.fields, | ||
[fieldPath]: { ...state.fields[fieldPath], | ||
[fieldPath]: { | ||
...state.fields[fieldPath], | ||
error | ||
@@ -344,3 +314,2 @@ } | ||
} | ||
validateFieldValue(fieldPath, { | ||
@@ -353,3 +322,2 @@ value, | ||
} | ||
const { | ||
@@ -363,3 +331,2 @@ validators = {} | ||
} | ||
updateRemoteErrors(errors) { | ||
@@ -374,7 +341,5 @@ this.setState(({ | ||
} | ||
} | ||
FormState.List = List["default"]; | ||
FormState.Nested = Nested["default"]; | ||
function fieldsWithErrors(fields, errors) { | ||
@@ -388,3 +353,2 @@ const errorDictionary = errors.reduce((accumulator, { | ||
} | ||
return utilities.set(accumulator, field, message); | ||
@@ -396,4 +360,4 @@ }, {}); | ||
} | ||
return { ...field, | ||
return { | ||
...field, | ||
error: errorDictionary[path] | ||
@@ -403,3 +367,2 @@ }; | ||
} | ||
function reconcileFormState(values, oldState, externalErrors = []) { | ||
@@ -412,7 +375,5 @@ const { | ||
const oldField = oldFields[key]; | ||
if (isEqual__default["default"](value, oldField.initialValue)) { | ||
return oldField; | ||
} | ||
dirtyFields.delete(key); | ||
@@ -425,3 +386,4 @@ return { | ||
}); | ||
return { ...oldState, | ||
return { | ||
...oldState, | ||
dirtyFields: Array.from(dirtyFields), | ||
@@ -431,3 +393,2 @@ fields: fieldsWithErrors(fields, externalErrors) | ||
} | ||
function createFormState(values, externalErrors = []) { | ||
@@ -449,3 +410,2 @@ const fields = utilities.mapObject(values, value => { | ||
} | ||
function initialValuesFromFields(fields) { | ||
@@ -456,3 +416,2 @@ return utilities.mapObject(fields, ({ | ||
} | ||
function runValidator(validate = () => {}, value, fields) { | ||
@@ -462,16 +421,11 @@ if (typeof validate === 'function') { | ||
} | ||
if (!Array.isArray(validate)) { | ||
return; | ||
} | ||
const errors = validate.map(validator => validator(value, fields)).filter(input => input != null); | ||
if (errors.length === 0) { | ||
return; | ||
} | ||
return errors; | ||
} | ||
function runAllValidators(state, props) { | ||
@@ -484,17 +438,16 @@ const { | ||
} = props; | ||
if (!validators) { | ||
return null; | ||
} | ||
const updatedFields = utilities.mapObject(fields, (field, path) => { | ||
return { ...field, | ||
return { | ||
...field, | ||
error: runValidator(validators[path], field.value, fields) | ||
}; | ||
}); | ||
return { ...state, | ||
return { | ||
...state, | ||
fields: updatedFields | ||
}; | ||
} | ||
function collectErrors(message) { | ||
@@ -504,3 +457,2 @@ if (!message) { | ||
} | ||
if (typeof message === 'string') { | ||
@@ -511,7 +463,5 @@ return [{ | ||
} | ||
if (Array.isArray(message)) { | ||
return utilities.flatMap(message, itemError => collectErrors(itemError)); | ||
} | ||
return utilities.flatMap(Object.values(message), nestedError => collectErrors(nestedError)); | ||
@@ -518,0 +468,0 @@ } |
@@ -28,3 +28,2 @@ 'use strict'; | ||
} | ||
return newValue; | ||
@@ -37,3 +36,4 @@ }); | ||
} else if (path.length === 1) { | ||
return { ...rootObject, | ||
return { | ||
...rootObject, | ||
[path[0]]: value | ||
@@ -43,3 +43,4 @@ }; | ||
const [current, ...rest] = path; | ||
return { ...rootObject, | ||
return { | ||
...rootObject, | ||
[current]: set(rootObject[current] || {}, rest, value) | ||
@@ -52,2 +53,3 @@ }; | ||
} | ||
/** | ||
@@ -57,3 +59,2 @@ * Transforms a boolean FieldDescriptor object to work with checkboxes and radios. | ||
*/ | ||
function asChoiceField({ | ||
@@ -60,0 +61,0 @@ value: checked, |
@@ -12,25 +12,18 @@ 'use strict'; | ||
const validate = validatorDictionary[field]; | ||
if (validate == null) { | ||
return null; | ||
} | ||
if (typeof validate === 'function') { | ||
return validate(value, fields); | ||
} | ||
if (!Array.isArray(validate)) { | ||
return; | ||
} | ||
const errors = validate.map(validator => validator(value, fields)).filter(input => input != null); | ||
if (errors.length === 0) { | ||
return; | ||
} | ||
return errors; | ||
}); | ||
const anyErrors = Object.keys(errors).map(key => errors[key]).some(value => value != null); | ||
if (anyErrors) { | ||
@@ -45,7 +38,5 @@ return errors; | ||
const errors = input.map(item => validateItem(item, fields)); | ||
if (errors.some(error => error != null)) { | ||
return errors; | ||
} | ||
return undefined; | ||
@@ -60,2 +51,3 @@ }; | ||
const matches = matcher(input, fields); | ||
/* | ||
@@ -65,15 +57,11 @@ always mark empty fields valid to match Polaris guidelines | ||
*/ | ||
if (predicates.isEmpty(input)) { | ||
return; | ||
} | ||
if (matches) { | ||
return; | ||
} | ||
if (typeof errorContent === 'function') { | ||
return errorContent(toString(input)); | ||
} | ||
return errorContent; | ||
@@ -85,11 +73,8 @@ }; | ||
const matches = matcher(input, fields); | ||
if (matches) { | ||
return; | ||
} | ||
if (typeof errorContent === 'function') { | ||
return errorContent(toString(input)); | ||
} | ||
return errorContent; | ||
@@ -102,29 +87,21 @@ }; | ||
}, | ||
lengthLessThan(length, errorContent) { | ||
return validate(predicates.lengthLessThan(length), errorContent); | ||
}, | ||
numericString(errorContent) { | ||
return validate(predicates.isNumericString, errorContent); | ||
}, | ||
positiveNumericString(errorContent) { | ||
return validate(predicates.isPositiveNumericString, errorContent); | ||
}, | ||
nonNumericString(errorContent) { | ||
return validate(predicates.notNumericString, errorContent); | ||
}, | ||
requiredString(errorContent) { | ||
return validateRequired(predicates.notEmptyString, errorContent); | ||
}, | ||
required(errorContent) { | ||
return validateRequired(predicates.notEmpty, errorContent); | ||
} | ||
}; | ||
function toString(obj) { | ||
@@ -134,6 +111,4 @@ if (obj == null) { | ||
} | ||
return obj.toString(); | ||
} | ||
var validators$1 = validators; | ||
@@ -140,0 +115,0 @@ |
{ | ||
"name": "@shopify/react-form-state", | ||
"version": "2.1.3", | ||
"version": "2.1.4", | ||
"license": "MIT", | ||
@@ -33,3 +33,3 @@ "description": "Manage React forms tersely and type-safely with no magic", | ||
"devDependencies": { | ||
"@shopify/react-testing": "^5.1.2" | ||
"@shopify/react-testing": "^5.1.5" | ||
}, | ||
@@ -36,0 +36,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
77662
1648