Skip to content

Commit

Permalink
Implement TextInput autoFocus natively on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
janicduplessis committed Jan 18, 2020
1 parent cc3e27d commit 022e79c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
16 changes: 8 additions & 8 deletions Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@ function useFocusOnMount(
const initialAutoFocusValue = useRef<?boolean>(initialAutoFocus);

useEffect(() => {
// On iOS autoFocus is handled natively.
if (Platform.OS === 'ios') {
return;
}

// We only want to autofocus on initial mount.
// Since initialAutoFocusValue and inputRef will never change
// this should match the expected behavior
Expand All @@ -708,14 +713,9 @@ function useFocusOnMount(
}
};

let rafId;
if (Platform.OS === 'android') {
// On Android this needs to be executed in a rAF callback
// otherwise the keyboard opens then closes immediately.
rafId = requestAnimationFrame(focus);
} else {
focus();
}
// On Android this needs to be executed in a rAF callback
// otherwise the keyboard opens then closes immediately.
const rafId = requestAnimationFrame(focus);

return () => {
if (rafId != null) {
Expand Down
1 change: 1 addition & 0 deletions Libraries/Text/TextInput/RCTBaseTextInputView.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, copy, nullable) RCTDirectEventBlock onScroll;

@property (nonatomic, assign) NSInteger mostRecentEventCount;
@property (nonatomic, assign) BOOL autoFocus;
@property (nonatomic, assign) BOOL blurOnSubmit;
@property (nonatomic, assign) BOOL selectTextOnFocus;
@property (nonatomic, assign) BOOL clearTextOnFocus;
Expand Down
9 changes: 8 additions & 1 deletion Libraries/Text/TextInput/RCTBaseTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ @implementation RCTBaseTextInputView {
BOOL _hasInputAccesoryView;
NSString *_Nullable _predictedText;
NSInteger _nativeEventCount;
BOOL _didMoveToWindow;
}

- (instancetype)initWithBridge:(RCTBridge *)bridge
Expand Down Expand Up @@ -519,7 +520,13 @@ - (void)reactBlur

- (void)didMoveToWindow
{
[self.backedTextInputView reactFocusIfNeeded];
if (self.autoFocus && !_didMoveToWindow) {
[self.backedTextInputView reactFocus];
} else {
[self.backedTextInputView reactFocusIfNeeded];
}

_didMoveToWindow = YES;
}

#pragma mark - Custom Input Accessory View
Expand Down
1 change: 1 addition & 0 deletions Libraries/Text/TextInput/RCTBaseTextInputViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ @implementation RCTBaseTextInputViewManager
RCT_REMAP_VIEW_PROPERTY(clearButtonMode, backedTextInputView.clearButtonMode, UITextFieldViewMode)
RCT_REMAP_VIEW_PROPERTY(scrollEnabled, backedTextInputView.scrollEnabled, BOOL)
RCT_REMAP_VIEW_PROPERTY(secureTextEntry, backedTextInputView.secureTextEntry, BOOL)
RCT_EXPORT_VIEW_PROPERTY(autoFocus, BOOL)
RCT_EXPORT_VIEW_PROPERTY(blurOnSubmit, BOOL)
RCT_EXPORT_VIEW_PROPERTY(clearTextOnFocus, BOOL)
RCT_EXPORT_VIEW_PROPERTY(keyboardType, UIKeyboardType)
Expand Down

0 comments on commit 022e79c

Please sign in to comment.