RCTBaseTextInputViewManager.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/RCTBaseTextInputViewManager.h>
  8. #import <React/RCTBridge.h>
  9. #import <React/RCTConvert.h>
  10. #import <React/RCTFont.h>
  11. #import <React/RCTShadowView+Layout.h>
  12. #import <React/RCTShadowView.h>
  13. #import <React/RCTUIManager.h>
  14. #import <React/RCTUIManagerUtils.h>
  15. #import <React/RCTUIManagerObserverCoordinator.h>
  16. #import <React/RCTBaseTextInputShadowView.h>
  17. #import <React/RCTBaseTextInputView.h>
  18. #import <React/RCTConvert+Text.h>
  19. @interface RCTBaseTextInputViewManager () <RCTUIManagerObserver>
  20. @end
  21. @implementation RCTBaseTextInputViewManager
  22. {
  23. NSHashTable<RCTBaseTextInputShadowView *> *_shadowViews;
  24. }
  25. RCT_EXPORT_MODULE()
  26. #pragma mark - Unified <TextInput> properties
  27. RCT_REMAP_VIEW_PROPERTY(autoCapitalize, backedTextInputView.autocapitalizationType, UITextAutocapitalizationType)
  28. RCT_REMAP_VIEW_PROPERTY(autoCorrect, backedTextInputView.autocorrectionType, UITextAutocorrectionType)
  29. RCT_REMAP_VIEW_PROPERTY(contextMenuHidden, backedTextInputView.contextMenuHidden, BOOL)
  30. RCT_REMAP_VIEW_PROPERTY(editable, backedTextInputView.editable, BOOL)
  31. RCT_REMAP_VIEW_PROPERTY(enablesReturnKeyAutomatically, backedTextInputView.enablesReturnKeyAutomatically, BOOL)
  32. RCT_REMAP_VIEW_PROPERTY(keyboardAppearance, backedTextInputView.keyboardAppearance, UIKeyboardAppearance)
  33. RCT_REMAP_VIEW_PROPERTY(placeholder, backedTextInputView.placeholder, NSString)
  34. RCT_REMAP_VIEW_PROPERTY(placeholderTextColor, backedTextInputView.placeholderColor, UIColor)
  35. RCT_REMAP_VIEW_PROPERTY(returnKeyType, backedTextInputView.returnKeyType, UIReturnKeyType)
  36. RCT_REMAP_VIEW_PROPERTY(selectionColor, backedTextInputView.tintColor, UIColor)
  37. RCT_REMAP_VIEW_PROPERTY(spellCheck, backedTextInputView.spellCheckingType, UITextSpellCheckingType)
  38. RCT_REMAP_VIEW_PROPERTY(caretHidden, backedTextInputView.caretHidden, BOOL)
  39. RCT_REMAP_VIEW_PROPERTY(clearButtonMode, backedTextInputView.clearButtonMode, UITextFieldViewMode)
  40. RCT_REMAP_VIEW_PROPERTY(scrollEnabled, backedTextInputView.scrollEnabled, BOOL)
  41. RCT_REMAP_VIEW_PROPERTY(secureTextEntry, backedTextInputView.secureTextEntry, BOOL)
  42. RCT_EXPORT_VIEW_PROPERTY(autoFocus, BOOL)
  43. RCT_EXPORT_VIEW_PROPERTY(blurOnSubmit, BOOL)
  44. RCT_EXPORT_VIEW_PROPERTY(clearTextOnFocus, BOOL)
  45. RCT_EXPORT_VIEW_PROPERTY(keyboardType, UIKeyboardType)
  46. RCT_EXPORT_VIEW_PROPERTY(maxLength, NSNumber)
  47. RCT_EXPORT_VIEW_PROPERTY(selectTextOnFocus, BOOL)
  48. RCT_EXPORT_VIEW_PROPERTY(selection, RCTTextSelection)
  49. RCT_EXPORT_VIEW_PROPERTY(inputAccessoryViewID, NSString)
  50. RCT_EXPORT_VIEW_PROPERTY(textContentType, NSString)
  51. RCT_EXPORT_VIEW_PROPERTY(passwordRules, NSString)
  52. RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
  53. RCT_EXPORT_VIEW_PROPERTY(onSelectionChange, RCTDirectEventBlock)
  54. RCT_EXPORT_VIEW_PROPERTY(onTextInput, RCTDirectEventBlock)
  55. RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock)
  56. RCT_EXPORT_VIEW_PROPERTY(mostRecentEventCount, NSInteger)
  57. RCT_EXPORT_SHADOW_PROPERTY(text, NSString)
  58. RCT_EXPORT_SHADOW_PROPERTY(placeholder, NSString)
  59. RCT_EXPORT_SHADOW_PROPERTY(onContentSizeChange, RCTBubblingEventBlock)
  60. RCT_CUSTOM_VIEW_PROPERTY(multiline, BOOL, UIView)
  61. {
  62. // No op.
  63. // This View Manager doesn't use this prop but it must be exposed here via ViewConfig to enable Fabric component use it.
  64. }
  65. - (RCTShadowView *)shadowView
  66. {
  67. RCTBaseTextInputShadowView *shadowView = [[RCTBaseTextInputShadowView alloc] initWithBridge:self.bridge];
  68. shadowView.textAttributes.fontSizeMultiplier = [[[self.bridge
  69. moduleForName:@"AccessibilityManager"
  70. lazilyLoadIfNecessary:YES] valueForKey:@"multiplier"] floatValue];
  71. [_shadowViews addObject:shadowView];
  72. return shadowView;
  73. }
  74. - (void)setBridge:(RCTBridge *)bridge
  75. {
  76. [super setBridge:bridge];
  77. _shadowViews = [NSHashTable weakObjectsHashTable];
  78. [bridge.uiManager.observerCoordinator addObserver:self];
  79. [[NSNotificationCenter defaultCenter] addObserver:self
  80. selector:@selector(handleDidUpdateMultiplierNotification)
  81. name:@"RCTAccessibilityManagerDidUpdateMultiplierNotification"
  82. object:[bridge moduleForName:@"AccessibilityManager"
  83. lazilyLoadIfNecessary:YES]];
  84. }
  85. RCT_EXPORT_METHOD(focus : (nonnull NSNumber *)viewTag)
  86. {
  87. [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
  88. UIView *view = viewRegistry[viewTag];
  89. [view reactFocus];
  90. }];
  91. }
  92. RCT_EXPORT_METHOD(blur : (nonnull NSNumber *)viewTag)
  93. {
  94. [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
  95. UIView *view = viewRegistry[viewTag];
  96. [view reactBlur];
  97. }];
  98. }
  99. RCT_EXPORT_METHOD(setTextAndSelection : (nonnull NSNumber *)viewTag
  100. mostRecentEventCount : (NSInteger)mostRecentEventCount
  101. value : (NSString *)value
  102. start : (NSInteger)start
  103. end : (NSInteger)end)
  104. {
  105. [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
  106. RCTBaseTextInputView *view = (RCTBaseTextInputView *)viewRegistry[viewTag];
  107. NSInteger eventLag = view.nativeEventCount - mostRecentEventCount;
  108. if (eventLag != 0) {
  109. return;
  110. }
  111. RCTExecuteOnUIManagerQueue(^{
  112. RCTBaseTextInputShadowView *shadowView = (RCTBaseTextInputShadowView *)[self.bridge.uiManager shadowViewForReactTag:viewTag];
  113. [shadowView setText:value];
  114. [self.bridge.uiManager setNeedsLayout];
  115. RCTExecuteOnMainQueue(^{
  116. [view setSelectionStart:start selectionEnd:end];
  117. });
  118. });
  119. }];
  120. }
  121. #pragma mark - RCTUIManagerObserver
  122. - (void)uiManagerWillPerformMounting:(__unused RCTUIManager *)uiManager
  123. {
  124. for (RCTBaseTextInputShadowView *shadowView in _shadowViews) {
  125. [shadowView uiManagerWillPerformMounting];
  126. }
  127. }
  128. #pragma mark - Font Size Multiplier
  129. - (void)handleDidUpdateMultiplierNotification
  130. {
  131. CGFloat fontSizeMultiplier = [[[self.bridge moduleForName:@"AccessibilityManager"]
  132. valueForKey:@"multiplier"] floatValue];
  133. NSHashTable<RCTBaseTextInputShadowView *> *shadowViews = _shadowViews;
  134. RCTExecuteOnUIManagerQueue(^{
  135. for (RCTBaseTextInputShadowView *shadowView in shadowViews) {
  136. shadowView.textAttributes.fontSizeMultiplier = fontSizeMultiplier;
  137. [shadowView dirtyLayout];
  138. }
  139. [self.bridge.uiManager setNeedsLayout];
  140. });
  141. }
  142. @end