Undo/Redo of TextField and TextEditor doesn't work well

Originator:mshibanami
Number:rdar://FB9087350 Date Originated:Apr 24, 2021 at 8:15 PM
Status:Open Resolved:No
Product:macOS Product Version:
Classification:SwiftUI Framework Reproducible:Always
 
Basic Information

Please provide a descriptive title for your feedback:
Undo/Redo of TextField and TextEditor doesn't work well

Which area are you seeing an issue with?
SwiftUI Framework

What type of issue are you reporting?
Incorrect/Unexpected Behavior

Description
Please describe the issue and what steps we can take to reproduce it:

## My environment

- macOS Big Sur 11.2.3 (20D91)
- Xcode 12.5 RC (12E262)

## Description of the problem

Let's say there is a SwiftUI view including this code:

@State var textFieldText = ""
@State var textEditorText = ""
var body: some View {
    VStack {
        TextField("", text: $textFieldText)
        TextEditor(text: $textEditorText)
    }
}

In this case, I confirmed that Undo/Redo of `TextField` and `TextEditor` don't work perfectly.

- `TextField`: Undo/Redo doesn't work at all.
- `TextEditor`: You can Undo/Redo, but you can still do that even after changing the focus to another view.

I attached a video that I did the following step-by-step set. Hope it'll be helpful. 🙂

## Step-by-step set of instructions to reproduce the problem

1. Open and run the attached Xcode project (SwiftUITExtFieldCanNotUndo.zip), then you'd see a window which has 2 text boxes. The top one is `TextField` and the bottom one is `TextEditor`
2. Click the first text box (`TextField`) and type "abc"
3. Open Edit menu on the menu bar, then:
  - Expected: "Undo" is selectable
  - Reality:  "Undo" is unselectable
5. Click the second text box (`TextEditor`) and type "abc"
6. Open Edit menu and click "Undo" to make the second text box empty. 
7. Click the first text box again.
8. Open Edit menu on the menu bar, then:
  - Expected: Redo is unselectable
  - Reality:  Redo is selectable and it redos the second text box even the first text box is focused

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!