Skip to content

Instantly share code, notes, and snippets.

@FleetAdmiralJakob
Created March 30, 2024 18:31
Show Gist options
  • Select an option

  • Save FleetAdmiralJakob/d9654dc186ea1b96f3a6d9184fe6b95c to your computer and use it in GitHub Desktop.

Select an option

Save FleetAdmiralJakob/d9654dc186ea1b96f3a6d9184fe6b95c to your computer and use it in GitHub Desktop.
shadcn/ui error message
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { Button } from "~/components/ui/button";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormMessage,
} from "~/components/ui/form";
import { Input } from "~/components/ui/input";
import React from "react";
import { formSchema } from "~/lib/validators";
export function SignInForm() {
const [isLoading, setIsLoading] = React.useState(false);
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
username: "",
},
});
async function onSubmit(values: z.infer<typeof formSchema>) {
setIsLoading(true);
console.log(values);
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="bg space-y-8">
<div className="flex">
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem className="w-2/3">
<FormControl>
<Input
placeholder="Username"
maxLength={20}
{...field}
onChange={(e) =>
field.onChange(
e.target.value
.toLowerCase()
.replace(" ", "")
.substring(0, 20),
)
}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="usernameId"
render={({ field }) => (
<FormItem className="w-1/3">
<FormControl>
<Input
type="number"
placeholder="01010"
maxLength={5}
{...field}
onChange={(e) => field.onChange(e.target.value.slice(0, 5))}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<span className="text-sm text-muted-foreground">
This is your display name and the way friends can add you. It consists
of the username and a ID.
</span>
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem className="flex-2">
<FormControl>
<Input
placeholder="Full Name"
type="text"
maxLength={20}
{...field}
onChange={(e) =>
field.onChange(e.target.value.substring(0, 20))
}
/>
</FormControl>
<FormDescription>
This is optional, so you can stay anonymous.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem className="flex-2">
<FormControl>
<Input
placeholder="Password"
type="password"
maxLength={20}
{...field}
onChange={(e) =>
field.onChange(e.target.value.substring(0, 20))
}
/>
</FormControl>
<FormDescription>
Password must be at least 8 characters.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit" aria-disabled={isLoading}>
Submit
</Button>
</form>
</Form>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment