@navikt/ds-react 2.6.1 → 2.6.2
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.
|
@@ -33,9 +33,13 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
33
33
|
}
|
|
34
34
|
return t;
|
|
35
35
|
};
|
|
36
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
|
+
};
|
|
36
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
40
|
/* https://github.com/mui/material-ui/blob/master/packages/mui-base/src/TextareaAutosize/TextareaAutosize.js */
|
|
38
41
|
const react_1 = __importStar(require("react"));
|
|
42
|
+
const react_dom_1 = __importDefault(require("react-dom"));
|
|
39
43
|
const __1 = require("..");
|
|
40
44
|
/**
|
|
41
45
|
* https://github.com/mui/material-ui/blob/master/packages/mui-utils/src/ownerDocument.ts
|
|
@@ -56,7 +60,7 @@ const TextareaAutosize = (0, react_1.forwardRef)((_a, ref) => {
|
|
|
56
60
|
const shadowRef = (0, react_1.useRef)(null);
|
|
57
61
|
const renders = (0, react_1.useRef)(0);
|
|
58
62
|
const [state, setState] = (0, react_1.useState)({});
|
|
59
|
-
const
|
|
63
|
+
const getUpdatedState = react_1.default.useCallback(() => {
|
|
60
64
|
if (!inputRef.current || !shadowRef.current)
|
|
61
65
|
return;
|
|
62
66
|
const input = inputRef.current;
|
|
@@ -97,42 +101,69 @@ const TextareaAutosize = (0, react_1.forwardRef)((_a, ref) => {
|
|
|
97
101
|
// Take the box sizing into account for applying this value as a style.
|
|
98
102
|
const outerHeightStyle = outerHeight + (boxSizing === "border-box" ? padding + border : 0);
|
|
99
103
|
const overflow = Math.abs(outerHeight - innerHeight) <= 1;
|
|
104
|
+
return { outerHeightStyle, overflow };
|
|
105
|
+
}, [maxRows, minRows, other === null || other === void 0 ? void 0 : other.placeholder]);
|
|
106
|
+
const syncHeight = react_1.default.useCallback(() => {
|
|
107
|
+
const newState = getUpdatedState();
|
|
108
|
+
if (isEmpty(newState)) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
100
111
|
setState((prevState) => {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
112
|
+
return updateState(prevState, newState);
|
|
113
|
+
});
|
|
114
|
+
}, [getUpdatedState]);
|
|
115
|
+
const updateState = (prevState, newState) => {
|
|
116
|
+
const { outerHeightStyle, overflow } = newState;
|
|
117
|
+
// Need a large enough difference to update the height.
|
|
118
|
+
// This prevents infinite rendering loop.
|
|
119
|
+
if (renders.current < 20 &&
|
|
120
|
+
((outerHeightStyle > 0 &&
|
|
121
|
+
Math.abs((prevState.outerHeightStyle || 0) - outerHeightStyle) > 1) ||
|
|
122
|
+
prevState.overflow !== overflow)) {
|
|
123
|
+
renders.current += 1;
|
|
124
|
+
return {
|
|
125
|
+
overflow,
|
|
126
|
+
outerHeightStyle,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (process.env.NODE_ENV !== "production") {
|
|
130
|
+
if (renders.current === 20) {
|
|
131
|
+
console.error([
|
|
132
|
+
"Textarea: Too many re-renders. The layout is unstable.",
|
|
133
|
+
"TextareaAutosize limits the number of renders to prevent an infinite loop.",
|
|
134
|
+
].join("\n"));
|
|
121
135
|
}
|
|
122
|
-
|
|
136
|
+
}
|
|
137
|
+
return prevState;
|
|
138
|
+
};
|
|
139
|
+
const withFlushSync = () => {
|
|
140
|
+
const newState = getUpdatedState();
|
|
141
|
+
if (isEmpty(newState)) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
// In React 18, state updates in a ResizeObserver's callback are happening after the paint which causes flickering
|
|
145
|
+
// when doing some visual updates in it. Using flushSync ensures that the dom will be painted after the states updates happen
|
|
146
|
+
// Related issue - https://github.com/facebook/react/issues/24331
|
|
147
|
+
react_dom_1.default.flushSync(() => {
|
|
148
|
+
setState((prevState) => {
|
|
149
|
+
return updateState(prevState, newState);
|
|
150
|
+
});
|
|
123
151
|
});
|
|
124
|
-
}
|
|
125
|
-
|
|
152
|
+
};
|
|
153
|
+
react_1.default.useEffect(() => {
|
|
126
154
|
const handleResize = (0, __1.debounce)(() => {
|
|
127
155
|
renders.current = 0;
|
|
128
|
-
|
|
156
|
+
if (inputRef.current) {
|
|
157
|
+
withFlushSync();
|
|
158
|
+
}
|
|
129
159
|
});
|
|
130
|
-
const containerWindow = ownerWindow(inputRef.current);
|
|
131
|
-
containerWindow.addEventListener("resize", handleResize);
|
|
132
160
|
let resizeObserver;
|
|
161
|
+
const input = inputRef.current;
|
|
162
|
+
const containerWindow = ownerWindow(input);
|
|
163
|
+
containerWindow.addEventListener("resize", handleResize);
|
|
133
164
|
if (typeof ResizeObserver !== "undefined") {
|
|
134
165
|
resizeObserver = new ResizeObserver(handleResize);
|
|
135
|
-
resizeObserver.observe(
|
|
166
|
+
resizeObserver.observe(input);
|
|
136
167
|
}
|
|
137
168
|
return () => {
|
|
138
169
|
handleResize.clear();
|
|
@@ -141,7 +172,7 @@ const TextareaAutosize = (0, react_1.forwardRef)((_a, ref) => {
|
|
|
141
172
|
resizeObserver.disconnect();
|
|
142
173
|
}
|
|
143
174
|
};
|
|
144
|
-
}
|
|
175
|
+
});
|
|
145
176
|
(0, __1.useClientLayoutEffect)(() => {
|
|
146
177
|
syncHeight();
|
|
147
178
|
});
|
|
@@ -171,4 +202,10 @@ const TextareaAutosize = (0, react_1.forwardRef)((_a, ref) => {
|
|
|
171
202
|
// Create a new layer, increase the isolation of the computed values
|
|
172
203
|
transform: "translateZ(0)" }, style) })));
|
|
173
204
|
});
|
|
205
|
+
function isEmpty(obj) {
|
|
206
|
+
return (obj === undefined ||
|
|
207
|
+
obj === null ||
|
|
208
|
+
Object.keys(obj).length === 0 ||
|
|
209
|
+
((obj === null || obj === void 0 ? void 0 : obj.outerHeightStyle) === 0 && !(obj === null || obj === void 0 ? void 0 : obj.overflow)));
|
|
210
|
+
}
|
|
174
211
|
exports.default = TextareaAutosize;
|
|
@@ -10,7 +10,8 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
10
10
|
return t;
|
|
11
11
|
};
|
|
12
12
|
/* https://github.com/mui/material-ui/blob/master/packages/mui-base/src/TextareaAutosize/TextareaAutosize.js */
|
|
13
|
-
import React, { forwardRef,
|
|
13
|
+
import React, { forwardRef, useEffect, useMemo, useRef, useState } from "react";
|
|
14
|
+
import ReactDOM from "react-dom";
|
|
14
15
|
import { debounce, mergeRefs, useClientLayoutEffect } from "..";
|
|
15
16
|
/**
|
|
16
17
|
* https://github.com/mui/material-ui/blob/master/packages/mui-utils/src/ownerDocument.ts
|
|
@@ -31,7 +32,7 @@ const TextareaAutosize = forwardRef((_a, ref) => {
|
|
|
31
32
|
const shadowRef = useRef(null);
|
|
32
33
|
const renders = useRef(0);
|
|
33
34
|
const [state, setState] = useState({});
|
|
34
|
-
const
|
|
35
|
+
const getUpdatedState = React.useCallback(() => {
|
|
35
36
|
if (!inputRef.current || !shadowRef.current)
|
|
36
37
|
return;
|
|
37
38
|
const input = inputRef.current;
|
|
@@ -72,42 +73,69 @@ const TextareaAutosize = forwardRef((_a, ref) => {
|
|
|
72
73
|
// Take the box sizing into account for applying this value as a style.
|
|
73
74
|
const outerHeightStyle = outerHeight + (boxSizing === "border-box" ? padding + border : 0);
|
|
74
75
|
const overflow = Math.abs(outerHeight - innerHeight) <= 1;
|
|
76
|
+
return { outerHeightStyle, overflow };
|
|
77
|
+
}, [maxRows, minRows, other === null || other === void 0 ? void 0 : other.placeholder]);
|
|
78
|
+
const syncHeight = React.useCallback(() => {
|
|
79
|
+
const newState = getUpdatedState();
|
|
80
|
+
if (isEmpty(newState)) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
75
83
|
setState((prevState) => {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
84
|
+
return updateState(prevState, newState);
|
|
85
|
+
});
|
|
86
|
+
}, [getUpdatedState]);
|
|
87
|
+
const updateState = (prevState, newState) => {
|
|
88
|
+
const { outerHeightStyle, overflow } = newState;
|
|
89
|
+
// Need a large enough difference to update the height.
|
|
90
|
+
// This prevents infinite rendering loop.
|
|
91
|
+
if (renders.current < 20 &&
|
|
92
|
+
((outerHeightStyle > 0 &&
|
|
93
|
+
Math.abs((prevState.outerHeightStyle || 0) - outerHeightStyle) > 1) ||
|
|
94
|
+
prevState.overflow !== overflow)) {
|
|
95
|
+
renders.current += 1;
|
|
96
|
+
return {
|
|
97
|
+
overflow,
|
|
98
|
+
outerHeightStyle,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
if (process.env.NODE_ENV !== "production") {
|
|
102
|
+
if (renders.current === 20) {
|
|
103
|
+
console.error([
|
|
104
|
+
"Textarea: Too many re-renders. The layout is unstable.",
|
|
105
|
+
"TextareaAutosize limits the number of renders to prevent an infinite loop.",
|
|
106
|
+
].join("\n"));
|
|
96
107
|
}
|
|
97
|
-
|
|
108
|
+
}
|
|
109
|
+
return prevState;
|
|
110
|
+
};
|
|
111
|
+
const withFlushSync = () => {
|
|
112
|
+
const newState = getUpdatedState();
|
|
113
|
+
if (isEmpty(newState)) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
// In React 18, state updates in a ResizeObserver's callback are happening after the paint which causes flickering
|
|
117
|
+
// when doing some visual updates in it. Using flushSync ensures that the dom will be painted after the states updates happen
|
|
118
|
+
// Related issue - https://github.com/facebook/react/issues/24331
|
|
119
|
+
ReactDOM.flushSync(() => {
|
|
120
|
+
setState((prevState) => {
|
|
121
|
+
return updateState(prevState, newState);
|
|
122
|
+
});
|
|
98
123
|
});
|
|
99
|
-
}
|
|
100
|
-
useEffect(() => {
|
|
124
|
+
};
|
|
125
|
+
React.useEffect(() => {
|
|
101
126
|
const handleResize = debounce(() => {
|
|
102
127
|
renders.current = 0;
|
|
103
|
-
|
|
128
|
+
if (inputRef.current) {
|
|
129
|
+
withFlushSync();
|
|
130
|
+
}
|
|
104
131
|
});
|
|
105
|
-
const containerWindow = ownerWindow(inputRef.current);
|
|
106
|
-
containerWindow.addEventListener("resize", handleResize);
|
|
107
132
|
let resizeObserver;
|
|
133
|
+
const input = inputRef.current;
|
|
134
|
+
const containerWindow = ownerWindow(input);
|
|
135
|
+
containerWindow.addEventListener("resize", handleResize);
|
|
108
136
|
if (typeof ResizeObserver !== "undefined") {
|
|
109
137
|
resizeObserver = new ResizeObserver(handleResize);
|
|
110
|
-
resizeObserver.observe(
|
|
138
|
+
resizeObserver.observe(input);
|
|
111
139
|
}
|
|
112
140
|
return () => {
|
|
113
141
|
handleResize.clear();
|
|
@@ -116,7 +144,7 @@ const TextareaAutosize = forwardRef((_a, ref) => {
|
|
|
116
144
|
resizeObserver.disconnect();
|
|
117
145
|
}
|
|
118
146
|
};
|
|
119
|
-
}
|
|
147
|
+
});
|
|
120
148
|
useClientLayoutEffect(() => {
|
|
121
149
|
syncHeight();
|
|
122
150
|
});
|
|
@@ -146,5 +174,11 @@ const TextareaAutosize = forwardRef((_a, ref) => {
|
|
|
146
174
|
// Create a new layer, increase the isolation of the computed values
|
|
147
175
|
transform: "translateZ(0)" }, style) })));
|
|
148
176
|
});
|
|
177
|
+
function isEmpty(obj) {
|
|
178
|
+
return (obj === undefined ||
|
|
179
|
+
obj === null ||
|
|
180
|
+
Object.keys(obj).length === 0 ||
|
|
181
|
+
((obj === null || obj === void 0 ? void 0 : obj.outerHeightStyle) === 0 && !(obj === null || obj === void 0 ? void 0 : obj.overflow)));
|
|
182
|
+
}
|
|
149
183
|
export default TextareaAutosize;
|
|
150
184
|
//# sourceMappingURL=TextareaAutoSize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TextareaAutoSize.js","sourceRoot":"","sources":["../../src/util/TextareaAutoSize.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA,+GAA+G;AAC/G,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"TextareaAutoSize.js","sourceRoot":"","sources":["../../src/util/TextareaAutoSize.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA,+GAA+G;AAC/G,OAAO,KAAK,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAChF,OAAO,QAAQ,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,qBAAqB,EAAE,MAAM,IAAI,CAAC;AAEhE;;;GAGG;AACH,MAAM,WAAW,GAAG,CAAC,IAAiB,EAAU,EAAE;IAChD,MAAM,GAAG,GAAG,CAAC,IAAI,IAAI,IAAI,CAAC,aAAa,CAAC,IAAI,QAAQ,CAAC;IACrD,OAAO,GAAG,CAAC,WAAW,IAAI,MAAM,CAAC;AACnC,CAAC,CAAC;AAEF,SAAS,aAAa,CAAC,aAAa,EAAE,QAAQ;IAC5C,OAAO,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC;AACpD,CAAC;AAkBD,MAAM,gBAAgB,GAAG,UAAU,CACjC,CACE,EAAqE,EACrE,GAAG,EACH,EAAE;QAFF,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,GAAG,CAAC,EAAE,KAAK,EAAE,KAAK,OAAY,EAAP,KAAK,cAAnE,iEA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C,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,KAAK,CAAC;QAC7B,CAAC,CAAA,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,gBAAgB,MAAK,CAAC,IAAI,CAAC,CAAA,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,QAAQ,CAAA,CAAC,CAChD,CAAC;AACJ,CAAC;AAED,eAAe,gBAAgB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@navikt/ds-react",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.2",
|
|
4
4
|
"description": "Aksel react-components for NAV designsystem",
|
|
5
5
|
"author": "Aksel | NAV designsystem team",
|
|
6
6
|
"license": "MIT",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@floating-ui/react": "0.17.0",
|
|
40
|
-
"@navikt/ds-icons": "^2.6.
|
|
40
|
+
"@navikt/ds-icons": "^2.6.2",
|
|
41
41
|
"@radix-ui/react-tabs": "1.0.0",
|
|
42
42
|
"@radix-ui/react-toggle-group": "1.0.0",
|
|
43
43
|
"clsx": "^1.2.1",
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
/* https://github.com/mui/material-ui/blob/master/packages/mui-base/src/TextareaAutosize/TextareaAutosize.js */
|
|
2
|
-
import React, {
|
|
3
|
-
|
|
4
|
-
useCallback,
|
|
5
|
-
useEffect,
|
|
6
|
-
useMemo,
|
|
7
|
-
useRef,
|
|
8
|
-
useState,
|
|
9
|
-
} from "react";
|
|
2
|
+
import React, { forwardRef, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import ReactDOM from "react-dom";
|
|
10
4
|
import { debounce, mergeRefs, useClientLayoutEffect } from "..";
|
|
11
5
|
|
|
12
6
|
/**
|
|
@@ -50,7 +44,7 @@ const TextareaAutosize = forwardRef<HTMLTextAreaElement, TextareaAutosizeProps>(
|
|
|
50
44
|
const renders = useRef(0);
|
|
51
45
|
const [state, setState] = useState<any>({});
|
|
52
46
|
|
|
53
|
-
const
|
|
47
|
+
const getUpdatedState = React.useCallback(() => {
|
|
54
48
|
if (!inputRef.current || !shadowRef.current) return;
|
|
55
49
|
const input = inputRef.current;
|
|
56
50
|
const containerWindow = ownerWindow(input);
|
|
@@ -102,50 +96,85 @@ const TextareaAutosize = forwardRef<HTMLTextAreaElement, TextareaAutosizeProps>(
|
|
|
102
96
|
outerHeight + (boxSizing === "border-box" ? padding + border : 0);
|
|
103
97
|
const overflow = Math.abs(outerHeight - innerHeight) <= 1;
|
|
104
98
|
|
|
99
|
+
return { outerHeightStyle, overflow };
|
|
100
|
+
}, [maxRows, minRows, other?.placeholder]);
|
|
101
|
+
|
|
102
|
+
const syncHeight = React.useCallback(() => {
|
|
103
|
+
const newState = getUpdatedState();
|
|
104
|
+
|
|
105
|
+
if (isEmpty(newState)) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
105
109
|
setState((prevState) => {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
renders.current < 20 &&
|
|
110
|
-
((outerHeightStyle > 0 &&
|
|
111
|
-
Math.abs((prevState.outerHeightStyle || 0) - outerHeightStyle) >
|
|
112
|
-
1) ||
|
|
113
|
-
prevState.overflow !== overflow)
|
|
114
|
-
) {
|
|
115
|
-
renders.current += 1;
|
|
116
|
-
return {
|
|
117
|
-
overflow,
|
|
118
|
-
outerHeightStyle,
|
|
119
|
-
};
|
|
120
|
-
}
|
|
110
|
+
return updateState(prevState, newState);
|
|
111
|
+
});
|
|
112
|
+
}, [getUpdatedState]);
|
|
121
113
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
114
|
+
const updateState = (prevState, newState) => {
|
|
115
|
+
const { outerHeightStyle, overflow } = newState;
|
|
116
|
+
// Need a large enough difference to update the height.
|
|
117
|
+
// This prevents infinite rendering loop.
|
|
118
|
+
if (
|
|
119
|
+
renders.current < 20 &&
|
|
120
|
+
((outerHeightStyle > 0 &&
|
|
121
|
+
Math.abs((prevState.outerHeightStyle || 0) - outerHeightStyle) > 1) ||
|
|
122
|
+
prevState.overflow !== overflow)
|
|
123
|
+
) {
|
|
124
|
+
renders.current += 1;
|
|
125
|
+
return {
|
|
126
|
+
overflow,
|
|
127
|
+
outerHeightStyle,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
if (process.env.NODE_ENV !== "production") {
|
|
131
|
+
if (renders.current === 20) {
|
|
132
|
+
console.error(
|
|
133
|
+
[
|
|
134
|
+
"Textarea: Too many re-renders. The layout is unstable.",
|
|
135
|
+
"TextareaAutosize limits the number of renders to prevent an infinite loop.",
|
|
136
|
+
].join("\n")
|
|
137
|
+
);
|
|
131
138
|
}
|
|
139
|
+
}
|
|
140
|
+
return prevState;
|
|
141
|
+
};
|
|
132
142
|
|
|
133
|
-
|
|
143
|
+
const withFlushSync = () => {
|
|
144
|
+
const newState = getUpdatedState();
|
|
145
|
+
|
|
146
|
+
if (isEmpty(newState)) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// In React 18, state updates in a ResizeObserver's callback are happening after the paint which causes flickering
|
|
151
|
+
// when doing some visual updates in it. Using flushSync ensures that the dom will be painted after the states updates happen
|
|
152
|
+
// Related issue - https://github.com/facebook/react/issues/24331
|
|
153
|
+
ReactDOM.flushSync(() => {
|
|
154
|
+
setState((prevState) => {
|
|
155
|
+
return updateState(prevState, newState);
|
|
156
|
+
});
|
|
134
157
|
});
|
|
135
|
-
}
|
|
158
|
+
};
|
|
136
159
|
|
|
137
|
-
useEffect(() => {
|
|
160
|
+
React.useEffect(() => {
|
|
138
161
|
const handleResize = debounce(() => {
|
|
139
162
|
renders.current = 0;
|
|
140
|
-
|
|
163
|
+
|
|
164
|
+
if (inputRef.current) {
|
|
165
|
+
withFlushSync();
|
|
166
|
+
}
|
|
141
167
|
});
|
|
142
|
-
|
|
168
|
+
let resizeObserver: ResizeObserver;
|
|
169
|
+
|
|
170
|
+
const input = inputRef.current!;
|
|
171
|
+
const containerWindow = ownerWindow(input);
|
|
172
|
+
|
|
143
173
|
containerWindow.addEventListener("resize", handleResize);
|
|
144
|
-
let resizeObserver;
|
|
145
174
|
|
|
146
175
|
if (typeof ResizeObserver !== "undefined") {
|
|
147
176
|
resizeObserver = new ResizeObserver(handleResize);
|
|
148
|
-
resizeObserver.observe(
|
|
177
|
+
resizeObserver.observe(input);
|
|
149
178
|
}
|
|
150
179
|
|
|
151
180
|
return () => {
|
|
@@ -155,7 +184,7 @@ const TextareaAutosize = forwardRef<HTMLTextAreaElement, TextareaAutosizeProps>(
|
|
|
155
184
|
resizeObserver.disconnect();
|
|
156
185
|
}
|
|
157
186
|
};
|
|
158
|
-
}
|
|
187
|
+
});
|
|
159
188
|
|
|
160
189
|
useClientLayoutEffect(() => {
|
|
161
190
|
syncHeight();
|
|
@@ -221,4 +250,13 @@ const TextareaAutosize = forwardRef<HTMLTextAreaElement, TextareaAutosizeProps>(
|
|
|
221
250
|
}
|
|
222
251
|
);
|
|
223
252
|
|
|
253
|
+
function isEmpty(obj: any) {
|
|
254
|
+
return (
|
|
255
|
+
obj === undefined ||
|
|
256
|
+
obj === null ||
|
|
257
|
+
Object.keys(obj).length === 0 ||
|
|
258
|
+
(obj?.outerHeightStyle === 0 && !obj?.overflow)
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
224
262
|
export default TextareaAutosize;
|