Skip to content

Commit fad5694

Browse files
committed
add native returnKeyType
1 parent fbc98ff commit fad5694

File tree

6 files changed

+36
-6
lines changed

6 files changed

+36
-6
lines changed

android/src/main/java/com/variabletextinput/VariableTextInputViewManager.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ public Map<String, Object> getConstants() {
376376
"onAndroidBlur",
377377
MapBuilder.of("registrationName", "onAndroidBlur")).put(
378378
"onAndroidFocus",
379-
MapBuilder.of("registrationName", "onAndroidFocus")).build();
379+
MapBuilder.of("registrationName", "onAndroidFocus")).put("onAndroidSubmitEditing",
380+
MapBuilder.of("registrationName", "onAndroidSubmitEditing")).build();
380381
}
381382
}

android/src/main/java/com/variabletextinput/view/VariableTextInput.java

+15
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ public void onPaste() {
245245
new TextView.OnEditorActionListener() {
246246
@Override
247247
public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) {
248+
Log.d("Input", "onEditorAction actionId:" + actionId);
249+
if (actionId == EditorInfo.IME_ACTION_SEARCH
250+
|| actionId == EditorInfo.IME_ACTION_SEND
251+
|| actionId == EditorInfo.IME_ACTION_DONE) {
252+
WritableMap event = Arguments.createMap();
253+
event.putString("text", editText.getText().toString());
254+
final Context context = getContext();
255+
if (context instanceof ReactContext) {
256+
((ReactContext) context).getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onAndroidSubmitEditing", event);
257+
}
258+
return true;
259+
}
248260
if ((actionId & EditorInfo.IME_MASK_ACTION) != 0 || actionId == EditorInfo.IME_NULL) {
249261
boolean isMultiline = true;
250262

@@ -588,12 +600,15 @@ private void updateImeOptions() {
588600
returnKeyFlag = EditorInfo.IME_ACTION_PREVIOUS;
589601
break;
590602
case "search":
603+
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
591604
returnKeyFlag = EditorInfo.IME_ACTION_SEARCH;
592605
break;
593606
case "send":
607+
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
594608
returnKeyFlag = EditorInfo.IME_ACTION_SEND;
595609
break;
596610
case "done":
611+
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
597612
returnKeyFlag = EditorInfo.IME_ACTION_DONE;
598613
break;
599614
}

ios/VariableTextInput.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ NS_ASSUME_NONNULL_BEGIN
4444
@property(assign, nonatomic, getter=isSupport) BOOL bSupport;
4545
@property(nonatomic, copy) RCTBubblingEventBlock onChange;
4646
@property (nonatomic, copy, nullable) RCTDirectEventBlock onContentSizeChange;
47-
@property(nonatomic, copy) RCTBubblingEventBlock onSubmitEditing;
47+
@property(nonatomic, copy, nullable) RCTDirectEventBlock onSubmitEditing;
4848
@property(nonatomic,copy, nullable)RCTDirectEventBlock onTag;
4949
@property(nonatomic, copy, nullable) RCTDirectEventBlock onTextInput;
5050
@property(nonatomic, copy, nullable) RCTDirectEventBlock onBlur;

ios/VariableTextInput.m

+8-1
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,20 @@ - (void)textViewDidChangeSelection:(UITextView *)textView {
347347
}
348348

349349
- (void)textViewDidChange:(UITextView *)textView {
350-
351350
if (_onChange) {
352351
_onChange(@{@"text":[textView.textStorage getPlainString]});
353352
}
354353
}
355354

356355
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
356+
// 处理键盘return事件
357+
BOOL returnStatus = textView.returnKeyType == UIReturnKeyDone || textView.returnKeyType == UIReturnKeySend || textView.returnKeyType == UIReturnKeySearch;
358+
if(returnStatus && [text isEqualToString:@"\n"]) {
359+
if (self.onSubmitEditing) {
360+
self.onSubmitEditing(@{@"text": [self.textStorage getPlainString]});
361+
}
362+
return NO;
363+
}
357364
NSString *oldStr = [self getStrContentInRange:NSMakeRange(0, [self.attributedText length])];
358365
NSString *newStr = [NSString stringWithFormat:@"%@%@",oldStr,text];
359366
[self handleTags:text];

ios/VariableTextInputViewManager.m

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ @implementation VariableTextInputViewManager
3535
RCT_EXPORT_VIEW_PROPERTY(onFocus, RCTBubblingEventBlock)
3636
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
3737
RCT_EXPORT_VIEW_PROPERTY(onContentSizeChange, RCTBubblingEventBlock)
38+
RCT_EXPORT_VIEW_PROPERTY(onSubmitEditing, RCTBubblingEventBlock)
3839
RCT_CUSTOM_VIEW_PROPERTY(textAlign, NSTextAlignment, VariableTextInput)
3940
{
4041
[view setTextAlignment:[RCTConvert NSTextAlignment:json]];

src/VariableTextInputView.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ interface INativeProps {
5454
e: NativeSyntheticEvent<TextInputContentSizeChangeEventData>
5555
) => void;
5656
keyboardType?: KeyboardTypeOptions | undefined;
57-
onSubmitEditing?: (text: string) => void;
58-
onAndroidSubmitEditing?: (text: string) => void;
57+
onSubmitEditing?: (e: NativeSyntheticEvent<TextInputChangeEventData>) => void;
58+
onAndroidSubmitEditing?: (e: IVTTextInputData) => void;
5959
submitBehavior?: 'submit';
6060
onBlur?: () => void;
6161
onFocus?: () => void;
@@ -239,7 +239,12 @@ const VariableTextInputView = forwardRef(
239239
insertMentionAndDelateKeyword: insertMentionAndDelateKeyword,
240240
};
241241
});
242-
const onAndroidSubmitEditing = () => {};
242+
const _onSubmitEditing = (e: NativeSyntheticEvent<TextInputChangeEventData>) => {
243+
props.onSubmitEditing && props.onSubmitEditing(e.nativeEvent.text);
244+
}
245+
const onAndroidSubmitEditing = (e: IVTTextInputData) => {
246+
props.onSubmitEditing && props.onSubmitEditing(e.nativeEvent.text);
247+
};
243248
const onAndroidTextInput = (e: IVTTextInputData) => {
244249
props.onTextInput && props.onTextInput(e);
245250
};
@@ -252,6 +257,7 @@ const VariableTextInputView = forwardRef(
252257
onAndroidChange={_onChange}
253258
onAndroidContentSizeChange={onContentSizeChange}
254259
{...props}
260+
onSubmitEditing={_onSubmitEditing}
255261
onAndroidSubmitEditing={onAndroidSubmitEditing}
256262
onAndroidTextInput={onAndroidTextInput}
257263
onAndroidBlur={props.onBlur}

0 commit comments

Comments
 (0)