aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2018-03-07 00:07:07 +0100
committerRobert Jaros <rjaros@finn.pl>2018-03-07 00:07:07 +0100
commitc87e1851919c60d57aeb77809a94f589d537f827 (patch)
tree4c1ba89595d7710711c35829baf149740e0cbf90 /src/main/kotlin/pl/treksoft/kvision
parent35a6bc496500d77a9f76fc9806713ed378cfbb68 (diff)
downloadkvision-c87e1851919c60d57aeb77809a94f589d537f827.tar.gz
kvision-c87e1851919c60d57aeb77809a94f589d537f827.tar.bz2
kvision-c87e1851919c60d57aeb77809a94f589d537f827.zip
More type-safe forms implementation.
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/Form.kt84
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt91
2 files changed, 148 insertions, 27 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt
index e91002e9..1b06156e 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt
@@ -23,6 +23,7 @@ package pl.treksoft.kvision.form
import kotlin.js.Date
import kotlin.js.Json
+import kotlin.reflect.KProperty1
/**
* Internal data class containing form field parameters.
@@ -48,23 +49,82 @@ class Form<K>(private val panel: FormPanel<K>? = null, private val modelFactory:
internal var validatorMessage: ((Form<K>) -> String?)? = null
internal var validator: ((Form<K>) -> Boolean?)? = null
+ internal fun <C : FormControl> addInternal(
+ key: KProperty1<K, *>, control: C, required: Boolean = false,
+ validatorMessage: ((C) -> String?)? = null,
+ validator: ((C) -> Boolean?)? = null
+ ): Form<K> {
+ this.fields[key.name] = control
+ this.fieldsParams[key.name] = FieldParams(required, validatorMessage, validator)
+ return this
+ }
+
/**
- * Adds a control to the form.
+ * Adds a string control to the form.
* @param key key identifier of the control
- * @param control the form control
+ * @param control the string form control
* @param required determines if the control is required
* @param validatorMessage optional function returning validation message
* @param validator optional validation function
* @return current form
*/
- fun <C : FormControl> add(
- key: String, control: C, required: Boolean = false,
+ fun <C : StringFormControl> add(
+ key: KProperty1<K, String?>, control: C, required: Boolean = false,
validatorMessage: ((C) -> String?)? = null,
validator: ((C) -> Boolean?)? = null
): Form<K> {
- this.fields[key] = control
- this.fieldsParams[key] = FieldParams(required, validatorMessage, validator)
- return this
+ return addInternal(key, control, required, validatorMessage, validator)
+ }
+
+ /**
+ * Adds a boolean control to the form.
+ * @param key key identifier of the control
+ * @param control the boolean form control
+ * @param required determines if the control is required
+ * @param validatorMessage optional function returning validation message
+ * @param validator optional validation function
+ * @return current form
+ */
+ fun <C : BoolFormControl> add(
+ key: KProperty1<K, Boolean?>, control: C, required: Boolean = false,
+ validatorMessage: ((C) -> String?)? = null,
+ validator: ((C) -> Boolean?)? = null
+ ): Form<K> {
+ return addInternal(key, control, required, validatorMessage, validator)
+ }
+
+ /**
+ * Adds a number control to the form.
+ * @param key key identifier of the control
+ * @param control the number form control
+ * @param required determines if the control is required
+ * @param validatorMessage optional function returning validation message
+ * @param validator optional validation function
+ * @return current form
+ */
+ fun <C : NumberFormControl> add(
+ key: KProperty1<K, Number?>, control: C, required: Boolean = false,
+ validatorMessage: ((C) -> String?)? = null,
+ validator: ((C) -> Boolean?)? = null
+ ): Form<K> {
+ return addInternal(key, control, required, validatorMessage, validator)
+ }
+
+ /**
+ * Adds a date control to the form.
+ * @param key key identifier of the control
+ * @param control the date form control
+ * @param required determines if the control is required
+ * @param validatorMessage optional function returning validation message
+ * @param validator optional validation function
+ * @return current form
+ */
+ fun <C : DateFormControl> add(
+ key: KProperty1<K, Date?>, control: C, required: Boolean = false,
+ validatorMessage: ((C) -> String?)? = null,
+ validator: ((C) -> Boolean?)? = null
+ ): Form<K> {
+ return addInternal(key, control, required, validatorMessage, validator)
}
/**
@@ -72,8 +132,8 @@ class Form<K>(private val panel: FormPanel<K>? = null, private val modelFactory:
* @param key key identifier of the control
* @return current form
*/
- fun remove(key: String): Form<K> {
- this.fields.remove(key)
+ fun remove(key: KProperty1<K, *>): Form<K> {
+ this.fields.remove(key.name)
return this
}
@@ -91,8 +151,8 @@ class Form<K>(private val panel: FormPanel<K>? = null, private val modelFactory:
* @param key key identifier of the control
* @return selected control
*/
- fun getControl(key: String): FormControl? {
- return this.fields[key]
+ fun getControl(key: KProperty1<K, *>): FormControl? {
+ return this.fields[key.name]
}
/**
@@ -100,7 +160,7 @@ class Form<K>(private val panel: FormPanel<K>? = null, private val modelFactory:
* @param key key identifier of the control
* @return value of the control
*/
- operator fun get(key: String): Any? {
+ operator fun get(key: KProperty1<K, *>): Any? {
return getControl(key)?.getValue()
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt
index c13fe9f1..5df2d299 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt
@@ -29,7 +29,9 @@ import pl.treksoft.kvision.form.check.Radio
import pl.treksoft.kvision.html.TAG
import pl.treksoft.kvision.html.Tag
import pl.treksoft.kvision.panel.SimplePanel
+import kotlin.js.Date
import kotlin.js.Json
+import kotlin.reflect.KProperty1
/**
* Bootstrap form layout options.
@@ -109,17 +111,8 @@ open class FormPanel<K>(
return cl
}
- /**
- * Adds a control to the form panel.
- * @param key key identifier of the control
- * @param control the form control
- * @param required determines if the control is required
- * @param validatorMessage optional function returning validation message
- * @param validator optional validation function
- * @return current form panel
- */
- open fun <C : FormControl> add(
- key: String, control: C, required: Boolean = false,
+ protected fun <C : FormControl> addInternal(
+ key: KProperty1<K, *>, control: C, required: Boolean = false,
validatorMessage: ((C) -> String?)? = null,
validator: ((C) -> Boolean?)? = null
): FormPanel<K> {
@@ -135,16 +128,84 @@ open class FormPanel<K>(
}
}
super.add(control)
- form.add(key, control, required, validatorMessage, validator)
+ form.addInternal(key, control, required, validatorMessage, validator)
return this
}
/**
+ * Adds a string control to the form panel.
+ * @param key key identifier of the control
+ * @param control the string form control
+ * @param required determines if the control is required
+ * @param validatorMessage optional function returning validation message
+ * @param validator optional validation function
+ * @return current form panel
+ */
+ open fun <C : StringFormControl> add(
+ key: KProperty1<K, String?>, control: C, required: Boolean = false,
+ validatorMessage: ((C) -> String?)? = null,
+ validator: ((C) -> Boolean?)? = null
+ ): FormPanel<K> {
+ return addInternal(key, control, required, validatorMessage, validator)
+ }
+
+ /**
+ * Adds a boolean control to the form panel.
+ * @param key key identifier of the control
+ * @param control the boolean form control
+ * @param required determines if the control is required
+ * @param validatorMessage optional function returning validation message
+ * @param validator optional validation function
+ * @return current form panel
+ */
+ open fun <C : BoolFormControl> add(
+ key: KProperty1<K, Boolean?>, control: C, required: Boolean = false,
+ validatorMessage: ((C) -> String?)? = null,
+ validator: ((C) -> Boolean?)? = null
+ ): FormPanel<K> {
+ return addInternal(key, control, required, validatorMessage, validator)
+ }
+
+ /**
+ * Adds a number control to the form panel.
+ * @param key key identifier of the control
+ * @param control the number form control
+ * @param required determines if the control is required
+ * @param validatorMessage optional function returning validation message
+ * @param validator optional validation function
+ * @return current form panel
+ */
+ open fun <C : NumberFormControl> add(
+ key: KProperty1<K, Number?>, control: C, required: Boolean = false,
+ validatorMessage: ((C) -> String?)? = null,
+ validator: ((C) -> Boolean?)? = null
+ ): FormPanel<K> {
+ return addInternal(key, control, required, validatorMessage, validator)
+ }
+
+ /**
+ * Adds a date control to the form panel.
+ * @param key key identifier of the control
+ * @param control the date form control
+ * @param required determines if the control is required
+ * @param validatorMessage optional function returning validation message
+ * @param validator optional validation function
+ * @return current form panel
+ */
+ open fun <C : DateFormControl> add(
+ key: KProperty1<K, Date?>, control: C, required: Boolean = false,
+ validatorMessage: ((C) -> String?)? = null,
+ validator: ((C) -> Boolean?)? = null
+ ): FormPanel<K> {
+ return addInternal(key, control, required, validatorMessage, validator)
+ }
+
+ /**
* Removes a control from the form panel.
* @param key key identifier of the control
* @return current form panel
*/
- open fun remove(key: String): FormPanel<K> {
+ open fun remove(key: KProperty1<K, *>): FormPanel<K> {
form.getControl(key)?.let {
super.remove(it)
}
@@ -164,7 +225,7 @@ open class FormPanel<K>(
* @param key key identifier of the control
* @return selected control
*/
- open fun getControl(key: String): FormControl? {
+ open fun getControl(key: KProperty1<K, *>): FormControl? {
return form.getControl(key)
}
@@ -173,7 +234,7 @@ open class FormPanel<K>(
* @param key key identifier of the control
* @return value of the control
*/
- operator fun get(key: String): Any? {
+ operator fun get(key: KProperty1<K, *>): Any? {
return getControl(key)?.getValue()
}