StandardInput improvements

This commit is contained in:
InsanusMokrassar 2022-11-15 21:50:15 +06:00
parent 36214aeba5
commit 17ae0b119b
2 changed files with 28 additions and 8 deletions

View File

@ -2,6 +2,8 @@
## 0.4.1 ## 0.4.1
* Now it is possible to use `StandardInput` with simple `T` types instead of states
## 0.4.0 ## 0.4.0
* `Kotlin`: `1.7.20` * `Kotlin`: `1.7.20`

View File

@ -11,11 +11,12 @@ import org.w3c.dom.HTMLInputElement
@Composable @Composable
fun <T> StandardInput( fun <T> StandardInput(
type: InputType<T>, type: InputType<T>,
state: MutableState<T>, value: T,
disabledState: State<Boolean>? = null, disabled: Boolean = false,
placeholder: String? = null, placeholder: String? = null,
vararg modifiers: UIKitModifier, vararg modifiers: UIKitModifier,
attributesCustomizer: AttrBuilderContext<HTMLInputElement> = {}, attributesCustomizer: AttrBuilderContext<HTMLInputElement> = {},
onChange: (T) -> Unit
) { ) {
Input(type) { Input(type) {
classes("uk-input") classes("uk-input")
@ -23,7 +24,7 @@ fun <T> StandardInput(
placeholder ?.let(::placeholder) placeholder ?.let(::placeholder)
state.value.let { value.let {
when (it) { when (it) {
is String -> value(it) is String -> value(it)
is Number -> value(it) is Number -> value(it)
@ -31,13 +32,30 @@ fun <T> StandardInput(
} }
} }
onInput { state.value = it.value } onInput { onChange(it.value) }
disabledState ?.let { if (disabled) {
if (it.value) {
disabled() disabled()
} }
}
attributesCustomizer() attributesCustomizer()
} }
} }
@Composable
fun <T> StandardInput(
type: InputType<T>,
state: MutableState<T>,
disabledState: State<Boolean>? = null,
placeholder: String? = null,
vararg modifiers: UIKitModifier,
attributesCustomizer: AttrBuilderContext<HTMLInputElement> = {},
) = StandardInput(
type,
state.value,
disabledState ?.value == true,
placeholder,
modifiers = modifiers,
attributesCustomizer = attributesCustomizer
) {
state.value = it
}