Form
poyraz/formFreeComponent
Poyraz Soft Glass form with semantic states and composable variants.
Install it
npx shadcn@latest add https://ui.poyrazavsever.com/r/form.jsonSource
1"use client";23import * as React from "react";4import { Slot } from "@radix-ui/react-slot";5import {6 Controller,7 ControllerProps,8 FieldPath,9 FieldValues,10 FormProvider,11 useFormContext,12} from "react-hook-form";1314import { cn } from "@/lib/utils";15import { Label } from "@/components/ui/atoms/label";1617const Form = FormProvider;1819type FormFieldContextValue<20 TFieldValues extends FieldValues = FieldValues,21 TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,22> = {23 name: TName;24};2526const FormFieldContext = React.createContext<FormFieldContextValue>({} as FormFieldContextValue);2728const FormField = <29 TFieldValues extends FieldValues = FieldValues,30 TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,31>({32 ...props33}: ControllerProps<TFieldValues, TName>) => {34 return (35 <FormFieldContext.Provider value={{ name: props.name }}>36 <Controller {...props} />37 </FormFieldContext.Provider>38 );39};4041const useFormField = () => {42 const fieldContext = React.useContext(FormFieldContext);43 const itemContext = React.useContext(FormItemContext);44 const { getFieldState, formState } = useFormContext();4546 const fieldState = getFieldState(fieldContext.name, formState);4748 if (!fieldContext) {49 throw new Error("useFormField should be used within <FormField>");50 }5152 const { id } = itemContext;5354 return {55 id,56 name: fieldContext.name,57 formItemId: `${id}-form-item`,58 formDescriptionId: `${id}-form-item-description`,59 formMessageId: `${id}-form-item-message`,60 ...fieldState,61 };62};6364type FormItemContextValue = {65 id: string;66};6768const FormItemContext = React.createContext<FormItemContextValue>({} as FormItemContextValue);6970const FormItem = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(71 ({ className, ...props }, ref) => {72 const id = React.useId();7374 return (75 <FormItemContext.Provider value={{ id }}>76 <div77 ref={ref}78 className={cn(79 "space-y-2 transition-colors duration-[var(--poyraz-motion-duration-fast)] ease-[var(--poyraz-motion-ease-out)]",80 className,81 )}82 {...props}83 />84 </FormItemContext.Provider>85 );86 },87);88FormItem.displayName = "FormItem";8990const FormLabel = React.forwardRef<91 React.ElementRef<typeof Label>,92 React.ComponentPropsWithoutRef<typeof Label>93>(({ className, ...props }, ref) => {94 const { error, formItemId } = useFormField();9596 return (97 <Label98// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from poyraz
All 71 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | poyraz | Components | Free | 2 deps | |
| Alertalert | poyraz | Components | Free | 2 deps | |
| Announcement Barannouncement-bar | poyraz | Components | Free | 2 deps | |
| Autocompleteautocomplete | poyraz | Components | Free | 1 dep | |
| Avataravatar | poyraz | Components | Free | 2 deps | |
| Badgebadge | poyraz | Components | Free | 1 dep | |
| Bg Patternbg-pattern | poyraz | Components | Free | no deps | |
| Breadcrumbbreadcrumb | poyraz | Components | Free | 1 dep |
