-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
TextArea.tsx
197 lines (178 loc) · 4.41 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
import React, {
CSSProperties,
FC,
forwardRef,
ReactNode,
TextareaHTMLAttributes,
} from "react";
import cn from "classnames";
import { bem } from "@react-md/theme";
import { WithForwardedRef } from "@react-md/utils";
import FloatingLabel from "./FloatingLabel";
import TextFieldContainer, {
TextFieldContainerOptions,
} from "./TextFieldContainer";
import useFocusState from "./useFocusState";
import useTextAreaHeightAnimation from "./useTextAreaHeightAnimation";
import useValuedState from "./useValuedState";
export interface TextAreaProps
extends TextareaHTMLAttributes<HTMLTextAreaElement>,
TextFieldContainerOptions {
/**
* An id for the textarea. This is required for a11y.
*/
id: string;
/**
* An optional label to display with the textarea. This will be wrapped in the `<Label>`
* component and required props.
*/
label?: ReactNode;
/**
* The default value to display.
*/
defaultValue?: string;
/**
*
*/
resize?: "auto" | "horizontal" | "vertical" | "both" | "none";
/**
*
*/
rows?: number;
/**
* The max number of lines that an animatiable textarea
*/
maxRows?: number;
areaStyle?: CSSProperties;
areaClassName?: string;
}
type WithRef = WithForwardedRef<HTMLTextAreaElement>;
type DefaultProps = Required<
Pick<
TextAreaProps,
| "theme"
| "resize"
| "defaultValue"
| "underlineDirection"
| "error"
| "rows"
| "maxRows"
>
>;
type WithDefaultProps = TextAreaProps & DefaultProps & WithRef;
const block = bem("rmd-text-field");
const TextArea: FC<TextAreaProps & WithRef> = providedProps => {
const {
style: propStyle,
className,
areaStyle,
areaClassName,
forwardedRef,
resize,
label,
theme,
error,
inline: propInline,
onBlur: propOnBlur,
onFocus: propOnFocus,
onChange: propOnChange,
underlineDirection,
maxRows,
...props
} = providedProps as WithDefaultProps;
const { id, defaultValue, rows } = props;
let inline = propInline;
if (typeof propInline !== "boolean") {
inline = resize === "horizontal" || resize === "both";
}
const filled = theme === "filled";
const outline = theme === "outline";
const underline = theme === "underline";
const unstyled = theme === "none";
const growable = resize === "auto";
const { style, maskRef, areaRef, update } = useTextAreaHeightAnimation({
style: propStyle,
ref: forwardedRef,
rows,
maxRows,
disabled: resize !== "auto",
});
const { focused, onBlur, onFocus } = useFocusState({
onBlur: propOnBlur,
onFocus: propOnFocus,
});
const { valued, onChange } = useValuedState({
defaultValue,
onChange: evt => {
const event = evt as React.ChangeEvent<HTMLTextAreaElement>;
if (propOnChange) {
propOnChange(event);
}
update(event.currentTarget.value);
},
});
const mergedClassName = block({
area: true,
"area-underline": underline || filled,
outline,
underline: underline || filled,
floating: !unstyled && label,
unresizable: growable || resize === "none",
"resize-h": resize === "horizontal",
"resize-v": resize === "vertical",
});
return (
<TextFieldContainer
style={style}
className={className}
inline={inline}
theme={theme}
error={error}
active={focused}
growable={growable}
underlineDirection={underlineDirection}
>
<FloatingLabel
htmlFor={id}
error={error}
active={focused}
covering={!outline}
valued={valued}
>
{label}
</FloatingLabel>
{growable && (
<textarea
aria-hidden
readOnly
tabIndex={-1}
ref={maskRef}
style={areaStyle}
className={cn(mergedClassName, "rmd-text-field--mask", areaClassName)}
/>
)}
<textarea
{...props}
ref={areaRef}
onFocus={onFocus}
onBlur={onBlur}
onChange={onChange}
style={areaStyle}
className={cn(mergedClassName, areaClassName)}
/>
</TextFieldContainer>
);
};
const defaultProps: DefaultProps = {
theme: "underline",
error: false,
resize: "auto",
defaultValue: "",
rows: 2,
maxRows: -1,
underlineDirection: "left",
};
TextArea.defaultProps = defaultProps;
export default forwardRef<HTMLTextAreaElement, TextAreaProps>((props, ref) => (
<TextArea {...props} forwardedRef={ref} />
));