@instructure/ui-text-area 11.6.0 → 11.6.1-snapshot-129

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CHANGELOG.md +40 -291
  2. package/es/TextArea/{index.js → v1/index.js} +2 -2
  3. package/es/TextArea/v2/index.js +350 -0
  4. package/es/TextArea/v2/props.js +26 -0
  5. package/es/TextArea/v2/styles.js +129 -0
  6. package/es/TextArea/v2/theme.js +76 -0
  7. package/es/{index.js → exports/a.js} +1 -1
  8. package/{src/index.ts → es/exports/b.js} +1 -2
  9. package/lib/TextArea/{index.js → v1/index.js} +4 -4
  10. package/lib/TextArea/v2/index.js +361 -0
  11. package/lib/TextArea/v2/props.js +31 -0
  12. package/lib/TextArea/v2/styles.js +135 -0
  13. package/lib/TextArea/v2/theme.js +82 -0
  14. package/lib/{index.js → exports/a.js} +2 -2
  15. package/lib/exports/b.js +12 -0
  16. package/package.json +43 -21
  17. package/src/TextArea/{index.tsx → v1/index.tsx} +2 -2
  18. package/src/TextArea/{props.ts → v1/props.ts} +1 -1
  19. package/src/TextArea/{styles.ts → v1/styles.ts} +4 -3
  20. package/src/TextArea/v2/README.md +136 -0
  21. package/src/TextArea/v2/index.tsx +434 -0
  22. package/src/TextArea/v2/props.ts +160 -0
  23. package/src/TextArea/v2/styles.ts +156 -0
  24. package/src/TextArea/v2/theme.ts +87 -0
  25. package/src/exports/a.ts +25 -0
  26. package/src/exports/b.ts +25 -0
  27. package/tsconfig.build.tsbuildinfo +1 -1
  28. package/types/TextArea/{index.d.ts → v1/index.d.ts} +1 -1
  29. package/types/TextArea/v1/index.d.ts.map +1 -0
  30. package/types/TextArea/{props.d.ts → v1/props.d.ts} +1 -1
  31. package/types/TextArea/v1/props.d.ts.map +1 -0
  32. package/types/TextArea/v1/styles.d.ts.map +1 -0
  33. package/types/TextArea/v1/theme.d.ts.map +1 -0
  34. package/types/TextArea/v2/index.d.ts +84 -0
  35. package/types/TextArea/v2/index.d.ts.map +1 -0
  36. package/types/TextArea/v2/props.d.ts +92 -0
  37. package/types/TextArea/v2/props.d.ts.map +1 -0
  38. package/types/TextArea/v2/styles.d.ts +21 -0
  39. package/types/TextArea/v2/styles.d.ts.map +1 -0
  40. package/types/TextArea/v2/theme.d.ts +10 -0
  41. package/types/TextArea/v2/theme.d.ts.map +1 -0
  42. package/types/exports/a.d.ts +3 -0
  43. package/types/exports/a.d.ts.map +1 -0
  44. package/types/exports/b.d.ts +3 -0
  45. package/types/exports/b.d.ts.map +1 -0
  46. package/types/TextArea/index.d.ts.map +0 -1
  47. package/types/TextArea/props.d.ts.map +0 -1
  48. package/types/TextArea/styles.d.ts.map +0 -1
  49. package/types/TextArea/theme.d.ts.map +0 -1
  50. package/types/index.d.ts +0 -3
  51. package/types/index.d.ts.map +0 -1
  52. /package/es/TextArea/{props.js → v1/props.js} +0 -0
  53. /package/es/TextArea/{styles.js → v1/styles.js} +0 -0
  54. /package/es/TextArea/{theme.js → v1/theme.js} +0 -0
  55. /package/lib/TextArea/{props.js → v1/props.js} +0 -0
  56. /package/lib/TextArea/{styles.js → v1/styles.js} +0 -0
  57. /package/lib/TextArea/{theme.js → v1/theme.js} +0 -0
  58. /package/src/TextArea/{README.md → v1/README.md} +0 -0
  59. /package/src/TextArea/{theme.ts → v1/theme.ts} +0 -0
  60. /package/types/TextArea/{styles.d.ts → v1/styles.d.ts} +0 -0
  61. /package/types/TextArea/{theme.d.ts → v1/theme.d.ts} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instructure/ui-text-area",
