From 56abe568e204dec4e2595c27244ce9ec40b070dd Mon Sep 17 00:00:00 2001 From: Halvor Haugan <83693529+HalvorHaugan@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:46:29 +0100 Subject: [PATCH] =?UTF-8?q?Textarea:=20debounce=20ResizeObserver=20for=20?= =?UTF-8?q?=C3=A5=20unng=C3=A5=20feilmelding=20(#2685)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/tasty-hounds-prove.md | 5 +++++ @navikt/core/react/src/util/TextareaAutoSize.tsx | 11 +++++------ 2 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 .changeset/tasty-hounds-prove.md diff --git a/.changeset/tasty-hounds-prove.md b/.changeset/tasty-hounds-prove.md new file mode 100644 index 0000000000..a54e61365d --- /dev/null +++ b/.changeset/tasty-hounds-prove.md @@ -0,0 +1,5 @@ +--- +"@navikt/ds-react": patch +--- + +Textarea: debounce ResizeObserver for å unngå feilmelding diff --git a/@navikt/core/react/src/util/TextareaAutoSize.tsx b/@navikt/core/react/src/util/TextareaAutoSize.tsx index fbc07e37a1..6a1afc7c31 100644 --- a/@navikt/core/react/src/util/TextareaAutoSize.tsx +++ b/@navikt/core/react/src/util/TextareaAutoSize.tsx @@ -167,7 +167,7 @@ const TextareaAutosize = forwardRef( }); }; - const handleResize = () => { + const handleResize = debounce(() => { renders.current = 0; if (inputRef.current?.style.height || inputRef.current?.style.width) { @@ -179,13 +179,12 @@ const TextareaAutosize = forwardRef( } syncHeightWithFlushSync(); - }; + }); - const debounceHandleResize = debounce(handleResize); const input = inputRef.current!; const containerWindow = ownerWindow(input); - containerWindow.addEventListener("resize", debounceHandleResize); + containerWindow.addEventListener("resize", handleResize); let resizeObserver: ResizeObserver; if (typeof ResizeObserver !== "undefined") { @@ -194,8 +193,8 @@ const TextareaAutosize = forwardRef( } return () => { - debounceHandleResize.clear(); - containerWindow.removeEventListener("resize", debounceHandleResize); + handleResize.clear(); + containerWindow.removeEventListener("resize", handleResize); if (resizeObserver) { resizeObserver.disconnect(); }