Send key when onKeyPress event is fired from TextInput

Summary:
Changelog: [Internal]

In `onKeyPress` event, we were not returning `key` property. This diff adds `key` property to `onKeyPress` event and removes other, redundant properties from `onKeyPress` event.

The implementation has been translated from Paper.

Reviewed By: shergin

Differential Revision: D21250411

fbshipit-source-id: f1e31381667acb9dec02d0b33883df8f8f5b2a4b
This commit is contained in:
Samuel Susla
2020-07-22 10:58:30 +02:00
committed by grabbou
parent a642c3f264
commit 42658cf58b
3 changed files with 41 additions and 4 deletions
@@ -302,7 +302,10 @@ using namespace facebook::react;
{
if (!_backedTextInputView.textWasPasted) {
if (_eventEmitter) {
std::static_pointer_cast<TextInputEventEmitter const>(_eventEmitter)->onKeyPress([self _textInputMetrics]);
KeyPressMetrics keyPressMetrics;
keyPressMetrics.text = RCTStringFromNSString(text);
keyPressMetrics.eventCount = _mostRecentEventCount;
std::static_pointer_cast<TextInputEventEmitter const>(_eventEmitter)->onKeyPress(keyPressMetrics);
}
}
@@ -37,6 +37,29 @@ static jsi::Value textInputMetricsPayload(
return payload;
};
static jsi::Value keyPressMetricsPayload(
jsi::Runtime &runtime,
KeyPressMetrics const &keyPressMetrics) {
auto payload = jsi::Object(runtime);
payload.setProperty(runtime, "eventCount", keyPressMetrics.eventCount);
std::string key;
if (keyPressMetrics.text.empty()) {
key = "Backspace";
} else {
if (keyPressMetrics.text.front() == '\n') {
key = "Enter";
} else if (keyPressMetrics.text.front() == '\t') {
key = "Tab";
} else {
key = keyPressMetrics.text.front();
}
}
payload.setProperty(
runtime, "key", jsi::String::createFromUtf8(runtime, key));
return payload;
};
void TextInputEventEmitter::onFocus(
TextInputMetrics const &textInputMetrics) const {
dispatchTextInputEvent("focus", textInputMetrics);
@@ -78,8 +101,13 @@ void TextInputEventEmitter::onSubmitEditing(
}
void TextInputEventEmitter::onKeyPress(
TextInputMetrics const &textInputMetrics) const {
dispatchTextInputEvent("keyPress", textInputMetrics);
KeyPressMetrics const &keyPressMetrics) const {
dispatchEvent(
"keyPress",
[keyPressMetrics](jsi::Runtime &runtime) {
return keyPressMetricsPayload(runtime, keyPressMetrics);
},
EventPriority::AsynchronousBatched);
}
void TextInputEventEmitter::dispatchTextInputEvent(
@@ -25,6 +25,12 @@ class TextInputMetrics {
int eventCount;
};
class KeyPressMetrics {
public:
std::string text;
int eventCount;
};
class TextInputEventEmitter : public ViewEventEmitter {
public:
using ViewEventEmitter::ViewEventEmitter;
@@ -37,7 +43,7 @@ class TextInputEventEmitter : public ViewEventEmitter {
void onSelectionChange(TextInputMetrics const &textInputMetrics) const;
void onEndEditing(TextInputMetrics const &textInputMetrics) const;
void onSubmitEditing(TextInputMetrics const &textInputMetrics) const;
void onKeyPress(TextInputMetrics const &textInputMetrics) const;
void onKeyPress(KeyPressMetrics const &textInputMetrics) const;
private:
void dispatchTextInputEvent(