3
- "version": "11.6.0",
3
+ "version": "11.6.1-snapshot-129",
4
4
  "description": "A styled HTML text area component",
5
5
  "author": "Instructure, Inc. Engineering and Product Design",
6
6
  "module": "./es/index.js",
@@ -15,25 +15,25 @@
15
15
  "license": "MIT",
16
16
  "dependencies": {
17
17
  "@babel/runtime": "^7.27.6",
18
- "@instructure/emotion": "11.6.0",
19
- "@instructure/debounce": "11.6.0",
20
- "@instructure/shared-types": "11.6.0",
21
- "@instructure/ui-a11y-utils": "11.6.0",
22
- "@instructure/ui-dom-utils": "11.6.0",
23
- "@instructure/ui-react-utils": "11.6.0",
24
- "@instructure/ui-utils": "11.6.0",
25
- "@instructure/uid": "11.6.0",
26
- "@instructure/ui-form-field": "11.6.0"
18
+ "@instructure/debounce": "11.6.1-snapshot-129",
19
+ "@instructure/shared-types": "11.6.1-snapshot-129",
20
+ "@instructure/emotion": "11.6.1-snapshot-129",
21
+ "@instructure/ui-dom-utils": "11.6.1-snapshot-129",
22
+ "@instructure/ui-a11y-utils": "11.6.1-snapshot-129",
23
+ "@instructure/ui-form-field": "11.6.1-snapshot-129",
24
+ "@instructure/ui-react-utils": "11.6.1-snapshot-129",
25
+ "@instructure/uid": "11.6.1-snapshot-129",
26
+ "@instructure/ui-utils": "11.6.1-snapshot-129"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@testing-library/jest-dom": "^6.6.3",
30
30
  "@testing-library/react": "15.0.7",
31
31
  "@testing-library/user-event": "^14.6.1",
32
32
  "vitest": "^3.2.2",
33
- "@instructure/ui-babel-preset": "11.6.0",
34
- "@instructure/ui-axe-check": "11.6.0",
35
- "@instructure/ui-color-utils": "11.6.0",
36
- "@instructure/ui-themes": "11.6.0"
33
+ "@instructure/ui-axe-check": "11.6.1-snapshot-129",
34
+ "@instructure/ui-color-utils": "11.6.1-snapshot-129",
35
+ "@instructure/ui-themes": "11.6.1-snapshot-129",
36
+ "@instructure/ui-babel-preset": "11.6.1-snapshot-129"
37
37
  },
38
38
  "peerDependencies": {
39
39
  "react": ">=18 <=19"
@@ -43,17 +43,39 @@
43
43
  },
44
44
  "sideEffects": false,
45
45
  "exports": {
46
- ".": {
47
- "types": "./types/index.d.ts",
48
- "import": "./es/index.js",
49
- "require": "./lib/index.js",
50
- "default": "./es/index.js"
51
- },
52
46
  "./lib/*": "./lib/*",
53
47
  "./es/*": "./es/*",
54
48
  "./types/*": "./types/*",
55
49
  "./package.json": "./package.json",
56
- "./src/*": "./src/*"
50
+ "./src/*": "./src/*",
51
+ ".": {
52
+ "src": "./src/exports/a.ts",
53
+ "types": "./types/exports/a.d.ts",
54
+ "import": "./es/exports/a.js",
55
+ "require": "./lib/exports/a.js",
56
+ "default": "./es/exports/a.js"
57
+ },
58
+ "./v11_6": {
59
+ "src": "./src/exports/a.ts",
60
+ "types": "./types/exports/a.d.ts",
61
+ "import": "./es/exports/a.js",
62
+ "require": "./lib/exports/a.js",
63
+ "default": "./es/exports/a.js"
64
+ },
65
+ "./v11_7": {
66
+ "src": "./src/exports/b.ts",
67
+ "types": "./types/exports/b.d.ts",
68
+ "import": "./es/exports/b.js",
69
+ "require": "./lib/exports/b.js",
70
+ "default": "./es/exports/b.js"
71
+ },
72
+ "./latest": {
73
+ "src": "./src/exports/b.ts",
74
+ "types": "./types/exports/b.d.ts",
75
+ "import": "./es/exports/b.js",
76
+ "require": "./lib/exports/b.js",
77
+ "default": "./es/exports/b.js"
78
+ }
57
79
  },
