@homebound/form-state 2.26.3 → 2.26.5

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/README.md CHANGED
@@ -23,6 +23,10 @@ It also keeps track of low-level form UX details like:
23
23
  - Supports collections of children, i.e. a `author: { books: [...} }` will include only changed books if necessary
24
24
  - Child collections can be either exhaustive (if any child changes, submit them all) or incremental (only include changed children), to match the backend endpoint's semantics
25
25
 
26
+ # Basic Usage
27
+
28
+ See our in-repo [sample app](https://github.com/homebound-team/form-state/blob/main/src/FormStateApp.tsx).
29
+
26
30
  # Main Features
27
31
 
28
32
  There are two main reasons why form-state exists:
@@ -90,19 +94,70 @@ Admittedly (and hopefully, b/c it makes the code simpler), the differences betwe
90
94
 
91
95
  These are usually simple/mechanistic changes, but nonetheless just some boilerplate that form-state provides conventions for.
92
96
 
93
- # Basic Usage
97
+ # Other Features
94
98
 
95
- See the [sample](https://github.com/homebound-team/form-state/blob/main/src/FormStateApp.tsx).
99
+ ## Incremental changedValue/dirty Management
96
100
 
97
- # Todo
101
+ In general, we have two types of forms:
98
102
 
99
- - Add conditional readonly logic, like `{ type: "field", readOnlyIf: i => i.isInternal.value }`
103
+ - `autoSave` forms
104
+ - `submit` forms
100
105
 
101
- - Add `omitFromValue` so we can have two fields, `book.author.id` / `book.author.name`, where `book.author.name` is used for showing the author name, but `book.author.id` is the only field that is submitted to the server on mutation (maybe pair this with `Ref` based mutations)
106
+ ### autoSave forms
102
107
 
103
- - Undo/redo would in theory be neat and easy to do on top of the existing infra
108
+ For auto save forms, the expectation is that you should:
109
+
110
+ - Pass `useFormState` an `init.input` and `init.map` that updates the form-state from the initial GraphQL request & any cache updates
111
+ - Pass `useFormState` an `autoSave` lambda that calls your GraphQL mutation, using `changedValue`.
112
+ - Have your `save` mutation response return the acked/updated entity/fragment
113
+
114
+ ```ts
115
+ autoSave: async () => {
116
+ const input = formState.changedValue;
117
+ await saveAuthor(input);
118
+ };
119
+ ```
120
+
121
+ With these in place, we will correctly handle interleaved edits/saves, i.e.:
122
+
123
+ 1. User changes `{ firstName: bob }`
124
+ 2. We submit `{ id: 1, firstName: bob }` to the backend
125
+ 3. While waiting for the response, the user sets `{ lastName: smith }`
126
+ 4. The GraphQL mutation acks that `{ id: 1, firstName: bob }` is committed
127
+ 5. The `init.map` updates `formState` to realize `firstName` is no longer dirty, but `lastName` keeps its WIP change
128
+ 6. formState will trigger a 2nd `autoSave` for just the `lastName` change
129
+
130
+ ### Submit forms
131
+
132
+ For submit forms, the expectation is that you should:
133
+
134
+ - Pass `useFormState` an `init.input` and `init.map` that updates the form-state from the initial GraphQL request & any cache updates
135
+ - In your `onClick` lambda, use `formState.changedValue` to call your GraphQL mutation
136
+ - Have your `save` mutation response return the acked/updated entity/fragment
104
137
 
105
- # Features
138
+ If you do this, you should not have to call `commitChanges` manually, because code like:
139
+
140
+ ```ts
141
+ onClick: async () => {
142
+ const input = formState.changedValue;
143
+ await saveAuthor(input);
144
+ // checks if formState.dirty is true before closing
145
+ closeModal();
146
+ };
147
+ ```
148
+
149
+ Will "just work" because the control flow will be:
150
+
151
+ - User changes `{ firstName: bob }` and clicks Submit
152
+ - `onClick` runs and we submit `{ id: 1, firstName: bob }` to `saveAuthor`
153
+ - _Before_ the `await` promise resolves, the GraphQL response of `saveAuthor { ...AuthorFragment }` will:
154
+ - Update the apollo cache
155
+ - Re-render the `AuthorEditor` component with the new data
156
+ - Call `init.map` to update `formState` with the new data
157
+ - Realize the `firstName` is no longer dirty
158
+ - When `closeModal` runs, no "You have unsaved changes?" will appear
159
+
160
+ Basically, in a correctly-setup form, you should never have to call `commitChanges` manually, and doing so risks losing edits that the user made while any saves (either auto save or submit save) were in-flight.
106
161
 
107
162
  ## Fragments
108
163
 
@@ -148,6 +203,14 @@ const form = useFormState({
148
203
  });
149
204
  ```
150
205
 
206
+ # Todo
207
+
208
+ - Add conditional readonly logic, like `{ type: "field", readOnlyIf: i => i.isInternal.value }`
209
+
210
+ - Add `omitFromValue` so we can have two fields, `book.author.id` / `book.author.name`, where `book.author.name` is used for showing the author name, but `book.author.id` is the only field that is submitted to the server on mutation (maybe pair this with `Ref` based mutations)
211
+
212
+ - Undo/redo would in theory be neat and easy to do on top of the existing infra
213
+
151
214
  # Internal Implementation Notes
152
215
 
153
216
  form-state keeps the "actual data" (basically a POJO of your form data) separate from the "mobx proxies that track reactivity" (the `ObjectState` interface with `.get` / `.set` / `.errors` other methods).
@@ -100,7 +100,8 @@ function newValueFieldState(parentCopy, parentInstance, parentState, key, rules,
100
100
  if (this.readOnly && !opts.resetting && !opts.refreshing) {
101
101
  throw new Error(`${String(key)} is currently readOnly`);
102
102
  }
103
- if (opts.refreshing && this.dirty && this.value !== value) {
103
+ const isAckingUnset = this.value === null && (value === null || value === undefined);
104
+ if (opts.refreshing && this.dirty && this.value !== value && !isAckingUnset) {
104
105
  // Ignore incoming values if we have changes (this.dirty) unless our latest change (this.value)
105
106
  // matches the incoming value (value), b/c if it does we should accept it and reset originalValue
106
107
  // so that we're no longer dirty.
@@ -735,6 +735,13 @@ describe("formState", () => {
735
735
  expect(a1.firstName.value).toBeNull();
736
736
  expect(a1.originalValue.firstName).toBe("asdf");
737
737
  expect(a1.firstName.dirty).toBeTruthy();
738
+ // And when the server acks as null
739
+ a1.set({ firstName: undefined }, { refreshing: true });
740
+ // Then it's no longer dirty
741
+ expect(a1.firstName.dirty).toBe(false);
742
+ expect(a1.firstName.originalValue).toBe(undefined);
743
+ expect(a1.firstName.value).toBe(undefined);
744
+ expect(a1.changedValue).toEqual({});
738
745
  });
739
746
  it("initializes null values to be undefined", () => {
740
747
  // Given a property that is initially set to null
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { f } from "./configBuilders";
2
2
  export { type FragmentField } from "./fields/fragmentField";
3
3
  export { type ListFieldState } from "./fields/listField";
4
- export { createObjectState, type Fragment, type ObjectState, type fragment } from "./fields/objectField";
4
+ export { createObjectState, type Fragment, type ObjectState, fragment } from "./fields/objectField";
5
5
  export { type FieldState, type ValueAdapter } from "./fields/valueField";
6
6
  export { required, type Rule } from "./rules";
7
7
  export { useFormState } from "./useFormState";
package/dist/index.js CHANGED
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useFormStates = exports.useFormState = exports.required = exports.createObjectState = exports.f = void 0;
3
+ exports.useFormStates = exports.useFormState = exports.required = exports.fragment = exports.createObjectState = exports.f = void 0;
4
4
  var configBuilders_1 = require("./configBuilders");
5
5
  Object.defineProperty(exports, "f", { enumerable: true, get: function () { return configBuilders_1.f; } });
6
6
  var objectField_1 = require("./fields/objectField");
7
7
  Object.defineProperty(exports, "createObjectState", { enumerable: true, get: function () { return objectField_1.createObjectState; } });
8
+ Object.defineProperty(exports, "fragment", { enumerable: true, get: function () { return objectField_1.fragment; } });
8
9
  var rules_1 = require("./rules");
9
10
  Object.defineProperty(exports, "required", { enumerable: true, get: function () { return rules_1.required; } });
10
11
  var useFormState_1 = require("./useFormState");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/form-state",
3
- "version": "2.26.3",
3
+ "version": "2.26.5",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",