Skip to content

Commit 7af7524

Browse files
dsibiskifacebook-github-bot-6
authored andcommitted
Implements blurOnSubmit
Summary: The default value (to retain current behavior) is set to `true`. Setting the value to `false` will prevent the textField from blurring but still fire the `onSubmitEditing` callback. However, the `onEndEditing` callback will not be fired. Addresses issue: facebook#2129 Closes facebook#2149 Reviewed By: svcscm Differential Revision: D2619822 Pulled By: nicklockwood fb-gh-sync-id: 9a61152892f4afb5c6c53e7b38dffae13bc7e13f
1 parent 66f7fed commit 7af7524

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

Examples/UIExplorer/TextInputExample.ios.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,60 @@ class RewriteExample extends React.Component {
120120
}
121121
}
122122

123+
var BlurOnSubmitExample = React.createClass({
124+
focusNextField(nextField) {
125+
this.refs[nextField].focus()
126+
},
127+
128+
render: function() {
129+
return (
130+
<View>
131+
<TextInput
132+
ref='1'
133+
style={styles.default}
134+
placeholder='blurOnSubmit = false'
135+
returnKeyType='next'
136+
blurOnSubmit={false}
137+
onSubmitEditing={() => this.focusNextField('2')}
138+
/>
139+
<TextInput
140+
ref='2'
141+
style={styles.default}
142+
keyboardType='email-address'
143+
placeholder='blurOnSubmit = false'
144+
returnKeyType='next'
145+
blurOnSubmit={false}
146+
onSubmitEditing={() => this.focusNextField('3')}
147+
/>
148+
<TextInput
149+
ref='3'
150+
style={styles.default}
151+
keyboardType='url'
152+
placeholder='blurOnSubmit = false'
153+
returnKeyType='next'
154+
blurOnSubmit={false}
155+
onSubmitEditing={() => this.focusNextField('4')}
156+
/>
157+
<TextInput
158+
ref='4'
159+
style={styles.default}
160+
keyboardType='numeric'
161+
placeholder='blurOnSubmit = false'
162+
blurOnSubmit={false}
163+
onSubmitEditing={() => this.focusNextField('5')}
164+
/>
165+
<TextInput
166+
ref='5'
167+
style={styles.default}
168+
keyboardType='numbers-and-punctuation'
169+
placeholder='blurOnSubmit = true'
170+
returnKeyType='done'
171+
/>
172+
</View>
173+
);
174+
}
175+
});
176+
123177
var styles = StyleSheet.create({
124178
page: {
125179
paddingBottom: 300,
@@ -443,5 +497,9 @@ exports.examples = [
443497
</View>
444498
);
445499
}
446-
}
500+
},
501+
{
502+
title: 'Blur on submit',
503+
render: function(): ReactElement { return <BlurOnSubmitExample />; },
504+
},
447505
];

Libraries/Components/TextInput/TextInput.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ if (Platform.OS === 'android') {
4747
var RCTTextField = requireNativeComponent('RCTTextField', null);
4848
}
4949

50+
type DefaultProps = {
51+
blurOnSubmit: boolean;
52+
};
53+
5054
type Event = Object;
5155

5256
/**
@@ -283,6 +287,12 @@ var TextInput = React.createClass({
283287
* @platform ios
284288
*/
285289
selectTextOnFocus: PropTypes.bool,
290+
/**
291+
* If true, the text field will blur when submitted.
292+
* The default value is true.
293+
* @platform ios
294+
*/
295+
blurOnSubmit: PropTypes.bool,
286296
/**
287297
* Styles
288298
*/
@@ -298,6 +308,12 @@ var TextInput = React.createClass({
298308
underlineColorAndroid: PropTypes.string,
299309
},
300310

311+
getDefaultProps: function(): DefaultProps {
312+
return {
313+
blurOnSubmit: true,
314+
};
315+
},
316+
301317
/**
302318
* `NativeMethodsMixin` will look for this when invoking `setNativeProps`. We
303319
* make `this` look like an actual native component class.

Libraries/Text/RCTTextField.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
@property (nonatomic, assign) BOOL caretHidden;
1717
@property (nonatomic, assign) BOOL autoCorrect;
1818
@property (nonatomic, assign) BOOL selectTextOnFocus;
19+
@property (nonatomic, assign) BOOL blurOnSubmit;
1920
@property (nonatomic, assign) UIEdgeInsets contentInset;
2021
@property (nonatomic, strong) UIColor *placeholderTextColor;
2122
@property (nonatomic, assign) NSInteger mostRecentEventCount;
@@ -26,5 +27,6 @@
2627

2728
- (void)textFieldDidChange;
2829
- (void)sendKeyValueForString:(NSString *)string;
30+
- (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField;
2931

3032
@end

Libraries/Text/RCTTextField.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ @implementation RCTTextField
2020
NSMutableArray<UIView *> *_reactSubviews;
2121
BOOL _jsRequestingFirstResponder;
2222
NSInteger _nativeEventCount;
23+
BOOL _submitted;
2324
}
2425

2526
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
@@ -165,6 +166,7 @@ - (void)textFieldEndEditing
165166
}
166167
- (void)textFieldSubmitEditing
167168
{
169+
_submitted = YES;
168170
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeSubmit
169171
reactTag:self.reactTag
170172
text:self.text
@@ -186,6 +188,15 @@ - (void)textFieldBeginEditing
186188
eventCount:_nativeEventCount];
187189
}
188190

191+
- (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField
192+
{
193+
if (_submitted) {
194+
_submitted = NO;
195+
return _blurOnSubmit;
196+
}
197+
return YES;
198+
}
199+
189200
- (BOOL)becomeFirstResponder
190201
{
191202
_jsRequestingFirstResponder = YES;

Libraries/Text/RCTTextFieldManager.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ - (BOOL)keyboardInputShouldDelete:(RCTTextField *)textField
6969
return YES;
7070
}
7171

72+
- (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField
73+
{
74+
return [textField textFieldShouldEndEditing:textField];
75+
}
76+
7277
RCT_EXPORT_VIEW_PROPERTY(caretHidden, BOOL)
7378
RCT_EXPORT_VIEW_PROPERTY(autoCorrect, BOOL)
7479
RCT_REMAP_VIEW_PROPERTY(editable, enabled, BOOL)
@@ -79,6 +84,7 @@ - (BOOL)keyboardInputShouldDelete:(RCTTextField *)textField
7984
RCT_EXPORT_VIEW_PROPERTY(clearButtonMode, UITextFieldViewMode)
8085
RCT_REMAP_VIEW_PROPERTY(clearTextOnFocus, clearsOnBeginEditing, BOOL)
8186
RCT_EXPORT_VIEW_PROPERTY(selectTextOnFocus, BOOL)
87+
RCT_EXPORT_VIEW_PROPERTY(blurOnSubmit, BOOL)
8288
RCT_EXPORT_VIEW_PROPERTY(keyboardType, UIKeyboardType)
8389
RCT_EXPORT_VIEW_PROPERTY(returnKeyType, UIReturnKeyType)
8490
RCT_EXPORT_VIEW_PROPERTY(enablesReturnKeyAutomatically, BOOL)

0 commit comments

Comments
 (0)