-
Notifications
You must be signed in to change notification settings - Fork 49
/
Textarea.tsx
200 lines (189 loc) · 4.51 KB
/
Textarea.tsx
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import classNames from "classnames";
import React, {
useCallback,
useEffect,
useId,
useLayoutEffect,
useRef,
useState,
} from "react";
import type { TextareaHTMLAttributes, ReactNode } from "react";
import Field from "../Field";
import type { ClassName, PropsWithSpread } from "types";
/**
* The props for the Textarea component.
*/
export type Props = PropsWithSpread<
{
/**
* The content for caution validation.
*/
caution?: ReactNode;
/**
* Optional class(es) to pass to the textarea element.
*/
className?: ClassName;
/**
* The content for error validation.
*/
error?: ReactNode;
/**
* Whether the textarea should grow to fit the content automatically.
*/
grow?: boolean;
/**
* Help text to show below the field.
*/
help?: ReactNode;
/**
* The id of the textarea.
*/
id?: string;
/**
* The label for the field.
*/
label?: ReactNode;
/**
* Optional class(es) to pass to the label component.
*/
labelClassName?: string;
/**
* Whether the field is required.
*/
required?: boolean;
/**
* Whether the form field should have a stacked appearance.
*/
stacked?: boolean;
/**
* The content for success validation.
*/
success?: ReactNode;
/**
* Whether to focus on the input on initial render.
*/
takeFocus?: boolean;
/**
* Optional class(es) to pass to the wrapping Field component
*/
wrapperClassName?: string;
/**
* Function to occur on keyboard event, 'CTRL + Enter'
*/
onControlEnter?: () => void;
},
TextareaHTMLAttributes<HTMLTextAreaElement>
>;
/**
* This is a [React](https://reactjs.org/) component for the Vanilla [Textarea](https://docs.vanillaframework.io/base/forms/#textarea).
*
* The Textarea component defines a multi-line text input control.
*/
const Textarea = ({
caution,
className,
error,
grow = false,
help,
id,
label,
labelClassName,
onKeyUp,
onControlEnter,
required,
stacked,
style,
success,
takeFocus = false,
wrapperClassName,
...props
}: Props): JSX.Element => {
const textareaRef = useRef(null);
const validationId = useId();
const helpId = useId();
const hasError = !!error;
const [innerValue, setInnervalue] = useState(props.defaultValue);
const defaultTextAreaId = useId();
const textAreaId = id || defaultTextAreaId;
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
if (
event.key === "Enter" &&
(event.ctrlKey || event.metaKey) &&
document.activeElement === textareaRef.current
) {
onControlEnter();
}
},
[onControlEnter]
);
useEffect(() => {
document.addEventListener("keydown", handleKeyDown);
return () => {
document.removeEventListener("keydown", handleKeyDown);
};
}, [handleKeyDown]);
useEffect(() => {
if (takeFocus) {
textareaRef.current.focus();
}
}, [takeFocus]);
useLayoutEffect(() => {
if (grow) {
const textArea = textareaRef.current;
if (textArea) {
textArea.style.height = "0px";
const scrollHeight = textArea.scrollHeight;
textArea.style.height = `${scrollHeight}px`;
}
}
}, [textareaRef, grow, innerValue, props.value]);
return (
<Field
caution={caution}
className={wrapperClassName}
error={error}
forId={textAreaId}
help={help}
helpId={helpId}
label={label}
labelClassName={labelClassName}
required={required}
stacked={stacked}
success={success}
validationId={validationId}
>
<textarea
aria-describedby={[help ? helpId : null, success ? validationId : null]
.filter(Boolean)
.join(" ")}
aria-errormessage={hasError ? validationId : null}
aria-invalid={hasError}
className={classNames("p-form-validation__input", className)}
id={textAreaId}
onKeyUp={(evt) => {
onKeyUp && onKeyUp(evt);
}}
onChange={(evt) => {
if (!props.value) {
setInnervalue(evt.target.value);
}
}}
ref={textareaRef}
style={
(grow && {
minHeight: "5rem",
resize: "none",
overflow: "hidden",
boxSizing: "border-box",
...style,
}) ||
style
}
{...props}
value={props.value ?? innerValue}
/>
</Field>
);
};
export default Textarea;