mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
69acb9569e
Summary: changelog: [internal] Event `onChangeText` does not exist in TextInput. Let's remove this method to avoid confusion. Reviewed By: philIip Differential Revision: D32882056 fbshipit-source-id: 37eb260b84dd7d6cce412ce1bc39c0cbf9cab112
127 lines
3.5 KiB
C++
127 lines
3.5 KiB
C++
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "TextInputEventEmitter.h"
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
static jsi::Value textInputMetricsPayload(
|
|
jsi::Runtime &runtime,
|
|
TextInputMetrics const &textInputMetrics) {
|
|
auto payload = jsi::Object(runtime);
|
|
|
|
payload.setProperty(
|
|
runtime,
|
|
"text",
|
|
jsi::String::createFromUtf8(runtime, textInputMetrics.text));
|
|
|
|
payload.setProperty(runtime, "eventCount", textInputMetrics.eventCount);
|
|
|
|
{
|
|
auto selection = jsi::Object(runtime);
|
|
selection.setProperty(
|
|
runtime, "start", textInputMetrics.selectionRange.location);
|
|
selection.setProperty(
|
|
runtime,
|
|
"end",
|
|
textInputMetrics.selectionRange.location +
|
|
textInputMetrics.selectionRange.length);
|
|
payload.setProperty(runtime, "selection", selection);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void TextInputEventEmitter::onBlur(
|
|
TextInputMetrics const &textInputMetrics) const {
|
|
dispatchTextInputEvent("blur", textInputMetrics);
|
|
}
|
|
|
|
void TextInputEventEmitter::onChange(
|
|
TextInputMetrics const &textInputMetrics) const {
|
|
dispatchTextInputEvent("change", textInputMetrics);
|
|
}
|
|
|
|
void TextInputEventEmitter::onContentSizeChange(
|
|
TextInputMetrics const &textInputMetrics) const {
|
|
dispatchTextInputEvent("contentSizeChange", textInputMetrics);
|
|
}
|
|
|
|
void TextInputEventEmitter::onSelectionChange(
|
|
TextInputMetrics const &textInputMetrics) const {
|
|
dispatchTextInputEvent("selectionChange", textInputMetrics);
|
|
}
|
|
|
|
void TextInputEventEmitter::onEndEditing(
|
|
TextInputMetrics const &textInputMetrics) const {
|
|
dispatchTextInputEvent("endEditing", textInputMetrics);
|
|
}
|
|
|
|
void TextInputEventEmitter::onSubmitEditing(
|
|
TextInputMetrics const &textInputMetrics) const {
|
|
dispatchTextInputEvent("submitEditing", textInputMetrics);
|
|
}
|
|
|
|
void TextInputEventEmitter::onKeyPress(
|
|
KeyPressMetrics const &keyPressMetrics) const {
|
|
dispatchEvent(
|
|
"keyPress",
|
|
[keyPressMetrics](jsi::Runtime &runtime) {
|
|
return keyPressMetricsPayload(runtime, keyPressMetrics);
|
|
},
|
|
EventPriority::AsynchronousBatched);
|
|
}
|
|
|
|
void TextInputEventEmitter::onScroll(
|
|
TextInputMetrics const &textInputMetrics) const {
|
|
dispatchTextInputEvent("scroll", textInputMetrics);
|
|
}
|
|
|
|
void TextInputEventEmitter::dispatchTextInputEvent(
|
|
std::string const &name,
|
|
TextInputMetrics const &textInputMetrics,
|
|
EventPriority priority) const {
|
|
dispatchEvent(
|
|
name,
|
|
[textInputMetrics](jsi::Runtime &runtime) {
|
|
return textInputMetricsPayload(runtime, textInputMetrics);
|
|
},
|
|
priority);
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|