Skip to content

Commit 1034a13

Browse files
authored
Merge pull request #48 from codersforcauses/issue-44-Add_Textarea_component
Issue 44 add textarea component
2 parents 96ab16f + fd23ac7 commit 1034a13

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"use client";
2+
import React, { useState } from "react";
3+
4+
import { Button } from "@/components/ui/button";
5+
import Textarea from "@/components/ui/textarea";
6+
7+
export default function TextareaTestPage() {
8+
const [message, setMessage] = useState("");
9+
10+
const handleSubmit = (e: React.FormEvent) => {
11+
e.preventDefault();
12+
alert(`Submitted text: ${message}`);
13+
};
14+
15+
return (
16+
<div className="min-h-screen p-8">
17+
<div className="mx-auto my-5 flex w-full max-w-lg flex-col items-center justify-center">
18+
<form onSubmit={handleSubmit} className="w-full rounded-md border p-4">
19+
<Textarea // basic textarea
20+
name="message"
21+
value={message}
22+
onChange={setMessage}
23+
/>
24+
{/* <Textarea // with customization
25+
id="message"
26+
name="message"
27+
label="Additional message"
28+
placeholder="This is an textarea with customization"
29+
rows={6}
30+
value={message}
31+
onChange={setMessage}
32+
required={true}
33+
className="bg-green-100 placeholder:text-red-600 w-[90%] bg-green-100"
34+
/> */}
35+
<Button type="submit" className="mx-auto mt-4">
36+
Submit
37+
</Button>
38+
</form>
39+
<p className="mt-4">Current value message: {message}</p>
40+
</div>
41+
</div>
42+
);
43+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)