@homebound/form-state 2.26.4 → 2.26.6
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 +13 -13
- package/dist/fields/listField.js +1 -1
- package/dist/formState.test.js +13 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -1
- package/package.json +1 -1
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,7 @@ 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
|
-
#
|
|
94
|
-
|
|
95
|
-
See the [sample](https://github.com/homebound-team/form-state/blob/main/src/FormStateApp.tsx).
|
|
96
|
-
|
|
97
|
-
# Todo
|
|
98
|
-
|
|
99
|
-
- Add conditional readonly logic, like `{ type: "field", readOnlyIf: i => i.isInternal.value }`
|
|
100
|
-
|
|
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)
|
|
102
|
-
|
|
103
|
-
- Undo/redo would in theory be neat and easy to do on top of the existing infra
|
|
104
|
-
|
|
105
|
-
# Features
|
|
97
|
+
# Other Features
|
|
106
98
|
|
|
107
99
|
## Incremental changedValue/dirty Management
|
|
108
100
|
|
|
@@ -211,6 +203,14 @@ const form = useFormState({
|
|
|
211
203
|
});
|
|
212
204
|
```
|
|
213
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
|
+
|
|
214
214
|
# Internal Implementation Notes
|
|
215
215
|
|
|
216
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).
|
package/dist/fields/listField.js
CHANGED
|
@@ -173,7 +173,7 @@ function newListFieldState(parentCopy, parentInstance, parentState, key, rules,
|
|
|
173
173
|
// passing us cloned rows from the parentCopy, but `getOrCreateChildState` will
|
|
174
174
|
// use the `copyMap` to recover the non-cloned rows, to avoid promoting the clone
|
|
175
175
|
// into a real row.
|
|
176
|
-
parentInstance[key] = values.map((child) => getOrCreateChildState(child, opts).value);
|
|
176
|
+
parentInstance[key] = (values !== null && values !== void 0 ? values : []).map((child) => getOrCreateChildState(child, opts).value);
|
|
177
177
|
// Make sure to tick first so that `setOriginalValue` sees the latest `rows`
|
|
178
178
|
_tick.value++;
|
|
179
179
|
// Reset originalCopy so that our dirty checks have the right # of rows.
|
package/dist/formState.test.js
CHANGED
|
@@ -152,6 +152,19 @@ describe("formState", () => {
|
|
|
152
152
|
expect(state.books.rows[2].value === b1).toEqual(true);
|
|
153
153
|
expect(ticks).toEqual(3);
|
|
154
154
|
});
|
|
155
|
+
it("list value can refresh on undefined", () => {
|
|
156
|
+
// Given a list field that is currently []
|
|
157
|
+
const a1 = { firstName: "a1", books: [] };
|
|
158
|
+
const state = createAuthorInputState(a1);
|
|
159
|
+
// When we refresh it with undefined
|
|
160
|
+
state.set({ books: undefined }, { refreshing: true });
|
|
161
|
+
// Then it doesn't blow up, and stays []
|
|
162
|
+
expect(state.books.value).toEqual([]);
|
|
163
|
+
expect(state.value).toEqual({ firstName: "a1", books: [] });
|
|
164
|
+
// And it's not considered changed
|
|
165
|
+
expect(state.books.dirty).toBe(false);
|
|
166
|
+
expect(state.books.originalValue).toEqual([]);
|
|
167
|
+
});
|
|
155
168
|
it("list value can observe changes", () => {
|
|
156
169
|
const b1 = { title: "t1" };
|
|
157
170
|
const a1 = { firstName: "a1", books: [b1] };
|
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,
|
|
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");
|