@homebound/form-state 2.17.1 → 2.18.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.
package/dist/config.d.ts CHANGED
@@ -63,6 +63,8 @@ export declare type ListFieldConfig<T, U> = {
63
63
  * default behavior.
64
64
  */
65
65
  update?: "exhaustive" | "incremental";
66
+ /** Set to not consider the order of the list when evaluating changed/dirty states. */
67
+ strictOrder?: false;
66
68
  };
67
69
  export declare type ObjectFieldConfig<U> = {
68
70
  type: "object";
@@ -8,4 +8,4 @@ export interface ListFieldState<U> extends Omit<FieldState<U[]>, "originalValue"
8
8
  add(value: U, index?: number): void;
9
9
  remove(indexOrValue: number | U): void;
10
10
  }
11
- export declare function newListFieldState<T, K extends keyof T, U>(parentInstance: T, parentState: () => ObjectState<T>, key: K, rules: Rule<readonly ObjectState<U>[]>[], listConfig: ListFieldConfig<T, U>, config: ObjectConfig<U>, maybeAutoSave: () => void): ListFieldState<U>;
11
+ export declare function newListFieldState<T, K extends keyof T, U>(parentInstance: T, parentState: () => ObjectState<T>, key: K, rules: Rule<readonly ObjectState<U>[]>[], listConfig: ListFieldConfig<T, U>, config: ObjectConfig<U>, strictOrder: boolean, maybeAutoSave: () => void): ListFieldState<U>;
@@ -5,7 +5,7 @@ const mobx_1 = require("mobx");
5
5
  const objectField_1 = require("./objectField");
6
6
  const rules_1 = require("../rules");
7
7
  const utils_1 = require("../utils");
