BREAKING: Better TextInput: `contentSize` property was removed from `<TextInput>.onChange` event (Second attempt)
Summary: `contentSize` was removed from both iOS and Android, tests was updated. USE `onContentSizeChange` INSTEAD. Why? * It always was a hack; * We already have dedicated event for it: `onContentSizeChange`; * `onChange` has nothing to do with layout actually; * We have to maintain `onChange` handler as fast and simple as possible, this feature complicates it a lot; * It was undocumented feature; * We already have native auto-expandable <TextInput>, so it illuminates 99% current use cases of this feature. Reviewed By: mmmulani Differential Revision: D4989881 fbshipit-source-id: 674bb98c89ada1fca7b3b20b304736b2a3b8304e
This commit is contained in:
parent
9fae268e5b
commit
bac84ce207
|
@ -30,8 +30,6 @@
|
|||
NSAttributedString *_pendingAttributedText;
|
||||
|
||||
UITextRange *_previousSelectionRange;
|
||||
NSUInteger _previousTextLength;
|
||||
CGFloat _previousContentHeight;
|
||||
NSString *_predictedText;
|
||||
|
||||
BOOL _blockTextShouldChange;
|
||||
|
@ -490,31 +488,12 @@ static BOOL findMismatch(NSString *first, NSString *second, NSRange *firstRange,
|
|||
_nativeUpdatesInFlight = NO;
|
||||
_nativeEventCount++;
|
||||
|
||||
// TODO: t16435709 This part will be removed soon.
|
||||
if (!self.reactTag || !_onChange) {
|
||||
return;
|
||||
}
|
||||
|
||||
// When the context size increases, iOS updates the contentSize twice; once
|
||||
// with a lower height, then again with the correct height. To prevent a
|
||||
// spurious event from being sent, we track the previous, and only send the
|
||||
// update event if it matches our expectation that greater text length
|
||||
// should result in increased height. This assumption is, of course, not
|
||||
// necessarily true because shorter text might include more linebreaks, but
|
||||
// in practice this works well enough.
|
||||
NSUInteger textLength = textView.text.length;
|
||||
CGFloat contentHeight = textView.contentSize.height;
|
||||
if (textLength >= _previousTextLength) {
|
||||
contentHeight = MAX(contentHeight, _previousContentHeight);
|
||||
}
|
||||
_previousTextLength = textLength;
|
||||
_previousContentHeight = contentHeight;
|
||||
_onChange(@{
|
||||
@"text": self.text,
|
||||
@"contentSize": @{
|
||||
@"height": @(contentHeight),
|
||||
@"width": @(textView.contentSize.width)
|
||||
},
|
||||
@"target": self.reactTag,
|
||||
@"eventCount": @(_nativeEventCount),
|
||||
});
|
||||
|
|
|
@ -23,20 +23,14 @@ public class ReactTextChangedEvent extends Event<ReactTextChangedEvent> {
|
|||
public static final String EVENT_NAME = "topChange";
|
||||
|
||||
private String mText;
|
||||
private float mContentWidth;
|
||||
private float mContentHeight;
|
||||
private int mEventCount;
|
||||
|
||||
public ReactTextChangedEvent(
|
||||
int viewId,
|
||||
String text,
|
||||
float contentSizeWidth,
|
||||
float contentSizeHeight,
|
||||
int eventCount) {
|
||||
super(viewId);
|
||||
mText = text;
|
||||
mContentWidth = contentSizeWidth;
|
||||
mContentHeight = contentSizeHeight;
|
||||
mEventCount = eventCount;
|
||||
}
|
||||
|
||||
|
@ -53,13 +47,7 @@ public class ReactTextChangedEvent extends Event<ReactTextChangedEvent> {
|
|||
private WritableMap serializeEventData() {
|
||||
WritableMap eventData = Arguments.createMap();
|
||||
eventData.putString("text", mText);
|
||||
|
||||
WritableMap contentSize = Arguments.createMap();
|
||||
contentSize.putDouble("width", mContentWidth);
|
||||
contentSize.putDouble("height", mContentHeight);
|
||||
eventData.putMap("contentSize", contentSize);
|
||||
eventData.putInt("eventCount", mEventCount);
|
||||
|
||||
eventData.putInt("target", getViewTag());
|
||||
return eventData;
|
||||
}
|
||||
|
|
|
@ -672,26 +672,12 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
|
|||
return;
|
||||
}
|
||||
|
||||
// TODO: remove contentSize from onTextChanged entirely now that onChangeContentSize exists?
|
||||
int contentWidth = mEditText.getWidth();
|
||||
int contentHeight = mEditText.getHeight();
|
||||
|
||||
// Use instead size of text content within EditText when available
|
||||
if (mEditText.getLayout() != null) {
|
||||
contentWidth = mEditText.getCompoundPaddingLeft() + mEditText.getLayout().getWidth() +
|
||||
mEditText.getCompoundPaddingRight();
|
||||
contentHeight = mEditText.getCompoundPaddingTop() + mEditText.getLayout().getHeight() +
|
||||
mEditText.getCompoundPaddingBottom();
|
||||
}
|
||||
|
||||
// The event that contains the event counter and updates it must be sent first.
|
||||
// TODO: t7936714 merge these events
|
||||
mEventDispatcher.dispatchEvent(
|
||||
new ReactTextChangedEvent(
|
||||
mEditText.getId(),
|
||||
s.toString(),
|
||||
PixelUtil.toDIPFromPixel(contentWidth),
|
||||
PixelUtil.toDIPFromPixel(contentHeight),
|
||||
mEditText.incrementAndGetEventCounter()));
|
||||
|
||||
mEventDispatcher.dispatchEvent(
|
||||
|
|
Loading…
Reference in New Issue