diff options
author | Robert Jaros <rjaros@finn.pl> | 2017-10-19 13:48:03 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2017-10-19 13:48:03 +0200 |
commit | d17f27058f41f2dddd9fd5e88149ed55f9f7bf0c (patch) | |
tree | b475544b8af881fcb5f09a6fb12c82155b060976 /src | |
parent | 94d1930aaf160d7271aabe97bf167a911391210b (diff) | |
download | kvision-d17f27058f41f2dddd9fd5e88149ed55f9f7bf0c.tar.gz kvision-d17f27058f41f2dddd9fd5e88149ed55f9f7bf0c.tar.bz2 kvision-d17f27058f41f2dddd9fd5e88149ed55f9f7bf0c.zip |
Form control: Radio
Unit tests
Diffstat (limited to 'src')
11 files changed, 219 insertions, 15 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/Showcase.kt b/src/main/kotlin/pl/treksoft/kvision/Showcase.kt index 1eb0f319..afe49299 100644 --- a/src/main/kotlin/pl/treksoft/kvision/Showcase.kt +++ b/src/main/kotlin/pl/treksoft/kvision/Showcase.kt @@ -7,6 +7,8 @@ import pl.treksoft.kvision.dropdown.DropDown import pl.treksoft.kvision.form.CHECKBOXSTYLE import pl.treksoft.kvision.form.CheckBox import pl.treksoft.kvision.form.INPUTSIZE +import pl.treksoft.kvision.form.RADIOSTYLE +import pl.treksoft.kvision.form.Radio import pl.treksoft.kvision.form.TEXTINPUTTYPE import pl.treksoft.kvision.form.Text import pl.treksoft.kvision.form.TextInput @@ -69,6 +71,22 @@ class Showcase : ApplicationBase() { change = { e -> println("change" + self.value) } } + val radio = Radio(true, name = "radios", label = "Opcja 1", inline = true, + style = RADIOSTYLE.DANGER, extraValue = "o1") + val radio2 = Radio(false, name = "radios", label = "Opcja 2", rich = true, inline = true, + style = RADIOSTYLE.WARNING, extraValue = "o2") + val radio3 = Radio(false, name = "radios", label = "Opcja 3", inline = true, + style = RADIOSTYLE.PRIMARY, squared = true, extraValue = "o3") + root.add(radio) + root.add(radio2) + root.add(radio3) + radio.setEventListener<CheckBox> { + click = { e -> + println("rclick" + self.value) + } + change = { e -> println("rchange" + self.value) } + } + val text = Text(placeholder = "Pole formularza", maxlength = 5, label = "To jest pole") root.add(text) diff --git a/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt b/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt index 2d7b3276..13551f67 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/CheckBox.kt @@ -70,7 +70,7 @@ open class CheckBox(value: Boolean = false, name: String? = null, style: CHECKBO } private val idc = "kv_form_checkbox_" + counter - val input: CheckBoxInput = CheckBoxInput(value, name, disabled, idc, setOf("styled")) + val input: CheckBoxInput = CheckBoxInput(CHECKINPUTTYPE.CHECKBOX, value, name, disabled, idc, null, setOf("styled")) val flabel: FieldLabel = FieldLabel(idc, label, rich) init { @@ -85,7 +85,7 @@ open class CheckBox(value: Boolean = false, name: String? = null, style: CHECKBO @Suppress("UNCHECKED_CAST") override fun <T : Widget> setEventListener(block: SnOn<T>.() -> Unit): Widget { - input.setEventListener<T>(block) + input.setEventListener(block) return this } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/CheckBoxInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/CheckBoxInput.kt index 44383629..5fa220e9 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/CheckBoxInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/CheckBoxInput.kt @@ -5,8 +5,14 @@ import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.snabbdom.StringBoolPair import pl.treksoft.kvision.snabbdom.StringPair -open class CheckBoxInput(override var value: Boolean = false, +enum class CHECKINPUTTYPE(val type: String) { + CHECKBOX("checkbox"), + RADIO("radio") +} + +open class CheckBoxInput(type: CHECKINPUTTYPE = CHECKINPUTTYPE.CHECKBOX, override var value: Boolean = false, name: String? = null, disabled: Boolean = false, id: String? = null, + extraValue: String? = null, classes: Set<String> = setOf()) : Widget(classes), BoolFormField { init { @@ -20,6 +26,11 @@ open class CheckBoxInput(override var value: Boolean = false, this.value = value refresh() } + var type: CHECKINPUTTYPE = type + set(value) { + field = value + refresh() + } var name: String? = name set(value) { field = value @@ -30,6 +41,11 @@ open class CheckBoxInput(override var value: Boolean = false, field = value refresh() } + var extraValue: String? = extraValue + set(value) { + field = value + refresh() + } override var size: INPUTSIZE? = null set(value) { field = value @@ -50,7 +66,7 @@ open class CheckBoxInput(override var value: Boolean = false, override fun getSnAttrs(): List<StringPair> { val sn = super.getSnAttrs().toMutableList() - sn.add("type" to "checkbox") + sn.add("type" to type.type) if (startValue) { sn.add("checked" to "true") } @@ -60,6 +76,9 @@ open class CheckBoxInput(override var value: Boolean = false, if (disabled) { sn.add("disabled" to "true") } + extraValue?.let { + sn.add("value" to it) + } return sn } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt b/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt new file mode 100644 index 00000000..f3f72a3d --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/form/Radio.kt @@ -0,0 +1,123 @@ +package pl.treksoft.kvision.form + +import pl.treksoft.kvision.core.Container +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.snabbdom.SnOn +import pl.treksoft.kvision.snabbdom.StringBoolPair + +enum class RADIOSTYLE(val className: String) { + DEFAULT("radio-default"), + PRIMARY("radio-primary"), + SUCCESS("radio-success"), + INFO("radio-info"), + WARNING("radio-warning"), + DANGER("radio-danger"), +} + +open class Radio(value: Boolean = false, extraValue: String? = null, name: String? = null, style: RADIOSTYLE? = null, + squared: Boolean = false, inline: Boolean = false, disabled: Boolean = false, + label: String? = null, rich: Boolean = false) : Container(), BoolFormField { + + override var value + get() = input.value + set(value) { + input.value = value + } + var startValue + get() = input.startValue + set(value) { + input.startValue = value + } + var extraValue + get() = input.extraValue + set(value) { + input.extraValue = value + } + var name + get() = input.name + set(value) { + input.name = value + } + override var disabled + get() = input.disabled + set(value) { + input.disabled = value + } + var label + get() = flabel.text + set(value) { + flabel.text = value + } + var rich + get() = flabel.rich + set(value) { + flabel.rich = value + } + var style = style + set(value) { + field = value + refresh() + } + var squared = squared + set(value) { + field = value + refresh() + } + var inline = inline + set(value) { + field = value + refresh() + } + override var size + get() = input.size + set(value) { + input.size = value + } + + private val idc = "kv_form_radio_" + counter + val input: CheckBoxInput = CheckBoxInput(CHECKINPUTTYPE.RADIO, value, name, disabled, idc, extraValue) + val flabel: FieldLabel = FieldLabel(idc, label, rich) + + init { + this.addInternal(input) + this.addInternal(flabel) + counter++ + } + + companion object { + var counter = 0 + } + + @Suppress("UNCHECKED_CAST") + override fun <T : Widget> setEventListener(block: SnOn<T>.() -> Unit): Widget { + input.setEventListener(block) + return this + } + + override fun setEventListener(block: SnOn<Widget>.() -> Unit): Widget { + input.setEventListener(block) + return this + } + + override fun getSnClass(): List<StringBoolPair> { + val cl = super.getSnClass().toMutableList() + if (!squared) { + cl.add("radio" to true) + style?.let { + cl.add(it.className to true) + } + if (inline) { + cl.add("radio-inline" to true) + } + } else { + cl.add("checkbox" to true) + style?.let { + cl.add(it.className.replace("radio", "checkbox") to true) + } + if (inline) { + cl.add("checkbox-inline" to true) + } + } + return cl + } +} diff --git a/src/test/kotlin/test/pl/treksoft/kvision/core/KVManagerSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/core/KVManagerSpec.kt index 9a5861df..f47e1900 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/core/KVManagerSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/core/KVManagerSpec.kt @@ -13,7 +13,7 @@ import kotlin.test.assertTrue class KVManagerSpec : DomSpec { @Test - fun patch_ById() { + fun patchById() { run { val vnode = h("span", snOpt { attrs = snAttrs(listOf("id" to "test_new")) @@ -26,7 +26,7 @@ class KVManagerSpec : DomSpec { } @Test - fun patch_ByVnode() { + fun patchByVnode() { run { val vnode1 = h("span", snOpt { attrs = snAttrs(listOf("id" to "test2")) diff --git a/src/test/kotlin/test/pl/treksoft/kvision/core/WidgetSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/core/WidgetSpec.kt index 179788d0..4e8c4ea1 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/core/WidgetSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/core/WidgetSpec.kt @@ -22,7 +22,7 @@ class WidgetSpec : WSpec { } @Test - fun render_ToDom() { + fun renderToDom() { runW { widget, element -> widget.addCssClass("testClass") widget.title = "test_title" diff --git a/src/test/kotlin/test/pl/treksoft/kvision/dropdown/DropDownSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/dropdown/DropDownSpec.kt index 5f417f03..4e42fbe2 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/dropdown/DropDownSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/dropdown/DropDownSpec.kt @@ -25,7 +25,7 @@ class DropDownSpec : DomSpec { } @Test - fun render_DropUp() { + fun renderDropUp() { run { val root = Root("test") val dd = DropDown("Dropdown", listOf("abc" to "#!/x", "def" to "#!/y"), "flag", dropup = true) @@ -38,7 +38,7 @@ class DropDownSpec : DomSpec { } @Test - fun render_HeaderElement() { + fun renderHeaderElement() { run { val root = Root("test") val dd = DropDown("Dropdown", listOf("abc" to DD.HEADER.type), "flag") @@ -51,7 +51,7 @@ class DropDownSpec : DomSpec { } @Test - fun render_SeparatorElement() { + fun renderSeparatorElement() { run { val root = Root("test") val dd = DropDown("Dropdown", listOf("abc" to DD.SEPARATOR.type), "flag") @@ -64,7 +64,7 @@ class DropDownSpec : DomSpec { } @Test - fun render_DisabledElement() { + fun renderDisabledElement() { run { val root = Root("test") val dd = DropDown("Dropdown", listOf("abc" to DD.DISABLED.type), "flag") diff --git a/src/test/kotlin/test/pl/treksoft/kvision/form/CheckBoxInputSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/form/CheckBoxInputSpec.kt index 780c0337..efc6d78e 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/form/CheckBoxInputSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/form/CheckBoxInputSpec.kt @@ -1,6 +1,7 @@ package test.pl.treksoft.kvision.form import pl.treksoft.kvision.core.Root +import pl.treksoft.kvision.form.CHECKINPUTTYPE import pl.treksoft.kvision.form.CheckBoxInput import pl.treksoft.kvision.form.TEXTINPUTTYPE import pl.treksoft.kvision.form.TextInput @@ -22,4 +23,15 @@ class CheckBoxInputSpec : DomSpec { } } + @Test + fun renderAsRadio() { + run { + val root = Root("test") + val ci = CheckBoxInput(type = CHECKINPUTTYPE.RADIO, value = true, name = "name", id = "idti", extraValue = "abc") + root.add(ci) + val element = document.getElementById("test") + assertEquals("<input id=\"idti\" type=\"radio\" checked=\"\" name=\"name\" value=\"abc\">", element?.innerHTML, "Should render correct radio button field") + } + } + }
\ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/form/RadioSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/form/RadioSpec.kt new file mode 100644 index 00000000..5b827a56 --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/form/RadioSpec.kt @@ -0,0 +1,32 @@ +package test.pl.treksoft.kvision.form + +import pl.treksoft.kvision.core.Root +import pl.treksoft.kvision.form.CHECKBOXSTYLE +import pl.treksoft.kvision.form.CheckBox +import pl.treksoft.kvision.form.RADIOSTYLE +import pl.treksoft.kvision.form.Radio +import test.pl.treksoft.kvision.DomSpec +import kotlin.browser.document +import kotlin.test.Test +import kotlin.test.assertEquals + +class RadioSpec : DomSpec { + + @Test + fun render() { + run { + val root = Root("test") + val ci = Radio(value = true, name = "name", style = RADIOSTYLE.DANGER, disabled = true, + inline = true, label = "Label", extraValue = "abc") + root.add(ci) + val element = document.getElementById("test") + val id = ci.input.id + assertEquals("<div class=\"radio radio-danger radio-inline\"><input id=\"$id\" type=\"radio\" checked=\"\" name=\"name\" disabled=\"\" value=\"abc\"><label for=\"$id\">Label</label></div>", element?.innerHTML, "Should render correct radio button form field") + ci.style = RADIOSTYLE.INFO + ci.squared = true + ci.inline = false + assertEquals("<div class=\"checkbox checkbox-info\"><input id=\"$id\" type=\"radio\" checked=\"\" name=\"name\" disabled=\"\" value=\"abc\"><label for=\"$id\">Label</label></div>", element?.innerHTML, "Should render correct radio button form field") + } + } + +}
\ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/html/ListSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/html/ListSpec.kt index 78631076..48a13f6d 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/html/ListSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/html/ListSpec.kt @@ -13,7 +13,7 @@ import kotlin.test.assertEquals class ListSpec : DomSpec { @Test - fun render_Elements() { + fun renderElements() { run { val root = Root("test") val list = ListTag(LIST.DL_HORIZ, listOf("a1", "a2", "b1", "b2")) @@ -24,7 +24,7 @@ class ListSpec : DomSpec { } @Test - fun render_AsContainer() { + fun renderAsContainer() { run { val root = Root("test") val list = ListTag(LIST.UL) diff --git a/src/test/kotlin/test/pl/treksoft/kvision/html/TagSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/html/TagSpec.kt index 997714db..8d6a19d1 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/html/TagSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/html/TagSpec.kt @@ -24,7 +24,7 @@ class TagSpec : DomSpec { } @Test - fun render_Rich() { + fun renderRich() { run { val root = Root("test") val tag = Tag(TAG.H1, "This is <b>h1</b>", rich = true, align = ALIGN.RIGHT) @@ -35,7 +35,7 @@ class TagSpec : DomSpec { } @Test - fun render_AsContainer() { + fun renderAsContainer() { run { val root = Root("test") val tag = Tag(TAG.P, align = ALIGN.RIGHT) |