Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add init.onlyOnce. #111

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/useFormState.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { click, clickAndWait, render, typeAndWait, wait } from "@homebound/rtl-utils";
import { act } from "@testing-library/react";
import { makeAutoObservable, reaction } from "mobx";
import { Observer } from "mobx-react";
import { useMemo, useState } from "react";
import { observer, Observer } from "mobx-react";
import { useMemo, useRef, useState } from "react";
import { TextField } from "src/FormStateApp";
import { ObjectConfig } from "src/config";
import { ObjectState } from "src/fields/objectField";
Expand Down Expand Up @@ -117,6 +117,31 @@ describe("useFormState", () => {
expect(r.baseElement.textContent).toEqual("");
});

it("uses init.onlyOnce to not react to identity changes", async () => {
// Given a component
type FormValue = Pick<AuthorInput, "firstName">;
const config: ObjectConfig<FormValue> = { firstName: { type: "value" } };
const TestComponent = observer(() => {
// And we have an `init` value that is dynamic, so we can observe whether init reruns
const renderCount = useRef(0).current++;
const form = useFormState({
config,
init: { input: { firstName: `${renderCount}` }, onlyOnce: true },
});
const [, setTick] = useState(0);
return (
<div>
<button data-testid="change" onClick={() => setTick(1)} />
<div data-testid="firstName">{form.firstName.value}</div>
</div>
);
});
const r = await render(<TestComponent />);
expect(r.firstName).toHaveTextContent("0");
click(r.change);
expect(r.firstName).toHaveTextContent("0");
});

it("keeps local changed values when a query refreshes", async () => {
// Given a component
function TestComponent() {
Expand Down
9 changes: 8 additions & 1 deletion src/useFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type InputAndMap<T, I> = {
input: I;
map?: (input: Exclude<I, null | undefined>) => T;
ifUndefined?: T;
onlyOnce?: boolean;
};

export type QueryAndMap<T, I> = {
Expand Down Expand Up @@ -98,7 +99,13 @@ export function useFormState<T, I>(opts: UseFormStateOpts<T, I>): ObjectState<T>
const [firstInitValue] = useState(() => initValue(config, init));
const isWrappingMobxProxy = !isPlainObject(firstInitValue);
// If they're using init.input, useMemo on it (and it might be an array), otherwise allow the identity of init be unstable
const dep = isInput(init) ? makeArray(init.input) : isQuery(init) ? [init.query.data, init.query.loading] : [];
const dep = isInput(init)
? init.onlyOnce
? []
: makeArray(init.input)
: isQuery(init)
? [init.query.data, init.query.loading]
: [];

const form = useMemo(
() => {
Expand Down
Loading