mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 20:35:43 +08:00
Open sourced the onSelectionChange event
Summary: public Open-sourced the onSelectionChange event for RCTTextView, and also added onSelectionChange support for RCTTextField. Reviewed By: javache Differential Revision: D2647541 fb-gh-sync-id: ab0ab37f5f087e708a199461ffc33231a47d2133
This commit is contained in:
committed by
facebook-github-bot-7
parent
397791fcea
commit
5a34a097f2
@@ -43,6 +43,7 @@
|
||||
NSAttributedString *_pendingAttributedText;
|
||||
NSMutableArray<UIView<RCTComponent> *> *_subviews;
|
||||
BOOL _blockTextShouldChange;
|
||||
UITextRange *_previousSelectionRange;
|
||||
}
|
||||
|
||||
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
|
||||
@@ -59,6 +60,8 @@
|
||||
_textView.scrollsToTop = NO;
|
||||
_textView.delegate = self;
|
||||
|
||||
_previousSelectionRange = _textView.selectedTextRange;
|
||||
|
||||
_subviews = [NSMutableArray new];
|
||||
[self addSubview:_textView];
|
||||
}
|
||||
@@ -284,6 +287,30 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
||||
}
|
||||
}
|
||||
|
||||
- (void)textViewDidChangeSelection:(RCTUITextView *)textView
|
||||
{
|
||||
if (_onSelectionChange &&
|
||||
textView.selectedTextRange != _previousSelectionRange &&
|
||||
![textView.selectedTextRange isEqual:_previousSelectionRange]) {
|
||||
|
||||
_previousSelectionRange = textView.selectedTextRange;
|
||||
|
||||
UITextRange *selection = textView.selectedTextRange;
|
||||
NSInteger start = [textView offsetFromPosition:[textView beginningOfDocument] toPosition:selection.start];
|
||||
NSInteger end = [textView offsetFromPosition:[textView beginningOfDocument] toPosition:selection.end];
|
||||
_onSelectionChange(@{
|
||||
@"selection": @{
|
||||
@"start": @(start),
|
||||
@"end": @(end),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (textView.editable && [textView isFirstResponder]) {
|
||||
[textView scrollRangeToVisible:textView.selectedRange];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setText:(NSString *)text
|
||||
{
|
||||
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
|
||||
|
||||
Reference in New Issue
Block a user