Skip to content

Instantly share code, notes, and snippets.

@luco
Forked from chih/showDoneButton.patch
Created August 19, 2017 01:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luco/9c0b497212ac899c289f6565e85e95bf to your computer and use it in GitHub Desktop.
Save luco/9c0b497212ac899c289f6565e85e95bf to your computer and use it in GitHub Desktop.
Adding showDoneButton property to react native's TextInput for number fields
diff --git a/node_modules/react-native/Libraries/Text/RCTTextField.h b/node_modules/react-native/Libraries/Text/RCTTextField.h
index c809f10..2d80af4 100644
--- a/node_modules/react-native/Libraries/Text/RCTTextField.h
+++ b/node_modules/react-native/Libraries/Text/RCTTextField.h
@@ -31,5 +31,6 @@
- (void)textFieldDidChange;
- (void)sendKeyValueForString:(NSString *)string;
- (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField;
+- (void)handleDoneButtonFromInputAccessory;
@end
diff --git a/node_modules/react-native/Libraries/Text/RCTTextField.m b/node_modules/react-native/Libraries/Text/RCTTextField.m
index 32650d8..d4d8497 100644
--- a/node_modules/react-native/Libraries/Text/RCTTextField.m
+++ b/node_modules/react-native/Libraries/Text/RCTTextField.m
@@ -208,6 +208,12 @@ - (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField
return YES;
}
+- (void)handleDoneButtonFromInputAccessory {
+ [self endEditing:YES];
+ [self textFieldSubmitEditing];
+}
+
+
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(RCTTextField *)textField
change:(NSDictionary *)change
diff --git a/node_modules/react-native/Libraries/Text/RCTTextFieldManager.m b/node_modules/react-native/Libraries/Text/RCTTextFieldManager.m
index 6b81a78..477484d 100644
--- a/node_modules/react-native/Libraries/Text/RCTTextFieldManager.m
+++ b/node_modules/react-native/Libraries/Text/RCTTextFieldManager.m
@@ -116,6 +116,23 @@ - (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField
{
view.font = [RCTFont updateFont:view.font withFamily:json ?: defaultView.font.familyName];
}
+RCT_CUSTOM_VIEW_PROPERTY(showDoneButton, BOOL, RCTTextField)
+{
+ if (json && ([RCTConvert BOOL:json])) {
+ UIToolbar* toolbar = [[UIToolbar alloc] init];
+ [toolbar sizeToFit];
+ UIBarButtonItem* flex = [[UIBarButtonItem alloc]
+ initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
+ target:nil action:nil];
+ UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]
+ initWithBarButtonSystemItem:UIBarButtonSystemItemDone
+ target:view action:@selector(handleDoneButtonFromInputAccessory)];
+ toolbar.items = @[flex, doneButton];
+ view.inputAccessoryView = toolbar;
+ } else {
+ view.inputAccessoryView = nil;
+ }
+}
RCT_EXPORT_VIEW_PROPERTY(mostRecentEventCount, NSInteger)
- (RCTViewManagerUIBlock)uiBlockToAmendWithShadowView:(RCTShadowView *)shadowView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment