Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit d721351

Browse files
authored
[Web] Fix extra new line when inputAction is not newline for a multil… (#53453)
## Description This PR prevents new line key event from being dispatched for a multiline text field when `TextField.textInputAction` is not `TextInputAction.newline`. Since #33428, web engine does not prevent new line key events. In #36893, I fixed a similar issue for single line text fields. At that time I was not sure if we want to fix it for multiline text fields. I checked again on non-web platforms (macos, iOS, Android) and the new line is not added if the input action is not `TextInputAction.newline`. For a **multiline field**, the default text input action is `TextInputAction.newline`. If the developer sets text input action to another value: - before this PR, the action is performed and a new line is added. - after this PR, the action is performed but no new line is added. ## Related Issue Fixes flutter/flutter#145051 ## Tests Adds 1 tests, updates 3 tests.
1 parent 302798a commit d721351

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

lib/web_ui/lib/src/engine/text_editing/text_editing.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,10 +1508,11 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
15081508
final DomKeyboardEvent event = e as DomKeyboardEvent;
15091509
if (event.keyCode == _kReturnKeyCode) {
15101510
onAction!(inputConfiguration.inputAction);
1511-
// Prevent the browser from inserting a new line when it's not a multiline input.
1512-
if (inputConfiguration.inputType is! MultilineInputType) {
1513-
event.preventDefault();
1511+
if (inputConfiguration.inputType is MultilineInputType && inputConfiguration.inputAction == 'TextInputAction.newline' ) {
1512+
return;
15141513
}
1514+
// Prevent the browser from inserting a new line.
1515+
event.preventDefault();
15151516
}
15161517
}
15171518
}

lib/web_ui/test/engine/text_editing_test.dart

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,35 @@ Future<void> testMain() async {
494494

495495
// Input action is triggered!
496496
expect(lastInputAction, 'TextInputAction.done');
497-
// And default behavior of keyboard event shouldn't have been prevented.
497+
// And default behavior of keyboard event should have been prevented.
498+
// Only TextInputAction.newline should not prevent default behavior
499+
// for a multiline field.
500+
expect(event.defaultPrevented, isTrue);
501+
});
502+
503+
test('Does not prevent default behavior when TextInputAction.newline', () {
504+
// Regression test for https://github.com/flutter/flutter/issues/145051.
505+
final InputConfiguration config = InputConfiguration(
506+
viewId: kImplicitViewId,
507+
inputAction: 'TextInputAction.newline',
508+
inputType: EngineInputType.multilineNone,
509+
);
510+
editingStrategy!.enable(
511+
config,
512+
onChange: trackEditingState,
513+
onAction: trackInputAction,
514+
);
515+
516+
// No input action so far.
517+
expect(lastInputAction, isNull);
518+
519+
final DomKeyboardEvent event = dispatchKeyboardEvent(
520+
editingStrategy!.domElement!,
521+
'keydown',
522+
keyCode: _kReturnKeyCode,
523+
);
524+
expect(lastInputAction, 'TextInputAction.newline');
525+
// And default behavior of keyboard event should't have been prevented.
498526
expect(event.defaultPrevented, isFalse);
499527
});
500528

@@ -520,8 +548,10 @@ Future<void> testMain() async {
520548

521549
// Input action is triggered!
522550
expect(lastInputAction, 'TextInputAction.done');
523-
// And default behavior of keyboard event shouldn't have been prevented.
524-
expect(event.defaultPrevented, isFalse);
551+
// And default behavior of keyboard event should have been prevented.
552+
// Only TextInputAction.newline should not prevent default behavior
553+
// for a multiline field.
554+
expect(event.defaultPrevented, isTrue);
525555
});
526556

527557
test('Triggers input action and prevent new line key event for single line field', () {
@@ -2542,8 +2572,10 @@ Future<void> testMain() async {
25422572
spy.messages[0].methodArguments,
25432573
<dynamic>[clientId, 'TextInputAction.next'],
25442574
);
2545-
// And default behavior of keyboard event shouldn't have been prevented.
2546-
expect(event.defaultPrevented, isFalse);
2575+
// And default behavior of keyboard event should have been prevented.
2576+
// Only TextInputAction.newline should not prevent default behavior
2577+
// for a multiline field.
2578+
expect(event.defaultPrevented, isTrue);
25472579
});
25482580

25492581
test('inserts element in the correct view', () async {

0 commit comments

Comments
 (0)