|
|
|
@ -1,8 +1,16 @@
|
|
|
|
|
import androidx.compose.foundation.border |
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth |
|
|
|
|
import androidx.compose.foundation.layout.height |
|
|
|
|
import androidx.compose.foundation.layout.padding |
|
|
|
|
import androidx.compose.runtime.Composable |
|
|
|
|
import androidx.compose.runtime.getValue |
|
|
|
|
import androidx.compose.runtime.rememberUpdatedState |
|
|
|
|
import androidx.compose.runtime.mutableStateOf |
|
|
|
|
import androidx.compose.runtime.remember |
|
|
|
|
import androidx.compose.runtime.setValue |
|
|
|
|
import androidx.compose.ui.Modifier |
|
|
|
|
import androidx.compose.ui.graphics.Color |
|
|
|
|
import androidx.compose.ui.interop.UIKitView |
|
|
|
|
import androidx.compose.ui.unit.dp |
|
|
|
|
import kotlinx.cinterop.ExperimentalForeignApi |
|
|
|
|
import kotlinx.cinterop.ObjCAction |
|
|
|
|
import platform.CoreGraphics.CGRectMake |
|
|
|
@ -10,24 +18,16 @@ import platform.Foundation.NSSelectorFromString
|
|
|
|
|
import platform.UIKit.UIControlEventEditingChanged |
|
|
|
|
import platform.UIKit.UITextField |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Compose wrapper for native UITextField. |
|
|
|
|
* @param value the input text to be shown in the text field. |
|
|
|
|
* @param onValueChange the callback that is triggered when the input service updates the text. An |
|
|
|
|
* updated text comes as a parameter of the callback |
|
|
|
|
* @param modifier a [Modifier] for this text field. Size should be specified in modifier. |
|
|
|
|
*/ |
|
|
|
|
@OptIn(ExperimentalForeignApi::class) |
|
|
|
|
@Composable |
|
|
|
|
fun ComposeUITextField(value: String, onValueChange: (String) -> Unit, modifier: Modifier) { |
|
|
|
|
val latestOnValueChanged by rememberUpdatedState(onValueChange) |
|
|
|
|
|
|
|
|
|
fun UseUITextField() { |
|
|
|
|
var message by remember { mutableStateOf("Hello, World!") } |
|
|
|
|
UIKitView( |
|
|
|
|
factory = { |
|
|
|
|
val textField = object : UITextField(CGRectMake(0.0, 0.0, 0.0, 0.0)) { |
|
|
|
|
@ObjCAction |
|
|
|
|
fun editingChanged() { |
|
|
|
|
latestOnValueChanged(text ?: "") |
|
|
|
|
message = text ?: "" |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
textField.addTarget( |
|
|
|
@ -37,9 +37,9 @@ fun ComposeUITextField(value: String, onValueChange: (String) -> Unit, modifier:
|
|
|
|
|
) |
|
|
|
|
textField |
|
|
|
|
}, |
|
|
|
|
modifier = modifier, |
|
|
|
|
modifier = Modifier.padding(4.dp).fillMaxWidth().height(30.dp).border(2.dp, Color.Blue), |
|
|
|
|
update = { textField -> |
|
|
|
|
textField.text = value |
|
|
|
|
textField.text = message |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
} |