KotlinLeaner
01/02/2025, 4:24 PMKotlinLeaner
01/02/2025, 4:25 PMKotlinLeaner
01/02/2025, 4:25 PM@Preview(showBackground = true)
@Composable
private fun SampleTextFieldView() {
var textFieldValue by remember { mutableStateOf(TextFieldValue("ABC-")) }
BasicFieldView(
textFieldValue,
onValueChange = { onValueChange ->
val digitOnly = onValueChange.text.filter { it.isDigit() }
val formattedString = buildString {
append("Abc-")
digitOnly.forEachIndexed { index, digit ->
if (index % 4 == 0 && index !=0 ){
append("-")
}
append(digit)
}
}
textFieldValue = onValueChange.copy(formattedString)
}
)
}
@Composable
fun BasicFieldView(
textFieldValue: TextFieldValue,
onValueChange: (TextFieldValue) -> Unit,
) {
Column(
modifier = Modifier.fillMaxSize()
) {
BasicTextField(
modifier = Modifier.fillMaxWidth(),
value = textFieldValue,
onValueChange = {
onValueChange(it)
},
)
}
}
Chrimaeon
01/02/2025, 5:23 PMKotlinLeaner
01/02/2025, 5:26 PMZach Klippenstein (he/him) [MOD]
01/02/2025, 8:14 PMKotlinLeaner
01/02/2025, 9:23 PMZach Klippenstein (he/him) [MOD]
01/02/2025, 9:24 PMKotlinLeaner
01/02/2025, 9:29 PMKotlinLeaner
01/02/2025, 9:38 PMKotlinLeaner
01/03/2025, 3:28 PM@Preview(showBackground = true)
@Composable
fun OutputDataPreview() {
val state = rememberTextFieldState()
Column(
modifier = Modifier.fillMaxSize(),
) {
BasicTextField(
state = state,
modifier = Modifier.fillMaxWidth(),
inputTransformation = InputTransformation.maxLength(16)
.then(DigitsOnlyTransformation),
outputTransformation = GroupingOutputTransformation(4, "-"),
decorator = { innerTextField ->
Row {
Text(text = "ABC-")
innerTextField()
}
},
)
}
}
@Stable
data class GroupingOutputTransformation(
private val groupSize: Int,
private val groupDelimiter: String,
) : OutputTransformation {
override fun TextFieldBuffer.transformOutput() {
repeat((length - 1) / groupSize) {
insert(it + (it + 1) * groupSize, groupDelimiter)
}
}
}
object DigitsOnlyTransformation : InputTransformation {
override val keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
override fun TextFieldBuffer.transformInput() {
if (!asCharSequence().isDigitsOnly()) {
revertAllChanges()
}
}
}
KotlinLeaner
01/03/2025, 3:33 PMBasicTextField
component in multiple places. I'm exploring how to use the label
and placeholder
properties, but I came across TextFieldDefaults.DecorationBox
, which requires a visualTransformation
. I want to apply a label animation decoration as well, but I'm not sure how to achieve this with the current setup.
Could you help with this?Zach Klippenstein (he/him) [MOD]
01/03/2025, 7:04 PMKotlinLeaner
01/03/2025, 7:08 PMZach Klippenstein (he/him) [MOD]
01/03/2025, 7:13 PMKotlinLeaner
01/03/2025, 9:09 PM