Controls the undo and redo history for a TextFieldState.

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

val state = rememberTextFieldState()

Column(Modifier.padding(8.dp)) {
    Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
        Button(onClick = { state.undoState.undo() }, enabled = state.undoState.canUndo) {
            Text("Undo")
        }

        Button(onClick = { state.undoState.redo() }, enabled = state.undoState.canRedo) {
            Text("Redo")
        }

        Button(
            onClick = { state.undoState.clearHistory() },
            enabled = state.undoState.canUndo || state.undoState.canRedo,
        ) {
            Text("Clear History")
        }
    }

    BasicTextField(
        state = state,
        modifier =
            Modifier.fillMaxWidth()
                .border(1.dp, Color.LightGray, RoundedCornerShape(6.dp))
                .padding(8.dp),
        textStyle = TextStyle(fontSize = 16.sp),
    )
}
See also
undoState

Summary

Public functions

Unit

Clears all undo and redo history up to this point.

Cmn
Unit

Re-applies a change that was previously reverted via undo.

Cmn
Unit

Reverts the latest edit action or a group of actions that are merged together.

Cmn

Public properties

Boolean

Whether a redo action can currently be performed.

Cmn
Boolean

Whether an undo action can currently be performed.

Cmn

Public functions

clearHistory

fun clearHistory(): Unit

Clears all undo and redo history up to this point.

Calling this sets both canUndo and canRedo to false.

redo

fun redo(): Unit

Re-applies a change that was previously reverted via undo.

If canRedo is false, this is a no-op.

See also
canRedo
undo

undo

fun undo(): Unit

Reverts the latest edit action or a group of actions that are merged together.

If canUndo is false, this is a no-op. Calling it repeatedly continues undoing previous actions.

See also
canUndo
redo

Public properties

canRedo

val canRedo: Boolean

Whether a redo action can currently be performed.

If this value is false, calling redo is a no-op. This property is backed by snapshot state and will cause recomposition when its value changes.

See also
redo
canUndo

canUndo

val canUndo: Boolean

Whether an undo action can currently be performed.

If this value is false, calling undo is a no-op. This property is backed by snapshot state and will cause recomposition when its value changes.

See also
undo
canRedo