Skip to content

Commit c136c54

Browse files
Peter Arganyfacebook-github-bot
Peter Argany
authored andcommitted
Refactor RCTInputAccessoryView view hierarchy and names
Reviewed By: shergin Differential Revision: D7196162 fbshipit-source-id: 07a5c13d4cdb876f5a632d7d53859eab5e235f49
1 parent aa323d4 commit c136c54

6 files changed

+52
-52
lines changed

Libraries/Text/TextInput/RCTBaseTextInputView.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ - (void)setCustomInputAccessoryViewWithNativeID:(NSString *)nativeID
419419
UIView *accessoryView = [strongSelf->_bridge.uiManager viewForNativeID:nativeID
420420
withRootTag:rootView.reactTag];
421421
if (accessoryView && [accessoryView isKindOfClass:[RCTInputAccessoryView class]]) {
422-
strongSelf.backedTextInputView.inputAccessoryView = ((RCTInputAccessoryView *)accessoryView).content.inputAccessoryView;
422+
strongSelf.backedTextInputView.inputAccessoryView = ((RCTInputAccessoryView *)accessoryView).inputAccessoryView;
423423
[strongSelf reloadInputViewsIfNecessary];
424424
}
425425
}

Libraries/Text/TextInput/RCTInputAccessoryView.h

-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,4 @@
1414

1515
- (instancetype)initWithBridge:(RCTBridge *)bridge;
1616

17-
@property (nonatomic, readonly, strong) RCTInputAccessoryViewContent *content;
18-
1917
@end

Libraries/Text/TextInput/RCTInputAccessoryView.m

+24-14
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,66 @@
1313

1414
#import "RCTInputAccessoryViewContent.h"
1515

16+
@interface RCTInputAccessoryView()
17+
18+
// Overriding `inputAccessoryView` to `readwrite`.
19+
@property (nonatomic, readwrite, retain) UIView *inputAccessoryView;
20+
21+
@end
22+
1623
@implementation RCTInputAccessoryView
1724
{
18-
BOOL _contentShouldBeFirstResponder;
25+
BOOL _shouldBecomeFirstResponder;
1926
}
2027

2128
- (instancetype)initWithBridge:(RCTBridge *)bridge
2229
{
2330
if (self = [super init]) {
24-
_content = [RCTInputAccessoryViewContent new];
31+
_inputAccessoryView = [RCTInputAccessoryViewContent new];
2532
RCTTouchHandler *const touchHandler = [[RCTTouchHandler alloc] initWithBridge:bridge];
26-
[touchHandler attachToView:_content.inputAccessoryView];
27-
[self addSubview:_content];
33+
[touchHandler attachToView:_inputAccessoryView];
2834
}
2935
return self;
3036
}
3137

38+
- (BOOL)canBecomeFirstResponder
39+
{
40+
return true;
41+
}
42+
3243
- (void)reactSetFrame:(CGRect)frame
3344
{
34-
[_content.inputAccessoryView setFrame:frame];
35-
[_content.contentView setFrame:frame];
45+
[_inputAccessoryView setFrame:frame];
3646

37-
if (_contentShouldBeFirstResponder) {
38-
_contentShouldBeFirstResponder = NO;
39-
[_content becomeFirstResponder];
47+
if (_shouldBecomeFirstResponder) {
48+
_shouldBecomeFirstResponder = NO;
49+
[self becomeFirstResponder];
4050
}
4151
}
4252

4353
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)index
4454
{
4555
[super insertReactSubview:subview atIndex:index];
46-
[_content insertReactSubview:subview atIndex:index];
56+
[_inputAccessoryView insertReactSubview:subview atIndex:index];
4757
}
4858

4959
- (void)removeReactSubview:(UIView *)subview
5060
{
5161
[super removeReactSubview:subview];
52-
[_content removeReactSubview:subview];
62+
[_inputAccessoryView removeReactSubview:subview];
5363
}
5464

5565
- (void)didUpdateReactSubviews
5666
{
57-
// Do nothing, as subviews are managed by `insertReactSubview:atIndex:`
67+
// Do nothing, as subviews are managed by `insertReactSubview:atIndex:`.
5868
}
5969

6070
- (void)didSetProps:(NSArray<NSString *> *)changedProps
6171
{
6272
// If the accessory view is not linked to a text input via nativeID, assume it is
63-
// a standalone component that should get focus whenever it is rendered
73+
// a standalone component that should get focus whenever it is rendered.
6474
if (![changedProps containsObject:@"nativeID"] && !self.nativeID) {
65-
_contentShouldBeFirstResponder = YES;
75+
_shouldBecomeFirstResponder = YES;
6676
}
6777
}
6878

Libraries/Text/TextInput/RCTInputAccessoryViewContent.h

