|
| 1 | +// Usage: |
| 2 | +// <Textarea id="componentIdentifier" // optional (defaults to name if not provided) |
| 3 | +// name="fieldName" |
| 4 | +// label="fieldLabel" // optional (defaults to name with first letter capitalized if not provided) |
| 5 | +// placeholder="placeholderText" // optional (defaults to name with first letter capitalized if not provided) |
| 6 | +// rows={4} // optional (defaults to 4 if not provided) |
| 7 | +// value={fieldValue} // useState variable |
| 8 | +// onChange={fieldSetterFunction} // useState setter function |
| 9 | +// required={true} // optional (if required, shows asterisk) |
| 10 | +// className="customTextareaClass" // optional, additional classNames for textarea |
| 11 | +// /> |
| 12 | + |
| 13 | +// Style (apart from border color: --border, other styles match InputField): |
| 14 | +// label: body-sm-bold, default text color |
| 15 | +// textarea: border color: --border, background color: --background, shadow: 0 4px 0 #D1D5DB |
| 16 | +// placeholder: text color: --bloom-gray |
| 17 | + |
| 18 | +"use client"; |
| 19 | +import React from "react"; |
| 20 | + |
| 21 | +import { cn } from "@/lib/utils"; |
| 22 | + |
| 23 | +interface TextareaProps { |
| 24 | + id?: string; |
| 25 | + name: string; |
| 26 | + label?: string; |
| 27 | + placeholder?: string; |
| 28 | + rows?: number; |
| 29 | + value: string; |
| 30 | + onChange: (value: string) => void; |
| 31 | + required?: boolean; |
| 32 | + className?: string; |
| 33 | +} |
| 34 | + |
| 35 | +// Use to capitalize the first letter of name as label placeholder if not passed |
| 36 | +const capitalizeFirstLetter = (str: string) => { |
| 37 | + return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(); |
| 38 | +}; |
| 39 | + |
| 40 | +const Textarea: React.FC<TextareaProps> = ({ |
| 41 | + id, |
| 42 | + name, |
| 43 | + label, |
| 44 | + placeholder, |
| 45 | + rows, |
| 46 | + value, |
| 47 | + onChange, |
| 48 | + required, |
| 49 | + className, |
| 50 | +}) => { |
| 51 | + return ( |
| 52 | + <> |
| 53 | + <label htmlFor={name} className={"body-sm-bold mb-1 block"}> |
| 54 | + {label ? label : capitalizeFirstLetter(name)} |
| 55 | + {required && <span className={"text-[var(--bloom-red)]"}> *</span>} |
| 56 | + </label> |
| 57 | + <textarea |
| 58 | + id={id ? id : name} |
| 59 | + name={name} |
| 60 | + placeholder={placeholder ? placeholder : capitalizeFirstLetter(name)} |
| 61 | + rows={rows ? rows : 4} |
| 62 | + className={cn( |
| 63 | + "w-full rounded-md border border-[hsl(var(--border))] bg-background shadow-[0_4px_0_0_#D1D5DB] outline-none", |
| 64 | + "body px-3 py-2 placeholder:text-bloom-gray", |
| 65 | + className, |
| 66 | + )} |
| 67 | + value={value} |
| 68 | + onChange={(e) => onChange(e.target.value)} |
| 69 | + /> |
| 70 | + </> |
| 71 | + ); |
| 72 | +}; |
| 73 | + |
| 74 | +export default Textarea; |
0 commit comments