-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextarea.tsx
More file actions
executable file
·69 lines (61 loc) · 1.69 KB
/
Textarea.tsx
File metadata and controls
executable file
·69 lines (61 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React, { JSX } from "react";
import { TextareaProps } from "./Textarea.types";
import Hint from "../Hint/Hint";
import ErrorMessage from "../ErrorMessage/ErrorMessage";
import Label from "../Label/Label";
const defaultProps: Partial<TextareaProps> = {
"aria-describedby": "",
rows: 5,
id: "",
name: "",
};
const Textarea: React.FC<TextareaProps> = React.forwardRef<
HTMLTextAreaElement,
TextareaProps
>((props: TextareaProps = defaultProps, ref) => {
const {
className,
"aria-describedby": describedBy,
errorMessage,
formGroup,
hint,
label,
id,
...attributes
} = props;
let describedByValue: string = describedBy || "";
let hintComponent: JSX.Element | null = null;
let errorMessageComponent: JSX.Element | null = null;
if (hint) {
const hintId: string = `${id}-hint`;
describedByValue += ` ${hintId}`;
hintComponent = <Hint {...hint} id={hintId} />;
}
if (errorMessage) {
const errorId: string = id ? `${id}-error` : "";
describedByValue += ` ${errorId}`;
errorMessageComponent = <ErrorMessage {...errorMessage} id={errorId} />;
}
return (
<div
className={`govuk-form-group${
errorMessage ? " govuk-form-group--error" : ""
} ${formGroup?.className || ""}`}
>
<Label {...label} htmlFor={id} />
{hintComponent}
{errorMessageComponent}
<textarea
{...attributes}
id={id}
ref={ref}
className={`govuk-textarea${
errorMessage ? " govuk-textarea--error" : ""
} ${className || ""}`}
aria-describedby={describedByValue?.trim() || undefined}
/>
</div>
);
});
Textarea.displayName = "Textarea";
export default Textarea;