mirror of
https://github.com/zhigang1992/react-native-web.git
synced 2026-03-27 22:55:56 +08: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"', () => {
|
||||
const onSubmitEditing = jest.fn();
|
||||
const preventDefault = jest.fn();
|
||||
|
||||
const input = findNativeTextarea(
|
||||
mount(
|
||||
<TextInput
|
||||
@@ -380,10 +382,13 @@ describe('components/TextInput', () => {
|
||||
);
|
||||
|
||||
// shift+enter should enter newline, not submit
|
||||
input.simulate('keyPress', { which: 13, shiftKey: true });
|
||||
input.simulate('keyPress', { which: 13 });
|
||||
expect(onSubmitEditing).toHaveBeenCalledTimes(1);
|
||||
input.simulate('keyPress', { which: 13, preventDefault, 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 ((blurOnSubmit || !multiline) && onSubmitEditing) {
|
||||
// prevent "Enter" from inserting a newline
|
||||
e.preventDefault();
|
||||
e.nativeEvent = { target: e.target, text: e.target.value };
|
||||
onSubmitEditing(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user