Skip to content

Commit

Permalink
Fix reload on Fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Dec 6, 2023
1 parent 5e4212e commit cbb3825
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions ios/MarkdownTextInputViewView.mm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import <React/RCTUITextField.h>
#import <react/debug/react_native_assert.h>

#import <react-native-markdown-text-input/MarkdownTextInputViewView.h>
#import <react-native-markdown-text-input/RCTBackedTextFieldDelegateAdapter+Markdown.h>
Expand All @@ -10,9 +11,37 @@
#import <react-native-markdown-text-input/RCTBaseTextInputView+Markdown.h>
#endif /* RCT_NEW_ARCH_ENABLED */

@implementation MarkdownTextInputViewView
@implementation MarkdownTextInputViewView {
#ifdef RCT_NEW_ARCH_ENABLED
__weak RCTTextInputComponentView *_textInput;
#else
__weak RCTBaseTextInputView *_textInput;
#endif /* RCT_NEW_ARCH_ENABLED */
__weak RCTBackedTextFieldDelegateAdapter *_adapter;
__weak RCTUITextView *_textView;
}

- (void)willMoveToWindow:(UIWindow *)newWindow
{
if (_textInput != nil) {
[_textInput setMarkdownEnabled:NO];
}
if (_adapter != nil) {
[_adapter setMarkdownEnabled:NO];
}
if (_textView != nil) {
[_textView setMarkdownEnabled:NO];
}
}

- (void)didMoveToWindow {
#ifdef RCT_NEW_ARCH_ENABLED
if (self.superview.superview == nil) {
// happens on reload on Fabric
return;
}
#endif /* RCT_NEW_ARCH_ENABLED */

#ifdef RCT_NEW_ARCH_ENABLED
NSArray *viewsArray = self.superview.superview.subviews;
NSUInteger currentIndex = [viewsArray indexOfObject:self.superview];
Expand All @@ -26,22 +55,22 @@ - (void)didMoveToWindow {

#ifdef RCT_NEW_ARCH_ENABLED
react_native_assert([view isKindOfClass:[RCTTextInputComponentView class]] && "Previous sibling component is not an instance of RCTTextInputComponentView.");
RCTTextInputComponentView *textInput = (RCTTextInputComponentView *)view;
UIView<RCTBackedTextInputViewProtocol> *backedTextInputView = [textInput valueForKey:@"_backedTextInputView"];
_textInput = (RCTTextInputComponentView *)view;
UIView<RCTBackedTextInputViewProtocol> *backedTextInputView = [_textInput valueForKey:@"_backedTextInputView"];
#else
react_native_assert([view isKindOfClass:[RCTBaseTextInputView class]] && "Previous sibling component is not an instance of RCTBaseTextInputView.");
RCTBaseTextInputView *textInput = (RCTBaseTextInputView *)view;
UIView<RCTBackedTextInputViewProtocol> *backedTextInputView = textInput.backedTextInputView;
_textInput = (RCTBaseTextInputView *)view;
UIView<RCTBackedTextInputViewProtocol> *backedTextInputView = _textInput.backedTextInputView;
#endif /* RCT_NEW_ARCH_ENABLED */

[textInput setMarkdownEnabled:YES];
[_textInput setMarkdownEnabled:YES];
if ([backedTextInputView isKindOfClass:[RCTUITextField class]]) {
RCTUITextField *textField = (RCTUITextField *)backedTextInputView;
RCTBackedTextFieldDelegateAdapter *adapter = [textField valueForKey:@"textInputDelegateAdapter"];
[adapter setMarkdownEnabled:YES];
_adapter = [textField valueForKey:@"textInputDelegateAdapter"];
[_adapter setMarkdownEnabled:YES];
} else if ([backedTextInputView isKindOfClass:[RCTUITextView class]]) {
RCTUITextView *textView = (RCTUITextView *)backedTextInputView;
[textView setMarkdownEnabled:YES];
_textView = (RCTUITextView *)backedTextInputView;
[_textView setMarkdownEnabled:YES];
} else {
react_native_assert(false && "Cannot enable Markdown for this type of TextInput.");
}
Expand Down

0 comments on commit cbb3825

Please sign in to comment.