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

[Web] Fix extra new line when inputAction is not newline for a multil… #53453

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/web_ui/lib/src/engine/text_editing/text_editing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1513,10 +1513,11 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
final DomKeyboardEvent event = e as DomKeyboardEvent;
if (event.keyCode == _kReturnKeyCode) {
onAction!(inputConfiguration.inputAction);
// Prevent the browser from inserting a new line when it's not a multiline input.
if (inputConfiguration.inputType is! MultilineInputType) {
event.preventDefault();
if (inputConfiguration.inputType is MultilineInputType && inputConfiguration.inputAction == 'TextInputAction.newline' ) {
return;
}
// Prevent the browser from inserting a new line.
event.preventDefault();
}
}
}
Expand Down
42 changes: 37 additions & 5 deletions lib/web_ui/test/engine/text_editing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,35 @@ Future<void> testMain() async {

// Input action is triggered!
expect(lastInputAction, 'TextInputAction.done');
// And default behavior of keyboard event shouldn't have been prevented.
// And default behavior of keyboard event should have been prevented.
// Only TextInputAction.newline should not prevent default behavior
// for a multiline field.
expect(event.defaultPrevented, isTrue);
});

test('Does not prevent default behavior when TextInputAction.newline', () {
// Regression test for https://github.com/flutter/flutter/issues/145051.
final InputConfiguration config = InputConfiguration(
viewId: kImplicitViewId,
inputAction: 'TextInputAction.newline',
inputType: EngineInputType.multilineNone,
);
editingStrategy!.enable(
config,
onChange: trackEditingState,
onAction: trackInputAction,
);

// No input action so far.
expect(lastInputAction, isNull);

final DomKeyboardEvent event = dispatchKeyboardEvent(
editingStrategy!.domElement!,
'keydown',
keyCode: _kReturnKeyCode,
);
expect(lastInputAction, 'TextInputAction.newline');
// And default behavior of keyboard event should't have been prevented.
expect(event.defaultPrevented, isFalse);
});

Expand All @@ -497,8 +525,10 @@ Future<void> testMain() async {

// Input action is triggered!
expect(lastInputAction, 'TextInputAction.done');
// And default behavior of keyboard event shouldn't have been prevented.
expect(event.defaultPrevented, isFalse);
// And default behavior of keyboard event should have been prevented.
// Only TextInputAction.newline should not prevent default behavior
// for a multiline field.
expect(event.defaultPrevented, isTrue);
});

test('Triggers input action and prevent new line key event for single line field', () {
Expand Down Expand Up @@ -2520,8 +2550,10 @@ Future<void> testMain() async {
spy.messages[0].methodArguments,
<dynamic>[clientId, 'TextInputAction.next'],
);
// And default behavior of keyboard event shouldn't have been prevented.
expect(event.defaultPrevented, isFalse);
// And default behavior of keyboard event should have been prevented.
// Only TextInputAction.newline should not prevent default behavior
// for a multiline field.
expect(event.defaultPrevented, isTrue);
});

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