iOS: a commit on the last row or column ends the key field's edit
Review #72 finding 7. Return on the last row (or Tab in the last column) committed, but the selection could not move, so the field never reset: its editor stayed up with the old text and further keys appended to it. Every successful commit now resets the field, as the Mac's endEditing does. Test: KeySinkTests.testCommitOnTheLastRowAndColumnResetsTheField.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+30
−2
native/apple/Sources/SheetsKit/SheetKeySink.swift+9 −2native/apple/Tests/SheetsKitTests/KeySinkTests.swift+21 −0
diff --git a/native/apple/Sources/SheetsKit/SheetKeySink.swift b/native/apple/Sources/SheetsKit/SheetKeySink.swift| index 7e0c219..07b3615 100644 |
| --- a/native/apple/Sources/SheetsKit/SheetKeySink.swift |
| +++ b/native/apple/Sources/SheetsKit/SheetKeySink.swift |
| @@ -124,6 +124,13 @@ struct SheetKeySink: UIViewRepresentable { |
| } |
| model.draft = value |
| } |
| + /// Every commit ends this edit, including one on the last row or column, |
| + /// where the selection cannot move and so would never reset the field. |
| + private func commit(moveRight: Bool) { |
| + guard let model else { return } |
| + model.commit(moveRight: moveRight) |
| + if !model.dirty { reset() } |
| + } |
| /// Return or F2 on a selected cell: edit it here, keeping its contents. |
| private func editInPlace() { |
| guard let model, model.mayWrite, !model.dirty else { return } |
| @@ -168,9 +175,9 @@ struct SheetKeySink: UIViewRepresentable { |
| guard let model else { return false } |
| switch code { |
| case .keyboardReturnOrEnter, .keypadEnter: |
| - if typing { model.commit(moveRight: false) } else { editInPlace() } |
| + if typing { commit(moveRight: false) } else { editInPlace() } |
| case .keyboardTab: |
| - if typing { model.commit(moveRight: true) } else { move(0, shift ? -1 : 1) } |
| + if typing { commit(moveRight: true) } else { move(0, shift ? -1 : 1) } |
| case .keyboardEscape: |
| guard typing else { return false } |
| model.discard(); reset() |
diff --git a/native/apple/Tests/SheetsKitTests/KeySinkTests.swift b/native/apple/Tests/SheetsKitTests/KeySinkTests.swift| index ee1a8c2..6b21e9a 100644 |
| --- a/native/apple/Tests/SheetsKitTests/KeySinkTests.swift |
| +++ b/native/apple/Tests/SheetsKitTests/KeySinkTests.swift |
| @@ -116,6 +116,27 @@ final class KeySinkTests: XCTestCase { |
| XCTAssertFalse(field.isAccessibilityElement) |
| } |
| + /// Review #72, finding 7: on the last row Return commits but the selection |
| + /// cannot move; the field must still reset rather than keep the old text. |
| + @MainActor func testCommitOnTheLastRowAndColumnResetsTheField() { |
| + let (model, field, window) = sink() |
| + defer { window.isHidden = true } |
| + let corner = Coordinate(r: 5, c: 3) |
| + model.select(corner) |
| + field.selectionChanged(to: corner) |
| + field.insertText("last") |
| + XCTAssertTrue(field.handle(.keyboardReturnOrEnter)) |
| + XCTAssertEqual(model.selection.start, corner, "nowhere further down to go") |
| + XCTAssertEqual(model.displayCell(corner)?.display, "last") |
| + XCTAssertFalse(field.typing) |
| + XCTAssertEqual(field.text, "") |
| + field.insertText("again") |
| + XCTAssertEqual(model.draft, "again", "a new edit, not appended to the old one") |
| + XCTAssertTrue(field.handle(.keyboardTab)) |
| + XCTAssertFalse(field.typing) |
| + XCTAssertEqual(model.displayCell(corner)?.display, "again") |
| + } |
| + |
| @MainActor func testReadOnlySheetsDoNotStartAnEdit() { |
| let (model, field, window) = sink("old") |
| defer { window.isHidden = true } |