Skip to content

Commit 2b03871

Browse files
committed
Add TextField which is not tested yet
1 parent 145ccec commit 2b03871

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ There is no plan to support Apple platforms until there is official support from
4141
- `IconButton`
4242
- `ScrollableList`/`LazyColumn` (visually inconsistent for now)
4343
- `Text`/`MaterialText`
44+
- `TextField`
4445
- `TopAppBarScaffold`
4546

4647
### 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
placeholder: String? = null,
17+
leadingIcon: @Composable (() -> Unit)? = null,
18+
trailingIcon: @Composable (() -> Unit)? = null,
19+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
placeholder: String?,
19+
leadingIcon: @Composable (() -> Unit)?,
20+
trailingIcon: @Composable (() -> Unit)?,
21+
) =
22+
MDCTextField(value,
23+
attrs = {
24+
onInput { onValueChange(it.value) }
25+
modifierOrAttrs.toAttrs()?.let { it() }
26+
},
27+
disabled = !enabled,
28+
label = label,
29+
helperText = placeholder,
30+
leadingIcon = leadingIcon?.let { { it() } },
31+
trailingIcon = trailingIcon?.let { { it() } }
32+
)
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.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+
placeholder: String?,
18+
leadingIcon: @Composable (() -> Unit)?,
19+
trailingIcon: @Composable (() -> Unit)?,
20+
) =
21+
androidx.compose.material.TextField(
22+
value,
23+
onValueChange,
24+
modifierOrAttrs.toModifier(),
25+
enabled = enabled,
26+
label = label?.let { { Text(it) } },
27+
placeholder = placeholder?.let { { Text(it) } },
28+
leadingIcon = leadingIcon,
29+
trailingIcon = trailingIcon
30+
)

0 commit comments

Comments
 (0)