Glass Form
washiveil/glass-formFreeComponent
react-hook-form wiring with the washiveil label and message registers.
Install it
npx shadcn@latest add https://washiveil.alexchih.com/r/glass-form.jsonSource
1'use client'23import * as React from 'react'4import {5 Controller,6 FormProvider,7 useFormContext,8 type ControllerProps,9 type FieldPath,10 type FieldValues,11} from 'react-hook-form'12import { Slot } from 'radix-ui'1314import { cn } from '@/lib/utils'15import { GlassLabel } from './glass-label'1617const GlassForm = FormProvider1819type GlassFormFieldContextValue<20 TFieldValues extends FieldValues = FieldValues,21 TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,22> = {23 name: TName24}2526const GlassFormFieldContext = React.createContext<GlassFormFieldContextValue>({} as GlassFormFieldContextValue)2728function GlassFormField<29 TFieldValues extends FieldValues = FieldValues,30 TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,31>({ ...props }: ControllerProps<TFieldValues, TName>) {32 return (33 <GlassFormFieldContext.Provider value={{ name: props.name }}>34 <Controller {...props} />35 </GlassFormFieldContext.Provider>36 )37}3839function useGlassFormField() {40 const fieldContext = React.useContext(GlassFormFieldContext)41 const itemContext = React.useContext(GlassFormItemContext)42 const { getFieldState, formState } = useFormContext()4344 if (!fieldContext.name) {45 throw new Error('useGlassFormField should be used within <GlassFormField>')46 }4748 const fieldState = getFieldState(fieldContext.name, formState)4950 const { id } = itemContext5152 return {53 id,54 name: fieldContext.name,55 formItemId: `${id}-form-item`,56 formDescriptionId: `${id}-form-item-description`,57 formMessageId: `${id}-form-item-message`,58 ...fieldState,59 }60}6162type GlassFormItemContextValue = {63 id: string64}6566const GlassFormItemContext = React.createContext<GlassFormItemContextValue>({} as GlassFormItemContextValue)6768const GlassFormItem = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(69 ({ className, ...props }, ref) => {70 const id = React.useId()7172 return (73 <GlassFormItemContext.Provider value={{ id }}>74 <div ref={ref} className={cn('grid gap-1.5', className)} {...props} />75 </GlassFormItemContext.Provider>76 )77 },78)79GlassFormItem.displayName = 'GlassFormItem'8081const GlassFormLabel = React.forwardRef<82 React.ComponentRef<typeof GlassLabel>,83 React.ComponentPropsWithoutRef<typeof GlassLabel>84>(({ className, ...props }, ref) => {85 const { error, formItemId } = useGlassFormField()8687// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from washiveil
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Ambient Fieldambient-field | washiveil | Components | Free | no deps | |
| Chipchip | washiveil | Components | Free | 1 dep | |
| Code Badgecode-badge | washiveil | Components | Free | no deps | |
| Glass Accordionglass-accordion | washiveil | Components | Free | 2 deps | |
| Glass Alertglass-alert | washiveil | Components | Free | 1 dep | |
| Glass Alert Dialogglass-alert-dialog | washiveil | Components | Free | 1 dep | |
| Glass Aspect Ratioglass-aspect-ratio | washiveil | Components | Free | 1 dep | |
| Glass Avatarglass-avatar | washiveil | Components | Free | 1 dep |
