mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
fix: dispatch ContentSizeChange event on Fabric on iOS (#35816)
Summary: On Fabric, `onContentSizeChange` of `TextInput` component was never fired on `iOS`, since the logic dispatching it was implemented in `RCTBaseTextInputShadowView` on Paper: https://github.com/facebook/react-native/blob/0f8dc067ac079f7b14696cfcafa37e3ec19a0409/Libraries/Text/TextInput/RCTBaseTextInputShadowView.m#L105. This class is not used on Fabric, therefore the event was never dispatched. On Paper, it was dispatched in `dirtyLayout` method, so I added dispatching of this event based on the change of content size in `layoutSubviews` method, since this method seems the closest one on Fabric. I am not sure if it is the best place for it though. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [IOS] [ADDED] - dispatch `onContentSizeChange` event on Fabric. Pull Request resolved: https://github.com/facebook/react-native/pull/35816 Test Plan: Try to use `onContentSizeChange` callback in `TextInput` component: ```tsx import React from 'react'; import {TextInput, SafeAreaView} from 'react-native'; const App = () => { return ( <SafeAreaView style={{flex: 1, backgroundColor: 'red'}}> <TextInput multiline={true} placeholder="type here" onContentSizeChange={e => console.log(e)} /> </SafeAreaView> ); }; export default App; ``` Reviewed By: christophpurrer Differential Revision: D42499974 Pulled By: sammy-SC fbshipit-source-id: 3e010ff096cf91cb3e7b87ed2753e9d0e7be9650
This commit is contained in:
committed by
Facebook GitHub Bot
parent
8befb740d6
commit
97c7c6a4b6
@@ -245,6 +245,11 @@ using namespace facebook::react;
|
||||
UIEdgeInsetsInsetRect(self.bounds, RCTUIEdgeInsetsFromEdgeInsets(layoutMetrics.borderWidth));
|
||||
_backedTextInputView.textContainerInset =
|
||||
RCTUIEdgeInsetsFromEdgeInsets(layoutMetrics.contentInsets - layoutMetrics.borderWidth);
|
||||
|
||||
if (_eventEmitter) {
|
||||
auto const &textInputEventEmitter = *std::static_pointer_cast<TextInputEventEmitter const>(_eventEmitter);
|
||||
textInputEventEmitter.onContentSizeChange([self _textInputMetrics]);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)prepareForRecycle
|
||||
|
||||
+31
-1
@@ -36,6 +36,23 @@ static jsi::Value textInputMetricsPayload(
|
||||
return payload;
|
||||
};
|
||||
|
||||
static jsi::Value textInputMetricsContentSizePayload(
|
||||
jsi::Runtime &runtime,
|
||||
TextInputMetrics const &textInputMetrics) {
|
||||
auto payload = jsi::Object(runtime);
|
||||
|
||||
{
|
||||
auto contentSize = jsi::Object(runtime);
|
||||
contentSize.setProperty(
|
||||
runtime, "width", textInputMetrics.contentSize.width);
|
||||
contentSize.setProperty(
|
||||
runtime, "height", textInputMetrics.contentSize.height);
|
||||
payload.setProperty(runtime, "contentSize", contentSize);
|
||||
}
|
||||
|
||||
return payload;
|
||||
};
|
||||
|
||||
static jsi::Value keyPressMetricsPayload(
|
||||
jsi::Runtime &runtime,
|
||||
KeyPressMetrics const &keyPressMetrics) {
|
||||
@@ -82,7 +99,8 @@ void TextInputEventEmitter::onChangeSync(
|
||||
|
||||
void TextInputEventEmitter::onContentSizeChange(
|
||||
TextInputMetrics const &textInputMetrics) const {
|
||||
dispatchTextInputEvent("contentSizeChange", textInputMetrics);
|
||||
dispatchTextInputContentSizeChangeEvent(
|
||||
"contentSizeChange", textInputMetrics);
|
||||
}
|
||||
|
||||
void TextInputEventEmitter::onSelectionChange(
|
||||
@@ -137,4 +155,16 @@ void TextInputEventEmitter::dispatchTextInputEvent(
|
||||
priority);
|
||||
}
|
||||
|
||||
void TextInputEventEmitter::dispatchTextInputContentSizeChangeEvent(
|
||||
std::string const &name,
|
||||
TextInputMetrics const &textInputMetrics,
|
||||
EventPriority priority) const {
|
||||
dispatchEvent(
|
||||
name,
|
||||
[textInputMetrics](jsi::Runtime &runtime) {
|
||||
return textInputMetricsContentSizePayload(runtime, textInputMetrics);
|
||||
},
|
||||
priority);
|
||||
}
|
||||
|
||||
} // namespace facebook::react
|
||||
|
||||
@@ -54,6 +54,11 @@ class TextInputEventEmitter : public ViewEventEmitter {
|
||||
std::string const &name,
|
||||
TextInputMetrics const &textInputMetrics,
|
||||
EventPriority priority = EventPriority::AsynchronousBatched) const;
|
||||
|
||||
void dispatchTextInputContentSizeChangeEvent(
|
||||
std::string const &name,
|
||||
TextInputMetrics const &textInputMetrics,
|
||||
EventPriority priority = EventPriority::AsynchronousBatched) const;
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
|
||||
Reference in New Issue
Block a user