-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@
99

1010
@interface RCTInputAccessoryViewContent : UIView
1111

12-
@property (nonatomic, readwrite, retain) UIView *contentView;
13-
1412
@end

Libraries/Text/TextInput/RCTInputAccessoryViewContent.m

+26-32
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,59 @@
99

1010
#import <React/UIView+React.h>
1111

12-
@interface RCTInputAccessoryViewContent()
13-
14-
// Overriding `inputAccessoryView` to `readwrite`.
15-
@property (nonatomic, readwrite, retain) UIView *inputAccessoryView;
16-
17-
@end
18-
1912
@implementation RCTInputAccessoryViewContent
13+
{
14+
UIView *_safeAreaContainer;
15+
}
2016

21-
- (BOOL)canBecomeFirstResponder
17+
- (instancetype)init
2218
{
23-
return true;
19+
if (self = [super init]) {
20+
_safeAreaContainer = [UIView new];
21+
[self addSubview:_safeAreaContainer];
22+
}
23+
return self;
2424
}
2525

26-
- (BOOL)becomeFirstResponder
26+
- (void)didMoveToSuperview
2727
{
28-
const BOOL becameFirstResponder = [super becomeFirstResponder];
2928

30-
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
31-
// Avoiding the home pill and notch (landscape mode) on iphoneX.
32-
if (becameFirstResponder) {
33-
if (@available(iOS 11.0, *)) {
34-
[_contentView.bottomAnchor
35-
constraintLessThanOrEqualToSystemSpacingBelowAnchor:_contentView.window.safeAreaLayoutGuide.bottomAnchor
29+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
30+
// Avoid the home pill (in portrait mode) and notch (in landscape mode) on iPhoneX.
31+
if (@available(iOS 11.0, *)) {
32+
if (self.window) {
33+
[_safeAreaContainer.bottomAnchor
34+
constraintLessThanOrEqualToSystemSpacingBelowAnchor:self.window.safeAreaLayoutGuide.bottomAnchor
3635
multiplier:1.0f].active = YES;
37-
[_contentView.leftAnchor
38-
constraintLessThanOrEqualToSystemSpacingAfterAnchor:_contentView.window.safeAreaLayoutGuide.leftAnchor
36+
[_safeAreaContainer.leftAnchor
37+
constraintGreaterThanOrEqualToSystemSpacingAfterAnchor:self.window.safeAreaLayoutGuide.leftAnchor
3938
multiplier:1.0f].active = YES;
40-
[_contentView.rightAnchor
41-
constraintLessThanOrEqualToSystemSpacingAfterAnchor:_contentView.window.safeAreaLayoutGuide.rightAnchor
39+
[_safeAreaContainer.rightAnchor
40+
constraintLessThanOrEqualToSystemSpacingAfterAnchor:self.window.safeAreaLayoutGuide.rightAnchor
4241
multiplier:1.0f].active = YES;
4342
}
4443
}
45-
#endif
44+
#endif
4645

47-
return becameFirstResponder;
4846
}
4947

50-
- (UIView *)inputAccessoryView
48+
- (void)setFrame:(CGRect)frame
5149
{
52-
if (!_inputAccessoryView) {
53-
_inputAccessoryView = [UIView new];
54-
_contentView = [UIView new];
55-
[_inputAccessoryView addSubview:_contentView];
56-
}
57-
return _inputAccessoryView;
50+
[super setFrame:frame];
51+
[_safeAreaContainer setFrame:frame];
5852
}
5953

6054
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)index
6155
{
6256
[super insertReactSubview:subview atIndex:index];
63-
[_contentView insertSubview:subview atIndex:index];
57+
[_safeAreaContainer insertSubview:subview atIndex:index];
6458
}
6559

6660
- (void)removeReactSubview:(UIView *)subview
6761
{
6862
[super removeReactSubview:subview];
6963
[subview removeFromSuperview];
70-
if ([[_inputAccessoryView subviews] count] == 0 && [self isFirstResponder]) {
64+
if ([[_safeAreaContainer subviews] count] == 0 && [self isFirstResponder]) {
7165
[self resignFirstResponder];
7266
}
7367
}

Libraries/Text/TextInput/RCTInputAccessoryViewManager.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ - (UIView *)view
2323
return [[RCTInputAccessoryView alloc] initWithBridge:self.bridge];
2424
}
2525

26-
RCT_REMAP_VIEW_PROPERTY(backgroundColor, content.inputAccessoryView.backgroundColor, UIColor)
26+
RCT_REMAP_VIEW_PROPERTY(backgroundColor, inputAccessoryView.backgroundColor, UIColor)
2727

2828
@end

0 commit comments

Comments
 (0)