Skip to content

Commit 7ac54c3

Browse files
authored
Merge pull request #2 from huanshankeji/text-field
Implement `TextField`
2 parents 2eef43c + c289bed commit 7ac54c3

File tree

6 files changed

+93
-0
lines changed
  • compose-multiplatform-common/src/jsMain/kotlin/com/huanshankeji/compose/ui
  • compose-multiplatform-material/src
    • commonMain/kotlin/com/huanshankeji/compose/material
    • jsMain/kotlin/com/huanshankeji/compose/material
    • jvmMain/kotlin/com/huanshankeji/compose/material
  • demo/src/commonMain/kotlin/com/huanshankeji/compose/material/demo

6 files changed

+93
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ This project is prototype and there is no documentation yet. Check out [the demo
3636
- `IconButton`
3737
- `ScrollableList`/`LazyColumn` (visually inconsistent for now)
3838
- `Text`/`MaterialText`
39+
- `TextField`
3940
- `TopAppBarScaffold`
4041

4142
### Styles

compose-multiplatform-common/src/jsMain/kotlin/com/huanshankeji/compose/ui/ModifierOrAttrsScope.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import org.jetbrains.compose.web.css.*
1313
import org.jetbrains.compose.web.dom.AttrBuilderContext
1414
import org.w3c.dom.HTMLElement
1515

16+
/*
17+
fun <TElement : Element, TAttrsScope : AttrsScope<TElement>> ModifierOrAttrs<TElement>.toAttrs2(): (TAttrsScope.() -> Unit)? =
18+
this?.let { { ModifierOrAttrsScope(this).it() } }
19+
*/
20+
1621
fun <TElement : Element> ModifierOrAttrs<TElement>.toAttrs(): AttrBuilderContext<TElement>? =
1722
this?.let { { ModifierOrAttrsScope(this).it() } }
1823

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.huanshankeji.compose.material
2+
3+
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.ui.Element
5+
import com.huanshankeji.compose.ui.ModifierOrAttrs
6+
7+
expect abstract class TextFieldElement : Element
8+
9+
@Composable
10+
expect fun TextField(
11+
value: String,
12+
onValueChange: (String) -> Unit,
13+
modifierOrAttrs: ModifierOrAttrs<TextFieldElement> = null,
14+
enabled: Boolean = true,
15+
label: String? = null,
16+
/*
17+
placeholder: String? = null,
18+
helperText: String? = null,
19+
*/
20+
leadingIcon: @Composable (() -> Unit)? = null,
21+
trailingIcon: @Composable (() -> Unit)? = null,
22+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.huanshankeji.compose.material
2+
3+
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.ui.ModifierOrAttrs
5+
import com.huanshankeji.compose.ui.toAttrs
6+
import dev.petuska.kmdc.textfield.MDCTextField
7+
import org.w3c.dom.HTMLInputElement
8+
9+
actual typealias TextFieldElement = HTMLInputElement
10+
11+
@Composable
12+
actual fun TextField(
13+
value: String,
14+
onValueChange: (String) -> Unit,
15+
modifierOrAttrs: ModifierOrAttrs<TextFieldElement>,
16+
enabled: Boolean,
17+
label: String?,
18+
leadingIcon: @Composable (() -> Unit)?,
19+
trailingIcon: @Composable (() -> Unit)?,
20+
) =
21+
MDCTextField(value,
22+
attrs = {
23+
onInput { onValueChange(it.value) }
24+
modifierOrAttrs.toAttrs()?.let { it() }
25+
},
26+
disabled = !enabled,
27+
label = label,
28+
leadingIcon = leadingIcon?.let { { it() } },
29+
trailingIcon = trailingIcon?.let { { it() } }
30+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.huanshankeji.compose.material
2+
3+
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.ui.Element
5+
import com.huanshankeji.compose.ui.ModifierOrAttrs
6+
import com.huanshankeji.compose.ui.toModifier
7+
8+
actual abstract class TextFieldElement : Element()
9+
10+
@Composable
11+
actual fun TextField(
12+
value: String,
13+
onValueChange: (String) -> Unit,
14+
modifierOrAttrs: ModifierOrAttrs<TextFieldElement>,
15+
enabled: Boolean,
16+
label: String?,
17+
leadingIcon: @Composable (() -> Unit)?,
18+
trailingIcon: @Composable (() -> Unit)?,
19+
) =
20+
androidx.compose.material.TextField(
21+
value,
22+
onValueChange,
23+
modifierOrAttrs.toModifier(),
24+
enabled = enabled,
25+
label = label?.let { { Text(it) } },
26+
leadingIcon = leadingIcon,
27+
trailingIcon = trailingIcon
28+
)

demo/src/commonMain/kotlin/com/huanshankeji/compose/material/demo/App.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ fun App() {
105105
DemoSquare(rgbaColor(0xFFU, 0U, 0U, 0x80U))
106106
DemoSquare(rgbaColor(0xFFU, 0U, 0U, 0.5F))
107107
}
108+
109+
var text by remember { mutableStateOf("") }
110+
TextField(
111+
text, { text = it },
112+
label = "Demo text field",
113+
leadingIcon = { Icon(MaterialIcons.Add, null) },
114+
trailingIcon = { Icon(MaterialIcons.Menu, null) })
108115
}
109116
}
110117
}

0 commit comments

Comments
 (0)