8
- function newListFieldState(parentInstance, parentState, key, rules, listConfig, config, maybeAutoSave) {
8
+ function newListFieldState(parentInstance, parentState, key, rules, listConfig, config, strictOrder, maybeAutoSave) {
9
9
  // Keep a map of "item in the parent list" -> "that item's ObjectState"
10
10
  const rowMap = new Map();
11
11
  const _tick = mobx_1.observable({ value: 1 });
@@ -37,7 +37,7 @@ function newListFieldState(parentInstance, parentState, key, rules, listConfig,
37
37
  this.set(v);
38
38
  },
39
39
  get dirty() {
40
- return this.rows.some((r) => r.dirty) || this.hasNewItems();
40
+ return this.rows.some((r) => r.dirty) || this.hasChanged();
41
41
  },
42
42
  get required() {
43
43
  return this.rules.some((rule) => rule === rules_1.required);
@@ -46,21 +46,22 @@ function newListFieldState(parentInstance, parentState, key, rules, listConfig,
46
46
  return parentState().isNewEntity;
47
47
  },
48
48
  // private
49
- hasNewItems() {
49
+ hasChanged() {
50
50
  const [current, original] = [this.value || [], originalCopy || []];
51
- // Instead of relying on just object identities, we look up each child's state
52
- // in rowMap, because we already dedup/check object identity (i.e. look for id fields)
53
- // when create object states.
54
- const a = current.every((e) => {
55
- const state = rowMap.get(e);
56
- return original.some((e) => rowMap.get(e) === state);
57
- });
58
- const b = original.every((e) => {
51
+ if (current.length !== original.length) {
52
+ return true;
53
+ }
54
+ if (strictOrder) {
55
+ return original.some((e, idx) => {
56
+ const originalState = rowMap.get(e);
57
+ const currentState = rowMap.get(current[idx]);
58
+ return originalState !== currentState;
59
+ });
60
+ }
61
+ return !original.every((e) => {
59
62
  const state = rowMap.get(e);
60
63
  return current.some((e) => rowMap.get(e) === state);
61
64
  });
62
- const isSame = a && b;
63
- return !isSame;
64
65
  },
65
66
  // And we can derive each value's ObjectState wrapper as needed from the rowMap cache
66
67
  get rows() {
@@ -82,7 +83,7 @@ function newListFieldState(parentInstance, parentState, key, rules, listConfig,
82
83
  },
83
84
  // TODO Should this be true when all rows are touched?
84
85
  get touched() {
85
- return this.rows.some((r) => r.touched) || this.hasNewItems();
86
+ return this.rows.some((r) => r.touched) || this.hasChanged();
86
87
  },
87
88
  set touched(touched) {
88
89
  this.rows.forEach((r) => (r.touched = touched));
@@ -35,7 +35,7 @@ function newObjectState(config, parentState, parentListState, instance, key, may
35
35
  return proxy;
36
36
  }
37
37
  const fieldStates = Object.entries(config).map(([_key, _config]) => {
38
- var _a;
38
+ var _a, _b;
39
39
  const key = _key;
40
40
  const config = _config;
41
41
  let field;
@@ -43,7 +43,7 @@ function newObjectState(config, parentState, parentListState, instance, key, may
43
43
  field = valueField_1.newValueFieldState(instance, getObjectState, key, config.rules || [], config.isIdKey || key === "id", config.isDeleteKey || false, config.isReadOnlyKey || false, config.computed || false, config.readOnly || false, (_a = config.strictOrder) !== null && _a !== void 0 ? _a : true, maybeAutoSave);
44
44
  }
45
45
  else if (config.type === "list") {
46
- field = listField_1.newListFieldState(instance, getObjectState, key, config.rules || [], config, config.config, maybeAutoSave);
46
+ field = listField_1.newListFieldState(instance, getObjectState, key, config.rules || [], config, config.config, (_b = config.strictOrder) !== null && _b !== void 0 ? _b : true, maybeAutoSave);
47
47
  }
48
48
  else if (config.type === "object") {
49
49
  if (!instance[key]) {
@@ -1320,6 +1320,41 @@ describe("formState", () => {
1320
1320
  // The list is again dirty
1321
1321
  expect(a1.books.dirty).toBeTruthy();
1322
1322
  });
1323
+ it("can properly evaluate dirty lists when order changes", () => {
1324
+ // Given a list state in a specific order
1325
+ const a = createAuthorInputState({ books: [{ title: "t1" }, { title: "t2" }] });
1326
+ expect(a.books.dirty).toBeFalsy();
1327
+ // When changing that order
1328
+ const book1 = a.books.rows[0].value;
1329
+ a.books.remove(book1);
1330
+ a.books.add(book1);
1331
+ // Then the list state is considered dirty.
1332
+ expect(a.books.dirty).toBeTruthy();
1333
+ // When putting the books back in the original order
1334
+ a.books.remove(book1);
1335
+ a.books.add(book1, 0);
1336
+ // Then the list state is no longer dirty
1337
+ expect(a.books.dirty).toBeFalsy();
1338
+ });
1339
+ it("can optionally ignore the order of list state rows when determining dirty state", () => {
1340
+ // Given a list state in a specific order, and the `strictOrder` option set to `false`
1341
+ const a = objectField_1.createObjectState({
1342
+ books: {
1343
+ type: "list",
1344
+ strictOrder: false,
1345
+ config: {
1346
+ title: { type: "value", rules: [rules_1.required] },
1347
+ },
1348
+ },
1349
+ }, { books: [{ title: "t1" }, { title: "t2" }] });
1350
+ expect(a.books.dirty).toBeFalsy();
1351
+ // When changing that order
1352
+ const book1 = a.books.rows[0].value;
1353
+ a.books.remove(book1);
1354
+ a.books.add(book1);
1355
+ // Then the list state is still not considered dirty.
1356
+ expect(a.books.dirty).toBeFalsy();
1357
+ });
1323
1358
  describe("fragments", () => {
1324
1359
  it("ignores changes within fragments", () => {
1325
1360
  const a = objectField_1.createObjectState({ title: { type: "value" }, data: { type: "fragment" } }, { title: "b1", data: objectField_1.fragment({ foo: "1" }) });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/form-state",
3
- "version": "2.17.1",
3
+ "version": "2.18.0",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",