@homebound/form-state 2.19.0 → 2.20.1

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.
@@ -13,7 +13,7 @@ var Color;
13
13
  Color["Red"] = "RED";
14
14
  Color["Blue"] = "BLUE";
15
15
  Color["Green"] = "GREEN";
16
- })(Color = exports.Color || (exports.Color = {}));
16
+ })(Color || (exports.Color = Color = {}));
17
17
  class DateOnly {
18
18
  constructor(date) {
19
19
  this.date = date;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { ObjectConfig } from "./config";
2
2
  export { ListFieldState } from "./fields/listField";
3
- export { createObjectState, ObjectState } from "./fields/objectField";
4
- export { FieldState } from "./fields/valueField";
5
- export { required, Rule } from "./rules";
3
+ export { ObjectState, createObjectState } from "./fields/objectField";
4
+ export { FieldState, ValueAdapter } from "./fields/valueField";
5
+ export { Rule, required } from "./rules";
6
6
  export { useFormState } from "./useFormState";
7
7
  export { useFormStates } from "./useFormStates";
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Creates a new combined object with keys in `overrides` taking precedence, and then
3
+ * any other keys falling back to `delegate`.
4
+ */
5
+ export declare function newDelegateProxy<T extends object, O extends object>(delegate: T, overrides: O): Omit<T, keyof O> & O;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.newDelegateProxy = void 0;
4
+ /**
5
+ * Creates a new combined object with keys in `overrides` taking precedence, and then
6
+ * any other keys falling back to `delegate`.
7
+ */
8
+ function newDelegateProxy(delegate, overrides) {
9
+ function pickTarget(key) {
10
+ return Reflect.has(overrides, key) ? overrides : delegate;
11
+ }
12
+ return new Proxy(delegate, {
13
+ get(object, key) {
14
+ return Reflect.get(pickTarget(key), key);
15
+ },
16
+ set(object, key, value) {
17
+ return Reflect.set(pickTarget(key), key, value);
18
+ },
19
+ });
20
+ }
21
+ exports.newDelegateProxy = newDelegateProxy;
package/dist/rules.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /** A validation rule, given the value and name, return the error string if valid, or undefined if valid. */
2
- export declare type Rule<V> = (opts: {
2
+ export type Rule<V> = (opts: {
3
3
  value: V;
4
4
  key: string;
5
5
  originalValue: V;
package/dist/rules.js CHANGED
@@ -5,7 +5,7 @@ const mobx_1 = require("mobx");
5
5
  /** A rule that validates `value` is not `undefined`, `null`, or empty string. */
6
6
  // We pre-emptively make this a mobx action so that it's identity doesn't change when proxied
7
7
  // and breaks our ability to do `rules.some(r => r === required)`.
8
- exports.required = mobx_1.action(({ value: v }) => {
8
+ exports.required = (0, mobx_1.action)(({ value: v }) => {
9
9
  const isEmptyString = typeof v === "string" ? v.trim() === "" : false;
10
10
  return v !== undefined && v !== null && !isEmptyString ? undefined : "Required";
11
11
  });
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  require("@testing-library/jest-dom");
4
4
  const mobx_1 = require("mobx");
5
5
  // formState doesn't use actions
6
- mobx_1.configure({ enforceActions: "never" });
6
+ (0, mobx_1.configure)({ enforceActions: "never" });
7
7
  beforeEach(() => {
8
- jest.useFakeTimers("modern");
8
+ jest.useFakeTimers();
9
9
  });
10
10
  afterEach(() => {
11
11
  jest.runOnlyPendingTimers();
@@ -1,21 +1,21 @@
1
1
  import { ObjectConfig } from "./config";
2
2
  import { ObjectState } from "./fields/objectField";
3
- export declare type Query<I> = {
3
+ export type Query<I> = {
4
4
  data: I;
5
5
  loading: boolean;
6
6
  error?: any;
7
7
  };
8
- export declare type InputAndMap<T, I> = {
8
+ export type InputAndMap<T, I> = {
9
9
  input: I;
10
10
  map: (input: Exclude<I, null | undefined>) => T;
11
11
  ifUndefined?: T;
12
12
  };
13
- export declare type QueryAndMap<T, I> = {
13
+ export type QueryAndMap<T, I> = {
14
14
  query: Query<I>;
15
15
  map: (input: Exclude<I, null | undefined>) => T;
16
16
  ifUndefined?: T;
17
17
  };
18
- export declare type UseFormStateOpts<T, I> = {
18
+ export type UseFormStateOpts<T, I> = {
19
19
  /** The form configuration, should be a module-level const or useMemo'd. */
20
20
  config: ObjectConfig<T>;
21
21
  /**
@@ -18,15 +18,15 @@ let pendingAutoSave = false;
18
18
  function useFormState(opts) {
19
19
  const { config, init, addRules, readOnly = false, loading, autoSave } = opts;
20
20
  // Use a ref so our memo'ized `onBlur` always see the latest value
21
- const autoSaveRef = react_1.useRef(autoSave);
21
+ const autoSaveRef = (0, react_1.useRef)(autoSave);
22
22
  autoSaveRef.current = autoSave;
23
- const firstRunRef = react_1.useRef(true);
23
+ const firstRunRef = (0, react_1.useRef)(true);
24
24
  // This is a little weird, but we need to know ahead of time, before the form useMemo, if we're working with classes/mobx proxies
25
- const [firstInitValue] = react_1.useState(() => utils_1.initValue(config, init));
26
- const isWrappingMobxProxy = !is_plain_object_1.isPlainObject(firstInitValue);
25
+ const [firstInitValue] = (0, react_1.useState)(() => (0, utils_1.initValue)(config, init));
26
+ const isWrappingMobxProxy = !(0, is_plain_object_1.isPlainObject)(firstInitValue);
27
27
  // If they're using init.input, useMemo on it (and it might be an array), otherwise allow the identity of init be unstable
28
- const dep = utils_1.isInput(init) ? makeArray(init.input) : utils_1.isQuery(init) ? [init.query.data, init.query.loading] : [];
29
- const form = react_1.useMemo(() => {
28
+ const dep = (0, utils_1.isInput)(init) ? makeArray(init.input) : (0, utils_1.isQuery)(init) ? [init.query.data, init.query.loading] : [];
29
+ const form = (0, react_1.useMemo)(() => {
30
30
  function maybeAutoSave() {
31
31
  if (isAutoSaving === "in-flight") {
32
32
  pendingAutoSave = true;
@@ -64,8 +64,8 @@ function useFormState(opts) {
64
64
  }, 0);
65
65
  }
66
66
  }
67
- const value = firstRunRef.current ? firstInitValue : utils_1.initValue(config, init);
68
- const form = objectField_1.createObjectState(config, value, { maybeAutoSave });
67
+ const value = firstRunRef.current ? firstInitValue : (0, utils_1.initValue)(config, init);
68
+ const form = (0, objectField_1.createObjectState)(config, value, { maybeAutoSave });
69
69
  form.readOnly = readOnly;
70
70
  setLoading(form, opts);
71
71
  // The identity of `addRules` is not stable, but assume that it is for better UX.
@@ -78,7 +78,7 @@ function useFormState(opts) {
78
78
  [config, ...(isWrappingMobxProxy ? dep : [])]);
79
79
  // We use useEffect so that any mutations to the proxies, which will call `setState`s on any observers to
80
80
  // queue their components' render), don't happen during our render, per https://fb.me/setstate-in-render.
81
- react_1.useEffect(() => {
81
+ (0, react_1.useEffect)(() => {
82
82
  // Ignore the 1st run b/c our 1st useMemo already initialized `form` with the current `init` value.
83
83
  // Also for mobx proxies, we recreate a new form-state every time init changes, so that our
84
84
  // fields fundamentally pointing to the right proxy. So just defer to the ^ useMemo.
@@ -93,10 +93,10 @@ function useFormState(opts) {
93
93
  // rows in the table that our side panel's form has open/is focused on" (which ideally would
94
94
  // be treated as a full component remount by having an `key` field somewhere in the parent
95
95
  // component, but it's unlikely the user will always remember to do this).
96
- form.set(utils_1.initValue(config, init), { refreshing: true });
96
+ form.set((0, utils_1.initValue)(config, init), { refreshing: true });
97
97
  }, [form, ...dep]);
98
98
  // Use useEffect so that we don't touch the form.init proxy during a render
99
- react_1.useEffect(() => {
99
+ (0, react_1.useEffect)(() => {
100
100
  form.readOnly = readOnly;
101
101
  if (loading !== undefined) {
102
102
  form.loading = loading;
@@ -111,11 +111,11 @@ function setLoading(form, opts) {
111
111
  // Prefer the explicit/top-level opts.loading if it's set
112
112
  form.loading = loading;
113
113
  }
114
- else if (utils_1.isInput(init) && !init.ifUndefined) {
114
+ else if ((0, utils_1.isInput)(init) && !init.ifUndefined) {
115
115
  // Otherwise, check for `init.input`
116
116
  form.loading = init.input === undefined;
117
117
  }
118
- else if (utils_1.isQuery(init)) {
118
+ else if ((0, utils_1.isQuery)(init)) {
119
119
  // Or `query.loading`
120
120
  form.loading = init.query.loading;
121
121
  }