diff options
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form')
6 files changed, 28 insertions, 24 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt index 19f7e68a..8f93c7ea 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt @@ -23,12 +23,15 @@ package pl.treksoft.kvision.form import kotlinx.serialization.KSerializer import kotlinx.serialization.Mapper +import kotlinx.serialization.context.MutableSerialContextImpl import kotlinx.serialization.decode -import kotlinx.serialization.json.JSON import kotlinx.serialization.serializer import pl.treksoft.kvision.i18n.I18n.trans -import pl.treksoft.kvision.types.KDate +import pl.treksoft.kvision.types.DateSerializer import pl.treksoft.kvision.types.KFile +import pl.treksoft.kvision.types.toStringF +import pl.treksoft.kvision.utils.JSON +import kotlin.js.Date import kotlin.js.Json import kotlin.reflect.KProperty1 @@ -84,8 +87,8 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali modelFactory = { val map = it.flatMap { entry -> when (entry.value) { - is KDate -> { - listOf(entry.key to entry.value, "${entry.key}.time" to (entry.value as KDate).time) + is Date -> { + listOf(entry.key to (entry.value as? Date)?.toStringF()) } is List<*> -> { @Suppress("UNCHECKED_CAST") @@ -93,9 +96,9 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali listOf(entry.key to entry.value, "${entry.key}.size" to list.size) + list.mapIndexed { index, kFile -> listOf( - "${entry.key}.${index}.name" to kFile.name, - "${entry.key}.${index}.size" to kFile.size, - "${entry.key}.${index}.content" to kFile.content + "${entry.key}.$index.name" to kFile.name, + "${entry.key}.$index.size" to kFile.size, + "${entry.key}.$index.content" to kFile.content ) }.flatten() } ?: listOf() @@ -104,6 +107,7 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali } }.toMap() val mapper = Mapper.InNullableMapper(FormMapWrapper(map)) + mapper.context = MutableSerialContextImpl().apply { registerSerializer(Date::class, DateSerializer) } mapper.decode(serializer) } } @@ -182,8 +186,8 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali * @param validator optional validation function * @return current form */ - fun <C : KDateFormControl> add( - key: KProperty1<K, KDate?>, control: C, required: Boolean = false, requiredMessage: String? = null, + fun <C : DateFormControl> add( + key: KProperty1<K, Date?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): Form<K> { @@ -278,7 +282,7 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali * @return data model as JSON */ fun getDataJson(): Json { - return kotlin.js.JSON.parse(JSON.stringify(serializer, getData())) + return kotlin.js.JSON.parse(JSON.plain.stringify(serializer, getData())) } /** diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormControl.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormControl.kt index 6aff842a..6759fef9 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FormControl.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FormControl.kt @@ -22,8 +22,8 @@ package pl.treksoft.kvision.form import pl.treksoft.kvision.core.Component -import pl.treksoft.kvision.types.KDate import pl.treksoft.kvision.types.KFile +import kotlin.js.Date /** * Input controls sizes. @@ -190,15 +190,15 @@ interface BoolFormControl : FormControl { /** * Base interface of a form control with a date value. */ -interface KDateFormControl : FormControl { +interface DateFormControl : FormControl { /** * Date value. */ - var value: KDate? + var value: Date? - override fun getValue(): KDate? = value + override fun getValue(): Date? = value override fun setValue(v: Any?) { - value = v as? KDate + value = v as? Date } override fun getValueAsString(): String? = value?.toString() diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt index 3811ed82..3533dee5 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt @@ -32,8 +32,8 @@ 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 pl.treksoft.kvision.types.KDate import pl.treksoft.kvision.types.KFile +import kotlin.js.Date import kotlin.js.Json import kotlin.reflect.KProperty1 @@ -287,8 +287,8 @@ open class FormPanel<K : Any>( * @param validator optional validation function * @return current form panel */ - open fun <C : KDateFormControl> add( - key: KProperty1<K, KDate?>, control: C, required: Boolean = false, requiredMessage: String? = null, + open fun <C : DateFormControl> add( + key: KProperty1<K, Date?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): FormPanel<K> { 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 f79c1b48..2df3a055 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/CheckInput.kt @@ -67,14 +67,14 @@ open class CheckInput( /** * The selection state of the input. */ - var value by refreshOnUpdate(value, { refreshState() }) + var value by refreshOnUpdate(value) { refreshState() } /** * The value attribute of the generated HTML input element. * * This value is placed directly in generated HTML code, while the [value] property is dynamically * bound to the input selection state. */ - var startValue by refreshOnUpdate(value, { this.value = it; refresh() }) + var startValue by refreshOnUpdate(value) { this.value = it; refresh() } /** * The type of the generated HTML input element. */ 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 fdc1e3af..e2f27c36 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/check/RadioGroup.kt @@ -54,12 +54,12 @@ open class RadioGroup( /** * A list of options (label to value pairs) for the group. */ - var options by refreshOnUpdate(options, { setChildrenFromOptions() }) + var options by refreshOnUpdate(options) { setChildrenFromOptions() } /** * A value of the selected option. */ - override var value by refreshOnUpdate(value, { setValueToChildren(it) }) + override var value by refreshOnUpdate(value) { setValueToChildren(it) } /** * Determines if the options are rendered inline. diff --git a/src/main/kotlin/pl/treksoft/kvision/form/text/AbstractTextInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/AbstractTextInput.kt index e41cfb8f..3a06f47b 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/text/AbstractTextInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/AbstractTextInput.kt @@ -51,14 +51,14 @@ abstract class AbstractTextInput( /** * Text input value. */ - var value by refreshOnUpdate(value, { refreshState() }) + var value by refreshOnUpdate(value) { refreshState() } /** * The value attribute of the generated HTML input element. * * This value is placed directly in generated HTML code, while the [value] property is dynamically * bound to the text input value. */ - var startValue by refreshOnUpdate(value, { this.value = it; refresh() }) + var startValue by refreshOnUpdate(value) { this.value = it; refresh() } /** * The placeholder for the text input. */ |
