Skip to content

Commit

Permalink
Merge pull request #91 from iway1/bugfix/form-and-default-values-prop
Browse files Browse the repository at this point in the history
now supports passing both form and default values
  • Loading branch information
iway1 authored Mar 14, 2023
2 parents 5d3b6f6 + 342ef57 commit e80f5df
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/__tests__/createSchemaForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,63 @@ 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
7 changes: 7 additions & 0 deletions src/createSchemaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
FunctionComponent,
ReactNode,
RefAttributes,
useEffect,
useRef,
} from "react";
import { ComponentProps } from "react";
Expand Down Expand Up @@ -363,6 +364,12 @@ export function createTsForm<
});
return uf;
})();

useEffect(() => {
if (form && defaultValues) {
form.reset(defaultValues);
}
}, []);
const { control, handleSubmit, setError } = _form;
const _schema = unwrapEffects(schema);
const shape: Record<string, RTFSupportedZodTypes> = _schema._def.shape();
Expand Down

1 comment on commit e80f5df

@vercel
Copy link

@vercel vercel bot commented on e80f5df 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.