Skip to content

Commit

Permalink
Merge pull request #90 from iway1/revert-89-bugfig/form-prop-with-def…
Browse files Browse the repository at this point in the history
…ault-values

Revert "fixes issue when passing defaultValues and 'form' prop"
  • Loading branch information
iway1 authored Mar 14, 2023
2 parents d909692 + f33cfea commit 5d3b6f6
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 67 deletions.
57 changes: 0 additions & 57 deletions src/__tests__/createSchemaForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1059,63 +1059,6 @@ describe("createSchemaForm", () => {
expect(screen.queryByText("req")).not.toBeInTheDocument();
expect(mockOnSubmit).toHaveBeenCalledWith({ number: 5 });
});
it("should be possible to pass 'defaultValues' prop and 'form' prop and apply the default values.", async () => {
const mockOnSubmit = jest.fn();
function Input() {
const {
field: { onChange, value },
error,
} = useTsController<number>();
const [_, setRerender] = useState(0);
return (
<>
<input
value={value !== undefined ? value + "" : ""}
onChange={(e) => {
const value = parseInt(e.target.value);
if (isNaN(value)) onChange(undefined);
else onChange(value);
}}
placeholder={"input"}
/>
<button type={"button"} onClick={() => setRerender((old) => old + 1)}>
rerender button
</button>
{error?.errorMessage && <span>{error.errorMessage}</span>}
</>
);
}

function Outer() {
const form = useForm<any>();

return (
<Form
onSubmit={mockOnSubmit}
schema={z.object({
number: z.number({ required_error: "req" }),
})}
form={form}
defaultValues={defaultValues}
renderAfter={() => <button>submit</button>}
/>
);
}

const mapping = [[z.number(), Input]] as const;
const Form = createTsForm(mapping);
const defaultValues = {
number: 5,
};

render(<Outer />);

const button = screen.getByText("submit");
await userEvent.click(button);

expect(screen.queryByText("req")).not.toBeInTheDocument();
expect(mockOnSubmit).toHaveBeenCalledWith({ number: 5 });
});
it("should render the correct component when a schema created with createSchemaForm is optional", () => {
const StringSchema = createUniqueFieldSchema(z.string(), "string");
const NumberSchema = createUniqueFieldSchema(z.number(), "number");
Expand Down
11 changes: 1 addition & 10 deletions src/createSchemaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -355,17 +355,8 @@ export function createTsForm<
throw new Error(useFormResultValueChangedErrorMesssage());
}
const resolver = zodResolver(schema);
const hasSetDefaultValues = useRef(false);
// need to manually reset the form state in case 'defaultValues' and 'form' are passed
if (form && defaultValues && !hasSetDefaultValues.current) {
form.reset(defaultValues);
hasSetDefaultValues.current = true;
}
const _form = (() => {
if (form) {
return form;
}

if (form) return form;
const uf = useForm({
resolver,
defaultValues,
Expand Down

1 comment on commit 5d3b6f6

@vercel
Copy link

@vercel vercel bot commented on 5d3b6f6 Mar 14, 2023

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.