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

Make context accessible in onSubmit #40

Merged
merged 1 commit into from
Oct 29, 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
5 changes: 5 additions & 0 deletions .changeset/light-radios-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"signal-form": patch
---

Add context in onSubmit callback
15 changes: 11 additions & 4 deletions lib/create-form-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FormEventHandler } from "react";
import type { SyntheticEvent } from "react";
import type { ReadonlySignal, Signal } from "@preact/signals-react";
import { signal } from "@preact/signals-react";
import type { AnyObjectSchema, InferType } from "yup";
Expand All @@ -7,14 +7,19 @@ import type { ValidationResult, ValidationError } from "~/utils/validate";
import { validateSync } from "~/utils/validate";
import type { DeepPartial } from "./utils/deep-partial";

export type OnSubmitHandler = (
event: SyntheticEvent<HTMLFormElement>,
context: FormContext<any>
) => void;

type CreateFormContextOptions<S extends AnyObjectSchema> = {
submittedErrors?: ValidationError[];
submittedData?: any;
defaultData?: DeepPartial<InferType<S>>;
data?: Signal<InferType<S>>;
schema: ReadonlySignal<S | undefined>;
id: string;
onSubmit?: FormEventHandler<HTMLFormElement>;
onSubmit?: OnSubmitHandler;
formRef: React.RefObject<HTMLFormElement>;
};

Expand Down Expand Up @@ -56,7 +61,7 @@ export function createFormContext<S extends AnyObjectSchema>({
}
}

return {
let context: FormContext<S> = {
data,
result,
errors,
Expand All @@ -66,7 +71,7 @@ export function createFormContext<S extends AnyObjectSchema>({
formRef,
validate,
onSubmit(event) {
onSubmit?.(event);
onSubmit?.(event, context);
if (!event.isPropagationStopped()) {
didSubmit.value = true;
let validationResult = validate();
Expand All @@ -92,4 +97,6 @@ export function createFormContext<S extends AnyObjectSchema>({
result.value = undefined;
},
};

return context;
}
4 changes: 3 additions & 1 deletion lib/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useSignal, type Signal } from "@preact/signals-react";
import type { AnyObjectSchema, InferType } from "yup";
import { FieldsContext, FormContext } from "~/context";
import { type ValidationError } from "~/utils/validate";
import type { OnSubmitHandler } from "./create-form-context";
import { createFormContext } from "./create-form-context";
import type { DeepPartial } from "./utils/deep-partial";
import { useForwardedRef } from "~/utils/use-forwarded-ref";
Expand All @@ -15,7 +16,8 @@ export type FormProps<S extends AnyObjectSchema> = {
data?: Signal<InferType<S>>;
submittedData?: DeepPartial<InferType<S>>;
submittedErrors?: ValidationError[];
} & FormHTMLAttributes<HTMLFormElement>;
onSubmit?: OnSubmitHandler;
} & Omit<FormHTMLAttributes<HTMLFormElement>, "onSubmit">;

export const Form = forwardRef(
<S extends AnyObjectSchema>(
Expand Down
4 changes: 3 additions & 1 deletion lib/remix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useSignal, type Signal } from "@preact/signals-react";
import type { AnyObjectSchema, InferType } from "yup";
import { FieldsContext, FormContext, useFormContext } from "~/context";
import type { ErrorActionData, ValidationErrorResult } from "~/utils/validate";
import type { OnSubmitHandler } from "./create-form-context";
import { createFormContext } from "./create-form-context";
import type { DeepPartial } from "./utils/deep-partial";
import { json } from "@remix-run/node";
Expand All @@ -23,7 +24,8 @@ export type FormProps<S extends AnyObjectSchema> = {
schema?: S;
defaultData?: DeepPartial<InferType<S>>;
data?: Signal<InferType<S>>;
} & RemixFormProps;
onSubmit?: OnSubmitHandler;
} & Omit<RemixFormProps, "onSubmit">;

export const Form = forwardRef(
<S extends AnyObjectSchema>(
Expand Down
Loading