diff --git a/lib/web_ui/lib/src/engine/text_editing/text_editing.dart b/lib/web_ui/lib/src/engine/text_editing/text_editing.dart index da49e877b2d7c..0a7a8de1c5514 100644 --- a/lib/web_ui/lib/src/engine/text_editing/text_editing.dart +++ b/lib/web_ui/lib/src/engine/text_editing/text_editing.dart @@ -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(); } } } diff --git a/lib/web_ui/test/engine/text_editing_test.dart b/lib/web_ui/test/engine/text_editing_test.dart index ac93b4c0c0657..eed507d8e786d 100644 --- a/lib/web_ui/test/engine/text_editing_test.dart +++ b/lib/web_ui/test/engine/text_editing_test.dart @@ -471,7 +471,35 @@ Future 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); }); @@ -497,8 +525,10 @@ Future 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', () { @@ -2520,8 +2550,10 @@ Future testMain() async { spy.messages[0].methodArguments, [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 {