Skip to content

Commit f3d0b6a

Browse files
authored
Fix TextField performing both new line and input action (flutter#36893)
Co-authored-by: Bruno Leroux <[email protected]>
1 parent 287a3ab commit f3d0b6a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,10 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
13761376
final DomKeyboardEvent event = e as DomKeyboardEvent;
13771377
if (event.keyCode == _kReturnKeyCode) {
13781378
onAction!(inputConfiguration.inputAction);
1379+
// Prevent the browser from inserting a new line when it's not a multiline input.
1380+
if (inputConfiguration.inputType is! MultilineInputType) {
1381+
event.preventDefault();
1382+
}
13791383
}
13801384
}
13811385
}

lib/web_ui/test/text_editing_test.dart

+22
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,28 @@ Future<void> testMain() async {
390390
expect(event.defaultPrevented, isFalse);
391391
});
392392

393+
test('Triggers input action and prevent new line key event for single line field', () {
394+
// Regression test for https://github.com/flutter/flutter/issues/113559
395+
final InputConfiguration config = InputConfiguration();
396+
editingStrategy!.enable(
397+
config,
398+
onChange: trackEditingState,
399+
onAction: trackInputAction,
400+
);
401+
402+
// No input action so far.
403+
expect(lastInputAction, isNull);
404+
405+
final DomKeyboardEvent event = dispatchKeyboardEvent(
406+
editingStrategy!.domElement!,
407+
'keydown',
408+
keyCode: _kReturnKeyCode,
409+
);
410+
expect(lastInputAction, 'TextInputAction.done');
411+
// And default behavior of keyboard event should have been prevented.
412+
expect(event.defaultPrevented, isTrue);
413+
});
414+
393415
test('globally positions and sizes its DOM element', () {
394416
editingStrategy!.enable(
395417
singlelineConfig,

0 commit comments

Comments
 (0)