-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Implement TextInput onContentSizeChange #8457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
589f1e1
4da65ea
1e1b928
44004de
a7f28dd
ee6dcf6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -310,6 +310,11 @@ const TextInput = React.createClass({ | |
| * Changed text is passed as an argument to the callback handler. | ||
| */ | ||
| onChangeText: PropTypes.func, | ||
| /** | ||
| * Callback this is called when the text input's content size changes. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| * The new content size is passed as an argument to the callback. | ||
| */ | ||
| onContentSizeChange: PropTypes.func, | ||
| /** | ||
| * Callback that is called when text input ends. | ||
| */ | ||
|
|
@@ -566,6 +571,7 @@ const TextInput = React.createClass({ | |
| onFocus={this._onFocus} | ||
| onBlur={this._onBlur} | ||
| onChange={this._onChange} | ||
| onContentSizeChange={this._onContentSizeChange} | ||
| onSelectionChange={onSelectionChange} | ||
| onTextInput={this._onTextInput} | ||
| onSelectionChangeShouldSetResponder={emptyFunction.thatReturnsTrue} | ||
|
|
@@ -673,6 +679,12 @@ const TextInput = React.createClass({ | |
| } | ||
| }, | ||
|
|
||
| _onContentSizeChange(event: Event) { | ||
| if (this.props.onContentSizeChange) { | ||
| this.props.onContentSizeChange(event.nativeEvent.contentSize); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems unlike any of the other event handlers we provide. If we add a new property to this event, it's really hard to make it backwards compatible.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea I wasn't sure about this, I did like With that said I don't mind changing this to passing the event either as I can see it has some advantages and we may use this pattern more often.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With destructuring assignment the syntax overhead of unpacking an object vs. getting just the relevant value is only |
||
| } | ||
| }, | ||
|
|
||
| _onChange: function(event: Event) { | ||
| // Make sure to fire the mostRecentEventCount first so it is already set on | ||
| // native when the text value is set. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,6 +77,9 @@ @implementation RCTTextView | |
| BOOL _blockTextShouldChange; | ||
| BOOL _nativeUpdatesInFlight; | ||
| NSInteger _nativeEventCount; | ||
|
|
||
| CGSize _previousContentSize; | ||
| BOOL _sendContentSizeUpdates; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename this to |
||
| } | ||
|
|
||
| - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher | ||
|
|
@@ -261,6 +264,17 @@ - (void)updateContentSize | |
| size.height = [_textView sizeThatFits:size].height; | ||
| _scrollView.contentSize = size; | ||
| _textView.frame = (CGRect){CGPointZero, size}; | ||
|
|
||
| if (_sendContentSizeUpdates && _onContentSizeChange && !CGSizeEqualToSize(_previousContentSize, size)) { | ||
| _previousContentSize = size; | ||
| _onContentSizeChange(@{ | ||
| @"contentSize": @{ | ||
| @"height": @(size.height), | ||
| @"width": @(size.width), | ||
| }, | ||
| @"target": self.reactTag, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| - (void)updatePlaceholder | ||
|
|
@@ -633,6 +647,11 @@ - (BOOL)resignFirstResponder | |
| - (void)layoutSubviews | ||
| { | ||
| [super layoutSubviews]; | ||
|
|
||
| // Start sending content size updates only after the view has been layed out | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. laid out |
||
| // otherwise we send multiple events with bad dimentions on initial render. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dimensions |
||
| _sendContentSizeUpdates = YES; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| [self updateFrames]; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here about FlowType: Should FlowType State type be added for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not needed as flow can infer the types from the object literal.