diff --git a/DOCS.md b/DOCS.md index 889b778..d7c5891 100644 --- a/DOCS.md +++ b/DOCS.md @@ -21,6 +21,8 @@

Toggles noValidate on Form

changeFormValue(formToChange, fieldName, value)Object

Changes FormField value.

+
changeFormValues(formToChange)Object
+

Changes few FormField values at once.

formHasChanged(formToCheck)boolean

Indicated if any value in form is different than initial value.

formFieldHasChanged(formField)boolean
@@ -156,6 +158,18 @@ | fieldName | string |

Key of FormField in form.

| | value | GenericTypeOfFormField.<U> |

Value to change

| + + +## changeFormValues(formToChange) ⇒ Object +

Changes few FormField values at once.

+ +**Kind**: global function + +| Param | Type | Description | +| --- | --- | --- | +| formToChange | string |

Form.

| +| fieldsToChange. | Object | | + ## formHasChanged(formToCheck) ⇒ boolean diff --git a/lib/form-utils.ts b/lib/form-utils.ts index 7f05c22..f8fd672 100644 --- a/lib/form-utils.ts +++ b/lib/form-utils.ts @@ -174,6 +174,29 @@ export function changeFormValue( }, formToChange) } +/** + * Changes few FormField values at once. + * @param {string} formToChange - Form. + * @param {Object} fieldsToChange. + * @returns {Object} + */ +export function changeFormValues( + formToChange: T, + fieldsToChange: U, +): T { + return Object.keys(fieldsToChange).reduce((prev: T, key) => { + const field = formToChange[key] + if (typeof field !== 'object') { + return prev + } + + return isFormField(field) + ? changeFormValue(prev, key, fieldsToChange[key]) + : // tslint:disable-next-line prefer-object-spread + Object.assign({}, prev, { [key]: changeFormValues(field, fieldsToChange[key]) }) + }, formToChange) +} + /** * Indicated if any value in form is different than initial value. * @param {Object} formToCheck - Form. diff --git a/package-lock.json b/package-lock.json index da51895..b13b700 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@currency-one/form-utils", - "version": "1.0.13", + "version": "1.0.14", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@currency-one/form-utils", - "version": "1.0.13", + "version": "1.0.14", "license": "MIT", "devDependencies": { "@babel/cli": "^7.12.10", diff --git a/package.json b/package.json index f1282cc..671b117 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@currency-one/form-utils", - "version": "1.0.13", + "version": "1.0.14", "description": "Form utils", "keywords": [ "form-utils", diff --git a/test/form-utils.spec.ts b/test/form-utils.spec.ts index 908875e..c279a40 100644 --- a/test/form-utils.spec.ts +++ b/test/form-utils.spec.ts @@ -1,6 +1,6 @@ import { changeFormFieldsValidation, - changeFormValue, + changeFormValue, changeFormValues, createFormField, createPasswordFormField, formFieldsToValues, isForm, @@ -119,6 +119,35 @@ describe('form-utils', () => { expect(formUpdated.field5.touched).toBeTruthy() }) + it('should changeFormValues() change many values in form', () => { + const form = { + field1: createFormField('PLN', []), + field2: createFormField('', []), + field3: 'string', + field4: { + subfield1: createFormField('', []), + subfield2: createFormField('text', []), + }, + field5: createFormField(undefined, []), + } + + expect(form.field1.touched).toBeFalsy() + + const formUpdated = changeFormValues(form, { + field1: 'EUR', + field4: { + subfield1: 'changedText', + }, + field5: true, + }) + expect(formUpdated.field1.val).toEqual('EUR') + expect(formUpdated.field1.touched).toBeTruthy() + expect(formUpdated.field4.subfield1.val).toEqual('changedText') + expect(formUpdated.field4.subfield1.touched).toBeTruthy() + expect(formUpdated.field5.val).toBeTruthy() + expect(formUpdated.field5.touched).toBeTruthy() + }) + it('should isFormFieldValueUpdated() return correct value', () => { const formField1 = createFormField('') const formField2 = createFormField('test')