RCTInputAccessoryView.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/RCTInputAccessoryView.h>
  8. #import <React/RCTBridge.h>
  9. #import <React/RCTTouchHandler.h>
  10. #import <React/UIView+React.h>
  11. #import <React/RCTInputAccessoryViewContent.h>
  12. @interface RCTInputAccessoryView()
  13. // Overriding `inputAccessoryView` to `readwrite`.
  14. @property (nonatomic, readwrite, retain) UIView *inputAccessoryView;
  15. @end
  16. @implementation RCTInputAccessoryView
  17. {
  18. BOOL _shouldBecomeFirstResponder;
  19. }
  20. - (instancetype)initWithBridge:(RCTBridge *)bridge
  21. {
  22. if (self = [super init]) {
  23. _inputAccessoryView = [RCTInputAccessoryViewContent new];
  24. RCTTouchHandler *const touchHandler = [[RCTTouchHandler alloc] initWithBridge:bridge];
  25. [touchHandler attachToView:_inputAccessoryView];
  26. }
  27. return self;
  28. }
  29. - (BOOL)canBecomeFirstResponder
  30. {
  31. return true;
  32. }
  33. - (void)reactSetFrame:(CGRect)frame
  34. {
  35. [_inputAccessoryView reactSetFrame:frame];
  36. if (_shouldBecomeFirstResponder) {
  37. _shouldBecomeFirstResponder = NO;
  38. [self becomeFirstResponder];
  39. }
  40. }
  41. - (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)index
  42. {
  43. [super insertReactSubview:subview atIndex:index];
  44. [_inputAccessoryView insertReactSubview:subview atIndex:index];
  45. }
  46. - (void)removeReactSubview:(UIView *)subview
  47. {
  48. [super removeReactSubview:subview];
  49. [_inputAccessoryView removeReactSubview:subview];
  50. }
  51. - (void)didUpdateReactSubviews
  52. {
  53. // Do nothing, as subviews are managed by `insertReactSubview:atIndex:`.
  54. }
  55. - (void)didSetProps:(NSArray<NSString *> *)changedProps
  56. {
  57. // If the accessory view is not linked to a text input via nativeID, assume it is
  58. // a standalone component that should get focus whenever it is rendered.
  59. if (![changedProps containsObject:@"nativeID"] && !self.nativeID) {
  60. _shouldBecomeFirstResponder = YES;
  61. }
  62. }
  63. @end