Skip to content

Commit 0e68147

Browse files
authored
iOS autoCapitalization (#92)
* feat: iOS autoCapitalization * fix: adjust prop change for blurred inputs * fix: remove default autocapitalization since we have JS default
1 parent 56bfb6e commit 0e68147

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

ios/ReactNativeRichTextEditorView.mm

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ @implementation ReactNativeRichTextEditorView {
3737
BOOL _emitHtml;
3838
UILabel *_placeholderLabel;
3939
UIColor *_placeholderColor;
40+
BOOL _emitFocusBlur;
4041
}
4142

4243
// MARK: - Component utils
@@ -76,6 +77,7 @@ - (void)setDefaults {
7677
_recentlyEmittedString = @"";
7778
_recentlyEmittedHtml = @"";
7879
_emitHtml = NO;
80+
_emitFocusBlur = YES;
7981

8082
defaultTypingAttributes = [[NSMutableDictionary<NSAttributedStringKey, id> alloc] init];
8183

@@ -288,6 +290,28 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
288290
}
289291
}
290292

293+
// autoCapitalize
294+
if(newViewProps.autoCapitalize != oldViewProps.autoCapitalize) {
295+
NSString *str = [NSString fromCppString:newViewProps.autoCapitalize];
296+
if([str isEqualToString: @"none"]) {
297+
textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
298+
} else if([str isEqualToString: @"sentences"]) {
299+
textView.autocapitalizationType = UITextAutocapitalizationTypeSentences;
300+
} else if([str isEqualToString: @"words"]) {
301+
textView.autocapitalizationType = UITextAutocapitalizationTypeWords;
302+
} else if([str isEqualToString: @"characters"]) {
303+
textView.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
304+
}
305+
306+
// textView needs to be refocused on autocapitalization type change and we don't want to emit these events
307+
if([textView isFirstResponder]) {
308+
_emitFocusBlur = NO;
309+
[textView reactBlur];
310+
[textView reactFocus];
311+
_emitFocusBlur = YES;
312+
}
313+
}
314+
291315
// isOnChangeHtmlSet
292316
_emitHtml = newViewProps.isOnChangeHtmlSet;
293317

@@ -805,8 +829,10 @@ - (void)anyTextMayHaveBeenModified {
805829
- (void)textViewDidBeginEditing:(UITextView *)textView {
806830
auto emitter = [self getEventEmitter];
807831
if(emitter != nullptr) {
808-
//send onFocus event
809-
emitter->onInputFocus({});
832+
//send onFocus event if allowed
833+
if(_emitFocusBlur) {
834+
emitter->onInputFocus({});
835+
}
810836

811837
NSString *textAtSelection = [[[NSMutableString alloc] initWithString:textView.textStorage.string] substringWithRange: textView.selectedRange];
812838
emitter->onChangeSelection({
@@ -821,7 +847,7 @@ - (void)textViewDidBeginEditing:(UITextView *)textView {
821847

822848
- (void)textViewDidEndEditing:(UITextView *)textView {
823849
auto emitter = [self getEventEmitter];
824-
if(emitter != nullptr) {
850+
if(emitter != nullptr && _emitFocusBlur) {
825851
//send onBlur event
826852
emitter->onInputBlur({});
827853
}

0 commit comments

Comments
 (0)