Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile] Enable Dismiss on PlainText in Android #20095

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions packages/block-editor/src/components/plain-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,39 @@ import styles from './style.scss';
export default class PlainText extends Component {
constructor() {
super( ...arguments );
this.isIOS = Platform.OS === 'ios';
this.isAndroid = Platform.OS === 'android';
}

componentDidMount() {
// if isSelected is true, we should request the focus on this TextInput
if (
this._input.isFocused() === false &&
this._input.props.isSelected === true
) {
this.focus();
if ( this._input.isFocused() === false && this.props.isSelected ) {
if ( this.isAndroid ) {
/*
* There seems to be an issue in React Native where the keyboard doesn't show if called shortly after rendering.
* As a common work around this delay is used.
* https://github.com/facebook/react-native/issues/19366#issuecomment-400603928
*/
this.timeoutID = setTimeout( () => {
this._input.focus();
}, 100 );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android TextInput has a bug where the keyboard doesn't pop when autoselecting a view in componentDidMount. to get around this a small timeout is set. This is an issue with ReactNative that seems to be opened then closed multiple times due to inactivity.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think that this is a core Android bug where you can't focus an EditText if the view isn't active, so you need to wait with a timeout 😄

} else {
this._input.focus();
}
}
}

componentDidUpdate( prevProps ) {
if ( ! this.props.isSelected && prevProps.isSelected && this.isIOS ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the hide keyboard button is selected componentDidUpdate is called with the changes of isSelected toggling from true to false the is iOS flag here was preventing android from getting the blur call to dismiss the keyboard

if ( ! this.props.isSelected && prevProps.isSelected ) {
this._input.blur();
}
}

componentWillUnmount() {
if ( this.isAndroid ) {
clearTimeout( this.timeoutID );
}
}

focus() {
this._input.focus();
}
Expand Down