Created
January 6, 2022 13:08
-
-
Save jershell/9fb8d8e04ea7fc6a7e0077ab3c16070a to your computer and use it in GitHub Desktop.
Example of composable Input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(ExperimentalComposeUiApi::class) | |
@Composable | |
fun Input( | |
value: TextFieldValue, | |
onValueChange: (TextFieldValue) -> Unit, | |
modifier: Modifier = Modifier, | |
placeholder: String? = null, | |
enabled: Boolean = true, | |
singleLine: Boolean = true, | |
onFocusChanged: (Boolean) -> Unit = {}, | |
maxLines: Int = Int.MAX_VALUE, | |
background: Color = colors.backgroundInput, | |
keyboardOptions: KeyboardOptions = KeyboardOptions.Default, | |
keyboardActions: KeyboardActions? = null, | |
visualTransformation: VisualTransformation = VisualTransformation.None, | |
) { | |
val keyboardController = LocalSoftwareKeyboardController.current | |
var hasFocus by remember { mutableStateOf(false) } | |
val defaultKeyboardActions = remember { | |
KeyboardActions( | |
onDone = { | |
keyboardController?.hide() | |
} | |
) | |
} | |
Column { | |
Box( | |
Modifier | |
.fillMaxWidth() | |
.clip(RoundedCornerShape(8.dp)) | |
.background(background) | |
.heightIn(min = 42.dp) | |
.padding(horizontal = 16.dp) | |
.then(modifier), | |
Alignment.CenterStart | |
) { | |
Box { | |
if (value.text.isEmpty() && !placeholder.isNullOrEmpty()) { | |
Text(placeholder, style = typography.placeholder) | |
} | |
BasicTextField( | |
value = value, | |
onValueChange = { | |
onValueChange(it) | |
}, | |
enabled = enabled, | |
maxLines = maxLines, | |
singleLine = singleLine, | |
keyboardOptions = keyboardOptions, | |
keyboardActions = keyboardActions ?: defaultKeyboardActions, | |
modifier = Modifier | |
.fillMaxWidth() | |
.onFocusChanged { focusState -> | |
hasFocus = focusState.isFocused | |
onFocusChanged(hasFocus) | |
}, | |
textStyle = typography.input, | |
visualTransformation = visualTransformation, | |
cursorBrush = SolidColor(colors.buttercup), | |
) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment