Skip to content

Commit 924ccbd

Browse files
duom青源duom青源
authored andcommitted
feat: 添加 onblur 和onfocus方法
1 parent 3bdd5f1 commit 924ccbd

File tree

7 files changed

+78
-6
lines changed

7 files changed

+78
-6
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public void setKeyboardType(VariableTextInput view, String keyboardType) {
318318

319319
/**
320320
* 接受交互通知
321-
*
321+
*
322322
* @return
323323
*/
324324
@Nullable
@@ -335,7 +335,7 @@ public Map<String, Integer> getCommandsMap() {
335335

336336
/**
337337
* 根据命令ID,处理对应任务
338-
*
338+
*
339339
* @param root
340340
* @param commandId
341341
* @param args
@@ -378,6 +378,10 @@ public Map<String, Object> getConstants() {
378378
"onAndroidChange",
379379
MapBuilder.of("registrationName", "onAndroidChange")).put("onAndroidContentSizeChange",
380380
MapBuilder.of("registrationName", "onAndroidContentSizeChange"))
381-
.put("onAndroidTextInput", MapBuilder.of("registrationName", "onAndroidTextInput")).build();
381+
.put("onAndroidTextInput", MapBuilder.of("registrationName", "onAndroidTextInput")).put(
382+
"onAndroidBlur",
383+
MapBuilder.of("registrationName", "onAndroidBlur")).put(
384+
"onAndroidFocus",
385+
MapBuilder.of("registrationName", "onAndroidFocus")).build();
382386
}
383387
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.view.KeyEvent;
2525
import android.view.Menu;
2626
import android.view.MenuItem;
27+
import android.view.View;
2728
import android.view.inputmethod.EditorInfo;
2829
import android.view.inputmethod.InputMethodManager;
2930
import android.widget.LinearLayout;
@@ -100,6 +101,31 @@ public VariableTextInput(Context context) {
100101
editText.setBackground(reactViewBackgroundDrawable);
101102
scrollView.addView(editText);
102103
this.addView(scrollView);
104+
//添加键盘onFocusChangeListener监听器
105+
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
106+
@Override
107+
public void onFocusChange(View v, boolean hasFocus) {
108+
if (hasFocus) {
109+
// 当 EditText 获取焦点时执行的操作
110+
// 例如:显示键盘
111+
WritableMap event = Arguments.createMap();
112+
event.putString("text", editText.getText().toString());
113+
final Context context = getContext();
114+
if (context instanceof ReactContext) {
115+
((ReactContext) context).getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onAndroidFocus", event);
116+
}
117+
} else {
118+
// 当 EditText 失去焦点时执行的操作
119+
// 例如:隐藏键盘
120+
WritableMap event = Arguments.createMap();
121+
event.putString("text", editText.getText().toString());
122+
final Context context = getContext();
123+
if (context instanceof ReactContext) {
124+
((ReactContext) context).getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onAndroidBlur", event);
125+
}
126+
}
127+
}
128+
});
103129
// 添加 TextWatcher 监听器
104130
editText.addTextChangedListener(new TextWatcher() {
105131
private int oldHeight = editText.getHeight(); // 保存旧的高度

example/src/App.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ export const App = () => {
8484
mentions={['@', '#']}
8585
onMention={onMention}
8686
keyboardAppearance={'dark'}
87+
onBlur={() => {
88+
console.log('==onBlur==');
89+
}}
90+
onFocus={() => {
91+
console.log('==onFocus==');
92+
}}
8793
/>
8894
<TouchableOpacity
8995
activeOpacity={0.85}

ios/VariableTextInput.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ NS_ASSUME_NONNULL_BEGIN
4747
@property(nonatomic, copy) RCTBubblingEventBlock onSubmitEditing;
4848
@property(nonatomic,copy, nullable)RCTDirectEventBlock onTag;
4949
@property(nonatomic, copy, nullable) RCTDirectEventBlock onTextInput;
50+
@property(nonatomic, copy, nullable) RCTDirectEventBlock onBlur;
51+
@property(nonatomic, copy, nullable) RCTDirectEventBlock onFocus;
5052
@property(nonatomic, strong) NSDictionary *defultTypingAttributes;
5153
@property(nonatomic, strong) NSArray *tags;
5254
@property(nonatomic, strong) NSString *keyWord;

ios/VariableTextInput.m

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,18 @@ - (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)tex
4343
if (self) {
4444
self.delegate = self;
4545
[self addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
46-
46+
4747
[self preparePlaceholder];
4848
}
49+
50+
//注册通知,监听键盘弹出事件
51+
52+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
53+
54+
//注册通知,监听键盘消失事件
55+
56+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden:) name:UIKeyboardDidHideNotification object:nil];
57+
4958
return self;
5059
}
5160
#else
@@ -58,6 +67,15 @@ - (id)initWithFrame:(CGRect)frame
5867

5968
[self preparePlaceholder];
6069
}
70+
71+
//注册通知,监听键盘弹出事件
72+
73+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
74+
75+
//注册通知,监听键盘消失事件
76+
77+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden:) name:UIKeyboardDidHideNotification object:nil];
78+
6179
return self;
6280
}
6381
#endif
@@ -136,7 +154,14 @@ - (void)preparePlaceholder
136154
options:NSKeyValueObservingOptionNew context:nil];
137155
}
138156
}
139-
157+
-(void)keyboardDidShow: (NSNotification *)notif {
158+
//todo
159+
_onFocus(@{@"text": [self.textStorage getPlainString]});
160+
}
161+
-(void)keyboardDidHidden: (NSNotification *)notif {
162+
//todo
163+
_onBlur(@{@"text": [self.textStorage getPlainString]});
164+
}
140165
- (void)setPlaceholder:(NSString *)placeholderText
141166
{
142167
_placeholder = [placeholderText copy];

ios/VariableTextInputViewManager.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ @implementation VariableTextInputViewManager
3131
}
3232
RCT_EXPORT_VIEW_PROPERTY(onTextInput, RCTDirectEventBlock)
3333
RCT_EXPORT_VIEW_PROPERTY(onTag, RCTDirectEventBlock)
34-
34+
RCT_EXPORT_VIEW_PROPERTY(onBlur, RCTBubblingEventBlock)
35+
RCT_EXPORT_VIEW_PROPERTY(onFocus, RCTBubblingEventBlock)
3536
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
3637
RCT_EXPORT_VIEW_PROPERTY(onContentSizeChange, RCTBubblingEventBlock)
3738
RCT_CUSTOM_VIEW_PROPERTY(textAlign, NSTextAlignment, VariableTextInput)

src/VariableTextInputView.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ interface INativeProps {
5656
onSubmitEditing?: (text: string) => void;
5757
onAndroidSubmitEditing?: (text: string) => void;
5858
submitBehavior?: 'submit';
59+
onBlur?: () => void;
60+
onFocus?: () => void;
61+
onAndroidFocus?: () => void;
62+
onAndroidBlur?: () => void;
5963
}
6064
interface IProps {
6165
style?: StyleProp<TextStyle> | undefined;
@@ -78,6 +82,8 @@ interface IProps {
7882
emojiData?: IEmojiData[];
7983
mentions?: string[]; //'@','#'
8084
onMention?: (data: IonMentionData) => void;
85+
onBlur?: () => void;
86+
onFocus?: () => void;
8187
}
8288
export type IATTextViewRef = React.ForwardedRef<IATTextViewBase>;
8389

@@ -245,6 +251,8 @@ const VariableTextInputView = forwardRef(
245251
{...props}
246252
onAndroidSubmitEditing={onAndroidSubmitEditing}
247253
onAndroidTextInput={onAndroidTextInput}
254+
onAndroidBlur={props.onBlur}
255+
onAndroidFocus={props.onFocus}
248256
style={style}
249257
/>
250258
);

0 commit comments

Comments
 (0)