Skip to content

Commit ee51a0a

Browse files
magicienaleclarson
authored andcommitted
Fix #18272 TextInput.setNativeProps({text: ''}) to work (#18278)
Summary: Fix #18272. Calling textInputRef.setNativeProps({text: ''}) or textInputRef.clear() should clear the text input. - All tests of `yarn run test` are passed - Test with [the sample app](https://github.com/magicien/react-native-textinput-clear). - TextInput.clear() and TextInput.setNativeProps({ text: '***' }) worked - When clear() or setNativeProps() called, onChange/onChangeText wasn't called - Same behavior as react 0.53.0 - When non-string values are given to `setNativeProps({text: ___})`, its behavior is the same as react 0.53.0. - Value Type | Result ---------- | ------------ null | same as empty string '' undefined | nothing changes number | throw error function | throw error object | throw error - When clear() or setNativeProps() called, attributed text keeps the attributes - When `value` prop is set, the text can't be changed - `clear()` doesn't work from the second time - `setNativeProps({text '***'})` doesn't work from the second time - Even when `value` prop is set, you can change the text ![ScreenShot_0.54.0](https://github.com/raw/magicien/react-native-textinput-clear/master/screenshot/0.54.0_test.gif) - `clear()` works every time - `setNativeProps({text '****'})` works every time ![ScreenShot_Clear_1](https://github.com/raw/magicien/react-native-textinput-clear/master/screenshot/clear_test_1.gif) ![ScreenShot_Clear_2](https://github.com/raw/magicien/react-native-textinput-clear/master/screenshot/clear_test_2.gif) - The text keeps the attributes (font family, size, color, text align) ![ScreenShot_Slider](https://github.com/raw/magicien/react-native-textinput-clear/master/screenshot/attributed_text_test.gif) - If `value` prop is set, the text should not be changed ![ScreenShot_Value](https://github.com/raw/magicien/react-native-textinput-clear/master/screenshot/value_test.gif) [IOS] [BUGFIX] [TextInput] - Fix TextInput.clear() and TextInput.setNativeProps({text: ''}) to work Pull Request resolved: facebook/react-native#18278 Reviewed By: shergin Differential Revision: D9692561 Pulled By: hramos fbshipit-source-id: b7ce8f6740fdf666e71d6a85743331ca4805edcb
1 parent 2c6182d commit ee51a0a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Libraries/Text/TextInput/RCTBaseTextInputShadowView.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ @implementation RCTBaseTextInputShadowView
2525
NSAttributedString *_Nullable _localAttributedText;
2626
CGSize _previousContentSize;
2727

28+
NSString *_text;
2829
NSTextStorage *_textStorage;
2930
NSTextContainer *_textContainer;
3031
NSLayoutManager *_layoutManager;
@@ -102,6 +103,20 @@ - (void)invalidateContentSize
102103
});
103104
}
104105

106+
- (NSString *)text
107+
{
108+
return _text;
109+
}
110+
111+
- (void)setText:(NSString *)text
112+
{
113+
_text = text;
114+
// Clear `_previousAttributedText` to notify the view about the change
115+
// when `text` native prop is set.
116+
_previousAttributedText = nil;
117+
[self dirtyLayout];
118+
}
119+
105120
#pragma mark - RCTUIManagerObserver
106121

107122
- (void)uiManagerWillPerformMounting

0 commit comments

Comments
 (0)