core/client/src/jsMain/kotlin/dev/inmo/postssystem/client/utils/DialogHelper.kt

65 lines
2.3 KiB
Kotlin

package dev.inmo.postssystem.client.utils
import com.benasher44.uuid.uuid4
import kotlinx.browser.document
import kotlinx.html.*
import kotlinx.html.dom.append
import kotlinx.html.js.*
import org.w3c.dom.*
object DialogHelper {
fun createOneFieldDialog(
title: String,
hint: String,
doneButtonText: String,
closeButtonText: String,
onClose: () -> Unit,
onSubmit: (String) -> Unit
): HTMLDialogElement {
lateinit var dialogElement: HTMLDialogElement
(document.getElementsByTagName("body").item(0) as? HTMLBodyElement) ?.append {
dialogElement = dialog("mdl-dialog") {
h4("mdl-dialog__title") {
+title
}
val id = "form_${uuid4()}_text"
div(classes = "mdl-dialog__content") {
form("#") {
div("mdl-textfield mdl-js-textfield mdl-textfield--floating-label") {
input(InputType.text, classes = "mdl-textfield__input") {
this.id = id
}
label(classes = "mdl-textfield__label") {
+hint
attributes["for"] = id
}
}
}
}
div(classes = "mdl-dialog__actions mdl-dialog__actions--full-width") {
button(classes = "mdl-button", type = ButtonType.button) {
+doneButtonText
onClickFunction = {
it.preventDefault()
val input = document.getElementById(id) as? HTMLInputElement
input ?.value ?.let {
onSubmit(it)
}
}
}
button(classes = "mdl-button", type = ButtonType.button) {
+closeButtonText
onClickFunction = {
it.preventDefault()
onClose()
}
}
}
}
}
return dialogElement
}
}