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

82 lines
2.5 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.Composable
import androidx.compose.web.events.SyntheticMouseEvent
2022-01-12 13:58:52 +00:00
import dev.inmo.jsuikit.modifiers.*
2021-12-22 08:38:12 +00:00
import org.jetbrains.compose.web.attributes.InputType
import org.jetbrains.compose.web.attributes.disabled
import org.jetbrains.compose.web.dom.*
import org.w3c.dom.HTMLButtonElement
import org.w3c.dom.HTMLDivElement
2021-12-22 08:38:12 +00:00
import org.w3c.dom.events.Event
@Composable
fun DefaultButton(
vararg modifiers: UIKitModifier,
2021-12-22 08:38:12 +00:00
disabled: Boolean = false,
onClick: ((SyntheticMouseEvent) -> Unit)? = null,
attributesCustomizer: AttrBuilderContext<HTMLButtonElement> = {},
2021-12-22 08:38:12 +00:00
contentAllocator: ContentBuilder<HTMLButtonElement>
) {
Button(
{
onClick ?.let { onClick(it) }
2022-01-25 06:54:19 +00:00
include(UIKitButton, *modifiers)
2021-12-22 08:38:12 +00:00
if (disabled) {
disabled()
}
attributesCustomizer()
2021-12-22 08:38:12 +00:00
}
) {
contentAllocator()
}
}
@Composable
fun DefaultButton(
text: String,
vararg modifiers: UIKitModifier,
2021-12-22 08:38:12 +00:00
disabled: Boolean = false,
preTextContentAllocator: ContentBuilder<HTMLButtonElement>? = null,
afterTextContentAllocator: ContentBuilder<HTMLButtonElement>? = null,
attributesCustomizer: AttrBuilderContext<HTMLButtonElement> = {},
2021-12-22 08:38:12 +00:00
onClick: ((SyntheticMouseEvent) -> Unit)? = null
2022-01-25 06:54:19 +00:00
) = DefaultButton(*modifiers, disabled = disabled, onClick = onClick, attributesCustomizer = attributesCustomizer) {
2021-12-22 08:38:12 +00:00
preTextContentAllocator ?.apply { preTextContentAllocator() }
Text(text)
afterTextContentAllocator ?.apply { afterTextContentAllocator() }
}
@Composable
fun UploadButton(
text: String,
vararg buttonModifiers: UIKitModifier,
2021-12-22 08:38:12 +00:00
containerModifiers: Array<UIKitModifier> = emptyArray(),
disabled: Boolean = false,
buttonType: UIKitButton.Type = UIKitButton.Type.Default,
attributesCustomizer: AttrBuilderContext<HTMLDivElement> = {},
2021-12-22 08:38:12 +00:00
onChange: (Event) -> Unit
) {
Div(
{
classes("js-upload", "uk-form-custom")
attr("uk-form-custom", "")
include(*containerModifiers)
attributesCustomizer()
2021-12-22 08:38:12 +00:00
}
) {
Input(InputType.File) { onChange { onChange(it.nativeEvent) } }
Button(
{
classes("uk-button")
include(*buttonModifiers, buttonType)
if (disabled) {
disabled()
}
}
) {
Text(text)
}
}
}