Skip to content

Commit

Permalink
Fix useFormStates.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenh committed Dec 8, 2023
1 parent b73ae88 commit a7a4d9f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/useFormStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function useFormStates<T, I = T>(opts: UseFormStatesOpts<T, I>): UseFormS

// If it didn't exist, then add to the cache.
if (!form) {
form = createObjectState(config, initValue(config, map ? { map, input } : input), {
form = createObjectState(config, initValue(config, { map, input }), {
maybeAutoSave: () => maybeAutoSave(form),
});
if (addRules) {
Expand All @@ -120,7 +120,7 @@ export function useFormStates<T, I = T>(opts: UseFormStatesOpts<T, I>): UseFormS

// If the source of truth changed, then update the existing state and return it.
if (existing && existing[1] !== input) {
(form as any as ObjectStateInternal<any>).set(initValue(config, map ? { map, input } : input), {
(form as any as ObjectStateInternal<any>).set(initValue(config, { map, input }), {
refreshing: true,
});
existing[1] = input;
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function assertNever(x: never): never {
throw new Error("Unexpected object: " + x);
}

/** Introspects the `init` prop to see if has a `map` function/etc. and returns the form value. */
/** Introspects the `init` prop to see if it has a `map` function/etc. and returns the form value. */
export function initValue<T>(config: ObjectConfig<T>, init: any): T {
let value: any;
if (isInput(init)) {
Expand All @@ -47,6 +47,7 @@ export function initValue<T>(config: ObjectConfig<T>, init: any): T {
} else {
throw new Error("init must have an input or query key");
}
// Given our form config, pick out only the subset of fields out of `value` (unless it's a mobx class)
return pickFields(config, value ?? {}) as T;
}

Expand Down

0 comments on commit a7a4d9f

Please sign in to comment.