1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import { unstable_debounce as debounce, unstable_useForkRef as useForkRef, unstable_useEnhancedEffect as useEnhancedEffect, unstable_ownerWindow as ownerWindow } from '@mui/utils';
|
6 | import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
7 | function getStyleValue(value) {
|
8 | return parseInt(value, 10) || 0;
|
9 | }
|
10 | const styles = {
|
11 | shadow: {
|
12 |
|
13 | visibility: 'hidden',
|
14 |
|
15 | position: 'absolute',
|
16 |
|
17 | overflow: 'hidden',
|
18 | height: 0,
|
19 | top: 0,
|
20 | left: 0,
|
21 |
|
22 | transform: 'translateZ(0)'
|
23 | }
|
24 | };
|
25 | function isEmpty(obj) {
|
26 | return obj === undefined || obj === null || Object.keys(obj).length === 0 || obj.outerHeightStyle === 0 && !obj.overflowing;
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const TextareaAutosize = React.forwardRef(function TextareaAutosize(props, forwardedRef) {
|
40 | const {
|
41 | onChange,
|
42 | maxRows,
|
43 | minRows = 1,
|
44 | style,
|
45 | value,
|
46 | ...other
|
47 | } = props;
|
48 | const {
|
49 | current: isControlled
|
50 | } = React.useRef(value != null);
|
51 | const inputRef = React.useRef(null);
|
52 | const handleRef = useForkRef(forwardedRef, inputRef);
|
53 | const heightRef = React.useRef(null);
|
54 | const shadowRef = React.useRef(null);
|
55 | const calculateTextareaStyles = React.useCallback(() => {
|
56 | const input = inputRef.current;
|
57 | const containerWindow = ownerWindow(input);
|
58 | const computedStyle = containerWindow.getComputedStyle(input);
|
59 |
|
60 |
|
61 | if (computedStyle.width === '0px') {
|
62 | return {
|
63 | outerHeightStyle: 0,
|
64 | overflowing: false
|
65 | };
|
66 | }
|
67 | const inputShallow = shadowRef.current;
|
68 | inputShallow.style.width = computedStyle.width;
|
69 | inputShallow.value = input.value || props.placeholder || 'x';
|
70 | if (inputShallow.value.slice(-1) === '\n') {
|
71 |
|
72 |
|
73 |
|
74 | inputShallow.value += ' ';
|
75 | }
|
76 | const boxSizing = computedStyle.boxSizing;
|
77 | const padding = getStyleValue(computedStyle.paddingBottom) + getStyleValue(computedStyle.paddingTop);
|
78 | const border = getStyleValue(computedStyle.borderBottomWidth) + getStyleValue(computedStyle.borderTopWidth);
|
79 |
|
80 |
|
81 | const innerHeight = inputShallow.scrollHeight;
|
82 |
|
83 |
|
84 | inputShallow.value = 'x';
|
85 | const singleRowHeight = inputShallow.scrollHeight;
|
86 |
|
87 |
|
88 | let outerHeight = innerHeight;
|
89 | if (minRows) {
|
90 | outerHeight = Math.max(Number(minRows) * singleRowHeight, outerHeight);
|
91 | }
|
92 | if (maxRows) {
|
93 | outerHeight = Math.min(Number(maxRows) * singleRowHeight, outerHeight);
|
94 | }
|
95 | outerHeight = Math.max(outerHeight, singleRowHeight);
|
96 |
|
97 |
|
98 | const outerHeightStyle = outerHeight + (boxSizing === 'border-box' ? padding + border : 0);
|
99 | const overflowing = Math.abs(outerHeight - innerHeight) <= 1;
|
100 | return {
|
101 | outerHeightStyle,
|
102 | overflowing
|
103 | };
|
104 | }, [maxRows, minRows, props.placeholder]);
|
105 | const syncHeight = React.useCallback(() => {
|
106 | const textareaStyles = calculateTextareaStyles();
|
107 | if (isEmpty(textareaStyles)) {
|
108 | return;
|
109 | }
|
110 | const outerHeightStyle = textareaStyles.outerHeightStyle;
|
111 | const input = inputRef.current;
|
112 | if (heightRef.current !== outerHeightStyle) {
|
113 | heightRef.current = outerHeightStyle;
|
114 | input.style.height = `${outerHeightStyle}px`;
|
115 | }
|
116 | input.style.overflow = textareaStyles.overflowing ? 'hidden' : '';
|
117 | }, [calculateTextareaStyles]);
|
118 | useEnhancedEffect(() => {
|
119 | const handleResize = () => {
|
120 | syncHeight();
|
121 | };
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | let rAF;
|
127 | const rAFHandleResize = () => {
|
128 | cancelAnimationFrame(rAF);
|
129 | rAF = requestAnimationFrame(() => {
|
130 | handleResize();
|
131 | });
|
132 | };
|
133 | const debounceHandleResize = debounce(handleResize);
|
134 | const input = inputRef.current;
|
135 | const containerWindow = ownerWindow(input);
|
136 | containerWindow.addEventListener('resize', debounceHandleResize);
|
137 | let resizeObserver;
|
138 | if (typeof ResizeObserver !== 'undefined') {
|
139 | resizeObserver = new ResizeObserver(process.env.NODE_ENV === 'test' ? rAFHandleResize : handleResize);
|
140 | resizeObserver.observe(input);
|
141 | }
|
142 | return () => {
|
143 | debounceHandleResize.clear();
|
144 | cancelAnimationFrame(rAF);
|
145 | containerWindow.removeEventListener('resize', debounceHandleResize);
|
146 | if (resizeObserver) {
|
147 | resizeObserver.disconnect();
|
148 | }
|
149 | };
|
150 | }, [calculateTextareaStyles, syncHeight]);
|
151 | useEnhancedEffect(() => {
|
152 | syncHeight();
|
153 | });
|
154 | const handleChange = event => {
|
155 | if (!isControlled) {
|
156 | syncHeight();
|
157 | }
|
158 | if (onChange) {
|
159 | onChange(event);
|
160 | }
|
161 | };
|
162 | return _jsxs(React.Fragment, {
|
163 | children: [_jsx("textarea", {
|
164 | value: value,
|
165 | onChange: handleChange,
|
166 | ref: handleRef
|
167 |
|
168 | ,
|
169 | rows: minRows,
|
170 | style: style,
|
171 | ...other
|
172 | }), _jsx("textarea", {
|
173 | "aria-hidden": true,
|
174 | className: props.className,
|
175 | readOnly: true,
|
176 | ref: shadowRef,
|
177 | tabIndex: -1,
|
178 | style: {
|
179 | ...styles.shadow,
|
180 | ...style,
|
181 | paddingTop: 0,
|
182 | paddingBottom: 0
|
183 | }
|
184 | })]
|
185 | });
|
186 | });
|
187 | process.env.NODE_ENV !== "production" ? TextareaAutosize.propTypes = {
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | |
193 |
|
194 |
|
195 | className: PropTypes.string,
|
196 | |
197 |
|
198 |
|
199 | maxRows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
200 | |
201 |
|
202 |
|
203 |
|
204 | minRows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
205 | |
206 |
|
207 |
|
208 | onChange: PropTypes.func,
|
209 | |
210 |
|
211 |
|
212 | placeholder: PropTypes.string,
|
213 | |
214 |
|
215 |
|
216 | style: PropTypes.object,
|
217 | |
218 |
|
219 |
|
220 | value: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string), PropTypes.number, PropTypes.string])
|
221 | } : void 0;
|
222 | export default TextareaAutosize; |
\ | No newline at end of file |