|
| 1 | +import { cva, VariantProps } from "class-variance-authority"; |
| 2 | +import * as React from "react"; |
| 3 | +import { cn } from "@/lib/utils"; |
| 4 | + |
| 5 | +const formFieldVariants = cva( |
| 6 | + "bg-input aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive theme-default:active:scale-[0.99] relative box-border inline-flex w-full shrink-0 cursor-text items-center justify-center gap-2 overflow-hidden text-sm transition-[color,box-shadow] duration-150 ease-in-out outline-none disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", |
| 7 | + { |
| 8 | + variants: { |
| 9 | + variant: { |
| 10 | + default: |
| 11 | + "text-input-foreground shadow-input-resting hover:shadow-input-hover focus-within:ring-ring/15 focus-within:ring-4", |
| 12 | + error: |
| 13 | + "text-destructive-foreground shadow-input-destructive-resting hover:shadow-input-destructive-hover focus-within:ring-destructive-border/15 focus-within:ring-4", |
| 14 | + }, |
| 15 | + size: { |
| 16 | + default: "h-14 rounded-2xl", |
| 17 | + sm: "h-12 rounded-2xl", |
| 18 | + lg: "h-16 rounded-2xl", |
| 19 | + }, |
| 20 | + }, |
| 21 | + defaultVariants: { |
| 22 | + variant: "default", |
| 23 | + size: "default", |
| 24 | + }, |
| 25 | + }, |
| 26 | +); |
| 27 | + |
| 28 | +export interface FormFieldProps |
| 29 | + extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "size"> { |
| 30 | + label: string; |
| 31 | + error?: boolean; |
| 32 | + helperText?: string; |
| 33 | + size?: VariantProps<typeof formFieldVariants>["size"]; |
| 34 | + variant?: VariantProps<typeof formFieldVariants>["variant"]; |
| 35 | + startAdornment?: React.ReactNode; |
| 36 | + endAdornment?: React.ReactNode; |
| 37 | +} |
| 38 | + |
| 39 | +const FormField = React.forwardRef<HTMLInputElement, FormFieldProps>( |
| 40 | + ( |
| 41 | + { |
| 42 | + className, |
| 43 | + variant, |
| 44 | + size, |
| 45 | + error, |
| 46 | + helperText, |
| 47 | + label, |
| 48 | + startAdornment, |
| 49 | + endAdornment, |
| 50 | + ...props |
| 51 | + }, |
| 52 | + ref, |
| 53 | + ) => { |
| 54 | + const [focused, setFocused] = React.useState(false); |
| 55 | + const [hasValue, setHasValue] = React.useState( |
| 56 | + Boolean(props.value || props.defaultValue), |
| 57 | + ); |
| 58 | + const isDisabled = props.disabled; |
| 59 | + |
| 60 | + const handleFocus = (e: React.FocusEvent<HTMLInputElement>) => { |
| 61 | + setFocused(true); |
| 62 | + props.onFocus?.(e); |
| 63 | + }; |
| 64 | + |
| 65 | + const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => { |
| 66 | + setFocused(false); |
| 67 | + setHasValue(Boolean(e.target.value)); |
| 68 | + props.onBlur?.(e); |
| 69 | + }; |
| 70 | + |
| 71 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 72 | + setHasValue(Boolean(e.target.value)); |
| 73 | + props.onChange?.(e); |
| 74 | + }; |
| 75 | + |
| 76 | + const isLabelFloating = focused || hasValue; |
| 77 | + |
| 78 | + return ( |
| 79 | + <div> |
| 80 | + <div |
| 81 | + className={cn( |
| 82 | + formFieldVariants({ variant: error ? "error" : variant, size }), |
| 83 | + "group relative items-end gap-0.5", |
| 84 | + isDisabled && |
| 85 | + "bg-input-muted text-input-foreground cursor-not-allowed opacity-50", |
| 86 | + isDisabled && variant === "default" && "bg-input-muted", |
| 87 | + startAdornment && "pl-[5px]", |
| 88 | + endAdornment && "pr-[5px]", |
| 89 | + className, |
| 90 | + )} |
| 91 | + > |
| 92 | + <label |
| 93 | + htmlFor={props.id} |
| 94 | + className={cn( |
| 95 | + "transition- pointer-events-none absolute top-1/2 left-3 -translate-y-1/2 text-sm duration-150 ease-in-out", |
| 96 | + startAdornment && "left-0", |
| 97 | + isLabelFloating && "top-4 text-xs", |
| 98 | + isLabelFloating && size === "sm" && "top-3.5 text-xs", |
| 99 | + error ? "text-destructive-foreground" : "text-muted-foreground", |
| 100 | + focused && !error && "text-primary", |
| 101 | + )} |
| 102 | + > |
| 103 | + {label} |
| 104 | + </label> |
| 105 | + {startAdornment && ( |
| 106 | + <div className="[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"> |
| 107 | + {startAdornment} |
| 108 | + </div> |
| 109 | + )} |
| 110 | + <div className="relative flex-1"> |
| 111 | + <input |
| 112 | + className={cn( |
| 113 | + "h-14 w-full flex-1 bg-transparent px-3 pt-6 pb-1 outline-none file:border-0 file:bg-transparent file:text-sm file:font-medium", |
| 114 | + isDisabled && |
| 115 | + "bg-input-muted text-input-foreground cursor-not-allowed opacity-50", |
| 116 | + startAdornment ? "pl-0" : "pl-3", |
| 117 | + endAdornment ? "pr-0" : "pr-3", |
| 118 | + size === "sm" && "h-12 pb-2 text-sm", |
| 119 | + size === "lg" && "h-16 pt-4 pb-0 text-base", |
| 120 | + )} |
| 121 | + ref={ref} |
| 122 | + onFocus={handleFocus} |
| 123 | + onBlur={handleBlur} |
| 124 | + onChange={handleChange} |
| 125 | + {...props} |
| 126 | + /> |
| 127 | + </div> |
| 128 | + {endAdornment && ( |
| 129 | + <div className="[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"> |
| 130 | + {endAdornment} |
| 131 | + </div> |
| 132 | + )} |
| 133 | + </div> |
| 134 | + {helperText && ( |
| 135 | + <p |
| 136 | + className={cn( |
| 137 | + "mt-1.5 text-xs", |
| 138 | + error ? "text-destructive-foreground" : "text-muted-foreground", |
| 139 | + )} |
| 140 | + > |
| 141 | + {helperText} |
| 142 | + </p> |
| 143 | + )} |
| 144 | + </div> |
| 145 | + ); |
| 146 | + }, |
| 147 | +); |
| 148 | + |
| 149 | +FormField.displayName = "FormField"; |
| 150 | + |
| 151 | +export { FormField, formFieldVariants }; |
0 commit comments