mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
Implement TextInput onContentSizeChange
Summary: This adds proper support for tracking a TextInput content size as discussed in #6552 by adding a new callback that is called every time the content size changes including when first rendering the view. Some points that are up for discussion are what do we want to do with the onChange callback as I don't see any use left for it now that we can track text change in onChangeText and size changes in onContentSizeChange. Also a bit off topic but should we consider renaming onChangeText to onTextChange to keep the naming more consistent (see [this naming justification](https://twitter.com/notbrent/status/709445076850597888)). This is split in 2 commits for easier review, one for iOS and one for android. The iOS implementation simply checks if the content size has changed everytime we update it and fire the callback, the only small issue was that the content size had several different values on initial render so I added a check to not fire events before the layoutSubviews where at this point the value is g Closes https://github.com/facebook/react-native/pull/8457 Differential Revision: D3528202 Pulled By: dmmiller fbshipit-source-id: fefe83f10cc5bfde1f5937c48c88b10408e58d9d
This commit is contained in:
committed by
Facebook Github Bot 1
parent
be0abd17e5
commit
2537157d99
@@ -77,6 +77,9 @@
|
||||
BOOL _blockTextShouldChange;
|
||||
BOOL _nativeUpdatesInFlight;
|
||||
NSInteger _nativeEventCount;
|
||||
|
||||
CGSize _previousContentSize;
|
||||
BOOL _viewDidCompleteInitialLayout;
|
||||
}
|
||||
|
||||
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
|
||||
@@ -261,6 +264,17 @@ static NSAttributedString *removeReactTagFromString(NSAttributedString *string)
|
||||
size.height = [_textView sizeThatFits:size].height;
|
||||
_scrollView.contentSize = size;
|
||||
_textView.frame = (CGRect){CGPointZero, size};
|
||||
|
||||
if (_viewDidCompleteInitialLayout && _onContentSizeChange && !CGSizeEqualToSize(_previousContentSize, size)) {
|
||||
_previousContentSize = size;
|
||||
_onContentSizeChange(@{
|
||||
@"contentSize": @{
|
||||
@"height": @(size.height),
|
||||
@"width": @(size.width),
|
||||
},
|
||||
@"target": self.reactTag,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
- (void)updatePlaceholder
|
||||
@@ -633,6 +647,11 @@ static BOOL findMismatch(NSString *first, NSString *second, NSRange *firstRange,
|
||||
- (void)layoutSubviews
|
||||
{
|
||||
[super layoutSubviews];
|
||||
|
||||
// Start sending content size updates only after the view has been laid out
|
||||
// otherwise we send multiple events with bad dimensions on initial render.
|
||||
_viewDidCompleteInitialLayout = YES;
|
||||
|
||||
[self updateFrames];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user