Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 25.2k
[TextInput] Add numberOfLines and maximumNumberOfLines props on iOS and fix Android implementation #35703
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
Uh oh!
There was an error while loading. Please reload this page.
[TextInput] Add numberOfLines and maximumNumberOfLines props on iOS and fix Android implementation #35703
Changes from all commits
c9e11d5e55eed8cd78e0d34acc7422d7189e5388e1448a5eb8080b34b58d914b29648d036d5ad24614c62551355e367bc0f77567d4bfc7bebac869ccfb5701d5dfc99f7d9693ef6fc1c39709bbc3e1c415a90e0d908eecabcb467dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -179,6 +179,13 @@ export type NativeProps = $ReadOnly<{| | ||
| */ | ||
| numberOfLines?: ?Int32, | ||
| /** | ||
| * Sets the maximum number of lines for a `TextInput`. Use it with multiline set to | ||
| * `true` to be able to fill the lines. | ||
| * @platform android | ||
| */ | ||
| maximumNumberOfLines?: ?Int32, | ||
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. Could we name this React Native isn't totally consistent with this since 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. Also cc @necolas who I saw had some comments around web prop naming. I can see that there isn't a direct equivalent to this on web, so the current naming here (augmenting the well-used RN prop) makes sense (unless there is work to actually remove the old ones). 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. Maybe maxRows? 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. I just renamed it in InternalTextInput & flow & TS so that end user can pass | ||
| /** | ||
| * When `false`, if there is a small amount of space available around a text input | ||
| * (e.g. landscape orientation on a phone), the OS may choose to have the user edit | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,6 +7,8 @@ | ||
| #import <React/RCTMultilineTextInputView.h> | ||
| #import <React/RCTMultilineTextInputViewManager.h> | ||
| #import <React/RCTUITextView.h> | ||
| #import <React/RCTBaseTextInputShadowView.h> | ||
| @implementation RCTMultilineTextInputViewManager | ||
| @@ -17,8 +19,21 @@ - (UIView *)view | ||
| return [[RCTMultilineTextInputView alloc] initWithBridge:self.bridge]; | ||
| } | ||
| - (RCTShadowView *)shadowView | ||
| { | ||
| RCTBaseTextInputShadowView *shadowView = (RCTBaseTextInputShadowView *)[super shadowView]; | ||
| shadowView.maximumNumberOfLines = 0; | ||
| shadowView.exactNumberOfLines = 0; | ||
Comment on lines
+26
to
+27
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. nit: Can we use a different value from Same below 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. I think that values was used on android in shadow nodes so I just copied it. | ||
| return shadowView; | ||
| } | ||
| #pragma mark - Multiline <TextInput> (aka TextView) specific properties | ||
| RCT_REMAP_VIEW_PROPERTY(dataDetectorTypes, backedTextInputView.dataDetectorTypes, UIDataDetectorTypes) | ||
| RCT_EXPORT_SHADOW_PROPERTY(maximumNumberOfLines, NSInteger) | ||
| RCT_REMAP_SHADOW_PROPERTY(numberOfLines, exactNumberOfLines, NSInteger) | ||
| @end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -16,6 +16,7 @@ NS_ASSUME_NONNULL_BEGIN | ||
| @property (nonatomic, copy, nullable) NSString *text; | ||
| @property (nonatomic, copy, nullable) NSString *placeholder; | ||
| @property (nonatomic, assign) NSInteger maximumNumberOfLines; | ||
| @property (nonatomic, assign) NSInteger exactNumberOfLines; | ||
Comment on lines
18
to
+19
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. Did 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. on fabric yes | ||
| @property (nonatomic, copy, nullable) RCTDirectEventBlock onContentSizeChange; | ||
| - (void)uiManagerWillPerformMounting; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -218,7 +218,22 @@ - (NSAttributedString *)measurableAttributedText | ||
| - (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize | ||
| { | ||
| NSAttributedString *attributedText = [self measurableAttributedText]; | ||
| NSMutableAttributedString *attributedText = [[self measurableAttributedText] mutableCopy]; | ||
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. If I'm reading this correctly, we never mutate the previous AttributedString, but may completely replace it with the filler line variant to do a different measurement. Could we then avoid the copy, and just use a different pointer if we want to operate on something different? 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. As far as I remember we need to sometimes add characters otherwise the size is not computed properly. 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. Ah I think on iOS we only add newlines but still we need to modify the object. | ||
| /* | ||
| * The block below is responsible for setting the exact height of the view in lines | ||
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. Maybe a question for @sammy-SC, is this kosher with any sort of AttributedString layout caching we are doing? I.e. is there something we need to cache bust if layout behavior here varies based on more of the number of line props? | ||
| * Unfortunatelly, iOS doesn't export any easy way to do it. So we set maximumNumberOfLines | ||
| * prop and then add random lines at the front. However, they are only used for layout | ||
| * so they are not visible on the screen. | ||
| */ | ||
| if (self.exactNumberOfLines) { | ||
| NSMutableString *newLines = [NSMutableString stringWithCapacity:self.exactNumberOfLines]; | ||
| for (NSUInteger i = 0UL; i < self.exactNumberOfLines; ++i) { | ||
| [newLines appendString:@"\n"]; | ||
| } | ||
| [attributedText insertAttributedString:[[NSAttributedString alloc] initWithString:newLines attributes:self.textAttributes.effectiveTextAttributes] atIndex:0]; | ||
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. Does the AttributedString still have all of the various text props that were set? Text can have things like, different font size in different parts, etc. I'm not sure the most sane way to handle this prop in combination with various metrics. E.g. we can have an outer TextInput that customizes font size, then a span on the first character which changes it to something else? Which do we pick (it seems like in the java case, we copy all the spans over the beginning of the text). 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. it actually works only when we use same style for the whole TextInput component. I think it makes sense. Let's imagine someone passes Text component as children to textInput and every line has different size. How should that behave? Which line height should we use for missing lines? | ||
| _maximumNumberOfLines = self.exactNumberOfLines; | ||
| } | ||
| if (!_textStorage) { | ||
| _textContainer = [NSTextContainer new]; | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Is this related to your change?