Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TextInput] Implements 'onKeyPress' for Android - PR fixes #12301

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions Examples/UIExplorer/js/TextInputExample.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class TextEventsExample extends React.Component {
curText: '<No Event>',
prevText: '<No Event>',
prev2Text: '<No Event>',
prev3Text: '<No Event>',
};

updateText = (text) => {
Expand All @@ -44,6 +45,7 @@ class TextEventsExample extends React.Component {
curText: text,
prevText: state.curText,
prev2Text: state.prevText,
prev3Text: state.prev2Text,
};
});
};
Expand All @@ -66,12 +68,16 @@ class TextEventsExample extends React.Component {
onSubmitEditing={(event) => this.updateText(
'onSubmitEditing text: ' + event.nativeEvent.text
)}
onKeyPress={(event) => this.updateText(
'onKeyPress key: ' + event.nativeEvent.key
)}
style={styles.singleLine}
/>
<Text style={styles.eventLabel}>
{this.state.curText}{'\n'}
(prev: {this.state.prevText}){'\n'}
(prev2: {this.state.prev2Text})
(prev3: {this.state.prev3Text})
</Text>
</View>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

package com.facebook.react.views.textinput;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.RCTEventEmitter;

public class ReactKeyDownEvent extends Event<ReactKeyDownEvent> {

private static final String EVENT_NAME = "topKeyDown";

private String mKey;

public ReactKeyDownEvent(int viewId, String key) {
super(viewId);
mKey = key;
}

@Override
public String getEventName() {
return EVENT_NAME;
}

@Override
public void dispatch(RCTEventEmitter rctEventEmitter) {
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), serializeEventData());
}

private WritableMap serializeEventData() {
WritableMap eventData = Arguments.createMap();

// WritableMap selectionData = Arguments.createMap();
eventData.putString("key", mKey);

// eventData.putMap("selection", selectionData);
return eventData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public Map<String, Object> getExportedCustomBubblingEventTypeConstants() {
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onBlur", "captured", "onBlurCapture")))
.put(
"topKeyDown",
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onKeyPress", "captured", "onKeyDownCapture")))
.build();
}

Expand Down Expand Up @@ -617,7 +622,26 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 0 && before == 0) {
return;
}

// If the string is only 1 character longer, we interpret it as a key press. It also triggers
// if only 1 character was pasted, but there is no way to monitor soft/virtual key presses
int diff = count - before;
if (diff == 1) {
// Mirrors behaviour of iOS
String key = "" + s.charAt(start+count-1);
key = key.equals("\n") ? "Enter" : key;
mEventDispatcher.dispatchEvent(
new ReactKeyDownEvent(
mEditText.getId(),
key));
}
// If the text is shorter we interpret as a backspace press (could also be a Cut from a
// selection)
if (diff < 0) {
mEventDispatcher.dispatchEvent(
new ReactKeyDownEvent(
mEditText.getId(),
"Backspace"));
}
Assertions.assertNotNull(mPreviousText);
String newText = s.toString().substring(start, start + count);
String oldText = mPreviousText.substring(start, start + before);
Expand Down Expand Up @@ -698,6 +722,10 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) {
actionId == EditorInfo.IME_NULL) {
EventDispatcher eventDispatcher =
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
eventDispatcher.dispatchEvent(
new ReactKeyDownEvent(
editText.getId(),
"Enter"));
eventDispatcher.dispatchEvent(
new ReactTextInputSubmitEditingEvent(
editText.getId(),
Expand Down