Skip to content

Commit

Permalink
fix: Fix focused fields ignoring incoming values. (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenh authored Dec 3, 2021
1 parent f129f75 commit 2f21e42
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/formState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ describe("formState", () => {
// And they have wip edits
formState.firstName.set("ff");
// When a mutation result sets both firstName and lastName
formState.set({ firstName: "f2", lastName: "l2" });
(formState as any).set({ firstName: "f2", lastName: "l2" }, { refreshing: true });
// Then we don't overwrite the user's WIP work
expect(formState.firstName.value).toEqual("ff");
expect(formState.lastName.value).toEqual("l2");
Expand All @@ -1101,7 +1101,7 @@ describe("formState", () => {
// Given first name is focused, but has not changed
formState.firstName.focus();
// When a mutation result sets both firstName and lastName
formState.set({ firstName: "f2", lastName: "l2" });
(formState as any).set({ firstName: "f2", lastName: "l2" }, { refreshing: true });
// Then we do update the value
expect(formState.firstName.value).toEqual("f2");
});
Expand Down
8 changes: 5 additions & 3 deletions src/formState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ function newObjectState<T, P = any>(
throw new Error(`${key || "formState"} is currently readOnly`);
}
getFields(this).forEach((field) => {
if (field.key in value && (!field.dirty || !field._focused)) {
if (field.key in value) {
field.set((value as any)[field.key], opts);
}
});
Expand Down Expand Up @@ -673,8 +673,10 @@ function newValueFieldState<T, K extends keyof T>(
throw new Error(`${key} is currently readOnly`);
}

if (opts.refreshing && this.dirty && value !== this.value) {
// Ignore refreshed values if we're already dirty
if (opts.refreshing && this.dirty && this.value !== value) {
// Ignore incoming values if we have changes (this.dirty) unless our latest change (this.value)
// matches the incoming value (value), b/c if it does we should accept it and reset originalValue
// so that we're not longer dirty.
return;
} else if (computed && (opts.resetting || opts.refreshing)) {
// Computeds can't be either reset or refreshed
Expand Down

0 comments on commit 2f21e42

Please sign in to comment.