@dbcdk/react-components 0.0.61 → 0.0.62
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
import { useCallback, useId, useMemo } from 'react';
|
|
3
|
+
import { useCallback, useId, useLayoutEffect, useMemo, useRef } from 'react';
|
|
4
4
|
import { useTooltipTrigger } from '../../../components/overlay/tooltip/useTooltipTrigger';
|
|
5
5
|
import styles from './Textarea.module.css';
|
|
6
6
|
import { InputContainer } from '../input-container/InputContainer';
|
|
@@ -9,17 +9,28 @@ export const Textarea = function Textarea({ value, inputChanged, disabled, rows
|
|
|
9
9
|
label, error, helpText, orientation = 'horizontal', labelWidth = '160px', fullWidth = false, required,
|
|
10
10
|
// Native textarea props
|
|
11
11
|
className, ...rest }) {
|
|
12
|
+
const HEIGHT_BUFFER_PX = 2;
|
|
12
13
|
const generatedId = useId();
|
|
13
14
|
const textareaId = id !== null && id !== void 0 ? id : `textarea-${generatedId}`;
|
|
15
|
+
const textareaRef = useRef(null);
|
|
16
|
+
const syncHeight = useCallback((textarea) => {
|
|
17
|
+
if (!adjustHeight)
|
|
18
|
+
return;
|
|
19
|
+
textarea.style.height = 'auto';
|
|
20
|
+
textarea.style.height = `${textarea.scrollHeight + HEIGHT_BUFFER_PX}px`;
|
|
21
|
+
}, [adjustHeight]);
|
|
22
|
+
useLayoutEffect(() => {
|
|
23
|
+
const textarea = textareaRef.current;
|
|
24
|
+
if (!textarea)
|
|
25
|
+
return;
|
|
26
|
+
syncHeight(textarea);
|
|
27
|
+
}, [syncHeight, value, rows]);
|
|
14
28
|
const onInput = useCallback((e) => {
|
|
15
29
|
const textarea = e.currentTarget;
|
|
16
|
-
|
|
17
|
-
textarea.style.height = 'auto';
|
|
18
|
-
textarea.style.height = `${textarea.scrollHeight}px`;
|
|
19
|
-
}
|
|
30
|
+
syncHeight(textarea);
|
|
20
31
|
inputChanged(e);
|
|
21
|
-
}, [inputChanged,
|
|
22
|
-
const inputField = useMemo(() => (_jsx("textarea", { ...rest, id: textareaId, className: [styles.textarea, className].filter(Boolean).join(' '), placeholder: placeholder, rows: rows, value: value, onChange: onInput, disabled: disabled, "aria-invalid": Boolean(error) || undefined })), [rest, textareaId, className, placeholder, rows, value, onInput, disabled, error]);
|
|
32
|
+
}, [inputChanged, syncHeight]);
|
|
33
|
+
const inputField = useMemo(() => (_jsx("textarea", { ...rest, ref: textareaRef, id: textareaId, className: [styles.textarea, className].filter(Boolean).join(' '), placeholder: placeholder, rows: rows, value: value, onChange: onInput, disabled: disabled, "aria-invalid": Boolean(error) || undefined })), [rest, textareaId, className, placeholder, rows, value, onInput, disabled, error]);
|
|
23
34
|
// Enable tooltip if tooltip exists, and if showTooltip is either true or undefined.
|
|
24
35
|
// (So you can deprecate showTooltip later without breaking callers.)
|
|
25
36
|
const tooltipEnabled = Boolean(tooltip) && (showTooltip !== null && showTooltip !== void 0 ? showTooltip : true);
|