mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-05-31 01:36:11 +00:00
[fix] TextInput multiline value with onSubmitEditing
Prevent a newline from being added when submitting a multiline text input using the "Enter" key. Fix #1013
This commit is contained in:
@@ -368,6 +368,8 @@ describe('components/TextInput', () => {
|
|||||||
|
|
||||||
test('multi-line input with "blurOnSubmit" triggers "onSubmitEditing"', () => {
|
test('multi-line input with "blurOnSubmit" triggers "onSubmitEditing"', () => {
|
||||||
const onSubmitEditing = jest.fn();
|
const onSubmitEditing = jest.fn();
|
||||||
|
const preventDefault = jest.fn();
|
||||||
|
|
||||||
const input = findNativeTextarea(
|
const input = findNativeTextarea(
|
||||||
mount(
|
mount(
|
||||||
<TextInput
|
<TextInput
|
||||||
@@ -380,10 +382,13 @@ describe('components/TextInput', () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// shift+enter should enter newline, not submit
|
// shift+enter should enter newline, not submit
|
||||||
input.simulate('keyPress', { which: 13, shiftKey: true });
|
input.simulate('keyPress', { which: 13, preventDefault, shiftKey: true });
|
||||||
input.simulate('keyPress', { which: 13 });
|
|
||||||
expect(onSubmitEditing).toHaveBeenCalledTimes(1);
|
|
||||||
expect(onSubmitEditing).not.toHaveBeenCalledWith(expect.objectContaining({ shiftKey: true }));
|
expect(onSubmitEditing).not.toHaveBeenCalledWith(expect.objectContaining({ shiftKey: true }));
|
||||||
|
expect(preventDefault).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
input.simulate('keyPress', { which: 13, preventDefault });
|
||||||
|
expect(onSubmitEditing).toHaveBeenCalledTimes(1);
|
||||||
|
expect(preventDefault).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -376,6 +376,8 @@ class TextInput extends Component<*> {
|
|||||||
|
|
||||||
if (!e.isDefaultPrevented() && e.which === 13 && !e.shiftKey) {
|
if (!e.isDefaultPrevented() && e.which === 13 && !e.shiftKey) {
|
||||||
if ((blurOnSubmit || !multiline) && onSubmitEditing) {
|
if ((blurOnSubmit || !multiline) && onSubmitEditing) {
|
||||||
|
// prevent "Enter" from inserting a newline
|
||||||
|
e.preventDefault();
|
||||||
e.nativeEvent = { target: e.target, text: e.target.value };
|
e.nativeEvent = { target: e.target, text: e.target.value };
|
||||||
onSubmitEditing(e);
|
onSubmitEditing(e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user