@domonda/form
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -6,2 +6,18 @@ # Change Log | ||
# [1.2.0](https://github.com/domonda/domonda-js/compare/@domonda/form@1.1.1...@domonda/form@1.2.0) (2019-07-19) | ||
### Bug Fixes | ||
* **Form:** update fields on reset after submit ([94a6403](https://github.com/domonda/domonda-js/commit/94a6403)) | ||
### Features | ||
* **FormField:** guarantee changed flag consistency ([deaa987](https://github.com/domonda/domonda-js/commit/deaa987)) | ||
## [1.1.1](https://github.com/domonda/domonda-js/compare/@domonda/form@1.1.0...@domonda/form@1.1.1) (2019-07-19) | ||
@@ -8,0 +24,0 @@ |
@@ -6,3 +6,4 @@ /** | ||
*/ | ||
import { FormDefaultValues, FormConfig, Form, FormDestroy } from './Form'; | ||
import { FormDefaultValues, FormConfig, Form, FormDestroy, FormFields } from './Form'; | ||
export declare function setChangedOnAllFormFields(fields: FormFields, changed: boolean): FormFields; | ||
export declare function createForm<DefaultValues extends FormDefaultValues>(defaultValues?: DefaultValues, initialConfig?: FormConfig<DefaultValues>): [Form<DefaultValues>, FormDestroy]; |
@@ -24,2 +24,6 @@ "use strict"; | ||
const DEFAULT_AUTO_SUBMIT_DELAY = 300; | ||
function setChangedOnAllFormFields(fields, changed) { | ||
return Object.keys(fields).reduce((acc, curr) => (Object.assign({}, acc, { [curr]: Object.assign({}, fields[curr], { changed }) })), {}); | ||
} | ||
exports.setChangedOnAllFormFields = setChangedOnAllFormFields; | ||
function createForm(defaultValues = {}, initialConfig = {}) { | ||
@@ -58,3 +62,3 @@ // eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
submit, | ||
reset: () => $.next(Object.assign({}, $.value, { values: $.value.defaultValues, submitting: false, submitError: null, fields: Object.keys($.value.fields).reduce((acc, curr) => (Object.assign({}, acc, { [curr]: Object.assign({}, $.value.fields[curr], { changed: false }) })), {}) })), | ||
reset: () => $.next(Object.assign({}, $.value, { values: $.value.defaultValues, submitting: false, submitError: null, fields: setChangedOnAllFormFields($.value.fields, false) })), | ||
resetSubmitError: () => $.next(Object.assign({}, $.value, { submitting: false, submitError: null })), | ||
@@ -91,6 +95,10 @@ makeFormField: (path, config) => createFormField_1.createFormField($, path, config), | ||
yield onSubmit($.value.values, form); | ||
$.next(Object.assign({}, $.value, { values: resetOnSuccessfulSubmit ? $.value.defaultValues : $.value.values, submitting: false })); | ||
$.next(Object.assign({}, $.value, { submitting: false, values: resetOnSuccessfulSubmit ? $.value.defaultValues : $.value.values, fields: resetOnSuccessfulSubmit | ||
? setChangedOnAllFormFields($.value.fields, false) | ||
: $.value.fields })); | ||
} | ||
catch (error) { | ||
$.next(Object.assign({}, $.value, { values: resetOnFailedSubmit ? $.value.defaultValues : $.value.values, submitting: false, submitError: error })); | ||
$.next(Object.assign({}, $.value, { submitting: false, submitError: error, values: resetOnFailedSubmit ? $.value.defaultValues : $.value.values, fields: resetOnFailedSubmit | ||
? setChangedOnAllFormFields($.value.fields, false) | ||
: $.value.fields })); | ||
} | ||
@@ -97,0 +105,0 @@ } |
@@ -31,3 +31,3 @@ "use strict"; | ||
const operators_1 = require("rxjs/operators"); | ||
function deriveState(path, state, isLocalNext) { | ||
function deriveState(path, state, setState, isLocalNext) { | ||
const { fields, defaultValues, values } = state; | ||
@@ -44,4 +44,11 @@ const field = fields[path]; | ||
} | ||
// update the `changed` flag if it is not consistent with the field itself | ||
// this case will happen when the value gets changed externally (from the form) | ||
const changed = !equality_1.equal(defaultValue, value); | ||
if (field.changed !== changed) { | ||
setState(Object.assign({}, state, { fields: Object.assign({}, state.fields, { [path]: Object.assign({}, field, { changed }) }) })); | ||
} | ||
return Object.assign({}, field, { defaultValue, | ||
value, changed: !equality_1.equal(defaultValue, value) }); | ||
value, | ||
changed }); | ||
} | ||
@@ -76,3 +83,3 @@ function createFormField(form$, path, // [K in keyof FormDefaultValues] | ||
// its just a type hack so that we dont assert everywhere else... | ||
operators_1.map((state) => deriveState(path, state, localNext)), | ||
operators_1.map((state) => deriveState(path, state, (nextState) => form$.next(nextState), localNext)), | ||
// complete stream when the field gets removed | ||
@@ -126,3 +133,3 @@ operators_1.takeWhile((state) => !!state), | ||
function getState() { | ||
const state = deriveState(path, form$.value, false); | ||
const state = deriveState(path, form$.value, (nextState) => form$.next(nextState), false); | ||
if (!state) { | ||
@@ -129,0 +136,0 @@ throw new Error('domonda-form: Field state should be available here!'); |
{ | ||
"name": "@domonda/form", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Powerful yet simple form library built on top of RxJS.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
25526
510