58
80
  "scripts": {
59
81
  "lint": "ui-scripts lint",
@@ -23,7 +23,7 @@
23
23
  */
24
24
 
25
25
  import { Fragment, Component } from 'react'
26
- import { FormField } from '@instructure/ui-form-field'
26
+ import { FormField } from '@instructure/ui-form-field/v11_6'
27
27
  import {
28
28
  addEventListener,
29
29
  isActiveElement,
@@ -33,7 +33,7 @@ import {
33
33
  import type { RequestAnimationFrameType } from '@instructure/ui-dom-utils'
34
34
  import { debounce } from '@instructure/debounce'
35
35
  import type { Debounced } from '@instructure/debounce'
36
- import { withStyle } from '@instructure/emotion'
36
+ import { withStyleLegacy as withStyle } from '@instructure/emotion'
37
37
  import { px } from '@instructure/ui-utils'
38
38
  import {
39
39
  omitProps,
@@ -29,7 +29,7 @@ import type {
29
29
  TextAreaTheme,
30
30
  PickPropsWithExceptions
31
31
  } from '@instructure/shared-types'
32
- import type { FormMessage, FormFieldOwnProps } from '@instructure/ui-form-field'
32
+ import type { FormMessage, FormFieldOwnProps } from '@instructure/ui-form-field/v11_6'
33
33
  import type {
34
34
  WithStyleProps,
35
35
  ComponentStyle,
@@ -88,9 +88,10 @@ const generateStyle = (
88
88
  transform: 'scale(1)',
89
89
  opacity: 1
90
90
  },
91
- '&[aria-invalid], &[aria-invalid]:focus, &[aria-invalid]:focus + [class$=-textArea__outline]': {
92
- borderColor: componentTheme.errorBorderColor
93
- },
91
+ '&[aria-invalid], &[aria-invalid]:focus, &[aria-invalid]:focus + [class$=-textArea__outline]':
92
+ {
93
+ borderColor: componentTheme.errorBorderColor
94
+ },
94
95
  '&::placeholder': {
95
96
  color: componentTheme.placeholderColor
96
97
  },
@@ -0,0 +1,136 @@
1
+ ---
2
+ describes: TextArea
3
+ ---
4
+
5
+ TextArea renders a standard HTML `textarea` element. By default, TextArea will
6
+ resize to fit its content. Use the `maxHeight` property to set a limit on the
7
+ component's height.
8
+
9
+ ```js
10
+ ---
11
+ type: example
12
+ ---
13
+ <div>
14
+ <TextArea
15
+ label="Enter/delete text to see TextArea resize"
16
+ />
17
+ <br />
18
+ <TextArea
19
+ label="I can't go any higher than 10rem (160px)"
20
+ maxHeight="10rem"
21
+ />
22
+ </div>
23
+ ```
24
+
25
+ To allow the user to manually resize TextArea (in supported browsers), use the
26
+ `resize` property.
27
+
28
+ ```js
29
+ ---
30
+ type: example
31
+ ---
32
+ <TextArea label="Description" resize="vertical" />
33
+ ```
34
+
35
+ A `TextArea` with errors:
36
+
37
+ ```js
38
+ ---
39
+ type: example
40
+ ---
41
+ <TextArea messages={[{ text: 'Invalid description', type: 'error' }]} label="Description" />
42
+ ```
43
+
44
+ A disabled `TextArea`:
45
+
46
+ ```js
47
+ ---
48
+ type: example
49
+ ---
50
+ <TextArea label="Description" disabled />
51
+ ```
52
+
53
+ A `TextArea` with a screenreader only label:
54
+
55
+ ```js
56
+ ---
57
+ type: example
58
+ ---
59
+ <TextArea
60
+ label={<ScreenReaderContent>Description</ScreenReaderContent>}
61
+ placeholder="describe something"
62
+ />
63
+ ```
64
+
65
+ An inline `TextArea` with a fixed width, initial height and maxHeight.
66
+
67
+ ```js
68
+ ---
69
+ type: example
70
+ ---
71
+ <div style={{ display: 'flex', alignItems: 'center' }}>
72
+ <TextArea
73
+ label={<ScreenReaderContent>Label</ScreenReaderContent>}
74
+ inline
75
+ width="10em"
76
+ height="10em"
77
+ maxHeight="250px"
78
+ />
79
+ </div>
80
+ ```
81
+
82
+ A 'controlled' TextArea:
83
+
84
+ ```js
85
+ ---
86
+ type: example
87
+ ---
88
+ const Example = () => {
89
+ const [description, setDescription] = useState('Hello World')
90
+
91
+ const handleChange = (e) => setDescription(e.target.value)
92
+
93
+ return (
94
+ <TextArea
95
+ label="Description"
96
+ value={description}
97
+ onChange={handleChange}
98
+ />
99
+ )
100
+ }
101
+
102
+ render(<Example />)
103
+ ```
104
+
105
+ ### Guidelines
106
+
107
+ ```js
108
+ ---
109
+ type: embed
110
+ ---
111
+ <Guidelines>
112
+ <Figure recommendation="yes" title="Do">
113
+ <Figure.Item>Align text to the left</Figure.Item>
114
+ </Figure>
115
+ <Figure recommendation="no" title="Don't">
116
+ <Figure.Item>Repeat label text if using placeholder text</Figure.Item>
117
+ <Figure.Item>Use for rich text editing</Figure.Item>
118
+ <Figure.Item>Allow resizing of the TextArea to break the layout of a page</Figure.Item>
119
+ <Figure.Item>Place labels to the right of the TextArea</Figure.Item>
120
+ </Figure>
121
+ </Guidelines>
122
+ ```
123
+
124
+ ```js
125
+ ---
126
+ type: embed
127
+ ---
128
+ <Guidelines>
129
+ <Alert
130
+ variant="info"
131
+ margin="small"
132
+ >
133
+ Every non-listed prop will be passed down to the underlying 'textarea' element, such as 'onBlur' and any other prop
134
+ </Alert>
135
+ </Guidelines>
136
+ ```
@@ -0,0 +1,434 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import {
26
+ forwardRef,
27
+ useRef,
28
+ useEffect,
29
+ useContext,
30
+ useImperativeHandle,
31
+ useCallback,
32
+ useMemo
33
+ } from 'react'
34
+ import { FormField } from '@instructure/ui-form-field/latest'
35
+ import {
36
+ addEventListener,
37
+ isActiveElement,
38
+ requestAnimationFrame,
39
+ getBoundingClientRect
40
+ } from '@instructure/ui-dom-utils'
41
+ import type { RequestAnimationFrameType } from '@instructure/ui-dom-utils'
42
+ import { debounce } from '@instructure/debounce'
43
+ import type { Debounced } from '@instructure/debounce'
44
+
45
+ import { useStyle } from '@instructure/emotion'
46
+ import { generateId, px } from '@instructure/ui-utils'
47
+
48
+ import {
49
+ passthroughProps,
50
+ pickProps,
51
+ DeterministicIdContext
52
+ } from '@instructure/ui-react-utils'
53
+
54
+ import generateStyle from './styles'
55
+ import type { TextAreaProps } from './props'
56
+
57
+ export type TextAreaElement = {
58
+ focus: () => void
59
+ readonly value: string
60
+ readonly minHeight: string
61
+ readonly focused: boolean
62
+ readonly invalid: boolean
63
+ readonly id: string
64
+ }
65
+
66
+ /**
67
+ ---
68
+ category: components
69
+ ---
70
+ **/
71
+ const TextArea = forwardRef<TextAreaElement, TextAreaProps>((props, ref) => {
72
+ const {
73
+ size = 'medium',
74
+ autoGrow = true,
75
+ resize = 'none',
76
+ inline = false,
77
+ messages = [],
78
+ disabled = false,
79
+ readOnly = false,
80
+ layout = 'stacked',
81
+ required = false,
82
+ placeholder,
83
+ value,
84
+ defaultValue,
85
+ width,
86
+ height,
87
+ maxHeight,
88
+ textareaRef,
89
+ margin,
90
+ label,
91
+ onChange,
92
+ id: propId,
93
+ themeOverride,
94
+ ...rest
95
+ } = props
96
+
97
+ // Use deterministic ID
98
+ const instanceCounterMap = useContext(DeterministicIdContext)
99
+ const defaultId = useMemo(
100
+ () => generateId('TextArea', instanceCounterMap),
101
+ [instanceCounterMap]
102
+ )
103
+ const id = propId || defaultId
104
+
105
+ // Use refs for mutable values
106
+ const _listener = useRef<{ remove(): void } | undefined>()
107
+ const _request = useRef<RequestAnimationFrameType | undefined>()
108
+ const _textareaResizeListener = useRef<ResizeObserver | undefined>()
109
+ const _debounced = useRef<Debounced<() => void> | undefined>()
110
+ const _textarea = useRef<HTMLTextAreaElement | null>(null)
111
+ const _container = useRef<HTMLDivElement | null>(null)
112
+ const _height = useRef<string | undefined>()
113
+ const _manuallyResized = useRef(false)
114
+ const formFieldRef = useRef<Element | null>(null)
115
+
116
+ // Compute invalid state
117
+ const { isInvalid, isSuccess } = useMemo(() => {
118
+ const isInvalid =
119
+ messages &&
120
+ messages.findIndex(
121
+ (message) => message.type === 'error' || message.type === 'newError'
122
+ ) >= 0
123
+ const isSuccess =
124
+ messages &&
125
+ messages.findIndex((message) => message.type === 'success') >= 0
126
+ return { isInvalid, isSuccess }
127
+ }, [messages])
128
+
129
+ // Use styles
130
+ const styles = useStyle({
131
+ generateStyle,
132
+ themeOverride,
133
+ params: {
134
+ size,
135
+ disabled,
136
+ readOnly,
137
+ success: isSuccess,
138
+ invalid: isInvalid
139
+ },
140
+ componentId: 'TextArea',
141
+ displayName: 'TextArea'
142
+ })
143
+
144
+ // Mock ResizeObserver for SSR
145
+ useEffect(() => {
146
+ if (typeof window === 'undefined') {
147
+ global.ResizeObserver = class ResizeObserver {
148
+ observe() {
149
+ // do nothing
150
+ }
151
+ unobserve() {
152
+ // do nothing
153
+ }
154
+ disconnect() {
155
+ // do nothing
156
+ }
157
+ }
158
+ }
159
+ }, [])
160
+
161
+ // Grow function
162
+ const grow = useCallback(() => {
163
+ if (!_textarea.current || _manuallyResized.current) {
164
+ return
165
+ }
166
+ const offset =
167
+ _textarea.current.offsetHeight - _textarea.current.clientHeight
168
+ let heightValue = ''
169
+
170
+ // Notes:
171
+ // 1. height has to be reset to `auto` every time this method runs, or scrollHeight will not reset
172
+ // 2. `_textarea.current.scrollHeight` will not reset if assigned to a variable; it needs to be written out each time
173
+ _textarea.current.style.height = 'auto'
174
+ _textarea.current.style.overflowY = 'hidden' // hide scrollbars for autoGrow textareas
175
+ heightValue = _textarea.current.scrollHeight + offset + 'px'
176
+
177
+ const maxHeightPx = maxHeight
178
+ ? px(maxHeight, _container.current)
179
+ : undefined
180
+
181
+ if (
182
+ maxHeight &&
183
+ maxHeightPx !== undefined &&
184
+ _textarea.current.scrollHeight > maxHeightPx
185
+ ) {
186
+ _textarea.current.style.overflowY = 'auto' // add scroll if scrollHeight exceeds maxHeight in pixels
187
+ } else if (height) {
188
+ if (_textarea.current.value === '') {
189
+ heightValue = height
190
+ } else if (
191
+ px(height, _container.current) > _textarea.current.scrollHeight
192
+ ) {
193
+ _textarea.current.style.overflowY = 'auto' // add scroll if scrollHeight exceeds height in pixels
194
+ heightValue = height
195
+ }
196
+ }
197
+
198
+ // preserve container height to prevent scroll jumping on long textareas,
199
+ // but make sure container doesn't exceed maxHeight prop
200
+ const heightExceedsMax =
201
+ maxHeightPx !== undefined && px(heightValue) > maxHeightPx
202
+ if (!heightExceedsMax && _container.current) {
203
+ _container.current.style.minHeight = heightValue
204
+ }
205
+
206
+ _height.current = heightValue
207
+ _textarea.current.style.height = heightValue
208
+ }, [height, maxHeight])
209
+
210
+ // Textarea resize handler
211
+ const _textareaResize = useCallback(() => {
212
+ if (!_textarea.current) return
213
+
214
+ const textareaHeight = _textarea.current.style.height
215
+ if (textareaHeight !== '' && textareaHeight !== _height.current) {
216
+ _manuallyResized.current = true
217
+ _textarea.current.style.overflowY = 'auto'
218
+
219
+ // update container minHeight to ensure focus ring always wraps input
220
+ if (_container.current) {
221
+ _container.current.style.minHeight = textareaHeight
222
+ }
223
+ }
224
+ }, [])
225
+
226
+ // AutoGrow effect
227
+ useEffect(() => {
228
+ if (autoGrow) {
229
+ if (!_debounced.current) {
230
+ _debounced.current = debounce(grow, 200, {
231
+ leading: false,
232
+ trailing: true
233
+ })
234
+ }
235
+
236
+ if (!_listener.current) {
237
+ _listener.current = addEventListener(
238
+ window,
239
+ 'resize',
240
+ _debounced.current
241
+ )
242
+ }
243
+
244
+ if (_textarea.current && !_textareaResizeListener.current) {
245
+ const { height: origHeight } = getBoundingClientRect(_textarea.current)
246
+ _textareaResizeListener.current = new ResizeObserver((entries) => {
247
+ for (const entry of entries) {
248
+ const { height: entryHeight } = entry.contentRect
249
+
250
+ if (origHeight !== entryHeight) {
251
+ _textareaResize()
252
+ }
253
+ }
254
+ })
255
+
256
+ _textareaResizeListener.current.observe(_textarea.current)
257
+ }
258
+
259
+ _request.current = requestAnimationFrame(grow)
260
+ }
261
+ }, [autoGrow, grow, _textareaResize])
262
+
263
+ // Cleanup effect
264
+ useEffect(() => {
265
+ return () => {
266
+ if (_listener.current) {
267
+ _listener.current.remove()
268
+ }
269
+
270
+ if (_textareaResizeListener.current) {
271
+ _textareaResizeListener.current.disconnect()
272
+ }
273
+
274
+ if (_request.current) {
275
+ _request.current.cancel()
276
+ }
277
+
278
+ if (_debounced.current) {
279
+ _debounced.current.cancel()
280
+ }
281
+ }
282
+ }, [])
283
+
284
+ // Handle change
285
+ const handleChange = useCallback(
286
+ (event: React.ChangeEvent<HTMLTextAreaElement>) => {
287
+ if (disabled || readOnly) {
288
+ event.preventDefault()
289
+ return
290
+ }
291
+
292
+ if (typeof value === 'undefined') {
293
+ // if uncontrolled
294
+ if (autoGrow) {
295
+ if (!_debounced.current) {
296
+ _debounced.current = debounce(grow, 200, {
297
+ leading: false,
298
+ trailing: true
299
+ })
300
+ }
301
+
302
+ if (!_listener.current) {
303
+ _listener.current = addEventListener(
304
+ window,
305
+ 'resize',
306
+ _debounced.current
307
+ )
308
+ }
309
+
310
+ if (_textarea.current && !_textareaResizeListener.current) {
311
+ const { height: origHeight } = getBoundingClientRect(
312
+ _textarea.current
313
+ )
314
+ _textareaResizeListener.current = new ResizeObserver((entries) => {
315
+ for (const entry of entries) {
316
+ const { height: entryHeight } = entry.contentRect
317
+
318
+ if (origHeight !== entryHeight) {
319
+ _textareaResize()
320
+ }
321
+ }
322
+ })
323
+
324
+ _textareaResizeListener.current.observe(_textarea.current)
325
+ }
326
+
327
+ _request.current = requestAnimationFrame(grow)
328
+ }
329
+ }
330
+
331
+ if (typeof onChange === 'function') {
332
+ onChange(event)
333
+ }
334
+ },
335
+ [disabled, readOnly, value, onChange, autoGrow, grow, _textareaResize]
336
+ )
337
+
338
+ // Expose API through ref using useImperativeHandle
339
+ useImperativeHandle(
340
+ ref,
341
+ () => ({
342
+ focus: () => {
343
+ _textarea.current?.focus()
344
+ },
345
+ get value() {
346
+ return _textarea.current?.value || ''
347
+ },
348
+ get minHeight() {
349
+ return _textarea.current?.style.minHeight || ''
350
+ },
351
+ get focused() {
352
+ return isActiveElement(_textarea.current)
353
+ },
354
+ get invalid() {
355
+ return (
356
+ messages &&
357
+ messages.findIndex(
358
+ (message) => message.type === 'error' || message.type === 'newError'
359
+ ) >= 0
360
+ )
361
+ },
362
+ get id() {
363
+ return id
364
+ }
365
+ }),
366
+ [id, messages]
367
+ )
368
+
369
+ const style = {
370
+ width,
371
+ resize,
372
+ height: !autoGrow ? height : undefined,
373
+ maxHeight
374
+ }
375
+
376
+ const textarea = (
377
+ <textarea
378
+ {...passthroughProps(rest)}
379
+ value={value}
380
+ defaultValue={defaultValue}
381
+ placeholder={placeholder}
382
+ ref={(textarea) => {
383
+ _textarea.current = textarea
384
+ if (typeof textareaRef === 'function') {
385
+ textareaRef(textarea)
386
+ }
387
+ }}
388
+ style={style}
389
+ id={id}
390
+ required={required}
391
+ aria-required={required}
392
+ aria-invalid={isInvalid ? 'true' : undefined}
393
+ disabled={disabled}
394
+ readOnly={readOnly}
395
+ css={styles?.textArea}
396
+ onChange={handleChange}
397
+ />
398
+ )
399
+
400
+ return (
401
+ <FormField
402
+ {...pickProps(props, FormField.allowedProps)}
403
+ label={label}
404
+ vAlign="top"
405
+ id={id}
406
+ elementRef={(el) => {
407
+ formFieldRef.current = el
408
+ }}
409
+ margin={margin}
410
+ isRequired={required}
411
+ disabled={disabled}
412
+ readOnly={readOnly}
413
+ data-cid="TextArea"
414
+ >
415
+ <div
416
+ css={styles?.textAreaLayout}
417
+ style={{
418
+ width,
419
+ maxHeight
420
+ }}
421
+ ref={(node) => {
422
+ _container.current = node
423
+ }}
424
+ >
425
+ {textarea}
426
+ </div>
427
+ </FormField>
428
+ )
429
+ })
430
+
431
+ TextArea.displayName = 'TextArea'
432
+
433
+ export default TextArea
434
+ export { TextArea }