Input

Komponen input untuk merender field isian teks standar

Jika kalian ingin membuat input teks seperti form, kalian bisa menggunakan komponen ini,

Button.jsx
1import { clsx } from "clsx"; 2import React, { forwardRef } from "react"; 3import { twMerge } from "tailwind-merge"; 4 5const variantClasses = { 6 default: "bg-white text-gray-900 rounded-md border border-gray-300 ", 7 filled: 8 "bg-gray-200 rounded-sm text-gray-700 focus:border-none focus:ring-0 border-none ", 9 ghost: 10 "border-none bg-transparent border-gray-600 border-2 text-gray-900 rounded-md focus:ring-0 focus:border-none", 11} as const; 12 13const sizeClasses = { 14 sm: "px-3 py-1 text-sm", 15 md: "px-4 py-2 text-base", 16 lg: "px-5 py-3 text-lg", 17} as const; 18export type InputProps = React.ComponentPropsWithRef<"input"> & { 19 as?: React.ElementType; 20 variant?: keyof typeof variantClasses; 21 size?: keyof typeof sizeClasses; 22 className?: string; 23}; 24 25export const Input: React.FC<InputProps> = forwardRef( 26 ( 27 { 28 as: Component = "input", 29 variant = "default", 30 size = "md", 31 className, 32 ...props 33 }, 34 ref 35 ) => { 36 return ( 37 <Component 38 ref={ref} 39 className={twMerge( 40 clsx( 41 variantClasses[variant], 42 sizeClasses[size as keyof typeof sizeClasses], 43 className 44 ) 45 )} 46 {...props} 47 /> 48 ); 49 } 50); 51 52Input.displayName = "Input"; 53
Button.jsx
1<Input 2 type="text" 3 id="first_name" 4 className="bg-gray-50 border border-gray-300 text-gray-700 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" 5 placeholder="Badriana" 6 required 7/> 8 <Input 9 type="text" 10 id="first_name" 11 className="w-full bg-gray-200 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" 12 placeholder="Badriana" 13 variant="filled" 14 required 15/> 16<Input 17 type="text" 18 id="first_name" 19 className="w-full" 20 placeholder="John" 21 variant="ghost" 22 required 23/>; 24};

Keunggulan dari Input

  • Fully typed dengan React.ComponentPropsWithRef, mendukung semua atribut standar <input> plus ref.
  • 🎨 Styling fleksibel dengan varian variant dan size, memudahkan konsistensi desain antar elemen form.
  • 🧩 Composable dan polymorphic melalui prop as, bisa diganti menjadi elemen lain seperti <textarea> atau komponen lain.
  • 🧠 Ref-friendly — dapat difokuskan, dipantau, atau dikontrol secara imperatif melalui ref.
  • Terintegrasi dengan Tailwind CSS menggunakan clsx dan twMerge untuk penggabungan kelas yang bersih dan efisien.
  • 🛡️ Type-safe dan reusable, memudahkan pengembangan form dengan TypeScript dan menjaga keakuratan props.