Skip to content

feat: add forms related components #7

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

Merged
merged 1 commit into from
Feb 18, 2025
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
4 changes: 3 additions & 1 deletion components.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"aliases": {
"components": "@/components",
"utils": "@/lib/utils",
"lib": "@/lib"
"lib": "@/lib",
"ui": "@/components",
"hooks": "@/hooks"
},
"iconLibrary": "lucide"
}
75 changes: 57 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@
"vitest": "^3.0.4"
},
"dependencies": {
"@radix-ui/react-label": "^2.1.1",
"@radix-ui/react-slot": "^1.1.1",
"@hookform/resolvers": "^4.1.0",
"@radix-ui/react-label": "^2.1.2",
"@radix-ui/react-slot": "^1.1.2",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"lucide-react": "^0.474.0",
"tailwind-merge": "^2.6.0"
"react-hook-form": "^7.54.2",
"tailwind-merge": "^2.6.0",
"zod": "^3.24.2"
}
}
70 changes: 70 additions & 0 deletions src/components/form.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { Meta, StoryObj } from "@storybook/react";
import { fn } from "@storybook/test";

import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { Button } from "./button";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "./form";
import { Input } from "./input";

const meta = {
title: "Form",
parameters: {
layout: "centered",
},
} satisfies Meta<typeof Form>;

export default meta;
type Story = StoryObj<typeof meta>;

const formSchema = z.object({
username: z.string().min(2, {
message: "Username must be at least 2 characters.",
}),
});

export const Example: Story = {
render() {
const form = useForm({
resolver: zodResolver(formSchema),
defaultValues: {
username: "",
},
});

const onSubmit = fn();

return (
<Form {...form}>
<form className="space-y-8" onSubmit={form.handleSubmit(onSubmit)}>
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="Enter username" {...field} />
</FormControl>
<FormDescription>
This is your public display name.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Submit</Button>
</form>
</Form>
);
},
};
Loading