From 42658cf58b373e0780752b2c5069b8fc0c649832 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Mon, 27 Apr 2020 11:44:14 -0700 Subject: [PATCH] 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 --- .../TextInput/RCTTextInputComponentView.mm | 5 ++- .../iostextinput/TextInputEventEmitter.cpp | 32 +++++++++++++++++-- .../iostextinput/TextInputEventEmitter.h | 8 ++++- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm b/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm index 094be116826..db40f71aaed 100644 --- a/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm @@ -302,7 +302,10 @@ using namespace facebook::react; { if (!_backedTextInputView.textWasPasted) { if (_eventEmitter) { - std::static_pointer_cast(_eventEmitter)->onKeyPress([self _textInputMetrics]); + KeyPressMetrics keyPressMetrics; + keyPressMetrics.text = RCTStringFromNSString(text); + keyPressMetrics.eventCount = _mostRecentEventCount; + std::static_pointer_cast(_eventEmitter)->onKeyPress(keyPressMetrics); } } diff --git a/ReactCommon/fabric/components/textinput/iostextinput/TextInputEventEmitter.cpp b/ReactCommon/fabric/components/textinput/iostextinput/TextInputEventEmitter.cpp index 7aec3aed53f..6342a6c1ff2 100644 --- a/ReactCommon/fabric/components/textinput/iostextinput/TextInputEventEmitter.cpp +++ b/ReactCommon/fabric/components/textinput/iostextinput/TextInputEventEmitter.cpp @@ -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( diff --git a/ReactCommon/fabric/components/textinput/iostextinput/TextInputEventEmitter.h b/ReactCommon/fabric/components/textinput/iostextinput/TextInputEventEmitter.h index 09989924b91..3a143dfbd1d 100644 --- a/ReactCommon/fabric/components/textinput/iostextinput/TextInputEventEmitter.h +++ b/ReactCommon/fabric/components/textinput/iostextinput/TextInputEventEmitter.h @@ -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(