From c87e1851919c60d57aeb77809a94f589d537f827 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 7 Mar 2018 00:07:07 +0100 Subject: More type-safe forms implementation. --- src/main/kotlin/pl/treksoft/kvision/form/Form.kt | 84 +++++++++++++++++--- .../kotlin/pl/treksoft/kvision/form/FormPanel.kt | 91 ++++++++++++++++++---- 2 files changed, 148 insertions(+), 27 deletions(-) (limited to 'src/main/kotlin') 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(private val panel: FormPanel? = null, private val modelFactory: internal var validatorMessage: ((Form) -> String?)? = null internal var validator: ((Form) -> Boolean?)? = null + internal fun addInternal( + key: KProperty1, control: C, required: Boolean = false, + validatorMessage: ((C) -> String?)? = null, + validator: ((C) -> Boolean?)? = null + ): Form { + 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 add( - key: String, control: C, required: Boolean = false, + fun add( + key: KProperty1, control: C, required: Boolean = false, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): Form { - 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 add( + key: KProperty1, control: C, required: Boolean = false, + validatorMessage: ((C) -> String?)? = null, + validator: ((C) -> Boolean?)? = null + ): Form { + 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 add( + key: KProperty1, control: C, required: Boolean = false, + validatorMessage: ((C) -> String?)? = null, + validator: ((C) -> Boolean?)? = null + ): Form { + 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 add( + key: KProperty1, control: C, required: Boolean = false, + validatorMessage: ((C) -> String?)? = null, + validator: ((C) -> Boolean?)? = null + ): Form { + return addInternal(key, control, required, validatorMessage, validator) } /** @@ -72,8 +132,8 @@ class Form(private val panel: FormPanel? = null, private val modelFactory: * @param key key identifier of the control * @return current form */ - fun remove(key: String): Form { - this.fields.remove(key) + fun remove(key: KProperty1): Form { + this.fields.remove(key.name) return this } @@ -91,8 +151,8 @@ class Form(private val panel: FormPanel? = 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): FormControl? { + return this.fields[key.name] } /** @@ -100,7 +160,7 @@ class Form(private val panel: FormPanel? = 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): 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( 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 add( - key: String, control: C, required: Boolean = false, + protected fun addInternal( + key: KProperty1, control: C, required: Boolean = false, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): FormPanel { @@ -135,16 +128,84 @@ open class FormPanel( } } 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 add( + key: KProperty1, control: C, required: Boolean = false, + validatorMessage: ((C) -> String?)? = null, + validator: ((C) -> Boolean?)? = null + ): FormPanel { + 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 add( + key: KProperty1, control: C, required: Boolean = false, + validatorMessage: ((C) -> String?)? = null, + validator: ((C) -> Boolean?)? = null + ): FormPanel { + 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 add( + key: KProperty1, control: C, required: Boolean = false, + validatorMessage: ((C) -> String?)? = null, + validator: ((C) -> Boolean?)? = null + ): FormPanel { + 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 add( + key: KProperty1, control: C, required: Boolean = false, + validatorMessage: ((C) -> String?)? = null, + validator: ((C) -> Boolean?)? = null + ): FormPanel { + 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 { + open fun remove(key: KProperty1): FormPanel { form.getControl(key)?.let { super.remove(it) } @@ -164,7 +225,7 @@ open class FormPanel( * @param key key identifier of the control * @return selected control */ - open fun getControl(key: String): FormControl? { + open fun getControl(key: KProperty1): FormControl? { return form.getControl(key) } @@ -173,7 +234,7 @@ open class FormPanel( * @param key key identifier of the control * @return value of the control */ - operator fun get(key: String): Any? { + operator fun get(key: KProperty1): Any? { return getControl(key)?.getValue() } -- cgit