aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/form/check
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2018-02-12 10:50:42 +0100
committerRobert Jaros <rjaros@finn.pl>2018-02-12 10:50:42 +0100
commitbafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c (patch)
tree39288bdcb59ec48cb70f1c5820fc1854949744da /src/main/kotlin/pl/treksoft/kvision/form/check
parent2feea5e7cf8d492663e826ebcfb0a58e61820352 (diff)
downloadkvision-bafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c.tar.gz
kvision-bafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c.tar.bz2
kvision-bafcdb6fc3f1d4ec4f1a0f4fcce776ba10d8136c.zip
Type safe DSL builders
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form/check')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt21
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt15
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt21
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt20
4 files changed, 65 insertions, 12 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt
index 0bad8440..4c373a2d 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckBox.kt
@@ -22,6 +22,7 @@
package pl.treksoft.kvision.form.check
import org.w3c.dom.events.MouseEvent
+import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.core.Widget
import pl.treksoft.kvision.form.BoolFormControl
@@ -153,10 +154,6 @@ open class CheckBox(
counter++
}
- companion object {
- internal var counter = 0
- }
-
@Suppress("UNCHECKED_CAST")
override fun <T : Widget> setEventListener(block: SnOn<T>.() -> Unit): Widget {
input.setEventListener(block)
@@ -201,4 +198,20 @@ open class CheckBox(
}
return this
}
+
+ companion object {
+ internal var counter = 0
+
+ /**
+ * DSL builder extension function
+ *
+ * It takes the same parameters as the constructor of the built component.
+ */
+ fun Container.checkBox(
+ value: Boolean = false, label: String? = null,
+ rich: Boolean = false, init: (CheckBox.() -> Unit)? = null
+ ) {
+ this.add(CheckBox(value, label, rich).apply { init?.invoke(this) })
+ }
+ }
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt
index 26a06fa7..8d083b42 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt
@@ -23,6 +23,7 @@ package pl.treksoft.kvision.form.check
import com.github.snabbdom.VNode
import org.w3c.dom.events.MouseEvent
+import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.core.StringPair
import pl.treksoft.kvision.core.Widget
@@ -179,4 +180,18 @@ open class CheckInput(
}
return this
}
+
+ companion object {
+ /**
+ * DSL builder extension function
+ *
+ * It takes the same parameters as the constructor of the built component.
+ */
+ fun Container.checkInput(
+ type: CHECKINPUTTYPE = CHECKINPUTTYPE.CHECKBOX, value: Boolean = false,
+ classes: Set<String> = setOf(), init: (CheckInput.() -> Unit)? = null
+ ) {
+ this.add(CheckInput(type, value, classes).apply { init?.invoke(this) })
+ }
+ }
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt
index a8059230..13f4c4cd 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/check/Radio.kt
@@ -22,6 +22,7 @@
package pl.treksoft.kvision.form.check
import org.w3c.dom.events.MouseEvent
+import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.core.Widget
import pl.treksoft.kvision.form.BoolFormControl
@@ -164,10 +165,6 @@ open class Radio(
counter++
}
- companion object {
- internal var counter = 0
- }
-
@Suppress("UNCHECKED_CAST")
override fun <T : Widget> setEventListener(block: SnOn<T>.() -> Unit): Widget {
input.setEventListener(block)
@@ -221,4 +218,20 @@ open class Radio(
}
return this
}
+
+ companion object {
+ internal var counter = 0
+
+ /**
+ * DSL builder extension function
+ *
+ * It takes the same parameters as the constructor of the built component.
+ */
+ fun Container.radio(
+ value: Boolean = false, extraValue: String? = null, name: String? = null, label: String? = null,
+ rich: Boolean = false, init: (Radio.() -> Unit)? = null
+ ) {
+ this.add(Radio(value, extraValue, name, label, rich).apply { init?.invoke(this) })
+ }
+ }
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt
index 7bd13d4e..80736d59 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt
@@ -21,6 +21,7 @@
*/
package pl.treksoft.kvision.form.check
+import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.core.StringPair
import pl.treksoft.kvision.core.Widget
@@ -110,10 +111,6 @@ open class RadioGroup(
counter++
}
- companion object {
- internal var counter = 0
- }
-
override fun getSnClass(): List<StringBoolPair> {
val cl = super.getSnClass().toMutableList()
if (validatorError != null) {
@@ -166,4 +163,19 @@ open class RadioGroup(
this.addInternal(validationInfo)
}
+ companion object {
+ internal var counter = 0
+
+ /**
+ * DSL builder extension function
+ *
+ * It takes the same parameters as the constructor of the built component.
+ */
+ fun Container.radioGroup(
+ options: List<StringPair>? = null, value: String? = null, inline: Boolean = false,
+ label: String? = null, rich: Boolean = false, init: (RadioGroup.() -> Unit)? = null
+ ) {
+ this.add(RadioGroup(options, value, inline, label, rich).apply { init?.invoke(this) })
+ }
+ }
}