Skip to content

Commit

Permalink
Fix Groups text input
Browse files Browse the repository at this point in the history
Reviewed By: javache

Differential Revision: D2590712

fb-gh-sync-id: 453e20970460c703230547d8fd649383ba7d4c4a
  • Loading branch information
nicklockwood authored and facebook-github-bot-5 committed Nov 4, 2015
1 parent 4763f89 commit d799558
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
17 changes: 10 additions & 7 deletions Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,16 @@ var TextInput = React.createClass({
this.props.onChange && this.props.onChange(event);
this.props.onChangeText && this.props.onChangeText(text);
this.setState({mostRecentEventCount: eventCount}, () => {
// This is a controlled component, so make sure to force the native value
// to match. Most usage shouldn't need this, but if it does this will be
// more correct but might flicker a bit and/or cause the cursor to jump.
if (text !== this.props.value && typeof this.props.value === 'string') {
this.refs.input.setNativeProps({
text: this.props.value,
});
// NOTE: this doesn't seem to be needed on iOS - keeping for now in case it's required on Android
if (Platform.OS === 'android') {
// This is a controlled component, so make sure to force the native value
// to match. Most usage shouldn't need this, but if it does this will be
// more correct but might flicker a bit and/or cause the cursor to jump.
if (text !== this.props.value && typeof this.props.value === 'string') {
this.refs.input.setNativeProps({
text: this.props.value,
});
}
}
});
},
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Text/RCTShadowRawText.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (void)contentSizeMultiplierDidChange:(NSNotification *)note

- (void)setText:(NSString *)text
{
if (_text != text) {
if (_text != text && ![_text isEqualToString:text]) {
_text = [text copy];
[self dirtyLayout];
[self dirtyText];
Expand Down
70 changes: 69 additions & 1 deletion Libraries/Text/RCTTextManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@
#import "RCTShadowText.h"
#import "RCTSparseArray.h"
#import "RCTText.h"
#import "RCTTextView.h"
#import "UIView+React.h"

@interface RCTShadowText (Private)

- (NSTextStorage *)buildTextStorageForWidth:(CGFloat)width;

@end


@implementation RCTTextManager

RCT_EXPORT_MODULE()
Expand Down Expand Up @@ -54,6 +62,7 @@ - (RCTShadowView *)shadowView

- (RCTViewManagerUIBlock)uiBlockToAmendWithShadowViewRegistry:(RCTSparseArray *)shadowViewRegistry
{
NSMutableSet *textViewTagsToUpdate = [NSMutableSet new];
for (RCTShadowView *rootView in shadowViewRegistry.allObjects) {
if (![rootView isReactRootView]) {
// This isn't a root view
Expand All @@ -77,6 +86,19 @@ - (RCTViewManagerUIBlock)uiBlockToAmendWithShadowViewRegistry:(RCTSparseArray *)
RCTLogError(@"Raw text cannot be used outside of a <Text> tag. Not rendering string: '%@'",
[(RCTShadowRawText *)shadowView text]);
} else {
NSNumber *reactTag = shadowView.reactTag;
// This isn't pretty, but hopefully it's temporary
// the problem is, there's no easy way (besides the viewName)
// to tell from the shadowView if the view is an RKTextView
if ([shadowView.viewName hasSuffix:@"TextView"]) {
// Add to textViewTagsToUpdate only if has a RCTShadowText subview
for (RCTShadowView *subview in shadowView.reactSubviews) {
if ([subview isKindOfClass:[RCTShadowText class]]) {
[textViewTagsToUpdate addObject:reactTag];
break;
}
}
}
for (RCTShadowView *child in [shadowView reactSubviews]) {
if ([child isTextDirty]) {
[queue addObject:child];
Expand All @@ -88,7 +110,53 @@ - (RCTViewManagerUIBlock)uiBlockToAmendWithShadowViewRegistry:(RCTSparseArray *)
}
}

return nil;
/**
* NOTE: this logic is included to support rich text editing inside multiline
* `<TextInput>` controls, a feature which is not yet supported in open source.
* It is required in order to ensure that the textStorage (aka attributed
* string) is copied over from the RCTShadowText to the RCTText view in time
* to be used to update the editable text content.
*/
if (textViewTagsToUpdate.count) {

NSMutableArray<RCTViewManagerUIBlock> *uiBlocks = [NSMutableArray new];
for (NSNumber *reactTag in textViewTagsToUpdate) {
RCTShadowView *shadowTextView = shadowViewRegistry[reactTag];
RCTShadowText *shadowText;
for (RCTShadowText *subview in shadowTextView.reactSubviews) {
if ([subview isKindOfClass:[RCTShadowText class]]) {
shadowText = subview;
break;
}
}

UIEdgeInsets padding = shadowText.paddingAsInsets;
CGFloat width = shadowText.frame.size.width - (padding.left + padding.right);
NSTextStorage *textStorage = [shadowText buildTextStorageForWidth:width];

[uiBlocks addObject:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) {
RCTTextView *textView = viewRegistry[reactTag];
RCTText *text;
for (RCTText *subview in textView.reactSubviews) {
if ([subview isKindOfClass:[RCTText class]]) {
text = subview;
break;
}
}

text.textStorage = textStorage;
[textView performTextUpdate];
}];
}

return ^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) {
for (RCTViewManagerUIBlock uiBlock in uiBlocks) {
uiBlock(uiManager, viewRegistry);
}
};
} else {
return nil;
}
}

- (RCTViewManagerUIBlock)uiBlockToAmendWithShadowView:(RCTShadowText *)shadowView
Expand Down
2 changes: 2 additions & 0 deletions Libraries/Text/RCTTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@

- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;

- (void)performTextUpdate;

@end
5 changes: 5 additions & 0 deletions Libraries/Text/RCTTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,9 @@ - (UIColor *)defaultPlaceholderTextColor
return [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.098/255.0 alpha:0.22];
}

- (void)performTextUpdate
{
// Not used (yet)
}

@end

0 comments on commit d799558

Please sign in to comment.