kjsuikit/src/jsMain/kotlin/dev/inmo/jsuikit/elements/StandardInput.kt

76 lines
2.0 KiB
Kotlin
Raw Normal View History

2022-01-12 10:27:49 +00:00
package dev.inmo.jsuikit.elements
2021-12-22 08:38:12 +00:00
import androidx.compose.runtime.*
2022-01-12 13:58:52 +00:00
import dev.inmo.jsuikit.modifiers.UIKitModifier
import dev.inmo.jsuikit.modifiers.include
2021-12-22 08:38:12 +00:00
import org.jetbrains.compose.web.attributes.*
import org.jetbrains.compose.web.dom.AttrBuilderContext
2021-12-22 08:38:12 +00:00
import org.jetbrains.compose.web.dom.Input
import org.w3c.dom.HTMLInputElement
2021-12-22 08:38:12 +00:00
@Composable
2023-03-05 08:41:30 +00:00
fun <T> StandardInput(
2021-12-22 08:38:12 +00:00
type: InputType<T>,
2022-11-15 15:50:15 +00:00
value: T,
2023-03-05 08:41:30 +00:00
vararg modifiers: UIKitModifier,
2022-11-15 15:50:15 +00:00
disabled: Boolean = false,
2021-12-22 08:38:12 +00:00
placeholder: String? = null,
attributesCustomizer: AttrBuilderContext<HTMLInputElement> = {},
onChange: HTMLInputElement.(T) -> Unit
2021-12-22 08:38:12 +00:00
) {
Input(type) {
classes("uk-input")
include(*modifiers)
placeholder ?.let(::placeholder)
2022-11-15 15:50:15 +00:00
value.let {
when (it) {
is String -> value(it)
is Number -> value(it)
else -> {}
}
}
onInput { event -> event.target.onChange(event.value) }
2022-11-15 15:50:15 +00:00
if (disabled) {
disabled()
2021-12-22 08:38:12 +00:00
}
attributesCustomizer()
2021-12-22 08:38:12 +00:00
}
}
2022-11-15 15:50:15 +00:00
2023-03-05 08:41:30 +00:00
@Deprecated("Renamed", ReplaceWith("StandardInput(type, value, *modifiers, disabled, placeholder, attributesCustomizer, onChange)"))
@Composable
fun <T> DefaultInput(
type: InputType<T>,
value: T,
disabled: Boolean = false,
placeholder: String? = null,
vararg modifiers: UIKitModifier,
attributesCustomizer: AttrBuilderContext<HTMLInputElement> = {},
onChange: (T) -> Unit
) = StandardInput(type, value, modifiers = modifiers, disabled, placeholder, attributesCustomizer) {
onChange(it)
}
2023-03-05 08:41:30 +00:00
2022-11-15 15:50:15 +00:00
@Composable
fun <T> StandardInput(
type: InputType<T>,
state: MutableState<T>,
disabledState: State<Boolean>? = null,
placeholder: String? = null,
vararg modifiers: UIKitModifier,
attributesCustomizer: AttrBuilderContext<HTMLInputElement> = {},
2023-03-05 08:41:30 +00:00
) = StandardInput(
2022-11-15 15:50:15 +00:00
type,
state.value,
2023-03-05 08:41:30 +00:00
modifiers = modifiers,
2022-11-15 15:50:15 +00:00
disabledState ?.value == true,
placeholder,
attributesCustomizer = attributesCustomizer
) {
state.value = it
}