Skip to content

Commit

Permalink
revert useField and unregisterField changes from v1.66.1 (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Hodgson authored Apr 21, 2021
1 parent d8b3688 commit 7732120
Show file tree
Hide file tree
Showing 5 changed files with 7,501 additions and 24,874 deletions.
6 changes: 0 additions & 6 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@ function Form(_props) {
};
const unregisterField = (name) => {
delete fields.current[name];

setState((state) =>
deletePath(state, `errors.${name}`, {
deleteEmptyObjects: { except: ["errors"] },
})
);
};
const providerValue = {
state,
Expand Down
69 changes: 0 additions & 69 deletions src/components/Form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ const hungryOptions = [
value: "maybe",
},
];
const yesNoOptions = [
{
label: "Yes",
value: "yes",
},
{
label: "No",
value: "no",
},
];

function SimpleForm({ testId }) {
const initialValues = {
Expand Down Expand Up @@ -344,65 +334,6 @@ describe("Form", () => {
).toBeInTheDocument();
});

it("with hidden fields", async () => {
const onSubmit = jest.fn();
const { container } = render(
<Form
initialValues={{ hasMiddleName: "", middleName: "" }}
onSubmit={onSubmit}
>
{({ state }) => (
<>
<RadioGroup
name="hasMiddleName"
label="Do you have a middle name?"
options={yesNoOptions}
/>
{state.values.hasMiddleName === "yes" && (
<Input name="middleName" label="Middle name" />
)}
<Button type="submit">Submit</Button>
</>
)}
</Form>
);

const yesInput = screen.getByLabelText("Yes");
const yesLabel = container.querySelector(
`label[for="${yesInput.getAttribute("id")}"]`
);
const noInput = screen.getByLabelText("No");
const noLabel = container.querySelector(
`label[for="${noInput.getAttribute("id")}"]`
);

userEvent.click(yesLabel);

const middleNameInput = screen.getByLabelText("Middle name");
middleNameInput.focus();
middleNameInput.blur();

// Wait until form state is updated with the error.
// Without this await, the test ends before form's state gets the error.
await waitFor(() => {
expect(screen.getByText("Required")).toBeInTheDocument();
});

userEvent.click(noLabel);
screen.getByRole("button", { name: "Submit" }).click();

await waitFor(() =>
expect(onSubmit).toBeCalledWith({
errors: {},
values: {
hasMiddleName: "no",
middleName: "",
},
setErrors: expect.any(Function),
})
);
});

it("with testId", () => {
const { container } = render(<SimpleForm testId="my-form" />);

Expand Down
37 changes: 10 additions & 27 deletions src/hooks/internal/useField.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react";
import { useEffect } from "react";
import useForm from "./useForm";
import { getPath } from "../../utils/objectPath";
import { notStringOrEmpty } from "../../utils/string";
Expand All @@ -18,21 +18,6 @@ function useField(componentName, { name, disabled, optional, validate, data }) {
unregisterField,
} = useForm(componentName);

// We use this ref to ensure that registerField and unregisterField are called only
// on component mount and unmount. In other words, we want to ensure that the useEffect
// below has an empty dependency array.
const registerDataRef = useRef();

registerDataRef.current = {
registerField,
unregisterField,
name,
disabled,
optional,
validate,
data,
};

if (typeof state.values === "undefined") {
throw new Error("Form is missing initialValues");
}
Expand All @@ -47,16 +32,6 @@ function useField(componentName, { name, disabled, optional, validate, data }) {
const hasErrors = Array.isArray(errors) && errors.length > 0;

useEffect(() => {
const {
registerField,
unregisterField,
name,
disabled,
optional,
validate,
data,
} = registerDataRef.current;

registerField(name, {
disabled,
optional,
Expand All @@ -67,7 +42,15 @@ function useField(componentName, { name, disabled, optional, validate, data }) {
return () => {
unregisterField(name);
};
}, []);
}, [
name,
disabled,
optional,
validate,
data,
registerField,
unregisterField,
]);

return {
value,
Expand Down
Loading

1 comment on commit 7732120

@vercel
Copy link

@vercel vercel bot commented on 7732120 Apr 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.