Skip to content

Commit 763389e

Browse files
authored
Merge pull request #159 from bbqsrc/fix/on-submit-editing-text-field
Fix onSubmitEditing for text fields
2 parents 0a5a3a7 + 2c57e7c commit 763389e

File tree

4 files changed

+107
-4
lines changed

4 files changed

+107
-4
lines changed

Libraries/Components/TextInput/TextInput.js

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,10 @@ const TextInput = React.createClass({
521521
},
522522

523523
render: function() {
524-
if (Platform.OS === 'ios' || Platform.OS === 'macos') {
524+
if (Platform.OS === 'ios') {
525525
return this._renderIOS();
526+
} else if (Platform.OS === 'macos') {
527+
return this._renderMacOS();
526528
} else if (Platform.OS === 'android') {
527529
return this._renderAndroid();
528530
}
@@ -534,6 +536,90 @@ const TextInput = React.createClass({
534536
this.props.defaultValue;
535537
},
536538

539+
_renderMacOS: function() {
540+
var textContainer;
541+
542+
var onSelectionChange;
543+
if (this.props.selectionState || this.props.onSelectionChange) {
544+
onSelectionChange = (event: Event) => {
545+
if (this.props.selectionState) {
546+
var selection = event.nativeEvent.selection;
547+
this.props.selectionState.update(selection.start, selection.end);
548+
}
549+
this.props.onSelectionChange && this.props.onSelectionChange(event);
550+
};
551+
}
552+
553+
var props = Object.assign({}, this.props);
554+
props.style = [styles.input, this.props.style];
555+
if (!props.multiline) {
556+
if (__DEV__) {
557+
for (var propKey in onlyMultiline) {
558+
if (props[propKey]) {
559+
const error = new Error(
560+
'TextInput prop `' + propKey + '` is only supported with multiline.'
561+
);
562+
warning(false, '%s', error.stack);
563+
}
564+
}
565+
}
566+
var TextField = props.password ? RCTSecureTextField : RCTTextField;
567+
textContainer =
568+
<TextField
569+
ref="input"
570+
{...props}
571+
onFocus={this._onFocus}
572+
onBlur={this._onBlur}
573+
onChange={this._onChange}
574+
onSubmitEditing={this.props.onSubmitEditing}
575+
onSelectionChange={onSelectionChange}
576+
onSelectionChangeShouldSetResponder={emptyFunction.thatReturnsTrue}
577+
text={this._getText()}
578+
/>;
579+
} else {
580+
var children = props.children;
581+
var childCount = 0;
582+
ReactChildren.forEach(children, () => ++childCount);
583+
invariant(
584+
!(props.value && childCount),
585+
'Cannot specify both value and children.'
586+
);
587+
if (childCount >= 1) {
588+
children = <Text style={props.style}>{children}</Text>;
589+
}
590+
if (props.inputView) {
591+
children = [children, props.inputView];
592+
}
593+
textContainer =
594+
<RCTTextView
595+
ref="input"
596+
{...props}
597+
children={children}
598+
onFocus={this._onFocus}
599+
onBlur={this._onBlur}
600+
onChange={this._onChange}
601+
onContentSizeChange={this.props.onContentSizeChange}
602+
onSelectionChange={onSelectionChange}
603+
onSubmitEditing={this.props.onSubmitEditing}
604+
onTextInput={this._onTextInput}
605+
onSelectionChangeShouldSetResponder={emptyFunction.thatReturnsTrue}
606+
placeholderTextColor={processColor(props.placeholderTextColor)}
607+
text={this._getText()}
608+
dataDetectorTypes={this.props.dataDetectorTypes}
609+
/>;
610+
}
611+
612+
return (
613+
<TouchableWithoutFeedback
614+
onLayout={props.onLayout}
615+
onPress={this._onPress}
616+
rejectResponderTermination={true}
617+
testID={props.testID}>
618+
{textContainer}
619+
</TouchableWithoutFeedback>
620+
);
621+
},
622+
537623
_renderIOS: function() {
538624
var textContainer;
539625

Libraries/Text/RCTTextField.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@interface TextFieldCellWithPaddings : NSTextFieldCell
1717
@end
1818

19-
@interface RCTTextField : NSTextField <NSTextFieldDelegate>
19+
@interface RCTTextField : NSTextField <NSTextFieldDelegate, NSControlTextEditingDelegate>
2020

2121
@property (nonatomic, assign) BOOL caretHidden;
2222
@property (nonatomic, assign) BOOL autoCorrect;
@@ -35,5 +35,6 @@
3535
//- (void)textFieldDidChange:(NSNotification *)aNotification;
3636
- (void)sendKeyValueForString:(NSString *)string;
3737
- (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField;
38+
- (void)textFieldSubmitEditingWithString:(NSString *)key;
3839

3940
@end

Libraries/Text/RCTTextField.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ - (void)textDidEndEditing:(NSNotification *)aNotification
185185
key:nil
186186
eventCount:_nativeEventCount];
187187
}
188-
- (void)textFieldSubmitEditing
188+
- (void)textFieldSubmitEditingWithString:(NSString *)key
189189
{
190190
_submitted = YES;
191191
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeSubmit
192192
reactTag:self.reactTag
193193
text:[self stringValue]
194-
key:nil
194+
key:key
195195
eventCount:_nativeEventCount];
196196
}
197197

Libraries/Text/RCTTextFieldManager.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ - (NSView *)view
4040
return textField;
4141
}
4242

43+
- (BOOL)control:(NSControl *)control textView:(NSTextView * __unused)textView doCommandBySelector:(SEL)commandSelector {
44+
if (![control isKindOfClass:[RCTTextField class]]) {
45+
return YES;
46+
}
47+
48+
RCTTextField *textField = (RCTTextField*)control;
49+
50+
if (commandSelector == @selector(insertNewline:)) {
51+
[textField textFieldSubmitEditingWithString:@"\n"];
52+
return YES;
53+
}
54+
55+
return NO;
56+
}
57+
4358
- (BOOL)textField:(RCTTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
4459
{
4560
// Only allow single keypresses for onKeyPress, pasted text will not be sent.
@@ -52,6 +67,7 @@ - (BOOL)textField:(RCTTextField *)textField shouldChangeCharactersInRange:(NSRan
5267
if (textField.maxLength == nil || [string isEqualToString:@"\n"]) { // Make sure forms can be submitted via return
5368
return YES;
5469
}
70+
5571
return YES;
5672
}
5773

0 commit comments

Comments
 (0)