@homebound/form-state 2.1.1 → 2.1.2
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.
- package/dist/formState.js +10 -13
- package/dist/formState.test.js +38 -1
- package/package.json +1 -1
package/dist/formState.js
CHANGED
|
@@ -163,7 +163,7 @@ function newObjectState(config, parentState, instance, key, onBlur) {
|
|
|
163
163
|
// Accepts new values in bulk, i.e. when setting the form initial state from the backend.
|
|
164
164
|
set(value) {
|
|
165
165
|
if (this.readOnly) {
|
|
166
|
-
throw new Error("
|
|
166
|
+
throw new Error(`${key || "formState"} is currently readOnly`);
|
|
167
167
|
}
|
|
168
168
|
getFields(this).forEach((field) => {
|
|
169
169
|
if (field.key in value && (!field.dirty || !field._focused)) {
|
|
@@ -296,9 +296,9 @@ function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, is
|
|
|
296
296
|
this.touched = true;
|
|
297
297
|
onBlur();
|
|
298
298
|
},
|
|
299
|
-
set(value) {
|
|
300
|
-
if (this.readOnly) {
|
|
301
|
-
throw new Error(
|
|
299
|
+
set(value, opts = {}) {
|
|
300
|
+
if (this.readOnly && !opts.resetting) {
|
|
301
|
+
throw new Error(`${key} is currently readOnly`);
|
|
302
302
|
}
|
|
303
303
|
// If the user has deleted/emptied a value that was originally set, keep it as `null`
|
|
304
304
|
// so that our partial update to the backend correctly unsets it.
|
|
@@ -311,11 +311,8 @@ function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, is
|
|
|
311
311
|
_tick.value++;
|
|
312
312
|
},
|
|
313
313
|
reset() {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
// using a `isReadOnlyKey` has made an entity read-only.
|
|
317
|
-
if (!computed && !this.readOnly) {
|
|
318
|
-
this.set(this.originalValue);
|
|
314
|
+
if (!computed) {
|
|
315
|
+
this.set(this.originalValue, { resetting: true });
|
|
319
316
|
}
|
|
320
317
|
this.touched = false;
|
|
321
318
|
},
|
|
@@ -436,9 +433,9 @@ function newListFieldState(parentInstance, parentState, key, rules, listConfig,
|
|
|
436
433
|
this.touched = true;
|
|
437
434
|
onBlur();
|
|
438
435
|
},
|
|
439
|
-
set(values) {
|
|
440
|
-
if (this.readOnly) {
|
|
441
|
-
throw new Error(
|
|
436
|
+
set(values, opts = {}) {
|
|
437
|
+
if (this.readOnly && !opts.resetting) {
|
|
438
|
+
throw new Error(`${key} is currently readOnly`);
|
|
442
439
|
}
|
|
443
440
|
// We should be passed values that are non-proxies.
|
|
444
441
|
parentInstance[key] = values.map((value) => {
|
|
@@ -475,7 +472,7 @@ function newListFieldState(parentInstance, parentState, key, rules, listConfig,
|
|
|
475
472
|
},
|
|
476
473
|
reset() {
|
|
477
474
|
if (originalCopy) {
|
|
478
|
-
this.set(originalCopy);
|
|
475
|
+
this.set(originalCopy, { resetting: true });
|
|
479
476
|
this.rows.forEach((r) => r.reset());
|
|
480
477
|
}
|
|
481
478
|
},
|
package/dist/formState.test.js
CHANGED
|
@@ -409,6 +409,43 @@ describe("formState", () => {
|
|
|
409
409
|
expect(a1.dirty).toBeFalsy();
|
|
410
410
|
expect(a1.touched).toBeFalsy();
|
|
411
411
|
});
|
|
412
|
+
it("resets values even if read only", () => {
|
|
413
|
+
// Given a form state
|
|
414
|
+
const a1 = createAuthorInputState({
|
|
415
|
+
firstName: "a1",
|
|
416
|
+
lastName: "aL1",
|
|
417
|
+
books: [
|
|
418
|
+
{ title: "b1", classification: formStateDomain_1.dd100 },
|
|
419
|
+
{ title: "b2", classification: formStateDomain_1.dd100 },
|
|
420
|
+
],
|
|
421
|
+
});
|
|
422
|
+
// And some values have been changed
|
|
423
|
+
expect(a1.dirty).toBeFalsy();
|
|
424
|
+
a1.firstName.set("a2");
|
|
425
|
+
a1.firstName.touched = true;
|
|
426
|
+
a1.lastName.set("aL2");
|
|
427
|
+
a1.books.rows[0].set({ title: "b2" });
|
|
428
|
+
a1.books.rows[1].set({ title: "bb2" });
|
|
429
|
+
a1.books.add({ title: "b3" });
|
|
430
|
+
expect(a1.books.touched).toEqual(true);
|
|
431
|
+
expect(a1.dirty).toBeTruthy();
|
|
432
|
+
// And the "readOnly=true" operation has "beat" the reset operation
|
|
433
|
+
a1.readOnly = true;
|
|
434
|
+
// When we reset
|
|
435
|
+
a1.reset();
|
|
436
|
+
// Then it still works
|
|
437
|
+
expect(a1.firstName.value).toBe("a1");
|
|
438
|
+
expect(a1.firstName.touched).toBeFalsy();
|
|
439
|
+
expect(a1.lastName.value).toBe("aL1");
|
|
440
|
+
expect(a1.books.rows.length).toBe(2);
|
|
441
|
+
expect(a1.books.touched).toBe(false);
|
|
442
|
+
expect(a1.books.rows[0].title.value).toBe("b1");
|
|
443
|
+
expect(a1.books.rows[0].title.dirty).toBe(false);
|
|
444
|
+
expect(a1.books.rows[0].title.touched).toBe(false);
|
|
445
|
+
expect(a1.books.rows[1].title.value).toBe("b2");
|
|
446
|
+
expect(a1.dirty).toBeFalsy();
|
|
447
|
+
expect(a1.touched).toBeFalsy();
|
|
448
|
+
});
|
|
412
449
|
it("saves values into _originalState", () => {
|
|
413
450
|
const a1 = createAuthorInputState({
|
|
414
451
|
firstName: "a1",
|
|
@@ -496,7 +533,7 @@ describe("formState", () => {
|
|
|
496
533
|
a1.readOnly = true;
|
|
497
534
|
fields.forEach((f) => expect(f.readOnly).toBeTruthy());
|
|
498
535
|
fields.forEach((f) => {
|
|
499
|
-
expect(() => f.set(null)).toThrow("
|
|
536
|
+
expect(() => f.set(null)).toThrow("is currently readOnly");
|
|
500
537
|
});
|
|
501
538
|
});
|
|
502
539
|
it("maintain field readOnly state when form is readOnly", () => {
|