From caf97c80a3b6ef6f7205d12ab59ef42c5ab2f071 Mon Sep 17 00:00:00 2001 From: Marcus Delang Date: Wed, 10 Aug 2022 09:01:44 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20set=20selected=20attribut?= =?UTF-8?q?e=20on=20option=20to=20draw=20it=20(#280)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cloneNode.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/cloneNode.ts b/src/cloneNode.ts index f3a56c7b..23e52fb4 100644 --- a/src/cloneNode.ts +++ b/src/cloneNode.ts @@ -101,6 +101,19 @@ function cloneInputValue(nativeNode: T, clonedNode: T) { } } +function cloneSelectValue(nativeNode: T, clonedNode: T) { + if (nativeNode instanceof HTMLSelectElement) { + const clonedSelect = clonedNode as any as HTMLSelectElement + const selectedOption = Array.from(clonedSelect.children).find( + (child) => nativeNode.value === child.getAttribute('value'), + ) + + if (selectedOption) { + selectedOption.setAttribute('selected', '') + } + } +} + async function decorate( nativeNode: T, clonedNode: T, @@ -113,6 +126,7 @@ async function decorate( .then(() => cloneCSSStyle(nativeNode, clonedNode)) .then(() => clonePseudoElements(nativeNode, clonedNode)) .then(() => cloneInputValue(nativeNode, clonedNode)) + .then(() => cloneSelectValue(nativeNode, clonedNode)) .then(() => clonedNode) }