@reactables/forms 0.5.4-alpha.0 → 0.6.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,3 @@
1
- import { Action } from '@reactables/core';
2
1
  import { AbstractControlConfig } from './Configs';
3
2
  import { FormErrors } from './FormErrors';
4
3
  import { ControlRef } from './ControlRef';
@@ -30,7 +29,12 @@ export interface FormControl<T> extends BaseControl<T>, Hub2Fields {
30
29
  }
31
30
  export interface BaseFormState<T> {
32
31
  form: BaseForm<T>;
33
- action: Action<unknown>;
32
+ changedControls?: {
33
+ [key: string]: BaseControl<unknown>;
34
+ };
35
+ removedControls?: {
36
+ [key: string]: BaseControl<unknown>;
37
+ };
34
38
  }
35
39
  export interface BaseForm<T> {
36
40
  root?: BaseControl<T>;
@@ -1,4 +1,4 @@
1
1
  import { Action } from '@reactables/core';
2
2
  import { BaseFormState } from '../../Models/Controls';
3
3
  import { AddControl } from '../../Models/Payloads';
4
- export declare const addControl: <T>(state: BaseFormState<T>, action: Action<AddControl>) => BaseFormState<T>;
4
+ export declare const addControl: <T>(state: BaseFormState<T>, action: Action<AddControl>, mergeChanges?: boolean) => BaseFormState<T>;
@@ -1,4 +1,4 @@
1
1
  import { Action } from '@reactables/core';
2
2
  import { BaseFormState } from '../../Models/Controls';
3
3
  import { ControlRef } from '../../Models/ControlRef';
4
- export declare const markControlAsPristine: <T>({ form }: BaseFormState<T>, action: Action<ControlRef>) => BaseFormState<T>;
4
+ export declare const markControlAsPristine: <T>(state: BaseFormState<T>, action: Action<ControlRef>, mergeChanges?: boolean) => BaseFormState<T>;
@@ -1,4 +1,4 @@
1
1
  import { Action } from '@reactables/core';
2
2
  import { BaseFormState } from '../../Models/Controls';
3
3
  import { MarkTouched } from '../../Models/Payloads';
4
- export declare const markControlAsTouched: <T>({ form }: BaseFormState<T>, action: Action<MarkTouched>) => BaseFormState<T>;
4
+ export declare const markControlAsTouched: <T>(state: BaseFormState<T>, action: Action<MarkTouched>, mergeChanges?: boolean) => BaseFormState<T>;
@@ -1,4 +1,4 @@
1
1
  import { Action } from '@reactables/core';
2
2
  import { BaseFormState } from '../../Models/Controls';
3
3
  import { ControlRef } from '../../Models/ControlRef';
4
- export declare const markControlAsUntouched: <T>({ form }: BaseFormState<T>, action: Action<ControlRef>) => BaseFormState<T>;
4
+ export declare const markControlAsUntouched: <T>(state: BaseFormState<T>, action: Action<ControlRef>, mergeChanges?: boolean) => BaseFormState<T>;
@@ -1,4 +1,4 @@
1
1
  import { Action } from '@reactables/core';
2
2
  import { BaseFormState } from '../../Models/Controls';
3
3
  import { AddControl } from '../../Models/Payloads';
4
- export declare const pushControl: <T>(state: BaseFormState<T>, action: Action<AddControl>) => BaseFormState<T>;
4
+ export declare const pushControl: <T>(state: BaseFormState<T>, action: Action<AddControl>, mergeChanges?: boolean) => BaseFormState<T>;
@@ -1,4 +1,4 @@
1
1
  import { Action } from '@reactables/core';
2
2
  import { BaseFormState } from '../../Models/Controls';
3
3
  import { ControlRef } from '../../Models/ControlRef';
4
- export declare const removeControl: <T>({ form }: BaseFormState<T>, action: Action<ControlRef>) => BaseFormState<T>;
4
+ export declare const removeControl: <T>(state: BaseFormState<T>, action: Action<ControlRef>, mergeChanges?: boolean) => BaseFormState<T>;
@@ -1,4 +1,4 @@
1
1
  import { Action } from '@reactables/core';
2
2
  import { BaseFormState } from '../../Models/Controls';
3
3
  import { ControlRef } from '../../Models/ControlRef';
4
- export declare const resetControl: <T>({ form }: BaseFormState<T>, action: Action<ControlRef>) => BaseFormState<T>;
4
+ export declare const resetControl: <T>(state: BaseFormState<T>, action: Action<ControlRef>, mergeChanges?: boolean) => BaseFormState<T>;
@@ -1,4 +1,4 @@
1
1
  import { Action } from '@reactables/core';
2
2
  import { BaseFormState } from '../../Models/Controls';
3
3
  import { ControlChange } from '../../Models/Payloads';
4
- export declare const updateValues: <T>({ form }: BaseFormState<T>, action: Action<ControlChange<unknown>>) => BaseFormState<T>;
4
+ export declare const updateValues: <T>({ form, changedControls, removedControls }: BaseFormState<T>, action: Action<ControlChange<unknown>>, mergeChanges?: boolean) => BaseFormState<T>;
@@ -1,15 +1,14 @@
1
- import { Form, BaseForm } from '../../Models/Controls';
2
- import { ControlRef } from '../../Models/ControlRef';
1
+ import { Form, FormControl, BaseFormState } from '../../Models/Controls';
3
2
  import { FormErrors } from '../../Models/FormErrors';
4
- export declare const mergePushControl: <T>(state: Form<T>, form: BaseForm<T>, controlRef: ControlRef) => {
5
- [x: string]: import("../../Models/Controls").FormControl<unknown> | {
3
+ export declare const mergeControls: <T>(state: Form<T>, { form, changedControls, removedControls }: BaseFormState<unknown>) => {
4
+ [x: string]: FormControl<unknown> | {
6
5
  errors: {
7
6
  [x: string]: boolean;
8
7
  };
9
8
  valid: boolean;
10
9
  childrenValid: boolean;
11
10
  pristineValue: unknown;
12
- controlRef: ControlRef;
11
+ controlRef: import("../..").ControlRef;
13
12
  value: unknown;
14
13
  dirty: boolean;
15
14
  touched: boolean;
@@ -23,5 +22,5 @@ export declare const mergePushControl: <T>(state: Form<T>, form: BaseForm<T>, co
23
22
  };
24
23
  pending?: boolean;
25
24
  };
26
- root?: import("../../Models/Controls").FormControl<unknown>;
25
+ root?: FormControl<unknown>;
27
26
  };
@@ -1,9 +1,9 @@
1
- import { Reactable, EffectsAndSources } from '@reactables/core';
1
+ import { Reactable, EffectsAndSources, Action } from '@reactables/core';
2
2
  import { ControlChange, AddControl, MarkTouched, PushControl } from '../Models/Payloads';
3
3
  import { ControlRef } from '../Models';
4
4
  import { FormControlConfig, FormArrayConfig, FormGroupConfig, AbstractControlConfig } from '../Models/Configs';
5
5
  import { ValidatorFn, ValidatorAsyncFn } from '../Models/Validators';
6
- import { Form } from '../Models/Controls';
6
+ import { Form, BaseFormState } from '../Models/Controls';
7
7
  type FbControl<T> = [T, (ValidatorFn | ValidatorFn[])?, (ValidatorAsyncFn | ValidatorAsyncFn[])?];
8
8
  export declare const control: <T>(config: FormControlConfig<T> | FbControl<T>) => FormControlConfig<T>;
9
9
  export declare const array: (config: FormArrayConfig) => FormArrayConfig;
@@ -18,5 +18,21 @@ export type RxFormActions = {
18
18
  markControlAsUntouched: (payload: ControlRef) => void;
19
19
  resetControl: (payload: ControlRef) => void;
20
20
  };
21
- export declare const build: (config: AbstractControlConfig, options?: EffectsAndSources) => Reactable<Form<unknown>, RxFormActions>;
21
+ export interface FormReducers {
22
+ updateValues: <T>(state: BaseFormState<T>, action: Action<ControlChange<unknown>>) => BaseFormState<T>;
23
+ removeControl: <T>(state: BaseFormState<T>, action: Action<ControlRef>) => BaseFormState<T>;
24
+ pushControl: <T>(state: BaseFormState<T>, action: Action<PushControl>) => BaseFormState<T>;
25
+ addControl: <T>(state: BaseFormState<T>, action: Action<AddControl>) => BaseFormState<T>;
26
+ markControlAsPristine: <T>(state: BaseFormState<T>, action: Action<ControlRef>) => BaseFormState<T>;
27
+ markControlAsTouched: <T>(state: BaseFormState<T>, action: Action<MarkTouched>) => BaseFormState<T>;
28
+ markControlAsUntouched: <T>(state: BaseFormState<T>, action: Action<ControlRef>) => BaseFormState<T>;
29
+ resetControl: <T>(state: BaseFormState<T>, action: Action<ControlRef>) => BaseFormState<T>;
30
+ }
31
+ export interface CustomReducers {
32
+ [key: string]: (reducers: FormReducers, state: BaseFormState<unknown>, action: Action<unknown>) => BaseFormState<unknown>;
33
+ }
34
+ export interface RxFormOptions<T extends CustomReducers> extends EffectsAndSources {
35
+ reducers?: T;
36
+ }
37
+ export declare const build: <T extends CustomReducers>(config: AbstractControlConfig, options?: RxFormOptions<T>) => Reactable<Form<unknown>, { [K in keyof T]: (payload?: any) => void; } & RxFormActions>;
22
38
  export {};
@@ -1 +1 @@
1
- export { build, group, array, control, RxFormActions } from './RxForm';
1
+ export { build, group, array, control, RxFormActions, FormReducers } from './RxForm';
package/dist/index.js CHANGED
@@ -79,6 +79,18 @@ var __assign = function() {
79
79
  return __assign.apply(this, arguments);
80
80
  };
81
81
 
82
+ function __rest(s, e) {
83
+ var t = {};
84
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
85
+ t[p] = s[p];
86
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
87
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
88
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
89
+ t[p[i]] = s[p[i]];
90
+ }
91
+ return t;
92
+ }
93
+
82
94
  function __spreadArray(to, from, pack) {
83
95
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
84
96
  if (ar || !(i in from)) {
@@ -171,71 +183,10 @@ var buildFormState = function (config, form, controlRef) {
171
183
  if (form === void 0) { form = { root: null }; }
172
184
  if (controlRef === void 0) { controlRef = []; }
173
185
  return {
174
- form: buildState(config, form, controlRef),
175
- action: null
186
+ form: buildState(config, form, controlRef)
176
187
  };
177
188
  };
178
189
 
179
- var getControl = function (controlRef, form) {
180
- return form[getFormKey(controlRef)];
181
- };
182
-
183
- // Includes the original control of interest unless excludeSelf === true
184
- var getAncestorControls = function (controlRef, form, excludeSelf) {
185
- if (excludeSelf === void 0) { excludeSelf = false; }
186
- var formControls = controlRef.reduce(function (acc, key) {
187
- var currentRef = acc.currentRef.concat(key);
188
- var formControls = acc.formControls.concat(getControl(currentRef, form));
189
- return {
190
- currentRef: currentRef,
191
- formControls: formControls
192
- };
193
- }, {
194
- currentRef: [],
195
- formControls: []
196
- }).formControls;
197
- var root = form['root'];
198
- var result = [root].concat(formControls);
199
- return result.filter(function (control) {
200
- return excludeSelf ? getFormKey(control.controlRef) !== getFormKey(controlRef) : true;
201
- });
202
- };
203
-
204
- // Includes the original control of interest unless excludeSelf === true
205
- var getDescendantControls = function (controlRef, form, excludeSelf) {
206
- if (excludeSelf === void 0) { excludeSelf = false; }
207
- if (!controlRef.length) {
208
- return Object.values(form).filter(function (_a) {
209
- var controlRef = _a.controlRef;
210
- return excludeSelf ? controlRef.length !== 0 : true;
211
- });
212
- }
213
- var control = getControl(controlRef, form);
214
- var value = control.value, config = control.config;
215
- var descendants;
216
- if (Array.isArray(config.controls)) {
217
- // If control is a Form Array
218
- descendants = value.reduce(function (acc, item, index) {
219
- return acc.concat(getDescendantControls(controlRef.concat(index), form));
220
- }, []);
221
- }
222
- else if (config.controls) {
223
- // If control is a Form Group
224
- descendants = Object.keys(value).reduce(function (acc, key) {
225
- return acc.concat(getDescendantControls(controlRef.concat(key), form));
226
- }, []);
227
- }
228
- if (excludeSelf)
229
- return descendants;
230
- return [control].concat(descendants || []);
231
- };
232
-
233
- var getControlBranch = function (controlRef, form) {
234
- var ancestors = getAncestorControls(controlRef, form);
235
- var childControls = getDescendantControls(controlRef, form).slice(1);
236
- return ancestors.concat(childControls).sort(function (a, b) { return a.controlRef.length - b.controlRef.length; });
237
- };
238
-
239
190
  var getScopedEffectsForControl = function (formControl) {
240
191
  var config = formControl.config, key = formControl.key;
241
192
  var asyncValidators = config.asyncValidators;
@@ -273,41 +224,18 @@ var getAsyncValidationActions = function (formControls) {
273
224
  var buildHub2Source = function (rx) {
274
225
  var hub1StateMapped$ = rx.state$.pipe(operators.map(function (payload) { return ({ type: 'formChange', payload: payload }); }));
275
226
  var sourceForHub2$ = hub1StateMapped$.pipe(operators.mergeMap(function (formChangeAction) {
276
- var _a = formChangeAction.payload, form = _a.form, action = _a.action;
277
- var newForm = form;
278
- var controlsToCheck;
279
- switch (action === null || action === void 0 ? void 0 : action.type) {
280
- case 'updateValues':
281
- controlsToCheck = getControlBranch(action.payload.controlRef, newForm);
282
- break;
283
- case 'pushControl':
284
- case 'addControl':
285
- var changedControl = getControl(action.payload.controlRef, newForm);
286
- if (Array.isArray(changedControl.config.controls)) {
287
- var index = changedControl.value.length - 1;
288
- var ref = action.payload.controlRef.concat(index);
289
- controlsToCheck = getControlBranch(ref, newForm);
290
- break;
291
- }
292
- else {
293
- controlsToCheck = getControlBranch(action.payload.controlRef, newForm);
294
- break;
295
- }
296
- case 'removeControl':
297
- controlsToCheck = getAncestorControls(action.payload.slice(0, -1), newForm);
298
- break;
299
- case 'resetControl':
300
- controlsToCheck = getControlBranch(action.payload, newForm);
301
- break;
302
- default:
303
- controlsToCheck = [];
304
- }
227
+ var changedControls = formChangeAction.payload.changedControls;
228
+ var controlsToCheck = changedControls ? Object.values(changedControls) : [];
305
229
  var asyncValidationActions = getAsyncValidationActions(controlsToCheck);
306
230
  return rxjs.of.apply(void 0, __spreadArray([formChangeAction], asyncValidationActions, false));
307
231
  }));
308
232
  return sourceForHub2$;
309
233
  };
310
234
 
235
+ var getControl = function (controlRef, form) {
236
+ return form[getFormKey(controlRef)];
237
+ };
238
+
311
239
  var UPDATE_ANCESTOR_VALUES = 'UPDATE_ANCESTOR_VALUES';
312
240
  var updateAncestorValues = function (form, _a) {
313
241
  var _b, _c;
@@ -342,17 +270,71 @@ var isChildRef = function (controlRef, parentRef) {
342
270
  controlRef.length === parentRef.length + 1);
343
271
  };
344
272
 
273
+ // Includes the original control of interest unless excludeSelf === true
274
+ var getDescendantControls = function (controlRef, form, excludeSelf) {
275
+ if (excludeSelf === void 0) { excludeSelf = false; }
276
+ if (!controlRef.length) {
277
+ return Object.values(form).filter(function (_a) {
278
+ var controlRef = _a.controlRef;
279
+ return excludeSelf ? controlRef.length !== 0 : true;
280
+ });
281
+ }
282
+ var control = getControl(controlRef, form);
283
+ var value = control.value, config = control.config;
284
+ var descendants;
285
+ if (Array.isArray(config.controls)) {
286
+ // If control is a Form Array
287
+ descendants = value.reduce(function (acc, item, index) {
288
+ return acc.concat(getDescendantControls(controlRef.concat(index), form));
289
+ }, []);
290
+ }
291
+ else if (config.controls) {
292
+ // If control is a Form Group
293
+ descendants = Object.keys(value).reduce(function (acc, key) {
294
+ return acc.concat(getDescendantControls(controlRef.concat(key), form));
295
+ }, []);
296
+ }
297
+ if (excludeSelf)
298
+ return descendants;
299
+ return [control].concat(descendants || []);
300
+ };
301
+
302
+ // Includes the original control of interest unless excludeSelf === true
303
+ var getAncestorControls = function (controlRef, form, excludeSelf) {
304
+ if (excludeSelf === void 0) { excludeSelf = false; }
305
+ var formControls = controlRef.reduce(function (acc, key) {
306
+ var currentRef = acc.currentRef.concat(key);
307
+ var formControls = acc.formControls.concat(getControl(currentRef, form));
308
+ return {
309
+ currentRef: currentRef,
310
+ formControls: formControls
311
+ };
312
+ }, {
313
+ currentRef: [],
314
+ formControls: []
315
+ }).formControls;
316
+ var root = form['root'];
317
+ var result = [root].concat(formControls);
318
+ return result.filter(function (control) {
319
+ return excludeSelf ? getFormKey(control.controlRef) !== getFormKey(controlRef) : true;
320
+ });
321
+ };
322
+
345
323
  var UPDATE_DESCENDANT_VALUES = 'UPDATE_DESCENDANT_VALUES';
346
- var updateDescendants = function (form, _a) {
324
+ var updateDescendants = function (state, _a) {
347
325
  var _b = _a.payload, controlRef = _b.controlRef, value = _b.value;
348
- var descendants = getDescendantControls(controlRef, form, true).map(function (control) { return [getFormKey(control.controlRef), control]; });
326
+ var descendants = getDescendantControls(controlRef, state.form, true).map(function (control) { return [getFormKey(control.controlRef), control]; });
349
327
  var result = descendants.reduce(function (acc, _a) {
350
- var _b;
328
+ var _b, _c;
351
329
  var key = _a[0], control = _a[1];
352
330
  if (isChildRef(control.controlRef, controlRef)) {
353
331
  var childValue = value[control.controlRef.at(-1)];
354
332
  var validatorErrors = getErrors(control, value);
355
- acc = __assign(__assign({}, acc), (_b = {}, _b[key] = __assign(__assign({}, control), { value: childValue, validatorErrors: validatorErrors, dirty: !isEqual__default["default"](childValue, control.pristineValue) }), _b));
333
+ var newControl = __assign(__assign({}, control), { value: childValue, validatorErrors: validatorErrors, dirty: !isEqual__default["default"](childValue, control.pristineValue) });
334
+ acc = {
335
+ form: __assign(__assign({}, acc.form), (_b = {}, _b[key] = newControl, _b)),
336
+ changedControls: __assign(__assign({}, acc.changedControls), (_c = {}, _c[newControl.key] = newControl, _c))
337
+ };
356
338
  var configControls = control.config.controls;
357
339
  if (configControls) {
358
340
  acc = updateDescendants(acc, {
@@ -362,19 +344,27 @@ var updateDescendants = function (form, _a) {
362
344
  }
363
345
  }
364
346
  return acc;
365
- }, form);
366
- return result;
347
+ }, { form: {}, changedControls: {} });
348
+ return {
349
+ form: __assign(__assign({}, state.form), result.form),
350
+ changedControls: __assign({}, result.changedControls)
351
+ };
367
352
  };
368
353
  // Will only update child controls that are present.
369
354
  // Use AddControl/RemoveControl action reducers to add/remove control
370
- var updateValues = function (_a, action) {
371
- var _b;
372
- var form = _a.form;
373
- var _c = action.payload, controlRef = _c.controlRef, value = _c.value;
355
+ var updateValues = function (_a, action, mergeChanges) {
356
+ var _b, _c;
357
+ var form = _a.form, _d = _a.changedControls, changedControls = _d === void 0 ? {} : _d, _e = _a.removedControls, removedControls = _e === void 0 ? {} : _e;
358
+ if (mergeChanges === void 0) { mergeChanges = false; }
359
+ var _f = action.payload, controlRef = _f.controlRef, value = _f.value;
374
360
  // Update its own value
375
361
  var ctrlKey = getFormKey(controlRef);
376
362
  var validatorErrors = getErrors(form[ctrlKey], value);
377
- var result = __assign(__assign({}, form), (_b = {}, _b[ctrlKey] = __assign(__assign({}, form[ctrlKey]), { validatorErrors: validatorErrors, dirty: !isEqual__default["default"](value, form[ctrlKey].pristineValue), value: value }), _b));
363
+ var newControl = __assign(__assign({}, form[ctrlKey]), { validatorErrors: validatorErrors, dirty: !isEqual__default["default"](value, form[ctrlKey].pristineValue), value: value });
364
+ var result = {
365
+ form: __assign(__assign({}, form), (_b = {}, _b[ctrlKey] = newControl, _b)),
366
+ changedControls: (_c = {}, _c[newControl.key] = newControl, _c)
367
+ };
378
368
  var configControls = form[ctrlKey].config.controls;
379
369
  // Update its children
380
370
  if (configControls) {
@@ -388,12 +378,17 @@ var updateValues = function (_a, action) {
388
378
  }
389
379
  // Update its Ancestors
390
380
  if (controlRef.length) {
391
- result = updateAncestorValues(result, {
392
- type: UPDATE_ANCESTOR_VALUES,
393
- payload: { controlRef: controlRef, value: value }
394
- });
381
+ result = __assign(__assign({}, result), { form: updateAncestorValues(result.form, {
382
+ type: UPDATE_ANCESTOR_VALUES,
383
+ payload: { controlRef: controlRef, value: value }
384
+ }) });
395
385
  }
396
- return { form: result, action: action };
386
+ var changedAncestorControls = getAncestorControls(controlRef, result.form).reduce(function (acc, control) {
387
+ var _a;
388
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
389
+ }, {});
390
+ var mergedResult = __assign(__assign({}, result), { changedControls: __assign(__assign(__assign({}, (mergeChanges ? changedControls : undefined)), changedAncestorControls), result.changedControls), removedControls: mergeChanges ? removedControls : undefined });
391
+ return mergedResult;
397
392
  };
398
393
 
399
394
  var UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL = 'UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL';
@@ -424,18 +419,22 @@ var updateAncestorValuesRemoveControl = function (form, _a) {
424
419
  return form;
425
420
  };
426
421
 
427
- var removeControl = function (_a, action) {
428
- var form = _a.form;
422
+ var removeControl = function (state, action, mergeChanges) {
423
+ var _a;
424
+ if (mergeChanges === void 0) { mergeChanges = false; }
425
+ var form = state.form;
429
426
  var controlRef = action.payload;
430
- if (!getControl(controlRef, form)) {
427
+ var controlToRemove = getControl(controlRef, form);
428
+ if (!controlToRemove) {
431
429
  throw 'Control not found';
432
430
  }
433
431
  // Can't remove the root of the form
434
432
  if (!controlRef.length)
435
- return { form: form, action: action };
433
+ return { form: form };
436
434
  var parentRef = controlRef.slice(0, -1);
437
435
  var parentIsFormArray = Array.isArray(getControl(parentRef, form).config.controls);
438
- var descendantkeys = getDescendantControls(controlRef, form).map(function (_a) {
436
+ var descendants = getDescendantControls(controlRef, form);
437
+ var descendantkeys = descendants.map(function (_a) {
439
438
  var controlRef = _a.controlRef;
440
439
  return getFormKey(controlRef);
441
440
  });
@@ -466,12 +465,50 @@ var removeControl = function (_a, action) {
466
465
  }
467
466
  return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
468
467
  }, {});
468
+ var result = updateAncestorValuesRemoveControl(controlRemoved, {
469
+ type: UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL,
470
+ payload: controlRef
471
+ });
472
+ var changedControls = __assign(__assign({}, (mergeChanges ? state.changedControls || {} : undefined)), getAncestorControls(controlRef.slice(0, -1), result).reduce(function (acc, control) {
473
+ var _a;
474
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
475
+ }, {}));
476
+ // Check for reindexing for changed controls
477
+ if (parentIsFormArray) {
478
+ changedControls = Object.entries(changedControls).reduce(function (acc, _a) {
479
+ var _b, _c;
480
+ var key = _a[0], control = _a[1];
481
+ var oldIndex = control.controlRef.at(parentRef.length);
482
+ if (
483
+ // If control is descendant.
484
+ parentRef.every(function (ref, index) { return control.controlRef[index] === ref; }) &&
485
+ control.controlRef.length > parentRef.length &&
486
+ // If the array item index was greater than the index of item removed
487
+ // we need to decrement its index by 1.
488
+ oldIndex > controlRef.at(-1)) {
489
+ var newRef = parentRef
490
+ .concat(oldIndex - 1)
491
+ .concat(control.controlRef.slice(parentRef.length + 1));
492
+ return __assign(__assign({}, acc), (_b = {}, _b[getFormKey(newRef)] = __assign(__assign({}, control), { controlRef: newRef }), _b));
493
+ }
494
+ else {
495
+ return __assign(__assign({}, acc), (_c = {}, _c[key] = control, _c));
496
+ }
497
+ }, {});
498
+ }
499
+ var removedControls = __assign(__assign({}, (mergeChanges ? state.removedControls || {} : undefined)), (_a = {}, _a[controlToRemove.key] = controlToRemove, _a));
500
+ descendants
501
+ .map(function (_a) {
502
+ var key = _a.key;
503
+ return key;
504
+ })
505
+ .forEach(function (key) {
506
+ delete changedControls[key];
507
+ });
469
508
  return {
470
- form: updateAncestorValuesRemoveControl(controlRemoved, {
471
- type: UPDATE_ANCESTOR_VALUES_REMOVE_CONTROL,
472
- payload: controlRef
473
- }),
474
- action: action
509
+ form: result,
510
+ changedControls: changedControls,
511
+ removedControls: removedControls
475
512
  };
476
513
  };
477
514
 
@@ -502,31 +539,38 @@ var updateAncestorValuesAddControl = function (form, _a) {
502
539
  return form;
503
540
  };
504
541
 
505
- var addControl = function (state, action) {
506
- var newControlRef;
542
+ var getControlBranch = function (controlRef, form) {
543
+ var ancestors = getAncestorControls(controlRef, form);
544
+ var childControls = getDescendantControls(controlRef, form).slice(1);
545
+ return ancestors.concat(childControls).sort(function (a, b) { return a.controlRef.length - b.controlRef.length; });
546
+ };
547
+
548
+ var addControl = function (state, action, mergeChanges) {
549
+ if (mergeChanges === void 0) { mergeChanges = false; }
507
550
  var _a = action.payload, config = _a.config, controlRef = _a.controlRef;
508
- var existingControl = getControl(controlRef, state.form);
509
- // If controlRef exists we are adding control to a Form Array
510
- if (existingControl && Array.isArray(existingControl.config.controls)) {
511
- newControlRef = controlRef.concat(existingControl.value.length);
551
+ // If controlRef does not exist we are adding control to a Form Group
552
+ if (!getControl(controlRef.slice(0, -1), state.form)) {
553
+ throw 'You are attempting to add a control to a non-existent form group';
512
554
  }
513
- else {
514
- // If controlRef does not exist we are adding control to a Form Group
515
- if (!getControl(controlRef.slice(0, -1), state.form)) {
516
- throw 'You are attempting to add a control to a non-existent form group';
517
- }
518
- newControlRef = controlRef;
519
- }
520
- var newForm = buildState(config, state.form, newControlRef);
521
- var newValue = getControl(newControlRef, newForm).value;
555
+ var newForm = buildState(config, state.form, controlRef);
556
+ var newValue = getControl(controlRef, newForm).value;
522
557
  var ancestorsUpdated = updateAncestorValuesAddControl(newForm, {
523
558
  type: UPDATE_ANCESTOR_VALUES_ADD_CONTROL,
524
- payload: { controlRef: newControlRef, value: newValue }
559
+ payload: { controlRef: controlRef, value: newValue }
525
560
  });
526
- return { form: ancestorsUpdated, action: action };
561
+ var changedControls = getControlBranch(controlRef, ancestorsUpdated).reduce(function (acc, control) {
562
+ var _a;
563
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
564
+ }, {});
565
+ return {
566
+ form: ancestorsUpdated,
567
+ changedControls: __assign(__assign({}, (mergeChanges ? state.changedControls || {} : undefined)), changedControls),
568
+ removedControls: mergeChanges ? state.removedControls || {} : undefined
569
+ };
527
570
  };
528
571
 
529
- var pushControl = function (state, action) {
572
+ var pushControl = function (state, action, mergeChanges) {
573
+ if (mergeChanges === void 0) { mergeChanges = false; }
530
574
  var newControlRef;
531
575
  var _a = action.payload, config = _a.config, controlRef = _a.controlRef;
532
576
  var existingControl = getControl(controlRef, state.form);
@@ -545,7 +589,15 @@ var pushControl = function (state, action) {
545
589
  type: UPDATE_ANCESTOR_VALUES_ADD_CONTROL,
546
590
  payload: { controlRef: newControlRef, value: newValue }
547
591
  });
548
- return { form: ancestorsUpdated, action: action };
592
+ var changedControls = getControlBranch(newControlRef, ancestorsUpdated).reduce(function (acc, control) {
593
+ var _a;
594
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
595
+ }, {});
596
+ return {
597
+ form: ancestorsUpdated,
598
+ changedControls: __assign(__assign({}, (mergeChanges ? state.changedControls || {} : undefined)), changedControls),
599
+ removedControls: mergeChanges ? state.removedControls || {} : undefined
600
+ };
549
601
  };
550
602
 
551
603
  // Same implementation as updateAncestor values except updating pristine values
@@ -584,8 +636,9 @@ var updateAncestorPristineValues = function (form, _a) {
584
636
  return form;
585
637
  };
586
638
 
587
- var markControlAsPristine = function (_a, action) {
588
- var form = _a.form;
639
+ var markControlAsPristine = function (state, action, mergeChanges) {
640
+ if (mergeChanges === void 0) { mergeChanges = false; }
641
+ var form = state.form;
589
642
  var controlRef = action.payload;
590
643
  var descendants = getDescendantControls(controlRef, form).reduce(function (acc, control) {
591
644
  var _a;
@@ -598,25 +651,44 @@ var markControlAsPristine = function (_a, action) {
598
651
  payload: controlRef
599
652
  });
600
653
  }
601
- return { form: result, action: action };
654
+ var changedControls = getControlBranch(controlRef, result).reduce(function (acc, control) {
655
+ var _a;
656
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
657
+ }, {});
658
+ return {
659
+ form: result,
660
+ changedControls: __assign(__assign({}, (mergeChanges ? state.changedControls || {} : undefined)), changedControls),
661
+ removedControls: mergeChanges ? state.removedControls || {} : undefined
662
+ };
602
663
  };
603
664
 
604
- var markControlAsTouched = function (_a, action) {
605
- var form = _a.form;
606
- var _b = action.payload, controlRef = _b.controlRef, markAll = _b.markAll;
665
+ var markControlAsTouched = function (state, action, mergeChanges) {
666
+ if (mergeChanges === void 0) { mergeChanges = false; }
667
+ var form = state.form;
668
+ var _a = action.payload, controlRef = _a.controlRef, markAll = _a.markAll;
607
669
  var controls = (markAll ? getControlBranch(controlRef, form) : getAncestorControls(controlRef, form)).reduce(function (acc, control) {
608
670
  var _a;
609
671
  return (__assign(__assign({}, acc), (_a = {}, _a[getFormKey(control.controlRef)] = __assign(__assign({}, control), { touched: true }), _a)));
610
672
  }, {});
611
- return {
673
+ var result = {
612
674
  form: __assign(__assign({}, form), controls),
613
675
  action: action
614
676
  };
677
+ var changedControls = getControlBranch(controlRef, result.form).reduce(function (acc, control) {
678
+ var _a;
679
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
680
+ }, {});
681
+ return {
682
+ form: __assign(__assign({}, form), controls),
683
+ changedControls: __assign(__assign({}, (mergeChanges ? state.changedControls || {} : undefined)), changedControls),
684
+ removedControls: mergeChanges ? state.removedControls || {} : undefined
685
+ };
615
686
  };
616
687
 
617
- var markControlAsUntouched = function (_a, action) {
618
- var _b;
619
- var form = _a.form;
688
+ var markControlAsUntouched = function (state, action, mergeChanges) {
689
+ var _a;
690
+ if (mergeChanges === void 0) { mergeChanges = false; }
691
+ var form = state.form;
620
692
  var controlRef = action.payload;
621
693
  var result = getDescendantControls(controlRef, form).reduce(function (acc, control) {
622
694
  var _a;
@@ -629,32 +701,59 @@ var markControlAsUntouched = function (_a, action) {
629
701
  while (currentRef.length > 0) {
630
702
  currentRef = currentRef.slice(0, -1);
631
703
  key = getFormKey(currentRef);
632
- result = __assign(__assign({}, result), (_b = {}, _b[key] = __assign(__assign({}, result[key]), { touched: getDescendantControls(currentRef, result, true).some(function (control) { return control.touched; }) }), _b));
704
+ result = __assign(__assign({}, result), (_a = {}, _a[key] = __assign(__assign({}, result[key]), { touched: getDescendantControls(currentRef, result, true).some(function (control) { return control.touched; }) }), _a));
633
705
  }
634
- return { form: result, action: action };
706
+ var changedControls = getControlBranch(controlRef, result).reduce(function (acc, control) {
707
+ var _a;
708
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
709
+ }, {});
710
+ return {
711
+ form: result,
712
+ changedControls: __assign(__assign({}, (mergeChanges ? state.changedControls || {} : undefined)), changedControls),
713
+ removedControls: mergeChanges ? state.removedControls || {} : undefined
714
+ };
635
715
  };
636
716
 
637
- var resetControl = function (_a, action) {
638
- var form = _a.form;
717
+ var resetControl = function (state, action, mergeChanges) {
718
+ var _a;
719
+ if (mergeChanges === void 0) { mergeChanges = false; }
720
+ var form = state.form;
639
721
  var controlRef = action.payload;
640
722
  var controlToReset = getControl(controlRef, form);
723
+ var descendantsRemoved = __assign({}, form);
641
724
  var descendants = getDescendantControls(controlRef, form);
725
+ var descendantKeys = descendants.map(function (_a) {
726
+ var controlRef = _a.controlRef;
727
+ return getFormKey(controlRef);
728
+ });
729
+ descendantKeys.forEach(function (key) {
730
+ delete descendantsRemoved[key];
731
+ });
642
732
  // Remove all descendants
643
- var descendantsRemoved = Object.entries(form).reduce(function (acc, _a) {
644
- var _b;
645
- var key = _a[0], control = _a[1];
646
- if (descendants.includes(control))
647
- return acc;
648
- return __assign(__assign({}, acc), (_b = {}, _b[key] = control, _b));
649
- }, {});
650
733
  var restoredControls = buildState(controlToReset.config, descendantsRemoved, controlToReset.controlRef);
651
734
  var restoredControlValue = getControl(controlRef, restoredControls).value;
735
+ var result = updateAncestorValues(restoredControls, {
736
+ type: UPDATE_ANCESTOR_VALUES,
737
+ payload: { controlRef: controlRef, value: restoredControlValue }
738
+ });
739
+ var changedControls = __assign(__assign({}, (mergeChanges ? state.changedControls || {} : undefined)), getControlBranch(controlRef, result).reduce(function (acc, control) {
740
+ var _a;
741
+ return (__assign(__assign({}, acc), (_a = {}, _a[control.key] = control, _a)));
742
+ }, {}));
743
+ var removedControls = __assign(__assign({}, (mergeChanges ? state.removedControls || {} : undefined)), (_a = {}, _a[controlToReset.key] = controlToReset, _a));
744
+ // If control is removed, we can delete it from the changedControls check
745
+ descendants
746
+ .map(function (_a) {
747
+ var key = _a.key;
748
+ return key;
749
+ })
750
+ .forEach(function (key) {
751
+ delete changedControls[key];
752
+ });
652
753
  return {
653
- form: updateAncestorValues(restoredControls, {
654
- type: UPDATE_ANCESTOR_VALUES,
655
- payload: { controlRef: controlRef, value: restoredControlValue }
656
- }),
657
- action: action
754
+ form: result,
755
+ changedControls: changedControls,
756
+ removedControls: removedControls
658
757
  };
659
758
  };
660
759
 
@@ -672,7 +771,7 @@ var asyncValidation = function (form, _a) {
672
771
  return __assign(__assign({}, form), updatedSelfAndAncestors);
673
772
  };
674
773
 
675
- var hasErrors$4 = function (errors) {
774
+ var hasErrors$3 = function (errors) {
676
775
  return Object.values(errors).some(function (hasError) { return hasError; });
677
776
  };
678
777
  var mergeBranchErrors = function (form, controlRef) {
@@ -686,7 +785,7 @@ var mergeBranchErrors = function (form, controlRef) {
686
785
  var _b;
687
786
  var key = _a[0], control = _a[1];
688
787
  var errors = __assign(__assign({}, control.validatorErrors), control.asyncValidatorErrors);
689
- var selfValid = !hasErrors$4(errors);
788
+ var selfValid = !hasErrors$3(errors);
690
789
  var childrenValid = true;
691
790
  if (Array.isArray(control.config.controls)) {
692
791
  // If control is a FormArray
@@ -738,7 +837,7 @@ var asyncValidationResponseSuccess = function (form, _a) {
738
837
  return mergeBranchErrors(ancestorsUpdated, control.controlRef);
739
838
  };
740
839
 
741
- var hasErrors$3 = function (errors) {
840
+ var hasErrors$2 = function (errors) {
742
841
  return Object.values(errors).some(function (hasError) { return hasError; });
743
842
  };
744
843
  // TODO: update merge errors to not do the entire form but just a subset of it
@@ -750,7 +849,7 @@ var mergeErrors = function (form) {
750
849
  var _b;
751
850
  var key = _a[0], control = _a[1];
752
851
  var errors = __assign(__assign({}, control.validatorErrors), control.asyncValidatorErrors);
753
- var selfValid = !hasErrors$3(errors);
852
+ var selfValid = !hasErrors$2(errors);
754
853
  var childrenValid = true;
755
854
  if (Array.isArray(control.config.controls)) {
756
855
  // If control is a FormArray
@@ -771,40 +870,6 @@ var mergeErrors = function (form) {
771
870
  return restoredOrder;
772
871
  };
773
872
 
774
- var hasErrors$2 = function (errors) {
775
- return Object.values(errors).some(function (hasError) { return hasError; });
776
- };
777
- var mergeValueUpdated = function (state, form, controlRef) {
778
- var controlBranch = getControlBranch(controlRef, form)
779
- .reverse()
780
- .reduce(function (acc, control) {
781
- var _a;
782
- var key = getFormKey(control.controlRef);
783
- var existingControl = state[key] || structuredClone(DEFAULT_HUB2_FIELDS);
784
- var errors = __assign(__assign({}, control.validatorErrors), existingControl.asyncValidatorErrors);
785
- var selfValid = !hasErrors$2(errors);
786
- var childrenValid = true;
787
- if (Array.isArray(control.config.controls)) {
788
- // If control is a FormArray
789
- childrenValid = control.value.every(function (item, index) {
790
- var formKey = getFormKey(control.controlRef.concat(index));
791
- var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
792
- return valid;
793
- });
794
- }
795
- else if (control.config.controls) {
796
- // If control is a FormGroup
797
- childrenValid = Object.keys(control.value).every(function (childKey) {
798
- var formKey = getFormKey(control.controlRef.concat(childKey));
799
- var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
800
- return valid;
801
- });
802
- }
803
- return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign(__assign({}, existingControl), control), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _a));
804
- }, {});
805
- return __assign(__assign({}, state), controlBranch);
806
- };
807
-
808
873
  var hasErrors$1 = function (errors) {
809
874
  return Object.values(errors).some(function (hasError) { return hasError; });
810
875
  };
@@ -848,70 +913,55 @@ var mergeRemoveControl = function (state, form, controlRef) {
848
913
  return __assign(__assign({}, removedControls), updatedControlBranch);
849
914
  };
850
915
 
851
- var mergeTouchUpdated = function (state, form, controlRef) {
852
- var controlBranch = getControlBranch(controlRef, form).reduce(function (acc, control) {
853
- var _a;
854
- var key = getFormKey(control.controlRef);
855
- return __assign(__assign({}, acc), (_a = {}, _a[key] = __assign(__assign({}, state[key]), control), _a));
856
- }, {});
857
- return __assign(__assign({}, state), controlBranch);
858
- };
859
-
860
916
  var hasErrors = function (errors) {
861
917
  return Object.values(errors).some(function (hasError) { return hasError; });
862
918
  };
863
- var mergePushControl = function (state, form, controlRef) {
864
- var baseFormArray = getControl(controlRef, form);
865
- //If Form Array
866
- // use the added control (last index)
867
- // merge descendants with default stuff
868
- var newItemIndex = baseFormArray.value.length - 1;
869
- var descendants = getDescendantControls(controlRef.concat(newItemIndex), form);
870
- var mergedDescendants = mergeErrors(descendants.reduce(function (acc, control) {
871
- var _a;
872
- var formKey = getFormKey(control.controlRef);
873
- return __assign(__assign({}, acc), (_a = {}, _a[formKey] = __assign(__assign({}, control), structuredClone(DEFAULT_HUB2_FIELDS)), _a));
874
- }, {}));
875
- var ancestors = getAncestorControls(controlRef, state)
919
+ var mergeControls = function (state, _a) {
920
+ var form = _a.form, _b = _a.changedControls, changedControls = _b === void 0 ? {} : _b, removedControls = _a.removedControls;
921
+ var controlsRemoved = removedControls
922
+ ? Object.values(removedControls).reduce(function (acc, _a) {
923
+ var controlRef = _a.controlRef;
924
+ return mergeRemoveControl(acc, form, controlRef);
925
+ }, state)
926
+ : state;
927
+ var updatedBranch = Object.values(changedControls)
876
928
  .reverse()
877
- .reduce(function (acc, control, index, arr) {
929
+ .reduce(function (acc, control) {
878
930
  var _a;
879
931
  var formKey = getFormKey(control.controlRef);
880
- var errors = __assign(__assign({}, form[formKey].validatorErrors), control.asyncValidatorErrors);
932
+ var existingControl = controlsRemoved && controlsRemoved[formKey];
933
+ var newControl = __assign(__assign({}, (existingControl
934
+ ? existingControl
935
+ : structuredClone(DEFAULT_HUB2_FIELDS))), control);
936
+ var errors = __assign(__assign({}, newControl.validatorErrors), newControl.asyncValidatorErrors);
881
937
  var selfValid = !hasErrors(errors);
882
938
  var childrenValid = true;
883
- if (index === 0) {
884
- childrenValid =
885
- control.childrenValid &&
886
- mergedDescendants[getFormKey(controlRef.concat(newItemIndex))].valid;
939
+ if (Array.isArray(control.config.controls)) {
940
+ // If control is a FormArray
941
+ childrenValid = control.value.every(function (item, index) {
942
+ var formKey = getFormKey(control.controlRef.concat(index));
943
+ var valid = acc[formKey] === undefined ? controlsRemoved[formKey].valid : acc[formKey].valid;
944
+ return valid;
945
+ });
887
946
  }
888
- else {
889
- if (Array.isArray(control.config.controls)) {
890
- // If control is a FormArray
891
- childrenValid = control.value.every(function (item, index) {
892
- var formKey = getFormKey(control.controlRef.concat(index));
893
- var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
894
- return valid;
895
- });
896
- }
897
- else if (control.config.controls) {
898
- // If control is a FormGroup
899
- childrenValid = Object.keys(control.value).every(function (childKey) {
900
- var formKey = getFormKey(control.controlRef.concat(childKey));
901
- var valid = acc[formKey] === undefined ? state[formKey].valid : acc[formKey].valid;
902
- return valid;
903
- });
904
- }
947
+ else if (control.config.controls) {
948
+ // If control is a FormGroup
949
+ childrenValid = Object.keys(control.value).every(function (childKey) {
950
+ var formKey = getFormKey(control.controlRef.concat(childKey));
951
+ var valid = acc[formKey] === undefined ? controlsRemoved[formKey].valid : acc[formKey].valid;
952
+ return valid;
953
+ });
905
954
  }
906
- return __assign(__assign({}, acc), (_a = {}, _a[formKey] = __assign(__assign(__assign({}, control), form[formKey]), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _a));
955
+ return __assign(__assign({}, acc), (_a = {}, _a[formKey] = __assign(__assign({}, newControl), { errors: errors, valid: selfValid && childrenValid, childrenValid: childrenValid }), _a));
907
956
  }, {});
908
- var controlBranchUpdated = __assign(__assign({}, ancestors), mergedDescendants);
909
- return __assign(__assign({}, state), controlBranchUpdated);
957
+ var result = __assign(__assign({}, controlsRemoved), updatedBranch);
958
+ return result;
910
959
  };
911
960
 
912
961
  var formChange = function (state, _a) {
913
962
  if (state === void 0) { state = null; }
914
- var _b = _a.payload, form = _b.form, action = _b.action;
963
+ var payload = _a.payload;
964
+ var form = payload.form;
915
965
  if (state === null) {
916
966
  return mergeErrors(Object.entries(form).reduce(function (acc, _a) {
917
967
  var _b;
@@ -919,30 +969,7 @@ var formChange = function (state, _a) {
919
969
  return __assign(__assign({}, acc), (_b = {}, _b[dictKey] = __assign(__assign({}, structuredClone(DEFAULT_HUB2_FIELDS)), baseControl), _b));
920
970
  }, {}));
921
971
  }
922
- switch (action === null || action === void 0 ? void 0 : action.type) {
923
- case 'pushControl':
924
- return mergePushControl(state, form, action.payload.controlRef);
925
- case 'addControl':
926
- case 'updateValues':
927
- return mergeValueUpdated(state, form, action.payload.controlRef);
928
- case 'resetControl':
929
- case 'removeControl':
930
- return mergeRemoveControl(state, form, action.payload);
931
- case 'markControlAsPristine':
932
- case 'markControlAsUntouched':
933
- return mergeTouchUpdated(state, form, action.payload);
934
- case 'markControlAsTouched':
935
- return mergeTouchUpdated(state, form, action.payload.controlRef);
936
- default:
937
- return mergeErrors(Object.entries(form).reduce(function (acc, _a) {
938
- var _b;
939
- var dictKey = _a[0], baseControl = _a[1];
940
- var existingControl = state && state[dictKey];
941
- return __assign(__assign({}, acc), (_b = {}, _b[dictKey] = __assign(__assign({}, (existingControl
942
- ? existingControl
943
- : structuredClone(DEFAULT_HUB2_FIELDS))), baseControl), _b));
944
- }, {}));
945
- }
972
+ return mergeControls(state, payload);
946
973
  };
947
974
 
948
975
  var control = function (config) {
@@ -961,18 +988,45 @@ var control = function (config) {
961
988
  };
962
989
  var array = function (config) { return config; };
963
990
  var group = function (config) { return config; };
991
+ var reducerTools = {
992
+ updateValues: function (state, action) {
993
+ return updateValues(state, action, true);
994
+ },
995
+ removeControl: function (state, action) {
996
+ return removeControl(state, action, true);
997
+ },
998
+ pushControl: function (state, action) {
999
+ return pushControl(state, action, true);
1000
+ },
1001
+ addControl: function (state, action) {
1002
+ return addControl(state, action, true);
1003
+ },
1004
+ markControlAsPristine: function (state, action) {
1005
+ return markControlAsPristine(state, action, true);
1006
+ },
1007
+ markControlAsTouched: function (state, action) {
1008
+ return markControlAsTouched(state, action, true);
1009
+ },
1010
+ markControlAsUntouched: function (state, action) {
1011
+ return markControlAsUntouched(state, action, true);
1012
+ },
1013
+ resetControl: function (state, action) {
1014
+ return resetControl(state, action, true);
1015
+ }
1016
+ };
964
1017
  var build = function (config, options) {
1018
+ if (options === void 0) { options = {}; }
965
1019
  var initialState = buildFormState(config);
966
- var rxHub1 = core.RxBuilder(__assign({ initialState: initialState, reducers: {
967
- updateValues: updateValues,
968
- removeControl: removeControl,
969
- addControl: addControl,
970
- pushControl: pushControl,
971
- markControlAsPristine: markControlAsPristine,
972
- markControlAsTouched: markControlAsTouched,
973
- markControlAsUntouched: markControlAsUntouched,
974
- resetControl: resetControl
975
- } }, options));
1020
+ var reducers = options.reducers, otherOptions = __rest(options, ["reducers"]);
1021
+ var customReducers = Object.entries(reducers || {}).reduce(function (acc, _a) {
1022
+ var _b;
1023
+ var key = _a[0], reducer = _a[1];
1024
+ return (__assign(__assign({}, acc), (_b = {}, _b[key] = function (_a, action) {
1025
+ var form = _a.form;
1026
+ return reducer(reducerTools, { form: form }, action);
1027
+ }, _b)));
1028
+ }, {});
1029
+ var rxHub1 = core.RxBuilder(__assign({ initialState: initialState, reducers: __assign({ updateValues: updateValues, removeControl: removeControl, addControl: addControl, pushControl: pushControl, markControlAsPristine: markControlAsPristine, markControlAsTouched: markControlAsTouched, markControlAsUntouched: markControlAsUntouched, resetControl: resetControl }, customReducers) }, otherOptions));
976
1030
  var rxHub2 = core.RxBuilder({
977
1031
  sources: [buildHub2Source(rxHub1)],
978
1032
  initialState: null,
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "author": "David Lai",
15
15
  "license": "ISC",
16
16
  "dependencies": {
17
- "@reactables/core": "^0.5.4-alpha.0",
17
+ "@reactables/core": "^0.6.0-alpha.0",
18
18
  "lodash.isequal": "^4.5.0"
19
19
  },
20
20
  "peerDependencies": {
@@ -23,5 +23,5 @@
23
23
  "devDependencies": {
24
24
  "lodash.clonedeep": "^4.5.0"
25
25
  },
26
- "version": "0.5.4-alpha.0"
26
+ "version": "0.6.0-alpha.0"
27
27
  }
@@ -1,6 +0,0 @@
1
- import { Form, BaseForm, FormControl } from '../../Models/Controls';
2
- import { ControlRef } from '../../Models/ControlRef';
3
- export declare const mergeTouchUpdated: <T>(state: Form<T>, form: BaseForm<T>, controlRef: ControlRef) => {
4
- [x: string]: FormControl<unknown>;
5
- root?: FormControl<T>;
6
- };
@@ -1,6 +0,0 @@
1
- import { Form, BaseForm, FormControl } from '../../Models/Controls';
2
- import { ControlRef } from '../../Models/ControlRef';
3
- export declare const mergeValueUpdated: <T>(state: Form<T>, form: BaseForm<T>, controlRef: ControlRef) => {
4
- [x: string]: FormControl<unknown>;
5
- root?: FormControl<T>;
6
- };