Replies: 3 comments 3 replies
-
Why can't you just add onClick={onSubmit} to the button in the dialog? Like you could do form.trigger() to trigger/check for errors, then if there aren't any errors you do onSubmit. onClick={() => {
form.trigger()
if (form.formState.isValid) onSubmit()
}} |
Beta Was this translation helpful? Give feedback.
1 reply
-
Shadcn has a bug, if you add an AlertDialog inside a form, the action does not propagate submit |
Beta Was this translation helpful? Give feedback.
2 replies
-
any solutions? import { zodResolver } from "@hookform/resolvers/zod";
import type { AlertDialogProps } from "@radix-ui/react-alert-dialog";
import type { FC } from "react";
import { useForm } from "react-hook-form";
import {
AlertDialog,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import type { LicenseType } from "@/types/licenses.types";
import { type LicenseSchemaType, licenseSchema } from "./_schema";
const LicensesDialog: FC<
{
type: "add" | "remove";
licenseTypes: LicenseType[];
} & AlertDialogProps
> = ({ type, licenseTypes, ...props }) => {
const form = useForm<LicenseSchemaType>({
defaultValues: {
amount: 1,
licenseType: "",
},
resolver: zodResolver(licenseSchema),
});
console.log(form.formState.errors);
return (
<Form {...form}>
<form onSubmit={form.handleSubmit((values) => console.log(values))}>
<AlertDialog {...props}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
<span className="capitalize">{type} Licenses</span>
</AlertDialogTitle>
<AlertDialogDescription>
{type === "add"
? "Specify how many licenses you want to add and their type."
: "Specify how many licenses you want to remove and their type."}
</AlertDialogDescription>
</AlertDialogHeader>
<main className="space-y-6">
<div>
<FormField
control={form.control}
name="amount"
render={({ field }) => (
<FormItem>
<FormLabel>License</FormLabel>
<FormControl>
<Input
{...field}
value={field.value ?? ""}
placeholder="Enter license number"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div>
<FormField
control={form.control}
name="licenseType"
render={({ field }) => (
<FormItem>
<FormLabel>License Type</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl>
<SelectTrigger className="w-full">
<SelectValue placeholder="Select license" />
</SelectTrigger>
</FormControl>
<SelectContent>
{licenseTypes.map((type) => (
<SelectItem key={type.id} value={type.key}>
{type.name}
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
</div>
</main>
<AlertDialogFooter>
<Button
onClick={() => {
if (props.onOpenChange) {
props.onOpenChange(false);
}
}}
variant={"outline"}
>
Cancel
</Button>
<Button type="submit">{type === "add" ? "Add" : "Remove"}</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</form>
</Form>
);
};
export default LicensesDialog; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've been having trouble attempting to use the Shadcn react-hook form wrapper with a dialog hook. the behavior im trying to implement is to trigger the onSubmit (or any action, such as passing data back to the parent component) from the button in the dialog.
ive found a couple of articles like this, but none of these approaches seem to work.
https://bdaneesha.medium.com/react-submit-form-using-button-in-parent-component-ii-d58c43abcfc8
any ideas?
Beta Was this translation helpful? Give feedback.
All reactions