-
Notifications
You must be signed in to change notification settings - Fork 6k
Fix TextField performing both new line and input action #36893
Fix TextField performing both new line and input action #36893
Conversation
4299ff1
to
30c1793
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix, and sorry for the late review!
@@ -1369,6 +1369,10 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements | |||
final DomKeyboardEvent event = e as DomKeyboardEvent; | |||
if (event.keyCode == _kReturnKeyCode) { | |||
onAction!(inputConfiguration.inputAction); | |||
// Stop key event propagation if the input type is not multiline. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e.stopPropagation()
is different from e.preventDefault()
, and your comment could be confused for the former.
You could phrase as follows: "Prevent the browser from inserting a new line when it's not a multiline input."
30c1793
to
b2944cd
Compare
@@ -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', () { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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)).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 🚀
…114696) * f3d0b6af7 Fix TextField performing both new line and input action (flutter/engine#36893) * e947833ce Roll Skia from 1272b520c082 to 7a45d3123f8b (4 revisions) (flutter/engine#37316)
Co-authored-by: Bruno Leroux <[email protected]>
…lutter#114696) * f3d0b6af7 Fix TextField performing both new line and input action (flutter/engine#36893) * e947833ce Roll Skia from 1272b520c082 to 7a45d3123f8b (4 revisions) (flutter/engine#37316)
…lutter#114696) * f3d0b6af7 Fix TextField performing both new line and input action (flutter/engine#36893) * e947833ce Roll Skia from 1272b520c082 to 7a45d3123f8b (4 revisions) (flutter/engine#37316)
#53453) ## Description This PR prevents new line key event from being dispatched for a multiline text field when `TextField.textInputAction` is not `TextInputAction.newline`. Since #33428, web engine does not prevent new line key events. In #36893, I fixed a similar issue for single line text fields. At that time I was not sure if we want to fix it for multiline text fields. I checked again on non-web platforms (macos, iOS, Android) and the new line is not added if the input action is not `TextInputAction.newline`. For a **multiline field**, the default text input action is `TextInputAction.newline`. If the developer sets text input action to another value: - before this PR, the action is performed and a new line is added. - after this PR, the action is performed but no new line is added. ## Related Issue Fixes flutter/flutter#145051 ## Tests Adds 1 tests, updates 3 tests.
Description
This PR prevents new line key event from being dispatched when
TextField.textInputAction
is notTextInputAction.newline
and the text field is not multiline.Related Issue
Fixes flutter/flutter#113559
Implementation choice
Since #33428, web engine does not prevent new line key events.
Here, I choose to prevent it whether the field is multiline or not.For a single line field (the default text input action is
TextInputAction.done
):TextInputAction.next
for instance), the newly focused field inserts the new line (see [Web] TextField receiving newline on TextInputAction.next flutter#113559).For a multiline field, the default text input action is
TextInputAction.newline
.If the developer sets text input action to another value:
after this PR, the action is performed but no new line is added.@mdebbar For multiline text fields, it seems that it was expected to always insert a new line.
I think that it should not when the user choose to set the input action to something else than.TextInputAction.newline
Edit: I have tested on Linux and MacOS and on both platforms a new line is inserted and the action is performed. So it is probably better to do the same on Web for the moment.
Tests
Adds 2 tests, updates 2 tests.