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

Fix TextField performing both new line and input action #36893

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
4 changes: 4 additions & 0 deletions lib/web_ui/lib/src/engine/text_editing/text_editing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions lib/web_ui/test/text_editing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

@mdebbar would it be worth adding a test to verify return key behavior for multiline inputs is preserved?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a test for that right above this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@htoor3 If I remember well this is already covered by an existing test. (I first I implemented this change for both single line and multiline inputs and it broke several tests. I ended targeting only single line, not because of those failing tests, but because changing the behavior for multilines inputs will require some discussions. I will open a new issue to discuss this, see flutter/flutter#113559 (comment)).

// 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,
Expand Down