From 53eed79835dc6bdd8b1210268f8b5bef86b06387 Mon Sep 17 00:00:00 2001 From: Oleksandr Karpovich Date: Mon, 12 Jul 2021 09:38:00 +0200 Subject: [PATCH] web: fix a bug for inputs taking string value (#871) It was not possible to make the input empty by setting the state to an empty string. Co-authored-by: Oleksandr Karpovich --- .../compose/web/elements/InputElements.kt | 4 +- .../InputsGenerateCorrectHtmlTests.kt | 66 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/InputElements.kt b/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/InputElements.kt index 0ed2a5441f..8a9d7e98fd 100644 --- a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/InputElements.kt +++ b/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/InputElements.kt @@ -9,7 +9,7 @@ private fun InputAttrsBuilder.applyAttrsWithStringValue( value: String, attrsBuilder: InputAttrsBuilder.() -> Unit ) { - if (value.isNotEmpty()) value(value) + value(value) attrsBuilder() } @@ -159,4 +159,4 @@ fun UrlInput(value: String = "", attrsBuilder: InputAttrsBuilder.() -> U @NonRestartableComposable fun WeekInput(value: String = "", attrsBuilder: InputAttrsBuilder.() -> Unit = {}) { Input(type = InputType.Week, attrs = { applyAttrsWithStringValue(value, attrsBuilder) }) -} \ No newline at end of file +} diff --git a/web/core/src/jsTest/kotlin/elements/InputsGenerateCorrectHtmlTests.kt b/web/core/src/jsTest/kotlin/elements/InputsGenerateCorrectHtmlTests.kt index b268e52fd8..e02ea61e42 100644 --- a/web/core/src/jsTest/kotlin/elements/InputsGenerateCorrectHtmlTests.kt +++ b/web/core/src/jsTest/kotlin/elements/InputsGenerateCorrectHtmlTests.kt @@ -5,8 +5,10 @@ import androidx.compose.runtime.setValue import androidx.compose.runtime.getValue import org.jetbrains.compose.web.attributes.* import org.jetbrains.compose.web.core.tests.runTest +import org.jetbrains.compose.web.core.tests.waitForAnimationFrame import org.jetbrains.compose.web.dom.* import org.w3c.dom.HTMLInputElement +import org.w3c.dom.HTMLTextAreaElement import kotlin.test.Test import kotlin.test.assertEquals @@ -437,4 +439,68 @@ class InputsGenerateCorrectHtmlTests { } assertEquals("""""", root.innerHTML) } + + @Test + fun textInputChangesItsValueFromTextToEmpty() = runTest { + var state by mutableStateOf("text") + + composition { + TextInput(value = state) + } + + assertEquals("text", (root.firstChild as HTMLInputElement).value) + + state = "" + waitForAnimationFrame() + + assertEquals("", (root.firstChild as HTMLInputElement).value) + } + + @Test + fun textInputChangesItsValueFromEmptyToText() = runTest { + var state by mutableStateOf("") + + composition { + TextInput(value = state) + } + + assertEquals("", (root.firstChild as HTMLInputElement).value) + + state = "text" + waitForAnimationFrame() + + assertEquals("text", (root.firstChild as HTMLInputElement).value) + } + + @Test + fun textAreaChangesItsValueFromTextToEmpty() = runTest { + var state by mutableStateOf("text") + + composition { + TextArea(value = state) + } + + assertEquals("text", (root.firstChild as HTMLTextAreaElement).value) + + state = "" + waitForAnimationFrame() + + assertEquals("", (root.firstChild as HTMLTextAreaElement).value) + } + + @Test + fun textAreaChangesItsValueFromEmptyToText() = runTest { + var state by mutableStateOf("") + + composition { + TextArea(value = state) + } + + assertEquals("", (root.firstChild as HTMLTextAreaElement).value) + + state = "text" + waitForAnimationFrame() + + assertEquals("text", (root.firstChild as HTMLTextAreaElement).value) + } }