Skip to content

Commit 600973c

Browse files
committed
Support different types of buttons and make Button default to Contained
1 parent 83ce685 commit 600973c

File tree

3 files changed

+35
-6
lines changed
  • compose-multiplatform-material/src
    • commonMain/kotlin/com/huanshankeji/compose/material
    • jsMain/kotlin/com/huanshankeji/compose/material
    • jvmMain/kotlin/com/huanshankeji/compose/material

3 files changed

+35
-6
lines changed

compose-multiplatform-material/src/commonMain/kotlin/com/huanshankeji/compose/material/Button.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ expect /*value*/ class ButtonScope {
1111

1212
expect abstract class ButtonElement : Element
1313

14+
enum class ButtonType {
15+
Contained, Outlined, Text
16+
}
17+
1418
@Composable
1519
expect fun Button(
1620
onClick: () -> Unit,
21+
buttonType: ButtonType = ButtonType.Contained,
1722
modifierOrAttrs: ModifierOrAttrs<ButtonElement> = null,
1823
content: @Composable ButtonScope.() -> Unit
1924
)
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.huanshankeji.compose.material
22

33
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.material.ButtonType.*
45
import com.huanshankeji.compose.ui.ModifierOrAttrs
56
import com.huanshankeji.compose.ui.toAttrs
67
import com.huanshankeji.compose.web.attributes.attrs
78
import com.huanshankeji.compose.web.attributes.plus
89
import dev.petuska.kmdc.button.Label
910
import dev.petuska.kmdc.button.MDCButton
1011
import dev.petuska.kmdc.button.MDCButtonScope
12+
import dev.petuska.kmdc.button.MDCButtonType
1113
import org.w3c.dom.HTMLButtonElement
1214

1315
actual class ButtonScope(val mdcButtonScope: MDCButtonScope<HTMLButtonElement>) {
@@ -18,13 +20,24 @@ actual class ButtonScope(val mdcButtonScope: MDCButtonScope<HTMLButtonElement>)
1820

1921
actual typealias ButtonElement = HTMLButtonElement
2022

23+
fun ButtonType.toMDCButtonType() =
24+
when (this) {
25+
Contained -> MDCButtonType.Raised
26+
Outlined -> MDCButtonType.Outlined
27+
Text -> MDCButtonType.Text
28+
}
29+
2130
@Composable
2231
actual fun Button(
23-
onClick: () -> Unit, modifierOrAttrs: ModifierOrAttrs<ButtonElement>, content: @Composable ButtonScope.() -> Unit
32+
onClick: () -> Unit,
33+
buttonType: ButtonType,
34+
modifierOrAttrs: ModifierOrAttrs<ButtonElement>,
35+
content: @Composable ButtonScope.() -> Unit
2436
) =
25-
MDCButton(attrs = attrs<ButtonElement> {
26-
onClick { onClick() }
27-
} + modifierOrAttrs.toAttrs()) {
37+
MDCButton(buttonType.toMDCButtonType(),
38+
attrs = attrs<ButtonElement> {
39+
onClick { onClick() }
40+
} + modifierOrAttrs.toAttrs()) {
2841
ButtonScope(this).content()
2942
}
3043

compose-multiplatform-material/src/jvmMain/kotlin/com/huanshankeji/compose/material/Button.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.huanshankeji.compose.material
22

33
import androidx.compose.foundation.layout.RowScope
4+
import androidx.compose.material.OutlinedButton
5+
import androidx.compose.material.TextButton
46
import androidx.compose.runtime.Composable
7+
import com.huanshankeji.compose.material.ButtonType.*
58
import com.huanshankeji.compose.ui.Element
69
import com.huanshankeji.compose.ui.ModifierOrAttrs
710
import com.huanshankeji.compose.ui.toModifier
@@ -17,7 +20,15 @@ actual abstract class ButtonElement : Element()
1720
@Composable
1821
actual fun Button(
1922
onClick: () -> Unit,
23+
buttonType: ButtonType,
2024
modifierOrAttrs: ModifierOrAttrs<ButtonElement>,
2125
content: @Composable ButtonScope.() -> Unit
22-
) =
23-
androidx.compose.material.Button(onClick, modifierOrAttrs.toModifier()) { ButtonScope(this).content() }
26+
) {
27+
val modifier = modifierOrAttrs.toModifier()
28+
val androidxContent: @Composable RowScope.() -> Unit = { ButtonScope(this).content() }
29+
when (buttonType) {
30+
Contained -> androidx.compose.material.Button(onClick, content = androidxContent)
31+
Outlined -> OutlinedButton(onClick, content = androidxContent)
32+
Text -> TextButton(onClick, content = androidxContent)
33+
}
34+
}

0 commit comments

Comments
 (0)