|
| 1 | +"use client"; |
| 2 | + |
1 | 3 | import * as React from "react"; |
| 4 | +import { Slot } from "@radix-ui/react-slot"; |
| 5 | +import { cva, type VariantProps } from "class-variance-authority"; |
2 | 6 |
|
3 | 7 | import { cn } from "@/lib/utils"; |
4 | 8 |
|
5 | | -const Input = React.forwardRef<HTMLInputElement, React.ComponentProps<"input">>( |
6 | | - ({ className, type, ...props }, ref) => { |
| 9 | +const inputVariants = cva( |
| 10 | + "peer inline-flex w-full appearance-none items-center justify-center outline-none transition focus:text-ink-gray-7 focus:ring-2 focus:ring-focus-light-default disabled:cursor-not-allowed disabled:select-none", |
| 11 | + { |
| 12 | + variants: { |
| 13 | + size: { |
| 14 | + sm: "h-7 rounded-ef-4 px-ef-8 py-ef-6 text-ef-base font-ef-regular leading-[115%] tracking-[0.07px]", |
| 15 | + md: "h-8 rounded-ef-4 px-ef-10 py-ef-8 text-ef-base font-ef-regular leading-[115%] tracking-[0.07px]", |
| 16 | + lg: "h-10 rounded-ef-5 px-ef-12 py-ef-11 text-ef-base font-ef-regular leading-[115%] tracking-[0.07px]", |
| 17 | + xl: "h-10 rounded-ef-5 px-ef-14 py-[9.5px] text-ef-xl font-ef-regular leading-[115%] tracking-[0.18px]", |
| 18 | + }, |
| 19 | + variant: { |
| 20 | + subtle: |
| 21 | + "bg-surface-gray-2 text-ink-gray-4 placeholder:text-ink-gray-4 focus:bg-surface-white ", |
| 22 | + outline: |
| 23 | + "border border-outline-gray-2 bg-surface-white placeholder:text-ink-gray-4", |
| 24 | + }, |
| 25 | + disabled: { |
| 26 | + true: "bg-surface-gray-1 text-ink-gray-3 placeholder:text-ink-gray-3", |
| 27 | + false: |
| 28 | + "active:text-ink-gray-7 active:ring-2 active:ring-focus-light-default", |
| 29 | + }, |
| 30 | + invalid: { |
| 31 | + true: "", |
| 32 | + false: "", |
| 33 | + }, |
| 34 | + }, |
| 35 | + compoundVariants: [ |
| 36 | + { |
| 37 | + variant: "subtle", |
| 38 | + disabled: false, |
| 39 | + className: "hover:bg-surface-gray-3 ", |
| 40 | + }, |
| 41 | + { |
| 42 | + variant: "outline", |
| 43 | + disabled: false, |
| 44 | + className: "hover:shadow-[0px_1px_4px_0px_rgba(0,0,0,0.10)]", |
| 45 | + }, |
| 46 | + { |
| 47 | + variant: "subtle", |
| 48 | + invalid: true, |
| 49 | + className: "bg-surface-red-2", |
| 50 | + }, |
| 51 | + { |
| 52 | + variant: "outline", |
| 53 | + invalid: true, |
| 54 | + className: "border-outline-red-3", |
| 55 | + }, |
| 56 | + ], |
| 57 | + defaultVariants: { |
| 58 | + size: "sm", |
| 59 | + variant: "subtle", |
| 60 | + }, |
| 61 | + }, |
| 62 | +); |
| 63 | + |
| 64 | +const prefixVariants = cva( |
| 65 | + "pointer-events-none absolute inset-y-0 left-0 flex items-center justify-center bg-transparent text-ink-gray-4 placeholder:text-ink-gray-4 peer-focus:text-ink-gray-7", |
| 66 | + { |
| 67 | + variants: { |
| 68 | + size: { |
| 69 | + sm: "px-ef-8", |
| 70 | + md: "pl-ef-10 pr-ef-8", |
| 71 | + lg: "pl-ef-12 pr-ef-8", |
| 72 | + xl: "pl-ef-14 pr-ef-10", |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | +); |
| 77 | + |
| 78 | +const suffixVariants = cva( |
| 79 | + "pointer-events-none absolute inset-y-0 right-0 flex items-center justify-center bg-transparent text-ink-gray-4 placeholder:text-ink-gray-4 peer-focus:text-ink-gray-7", |
| 80 | + { |
| 81 | + variants: { |
| 82 | + size: { |
| 83 | + sm: "pl-ef-13 pr-ef-8", |
| 84 | + md: "pl-ef-13 pr-ef-10", |
| 85 | + lg: "pl-ef-13 pr-ef-12", |
| 86 | + xl: "pl-ef-13 pr-ef-14", |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | +); |
| 91 | + |
| 92 | +export interface InputProps |
| 93 | + extends Omit< |
| 94 | + React.InputHTMLAttributes<HTMLInputElement>, |
| 95 | + "disabled" | "prefix" | "size" |
| 96 | + >, |
| 97 | + VariantProps<typeof inputVariants> { |
| 98 | + asChild?: boolean; |
| 99 | + prefix?: React.ReactNode; |
| 100 | + suffix?: React.ReactNode; |
| 101 | +} |
| 102 | + |
| 103 | +const Input = React.forwardRef<HTMLInputElement, InputProps>( |
| 104 | + ( |
| 105 | + { |
| 106 | + className, |
| 107 | + type, |
| 108 | + size = "md", |
| 109 | + variant = "subtle", |
| 110 | + invalid = false, |
| 111 | + disabled = false, |
| 112 | + asChild = false, |
| 113 | + prefix, |
| 114 | + suffix, |
| 115 | + ...props |
| 116 | + }, |
| 117 | + ref, |
| 118 | + ) => { |
| 119 | + const Comp = asChild ? Slot : "input"; |
| 120 | + |
| 121 | + const [_, setHasPaddingCalculated] = React.useState(false); |
| 122 | + const inputInlineStyles = React.useRef<Record<string, number>>({}); |
| 123 | + const prefixRef = React.useRef<HTMLDivElement>(null); |
| 124 | + const suffixRef = React.useRef<HTMLDivElement>(null); |
| 125 | + |
| 126 | + React.useLayoutEffect(() => { |
| 127 | + let key = ""; |
| 128 | + |
| 129 | + const prefixElement = prefixRef.current; |
| 130 | + const suffixElement = suffixRef.current; |
| 131 | + |
| 132 | + if (prefix && prefixElement) { |
| 133 | + key = "paddingLeft"; |
| 134 | + |
| 135 | + if (!key) return; |
| 136 | + inputInlineStyles.current[key] = |
| 137 | + prefixElement.getBoundingClientRect().width; |
| 138 | + } |
| 139 | + |
| 140 | + if (suffix && suffixElement) { |
| 141 | + key = "paddingRight"; |
| 142 | + |
| 143 | + if (!key) return; |
| 144 | + inputInlineStyles.current[key] = |
| 145 | + suffixElement.getBoundingClientRect().width; |
| 146 | + } |
| 147 | + |
| 148 | + setHasPaddingCalculated(true); |
| 149 | + }, [prefix, suffix]); |
| 150 | + |
7 | 151 | return ( |
8 | | - <input |
9 | | - type={type} |
10 | | - className={cn( |
11 | | - "flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm", |
12 | | - className, |
| 152 | + <div className="relative inline-flex w-full"> |
| 153 | + <Comp |
| 154 | + type={type} |
| 155 | + className={cn( |
| 156 | + inputVariants({ |
| 157 | + size, |
| 158 | + variant, |
| 159 | + invalid, |
| 160 | + disabled, |
| 161 | + className, |
| 162 | + }), |
| 163 | + )} |
| 164 | + disabled={disabled ?? false} |
| 165 | + ref={ref} |
| 166 | + style={{ ...inputInlineStyles.current }} |
| 167 | + {...props} |
| 168 | + /> |
| 169 | + |
| 170 | + {prefix && ( |
| 171 | + <div ref={prefixRef} className={cn(prefixVariants({ size }))}> |
| 172 | + {prefix} |
| 173 | + </div> |
13 | 174 | )} |
14 | | - ref={ref} |
15 | | - {...props} |
16 | | - /> |
| 175 | + |
| 176 | + {suffix && ( |
| 177 | + <div ref={suffixRef} className={cn(suffixVariants({ size }))}> |
| 178 | + {suffix} |
| 179 | + </div> |
| 180 | + )} |
| 181 | + </div> |
17 | 182 | ); |
18 | 183 | }, |
19 | 184 | ); |
| 185 | + |
20 | 186 | Input.displayName = "Input"; |
21 | 187 |
|
22 | 188 | export { Input }; |
0 commit comments