Fix TextField performing both new line and input action (flutter/engine#36893)

Co-authored-by: Bruno Leroux <bruno.leroux@gmail.com>
This commit is contained in:
Bruno Leroux
2022-11-04 17:01:01 +01:00
committed by GitHub
parent 256d12573c
commit 9bffbda509
2 changed files with 26 additions and 0 deletions

View File

@@ -1376,6 +1376,10 @@ 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();
}
}
}
}

View File

@@ -390,6 +390,28 @@ Future<void> testMain() async {
expect(event.defaultPrevented, isFalse);
});
test('Triggers input action and prevent new line key event for single line field', () {
// Regression test for https://github.com/flutter/flutter/issues/113559
final InputConfiguration config = InputConfiguration();
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.done');
// And default behavior of keyboard event should have been prevented.
expect(event.defaultPrevented, isTrue);
});
test('globally positions and sizes its DOM element', () {
editingStrategy!.enable(
singlelineConfig,