From d17f27058f41f2dddd9fd5e88149ed55f9f7bf0c Mon Sep 17 00:00:00 2001
From: Robert Jaros <rjaros@finn.pl>
Date: Thu, 19 Oct 2017 13:48:03 +0200
Subject: Form control: Radio Unit tests

---
 src/main/kotlin/pl/treksoft/kvision/Showcase.kt    |  18 +++
 .../kotlin/pl/treksoft/kvision/form/CheckBox.kt    |   4 +-
 .../pl/treksoft/kvision/form/CheckBoxInput.kt      |  23 +++-
 src/main/kotlin/pl/treksoft/kvision/form/Radio.kt  | 123 +++++++++++++++++++++
 4 files changed, 164 insertions(+), 4 deletions(-)
 create mode 100644 src/main/kotlin/pl/treksoft/kvision/form/Radio.kt

(limited to 'src/main/kotlin/pl')

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
+    }
+}
-- 
cgit