Skip to content

Commit

Permalink
fix: dispatch ContentSizeChange event on Fabric on iOS (#35816)
Browse files Browse the repository at this point in the history
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: #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
  • Loading branch information
WoLewicki authored and facebook-github-bot committed Jan 13, 2023
1 parent 8befb74 commit 97c7c6a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ - (void)updateLayoutMetrics:(LayoutMetrics const &)layoutMetrics
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -82,7 +99,8 @@ void TextInputEventEmitter::onChangeSync(

void TextInputEventEmitter::onContentSizeChange(
TextInputMetrics const &textInputMetrics) const {
dispatchTextInputEvent("contentSizeChange", textInputMetrics);
dispatchTextInputContentSizeChangeEvent(
"contentSizeChange", textInputMetrics);
}

void TextInputEventEmitter::onSelectionChange(
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 97c7c6a

Please sign in to comment.