forEachChange

Function

Common
@ExperimentalFoundationApi
inline fun ChangeList.forEachChange(block: (range: TextRange, originalRange: TextRange) -> Unit)

Iterates over all the changes in this ChangeList.

Changes are iterated by index, so any changes made by block after the current one will be visited by block. block should not make any new changes before the current one or changes will be visited more than once. If you need to make changes, consider using forEachChangeReversed.

Code Examples

BasicTextFieldChangeIterationSample

@Composable
fun BasicTextFieldChangeIterationSample() {
    // Print a log message every time the text is changed.
    BasicTextField(
        state = rememberTextFieldState(),
        inputTransformation = {
            changes.forEachChange { sourceRange, replacedLength ->
                val newString = asCharSequence().substring(sourceRange)
                println("""$replacedLength characters were replaced with "$newString"""")
            }
        },
    )
}