@domonda/form
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -6,2 +6,20 @@ # Change Log | ||
# [2.1.0](https://github.com/domonda/domonda-js/compare/@domonda/form@2.0.1...@domonda/form@2.1.0) (2019-08-29) | ||
### Bug Fixes | ||
* **FormField:** set internal values only on change ([2ba8c16](https://github.com/domonda/domonda-js/commit/2ba8c16)) | ||
### Features | ||
* **equality:** use `fast-equals` ([e86cb8d](https://github.com/domonda/domonda-js/commit/e86cb8d)) | ||
* **FormTag:** introduce and use plumb tags ([640672a](https://github.com/domonda/domonda-js/commit/640672a)) | ||
* **submit:** autosubmit even if currently submitting ([1088a2e](https://github.com/domonda/domonda-js/commit/1088a2e)) | ||
## [2.0.1](https://github.com/domonda/domonda-js/compare/@domonda/form@2.0.0...@domonda/form@2.0.1) (2019-08-02) | ||
@@ -8,0 +26,0 @@ |
@@ -8,6 +8,7 @@ "use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
@@ -18,8 +19,9 @@ }); | ||
const plumb_1 = require("@domonda/plumb"); | ||
const fast_equals_1 = require("fast-equals"); | ||
// form | ||
const Form_1 = require("./Form"); | ||
const createFormField_1 = require("./createFormField"); | ||
const FormTag_1 = require("./FormTag"); | ||
const DEFAULT_AUTO_SUBMIT_DELAY = 300; | ||
function createForm(defaultValues = {}, initialConfig = {}) { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
const configRef = new Form_1.FormConfigRef(initialConfig, handleSubmit); | ||
@@ -38,9 +40,9 @@ const plumb = plumb_1.createPlumb({ | ||
let currTimeout; | ||
plumb.subscribe((nextState) => { | ||
plumb.subscribe((nextState, tag) => { | ||
(() => { | ||
if (nextState.submitting) { | ||
if (tag !== FormTag_1.FormTag.FIELD_VALUE_CHANGE && tag !== FormTag_1.FormTag.VALUES_CHANGE) { | ||
return; | ||
} | ||
if (plumb_1.equal(currState.values, nextState.values) || | ||
plumb_1.equal(nextState.defaultValues, nextState.values)) { | ||
if (fast_equals_1.deepEqual(currState.values, nextState.values) || | ||
fast_equals_1.deepEqual(nextState.defaultValues, nextState.values)) { | ||
return; | ||
@@ -53,3 +55,2 @@ } | ||
currTimeout = setTimeout(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
submit(); | ||
@@ -59,3 +60,2 @@ }, autoSubmitDelay); | ||
else { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
submit(); | ||
@@ -78,9 +78,8 @@ } | ||
configRef, | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
submit, | ||
reset: () => { | ||
plumb.next(Object.assign({}, plumb.state, { values: plumb.state.defaultValues, submitting: false, submitError: null })); | ||
plumb.next(Object.assign(Object.assign({}, plumb.state), { values: plumb.state.defaultValues, submitting: false, submitError: null }), FormTag_1.FormTag.VALUES_RESET); | ||
}, | ||
resetSubmitError: () => { | ||
plumb.next(Object.assign({}, plumb.state, { submitting: false, submitError: null })); | ||
plumb.next(Object.assign(Object.assign({}, plumb.state), { submitting: false, submitError: null }), FormTag_1.FormTag.SUBMIT_ERROR_RESET); | ||
}, | ||
@@ -96,3 +95,3 @@ makeFormField: (path, config) => createFormField_1.createFormField(plumb, path, config), | ||
} | ||
plumb.next(Object.assign({}, plumb.state, { submitting: true, submitError: null })); | ||
plumb.next(Object.assign(Object.assign({}, plumb.state), { submitting: true, submitError: null }), FormTag_1.FormTag.SUBMIT); | ||
const { fields } = plumb.state; | ||
@@ -107,3 +106,3 @@ const validityMessages = Object.keys(fields).reduce((acc, key) => { | ||
if (validityMessages.some((validityMessages) => validityMessages != null)) { | ||
plumb.next(Object.assign({}, plumb.state, { submitting: false })); | ||
plumb.next(Object.assign(Object.assign({}, plumb.state), { submitting: false }), FormTag_1.FormTag.SUBMIT); | ||
return; | ||
@@ -115,3 +114,3 @@ } | ||
if (!plumb.disposed) { | ||
plumb.next(Object.assign({}, plumb.state, { submitting: false, values: resetOnSuccessfulSubmit ? plumb.state.defaultValues : plumb.state.values })); | ||
plumb.next(Object.assign(Object.assign({}, plumb.state), { submitting: false, values: resetOnSuccessfulSubmit ? plumb.state.defaultValues : plumb.state.values }), FormTag_1.FormTag.SUBMIT); | ||
} | ||
@@ -121,3 +120,3 @@ } | ||
if (!plumb.disposed) { | ||
plumb.next(Object.assign({}, plumb.state, { submitting: false, submitError: error, values: resetOnFailedSubmit ? plumb.state.defaultValues : plumb.state.values })); | ||
plumb.next(Object.assign(Object.assign({}, plumb.state), { submitting: false, submitError: error, values: resetOnFailedSubmit ? plumb.state.defaultValues : plumb.state.values }), FormTag_1.FormTag.SUBMIT); | ||
} | ||
@@ -124,0 +123,0 @@ } |
@@ -9,3 +9,4 @@ /** | ||
import { FormField, FormFieldConfig, FormFieldDispose } from './FormField'; | ||
export declare function createFormField<DefaultValues extends FormDefaultValues, Value>(form: Plumb<FormState<DefaultValues>>, path: string, // [K in keyof FormDefaultValues] | ||
import { FormTag } from './FormTag'; | ||
export declare function createFormField<DefaultValues extends FormDefaultValues, Value>(form: Plumb<FormState<DefaultValues>, FormTag>, path: string, // [K in keyof FormDefaultValues] | ||
config?: FormFieldConfig<Value>): [FormField<Value>, FormFieldDispose]; |
@@ -22,3 +22,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const plumb_1 = require("@domonda/plumb"); | ||
const get_1 = __importDefault(require("lodash/get")); | ||
@@ -28,2 +27,4 @@ const setWith_1 = __importDefault(require("lodash/fp/setWith")); | ||
const omit_1 = __importDefault(require("lodash/fp/omit")); | ||
const fast_equals_1 = require("fast-equals"); | ||
const FormTag_1 = require("./FormTag"); | ||
function selector(path, state) { | ||
@@ -42,3 +43,3 @@ const { fields, defaultValues, values } = state; | ||
} | ||
return Object.assign({}, field, { defaultValue, | ||
return Object.assign(Object.assign({}, field), { defaultValue, | ||
value }); | ||
@@ -55,3 +56,3 @@ } | ||
transformer: (selectedState) => { | ||
const changed = !plumb_1.shallowEqual(selectedState.defaultValue, selectedState.value); | ||
const changed = !fast_equals_1.shallowEqual(selectedState.defaultValue, selectedState.value); | ||
let validityMessage = selectedState.validityMessage; | ||
@@ -62,3 +63,3 @@ if (validate && (changed || (immediateValidate && initialTransform))) { | ||
initialTransform = false; | ||
return Object.assign({}, selectedState, { changed, | ||
return Object.assign(Object.assign({}, selectedState), { changed, | ||
validityMessage }); | ||
@@ -68,3 +69,3 @@ }, | ||
var { changed, validityMessage } = _a, rest = __rest(_a, ["changed", "validityMessage"]); | ||
return (Object.assign({}, state, { values: setWith_1.default(clone_1.default, path, rest.value, form.state.values), fields: Object.assign({}, form.state.fields, { [path]: { | ||
return (Object.assign(Object.assign({}, state), { values: setWith_1.default(clone_1.default, path, rest.value, form.state.values), fields: Object.assign(Object.assign({}, form.state.fields), { [path]: { | ||
validityMessage, | ||
@@ -75,4 +76,4 @@ changed, | ||
filter: (selectedState) => { | ||
const changed = !plumb_1.shallowEqual(value, selectedState.value) || | ||
!plumb_1.shallowEqual(defaultValue, selectedState.defaultValue); | ||
const changed = !fast_equals_1.shallowEqual(value, selectedState.value) || | ||
!fast_equals_1.shallowEqual(defaultValue, selectedState.defaultValue); | ||
defaultValue = selectedState.defaultValue; | ||
@@ -82,6 +83,6 @@ value = selectedState.value; | ||
}, | ||
}); | ||
}, FormTag_1.FormTag.CREATE_FIELD); | ||
plumb.subscribe({ | ||
dispose: () => { | ||
form.next(Object.assign({}, form.state, { fields: omit_1.default(path, form.state.fields) })); | ||
form.next(Object.assign(Object.assign({}, form.state), { fields: omit_1.default(path, form.state.fields) }), FormTag_1.FormTag.DISPOSE_FIELD); | ||
}, | ||
@@ -99,12 +100,12 @@ }); | ||
setValue: (nextValue) => { | ||
value = nextValue; | ||
if (!plumb_1.shallowEqual(plumb.state.value, value)) { | ||
plumb.next(Object.assign({}, plumb.state, { value: nextValue })); | ||
if (!fast_equals_1.shallowEqual(plumb.state.value, nextValue)) { | ||
value = nextValue; | ||
plumb.next(Object.assign(Object.assign({}, plumb.state), { value: nextValue }), FormTag_1.FormTag.FIELD_VALUE_CHANGE); | ||
} | ||
}, | ||
resetValue: () => { | ||
defaultValue = plumb.state.defaultValue; | ||
value = plumb.state.value; | ||
if (!plumb_1.shallowEqual(value, defaultValue)) { | ||
plumb.next(Object.assign({}, plumb.state, { value: plumb.state.defaultValue })); | ||
if (!fast_equals_1.shallowEqual(plumb.state.defaultValue, plumb.state.value)) { | ||
defaultValue = plumb.state.defaultValue; | ||
value = plumb.state.value; | ||
plumb.next(Object.assign(Object.assign({}, plumb.state), { value: plumb.state.defaultValue }), FormTag_1.FormTag.FIELD_VALUE_RESET); | ||
} | ||
@@ -111,0 +112,0 @@ }, |
@@ -8,2 +8,3 @@ /** | ||
import { Plumb } from '@domonda/plumb'; | ||
import { FormTag } from './FormTag'; | ||
export declare class FormConfigRef<DefaultValues extends FormDefaultValues> { | ||
@@ -57,3 +58,3 @@ private submitHandler; | ||
export interface Form<T extends FormDefaultValues> { | ||
readonly plumb: Plumb<FormState<T>>; | ||
readonly plumb: Plumb<FormState<T>, FormTag>; | ||
readonly state: FormState<T>; | ||
@@ -60,0 +61,0 @@ readonly values: T; |
@@ -8,2 +8,3 @@ /** | ||
import { Plumb } from '@domonda/plumb'; | ||
import { FormTag } from './FormTag'; | ||
export interface FormFieldStateWithValues<T> extends FormFieldState { | ||
@@ -20,3 +21,3 @@ defaultValue: Readonly<T>; | ||
export interface FormField<T> { | ||
readonly plumb: Plumb<FormFieldStateWithValues<T>>; | ||
readonly plumb: Plumb<FormFieldStateWithValues<T>, FormTag>; | ||
readonly state: FormFieldStateWithValues<T>; | ||
@@ -23,0 +24,0 @@ readonly value: T; |
@@ -5,1 +5,2 @@ export * from './Form'; | ||
export * from './createFormField'; | ||
export * from './FormTag'; |
@@ -9,1 +9,2 @@ "use strict"; | ||
__export(require("./createFormField")); | ||
__export(require("./FormTag")); |
{ | ||
"name": "@domonda/form", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Powerful yet simple form library built using @domonda/plumb.", | ||
@@ -23,3 +23,4 @@ "keywords": [ | ||
"dependencies": { | ||
"@domonda/plumb": "^2.0.1", | ||
"@domonda/plumb": "^2.1.0", | ||
"fast-equals": "^2.0.0", | ||
"lodash.clone": "^4.5.0", | ||
@@ -26,0 +27,0 @@ "lodash.get": "^4.4.2", |
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
22670
15
435
6
+ Addedfast-equals@^2.0.0
+ Addedfast-equals@2.0.4(transitive)
Updated@domonda/plumb@^2.1.0