From 45472bd0b17482b06368dd24b2a879f486e28a42 Mon Sep 17 00:00:00 2001 From: sheng <49238630+zsh-eng@users.noreply.github.com> Date: Sat, 20 Apr 2024 12:06:54 +0800 Subject: [PATCH 01/11] feat: add generic form inputs --- package.json | 2 + pnpm-lock.yaml | 23 ++++ src/components/form/form-input.tsx | 52 ++++++++ src/components/form/form-textarea.tsx | 55 ++++++++ src/components/form/input.types.ts | 8 ++ src/components/ui/form.tsx | 177 ++++++++++++++++++++++++++ 6 files changed, 317 insertions(+) create mode 100644 src/components/form/form-input.tsx create mode 100644 src/components/form/form-textarea.tsx create mode 100644 src/components/form/input.types.ts create mode 100644 src/components/ui/form.tsx diff --git a/package.json b/package.json index 87791ea..d2d5ae7 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "db:seed": "tsx --env-file=.env src/seed.ts" }, "dependencies": { + "@hookform/resolvers": "^3.3.4", "@libsql/client": "^0.6.0", "@milkdown/core": "^7.3.6", "@milkdown/ctx": "^7.3.6", @@ -53,6 +54,7 @@ "next-themes": "^0.3.0", "react": "^18", "react-dom": "^18", + "react-hook-form": "^7.51.3", "sonner": "^1.4.41", "superjson": "^2.2.1", "tailwind-merge": "^2.2.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index efa0b97..ff0df6e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + '@hookform/resolvers': + specifier: ^3.3.4 + version: 3.3.4(react-hook-form@7.51.3) '@libsql/client': specifier: ^0.6.0 version: 0.6.0 @@ -125,6 +128,9 @@ dependencies: react-dom: specifier: ^18 version: 18.2.0(react@18.2.0) + react-hook-form: + specifier: ^7.51.3 + version: 7.51.3(react@18.2.0) sonner: specifier: ^1.4.41 version: 1.4.41(react-dom@18.2.0)(react@18.2.0) @@ -706,6 +712,14 @@ packages: resolution: {integrity: sha512-9TANp6GPoMtYzQdt54kfAyMmz1+osLlXdg2ENroU7zzrtflTLrrC/lgrIfaSe+Wu0b89GKccT7vxXA0MoAIO+Q==} dev: false + /@hookform/resolvers@3.3.4(react-hook-form@7.51.3): + resolution: {integrity: sha512-o5cgpGOuJYrd+iMKvkttOclgwRW86EsWJZZRC23prf0uU2i48Htq4PuT73AVb9ionFyZrwYEITuOFGF+BydEtQ==} + peerDependencies: + react-hook-form: ^7.0.0 + dependencies: + react-hook-form: 7.51.3(react@18.2.0) + dev: false + /@humanwhocodes/config-array@0.11.14: resolution: {integrity: sha512-3T8LkOmg45BV5FICb15QQMsyUSWrQ8AygVfC7ZG32zOalnqrilm018ZVCw0eapXux8FtA33q8PSRSstjee3jSg==} engines: {node: '>=10.10.0'} @@ -5175,6 +5189,15 @@ packages: scheduler: 0.23.0 dev: false + /react-hook-form@7.51.3(react@18.2.0): + resolution: {integrity: sha512-cvJ/wbHdhYx8aviSWh28w9ImjmVsb5Y05n1+FW786vEZQJV5STNM0pW6ujS+oiBecb0ARBxJFyAnXj9+GHXACQ==} + engines: {node: '>=12.22.0'} + peerDependencies: + react: ^16.8.0 || ^17 || ^18 + dependencies: + react: 18.2.0 + dev: false + /react-is@16.13.1: resolution: {integrity: sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==} dev: true diff --git a/src/components/form/form-input.tsx b/src/components/form/form-input.tsx new file mode 100644 index 0000000..1a3105f --- /dev/null +++ b/src/components/form/form-input.tsx @@ -0,0 +1,52 @@ +import { FormInputProps } from "@/components/form/input.types"; +import { + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { FieldValues } from "react-hook-form"; + +type FormTextInputProps = + FormInputProps & { + placeholder?: string; + description?: string; + className?: string; + }; + +export function FormTextInput({ + form, + name, + label, + disabled, + placeholder, + description, + className, +}: FormTextInputProps) { + return ( + ( + + {label && {label}} + + + + {description && {description}} + + + )} + /> + ); +} + +FormTextInput.displayName = "FormTextInput"; diff --git a/src/components/form/form-textarea.tsx b/src/components/form/form-textarea.tsx new file mode 100644 index 0000000..6ef4ae5 --- /dev/null +++ b/src/components/form/form-textarea.tsx @@ -0,0 +1,55 @@ +import { FormInputProps } from "@/components/form/input.types"; +import { + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Textarea } from "@/components/ui/textarea"; +import { FieldValues } from "react-hook-form"; + +type FormTextareaProps = + FormInputProps & { + placeholder?: string; + description?: string; + className?: string; + rows?: number; + }; + +export function FormTextarea({ + form, + name, + label, + disabled, + className, + placeholder, + description, + rows, +}: FormTextareaProps) { + return ( + ( + + {label && {label}} + +