Fix bug where Enter doesn't add new line in multi-line fields (flutter/engine#13630)

This commit is contained in:
Mouad Debbar
2019-11-05 13:31:40 -08:00
committed by GitHub
parent 423ca2c518
commit 0fac649339
3 changed files with 62 additions and 3 deletions

View File

@@ -59,6 +59,9 @@ abstract class EngineInputType {
/// <https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode>.
String get inputmodeAttribute;
/// Whether this input type allows the "Enter" key to submit the input action.
bool get submitActionOnEnter => true;
/// Create the appropriate DOM element for this input type.
html.HtmlElement createDomElement() => html.InputElement();
@@ -124,6 +127,9 @@ class MultilineInputType extends EngineInputType {
@override
final String inputmodeAttribute = null;
@override
bool get submitActionOnEnter => false;
@override
html.HtmlElement createDomElement() => html.TextAreaElement();
}

View File

@@ -437,7 +437,8 @@ class TextEditingElement {
}
void _maybeSendAction(html.KeyboardEvent event) {
if (event.keyCode == _kReturnKeyCode) {
if (_inputConfiguration.inputType.submitActionOnEnter &&
event.keyCode == _kReturnKeyCode) {
event.preventDefault();
_onAction(_inputConfiguration.inputAction);
}

View File

@@ -13,6 +13,9 @@ import 'package:test/test.dart';
import 'matchers.dart';
/// The `keyCode` of the "Enter" key.
const int _kReturnKeyCode = 13;
const MethodCodec codec = JSONMethodCodec();
TextEditingElement editingElement;
@@ -250,10 +253,41 @@ void main() {
// No input action so far.
expect(lastInputAction, isNull);
dispatchKeyboardEvent(editingElement.domElement, 'keydown', keyCode: 13);
dispatchKeyboardEvent(
editingElement.domElement,
'keydown',
keyCode: _kReturnKeyCode,
);
expect(lastInputAction, 'TextInputAction.done');
});
test('Does not trigger input action in multi-line mode', () {
final InputConfiguration config = InputConfiguration(
inputType: EngineInputType.multiline,
obscureText: false,
inputAction: 'TextInputAction.done',
);
editingElement.enable(
config,
onChange: trackEditingState,
onAction: trackInputAction,
);
// No input action so far.
expect(lastInputAction, isNull);
final KeyboardEvent event = dispatchKeyboardEvent(
editingElement.domElement,
'keydown',
keyCode: _kReturnKeyCode,
);
// Still no input action.
expect(lastInputAction, isNull);
// And default behavior of keyboard event shouldn't have been prevented.
expect(event.defaultPrevented, isFalse);
});
group('[persistent mode]', () {
test('Does not accept dom elements of a wrong type', () {
// A regular <span> shouldn't be accepted.
@@ -883,7 +917,7 @@ void main() {
dispatchKeyboardEvent(
textEditing.editingElement.domElement,
'keydown',
keyCode: 13,
keyCode: _kReturnKeyCode,
);
expect(spy.messages, hasLength(1));
@@ -894,6 +928,24 @@ void main() {
<dynamic>[clientId, 'TextInputAction.next'],
);
});
test('does not send input action in multi-line mode', () {
showKeyboard(
inputType: 'multiline',
inputAction: 'TextInputAction.next',
);
final KeyboardEvent event = dispatchKeyboardEvent(
textEditing.editingElement.domElement,
'keydown',
keyCode: _kReturnKeyCode,
);
// No input action and no platform message have been sent.
expect(spy.messages, isEmpty);
// And default behavior of keyboard event shouldn't have been prevented.
expect(event.defaultPrevented, isFalse);
});
});
group('EditingState', () {