Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

adding form in

Jonathan Summers-Muir02ba30934c4c
diff --git a/package-lock.json b/package-lock.json
index b846e84fff0..9914a9fcb9d 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -3913,6 +3913,14 @@
"react": ">= 16"
}
},
+ "node_modules/@hookform/resolvers": {
+ "version": "3.1.0",
+ "resolved": "https://registry.npmjs.org/@hookform/resolvers/-/resolvers-3.1.0.tgz",
+ "integrity": "sha512-z0A8K+Nxq+f83Whm/ajlwE6VtQlp/yPHZnXw7XWVPIGm1Vx0QV8KThU3BpbBRfAZ7/dYqCKKBNnQh85BkmBKkA==",
+ "peerDependencies": {
+ "react-hook-form": "^7.0.0"
+ }
+ },
"node_modules/@humanwhocodes/config-array": {
"version": "0.9.5",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz",
@@ -37862,6 +37870,21 @@
"node": ">=6"
}
},
+ "node_modules/react-hook-form": {
+ "version": "7.43.9",
+ "resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.43.9.tgz",
+ "integrity": "sha512-AUDN3Pz2NSeoxQ7Hs6OhQhDr6gtF9YRuutGDwPQqhSUAHJSgGl2VeY3qN19MG0SucpjgDiuMJ4iC5T5uB+eaNQ==",
+ "engines": {
+ "node": ">=12.22.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/react-hook-form"
+ },
+ "peerDependencies": {
+ "react": "^16.8.0 || ^17 || ^18"
+ }
+ },
"node_modules/react-hot-toast": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/react-hot-toast/-/react-hot-toast-2.3.0.tgz",
@@ -45465,6 +45488,14 @@
"resolved": "https://registry.npmjs.org/yup-password/-/yup-password-0.2.2.tgz",
"integrity": "sha512-2PHfqGWtbXg4OfDV7VKFIb3hyEaYgTYpEORnFqgGAYqzENWmGzWMoeGvJg2Ohmq6maTRxhJzLZpNTITrqlZTrA=="
},
+ "node_modules/zod": {
+ "version": "3.21.4",
+ "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz",
+ "integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==",
+ "funding": {
+ "url": "https://github.com/sponsors/colinhacks"
+ }
+ },
"node_modules/zwitch": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/zwitch/-/zwitch-1.0.5.tgz",
@@ -45574,14 +45605,17 @@
"license": "MIT",
"dependencies": {
"@headlessui/react": "^1.0.0",
+ "@hookform/resolvers": "^3.1.0",
"@mertasan/tailwindcss-variables": "^2.0.1",
"@radix-ui/react-accordion": "^1.1.0",
"@radix-ui/react-collapsible": "^1.0.1",
"@radix-ui/react-context-menu": "^2.1.2",
"@radix-ui/react-dialog": "^1.0.2",
"@radix-ui/react-dropdown-menu": "^2.0.3",
+ "@radix-ui/react-id": "^0.0.6",
"@radix-ui/react-popover": "^1.0.4",
"@radix-ui/react-portal": "^1.0.1",
+ "@radix-ui/react-slot": "^1.0.1",
"@radix-ui/react-tabs": "^1.0.2",
"@supabase/auth-helpers-react": "^0.3.1",
"@tailwindcss/forms": "^0.4.0",
@@ -45602,13 +45636,15 @@
"react-copy-to-clipboard": "^5.1.0",
"react-countdown": "^2.3.5",
"react-feather": "^2.0.10",
+ "react-hook-form": "^7.43.9",
"react-markdown": "^8.0.3",
"react-syntax-highlighter": "^15.5.0",
"remark-gfm": "^3.0.1",
"sse.js": "^0.6.1",
"tailwind-merge": "^1.10.0",
"tailwindcss": "^3.0.15",
- "tailwindcss-radix": "^1.6.0"
+ "tailwindcss-radix": "^1.6.0",
+ "zod": "^3.21.4"
},
"devDependencies": {
"@babel/plugin-syntax-flow": "^7.21.4",
@@ -45639,6 +45675,17 @@
"typescript": "4.3.4"
}
},
+ "packages/ui/node_modules/@radix-ui/react-id": {
+ "version": "0.0.6",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-id/-/react-id-0.0.6.tgz",
+ "integrity": "sha512-PzmraF34fYggsYvTIZVJ5S68WMp3aKUN3HkSmGnz4zn9zpRjkAbbg7Xn3ueQI3FQsLWKgyUfnpsmWFDndpcqYg==",
+ "dependencies": {
+ "@babel/runtime": "^7.13.10"
+ },
+ "peerDependencies": {
+ "react": "^16.8 || ^17.0"
+ }
+ },
"packages/ui/node_modules/@storybook/addon-actions": {
"version": "7.0.9",
"resolved": "https://registry.npmjs.org/@storybook/addon-actions/-/addon-actions-7.0.9.tgz",
@@ -49173,6 +49220,12 @@
"integrity": "sha512-JJCXydOFWMDpCP4q13iEplA503MQO3xLoZiKum+955ZCtHINWnx26CUxVxxFQu/uLb4LW3ge15ZpzIkXKkJ8oQ==",
"requires": {}
},
+ "@hookform/resolvers": {
+ "version": "3.1.0",
+ "resolved": "https://registry.npmjs.org/@hookform/resolvers/-/resolvers-3.1.0.tgz",
+ "integrity": "sha512-z0A8K+Nxq+f83Whm/ajlwE6VtQlp/yPHZnXw7XWVPIGm1Vx0QV8KThU3BpbBRfAZ7/dYqCKKBNnQh85BkmBKkA==",
+ "requires": {}
+ },
"@humanwhocodes/config-array": {
"version": "0.9.5",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz",
@@ -75511,6 +75564,12 @@
}
}
},
+ "react-hook-form": {
+ "version": "7.43.9",
+ "resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.43.9.tgz",
+ "integrity": "sha512-AUDN3Pz2NSeoxQ7Hs6OhQhDr6gtF9YRuutGDwPQqhSUAHJSgGl2VeY3qN19MG0SucpjgDiuMJ4iC5T5uB+eaNQ==",
+ "requires": {}
+ },
"react-hot-toast": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/react-hot-toast/-/react-hot-toast-2.3.0.tgz",
@@ -80117,14 +80176,17 @@
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.21.4",
"@headlessui/react": "^1.0.0",
+ "@hookform/resolvers": "^3.1.0",
"@mertasan/tailwindcss-variables": "^2.0.1",
"@radix-ui/react-accordion": "^1.1.0",
"@radix-ui/react-collapsible": "^1.0.1",
"@radix-ui/react-context-menu": "^2.1.2",
"@radix-ui/react-dialog": "^1.0.2",
"@radix-ui/react-dropdown-menu": "^2.0.3",
+ "@radix-ui/react-id": "^0.0.6",
"@radix-ui/react-popover": "^1.0.4",
"@radix-ui/react-portal": "^1.0.1",
+ "@radix-ui/react-slot": "^1.0.1",
"@radix-ui/react-tabs": "^1.0.2",
"@storybook/addon-actions": "^7.0.5",
"@storybook/addon-mdx-gfm": "^7.0.5",
@@ -80160,6 +80222,7 @@
"react-copy-to-clipboard": "^5.1.0",
"react-countdown": "^2.3.5",
"react-feather": "^2.0.10",
+ "react-hook-form": "^7.43.9",
"react-markdown": "^8.0.3",
"react-syntax-highlighter": "^15.5.0",
"remark-gfm": "^3.0.1",
@@ -80172,9 +80235,18 @@
"tailwindcss": "^3.0.15",
"tailwindcss-radix": "^1.6.0",
"tsconfig-paths-webpack-plugin": "^4.0.1",
- "typescript": "4.3.4"
+ "typescript": "4.3.4",
+ "zod": "^3.21.4"
},
"dependencies": {
+ "@radix-ui/react-id": {
+ "version": "0.0.6",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-id/-/react-id-0.0.6.tgz",
+ "integrity": "sha512-PzmraF34fYggsYvTIZVJ5S68WMp3aKUN3HkSmGnz4zn9zpRjkAbbg7Xn3ueQI3FQsLWKgyUfnpsmWFDndpcqYg==",
+ "requires": {
+ "@babel/runtime": "^7.13.10"
+ }
+ },
"@storybook/addon-actions": {
"version": "7.0.9",
"resolved": "https://registry.npmjs.org/@storybook/addon-actions/-/addon-actions-7.0.9.tgz",
@@ -82642,6 +82714,11 @@
"resolved": "https://registry.npmjs.org/yup-password/-/yup-password-0.2.2.tgz",
"integrity": "sha512-2PHfqGWtbXg4OfDV7VKFIb3hyEaYgTYpEORnFqgGAYqzENWmGzWMoeGvJg2Ohmq6maTRxhJzLZpNTITrqlZTrA=="
},
+ "zod": {
+ "version": "3.21.4",
+ "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz",
+ "integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw=="
+ },
"zwitch": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/zwitch/-/zwitch-1.0.5.tgz",
diff --git a/packages/ui/.storybook/main.ts b/packages/ui/.storybook/main.ts
index 93e7c8ba318..b138249d283 100644
--- a/packages/ui/.storybook/main.ts
+++ b/packages/ui/.storybook/main.ts
@@ -12,6 +12,8 @@ const config: any = {
'../src/components/Collapsible/Collapsible.stories.tsx',
// '../src/components/ContextMenu/ContextMenu.stories.tsx',
'../src/components/Dropdown/Dropdown.stories.tsx',
+
+ '../src/components/shadcn/form/Form.stories.tsx',
],
addons: [
'storybook-dark-mode',
diff --git a/packages/ui/package.json b/packages/ui/package.json
index 256e0a39ff8..221d6ee632d 100644
--- a/packages/ui/package.json
+++ b/packages/ui/package.json
@@ -17,14 +17,17 @@
},
"dependencies": {
"@headlessui/react": "^1.0.0",
+ "@hookform/resolvers": "^3.1.0",
"@mertasan/tailwindcss-variables": "^2.0.1",
"@radix-ui/react-accordion": "^1.1.0",
"@radix-ui/react-collapsible": "^1.0.1",
"@radix-ui/react-context-menu": "^2.1.2",
"@radix-ui/react-dialog": "^1.0.2",
"@radix-ui/react-dropdown-menu": "^2.0.3",
+ "@radix-ui/react-id": "^0.0.6",
"@radix-ui/react-popover": "^1.0.4",
"@radix-ui/react-portal": "^1.0.1",
+ "@radix-ui/react-slot": "^1.0.1",
"@radix-ui/react-tabs": "^1.0.2",
"@supabase/auth-helpers-react": "^0.3.1",
"@tailwindcss/forms": "^0.4.0",
@@ -45,13 +48,15 @@
"react-copy-to-clipboard": "^5.1.0",
"react-countdown": "^2.3.5",
"react-feather": "^2.0.10",
+ "react-hook-form": "^7.43.9",
"react-markdown": "^8.0.3",
"react-syntax-highlighter": "^15.5.0",
"remark-gfm": "^3.0.1",
"sse.js": "^0.6.1",
"tailwind-merge": "^1.10.0",
"tailwindcss": "^3.0.15",
- "tailwindcss-radix": "^1.6.0"
+ "tailwindcss-radix": "^1.6.0",
+ "zod": "^3.21.4"
},
"devDependencies": {
"@babel/plugin-syntax-flow": "^7.21.4",
diff --git a/packages/ui/src/components/shadcn/form.tsx b/packages/ui/src/components/shadcn/form.tsx
new file mode 100644
index 00000000000..80674dfe20b
--- /dev/null
+++ b/packages/ui/src/components/shadcn/form.tsx
@@ -0,0 +1,170 @@
+import * as React from 'react'
+import * as LabelPrimitive from '@radix-ui/react-label'
+import { Slot } from '@radix-ui/react-slot'
+import {
+ Controller,
+ ControllerProps,
+ FieldPath,
+ FieldValues,
+ FormProvider,
+ useFormContext,
+} from 'react-hook-form'
+
+import { cn } from '@ui/lib/utils'
+import { Label } from '@ui/components/shadcn/label'
+
+import { useId } from '@radix-ui/react-id'
+
+const Form = FormProvider
+
+type FormFieldContextValue<
+ TFieldValues extends FieldValues = FieldValues,
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
+> = {
+ name: TName
+}
+
+const FormFieldContext = React.createContext<FormFieldContextValue>({} as FormFieldContextValue)
+
+const FormField = <
+ TFieldValues extends FieldValues = FieldValues,
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
+>({
+ ...props
+}: ControllerProps<TFieldValues, TName>) => {
+ return (
+ <FormFieldContext.Provider value={{ name: props.name }}>
+ <Controller {...props} />
+ </FormFieldContext.Provider>
+ )
+}
+
+const useFormField = () => {
+ const fieldContext = React.useContext(FormFieldContext)
+ const itemContext = React.useContext(FormItemContext)
+ const { getFieldState, formState } = useFormContext()
+
+ const fieldState = getFieldState(fieldContext.name, formState)
+
+ if (!fieldContext) {
+ throw new Error('useFormField should be used within <FormField>')
+ }
+
+ const { id } = itemContext
+
+ return {
+ id,
+ name: fieldContext.name,
+ formItemId: `${id}-form-item`,
+ formDescriptionId: `${id}-form-item-description`,
+ formMessageId: `${id}-form-item-message`,
+ ...fieldState,
+ }
+}
+
+type FormItemContextValue = {
+ id: string
+}
+
+const FormItemContext = React.createContext<FormItemContextValue>({} as FormItemContextValue)
+
+const FormItem = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
+ ({ className, ...props }, ref) => {
+ // const id = React.useId()
+ const id = useId()
+ console.log('id', id)
+ return (
+ <FormItemContext.Provider value={{ id }}>
+ <div ref={ref} className={cn('space-y-2', className)} {...props} />
+ </FormItemContext.Provider>
+ )
+ }
+)
+FormItem.displayName = 'FormItem'
+
+const FormLabel = React.forwardRef<
+ React.ElementRef<typeof LabelPrimitive.Root>,
+ React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root>
+>(({ className, ...props }, ref) => {
+ const { error, formItemId } = useFormField()
+
+ return (
+ <Label
+ ref={ref}
+ className={cn(error && 'text-destructive', className)}
+ htmlFor={formItemId}
+ {...props}
+ />
+ )
+})
+FormLabel.displayName = 'FormLabel'
+
+const FormControl = React.forwardRef<
+ React.ElementRef<typeof Slot>,
+ React.ComponentPropsWithoutRef<typeof Slot>
+>(({ ...props }, ref) => {
+ const { error, formItemId, formDescriptionId, formMessageId } = useFormField()
+
+ return (
+ <Slot
+ ref={ref}
+ id={formItemId}
+ aria-describedby={!error ? `${formDescriptionId}` : `${formDescriptionId} ${formMessageId}`}
+ aria-invalid={!!error}
+ {...props}
+ />
+ )
+})
+FormControl.displayName = 'FormControl'
+
+const FormDescription = React.forwardRef<
+ HTMLParagraphElement,
+ React.HTMLAttributes<HTMLParagraphElement>
+>(({ className, ...props }, ref) => {
+ const { formDescriptionId } = useFormField()
+
+ return (
+ <p
+ ref={ref}
+ id={formDescriptionId}
+ className={cn('text-sm text-muted-foreground', className)}
+ {...props}
+ />
+ )
+})
+FormDescription.displayName = 'FormDescription'
+
+const FormMessage = React.forwardRef<
+ HTMLParagraphElement,
+ React.HTMLAttributes<HTMLParagraphElement>
+>(({ className, children, ...props }, ref) => {
+ const { error, formMessageId } = useFormField()
+ const body = error ? String(error?.message) : children
+
+ if (!body) {
+ return null
+ }
+
+ return (
+ <p
+ ref={ref}
+ id={formMessageId}
+ className={cn('text-sm font-medium text-red-900', className)}
+ {...props}
+ >
+ {body}
+ </p>
+ )
+})
+FormMessage.displayName = 'FormMessage'
+
+export {
+ useFormField,
+ Form,
+ FormItem,
+ FormLabel,
+ FormControl,
+ FormDescription,
+ FormMessage,
+ FormField,
+}
diff --git a/packages/ui/src/components/shadcn/form/Form.stories.tsx b/packages/ui/src/components/shadcn/form/Form.stories.tsx
new file mode 100644
index 00000000000..7560d88ccd3
--- /dev/null
+++ b/packages/ui/src/components/shadcn/form/Form.stories.tsx
@@ -0,0 +1,88 @@
+// Button.stories.ts|tsx
+
+import type { Meta, StoryObj } from '@storybook/react'
+import { useForm } from 'react-hook-form'
+
+import { IdProvider } from '@radix-ui/react-id'
+
+import { zodResolver } from '@hookform/resolvers/zod'
+import * as z from 'zod'
+
+import {
+ Form,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormControl,
+ FormDescription,
+ FormMessage,
+} from './../form'
+
+import { Input } from './../input'
+import { Button } from '@ui/components/Button'
+
+const meta: Meta<typeof Form> = {
+ /* πŸ‘‡ The title prop is optional.
+ * See https://storybook.js.org/docs/react/configure/overview#configure-story-loading
+ * to learn how to generate automatic titles
+ */
+ title: 'Forms/Form',
+ component: Form,
+}
+
+export default meta
+
+type Story = StoryObj<typeof Form>
+
+/*
+ *πŸ‘‡ Render functions are a framework specific feature to allow you control on how the component renders.
+ * See https://storybook.js.org/docs/react/api/csf
+ * to learn how to use render functions.
+ */
+
+export const Primary = {
+ args: {},
+ render: ({}: any) => {
+ const formSchema = z.object({
+ username: z.string().min(2, {
+ message: 'Username must be at least 2 characters.',
+ }),
+ })
+
+ // 1. Define your form.
+ const form = useForm<z.infer<typeof formSchema>>({
+ resolver: zodResolver(formSchema),
+ })
+
+ // 2. Define a submit handler.
+ function onSubmit(values: z.infer<typeof formSchema>) {
+ // Do something with the form values.
+ // βœ… This will be type-safe and validated.
+ console.log(values)
+ }
+
+ return (
+ <IdProvider>
+ <Form {...form}>
+ <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
+ <FormField
+ control={form.control}
+ name="username"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Username</FormLabel>
+ <FormControl>
+ <Input placeholder="shadcn" {...field} />
+ </FormControl>
+ <FormDescription>This is your public display name.</FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <Button htmlType="submit">Submit</Button>
+ </form>
+ </Form>
+ </IdProvider>
+ )
+ },
+}
diff --git a/packages/ui/src/components/shadcn/input.tsx b/packages/ui/src/components/shadcn/input.tsx
new file mode 100644
index 00000000000..16a52c8bf59
--- /dev/null
+++ b/packages/ui/src/components/shadcn/input.tsx
@@ -0,0 +1,30 @@
+import * as React from 'react'
+
+import { cn } from '@ui/lib/utils'
+
+export interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {}
+
+const Input = React.forwardRef<HTMLInputElement, InputProps>(
+ ({ className, type, ...props }, ref) => {
+ return (
+ <input
+ type={type}
+ className={cn(
+ `flex h-10 w-full rounded-md
+ border border-scale-700 bg-transparent px-3 py-2
+ text-sm text-scale-1200 ring-offset-background
+ file:border-0 file:bg-transparent file:text-sm file:font-medium
+ placeholder:text-scale-700
+ focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring
+ focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50`,
+ className
+ )}
+ ref={ref}
+ {...props}
+ />
+ )
+ }
+)
+Input.displayName = 'Input'
+
+export { Input }
diff --git a/packages/ui/src/components/shadcn/label.tsx b/packages/ui/src/components/shadcn/label.tsx
new file mode 100644
index 00000000000..ec43020bf5e
--- /dev/null
+++ b/packages/ui/src/components/shadcn/label.tsx
@@ -0,0 +1,19 @@
+import * as React from 'react'
+import * as LabelPrimitive from '@radix-ui/react-label'
+import { VariantProps, cva } from 'class-variance-authority'
+
+import { cn } from '@ui/lib/utils'
+
+const labelVariants = cva(
+ 'text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70'
+)
+
+const Label = React.forwardRef<
+ React.ElementRef<typeof LabelPrimitive.Root>,
+ React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> & VariantProps<typeof labelVariants>
+>(({ className, ...props }, ref) => (
+ <LabelPrimitive.Root ref={ref} className={cn(labelVariants(), className)} {...props} />
+))
+Label.displayName = LabelPrimitive.Root.displayName
+
+export { Label }