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

Fix options list input unfocus #12057

Merged
merged 11 commits into from
Oct 31, 2022
5 changes: 5 additions & 0 deletions src/components/OptionsList/BaseOptionsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ const propTypes = {
/** Called when the user begins to drag the scroll view */
onScrollBeginDrag: PropTypes.func,

/** Callback executed on scroll */
onScroll: PropTypes.func,

...optionsListPropTypes,
};

const defaultProps = {
keyboardDismissMode: 'none',
onScrollBeginDrag: () => {},
onScroll: () => {},
...optionsListDefaultProps,
};

Expand Down Expand Up @@ -230,6 +234,7 @@ class BaseOptionsList extends Component {
keyboardShouldPersistTaps="always"
keyboardDismissMode={this.props.keyboardDismissMode}
onScrollBeginDrag={this.props.onScrollBeginDrag}
onScroll={this.props.onScroll}
contentContainerStyle={this.props.contentContainerStyles}
showsVerticalScrollIndicator={false}
sections={this.props.sections}
Expand Down
79 changes: 67 additions & 12 deletions src/components/OptionsList/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,74 @@
import React, {forwardRef} from 'react';
import React, {Component, forwardRef} from 'react';
import {Keyboard} from 'react-native';
import _ from 'underscore';
import BaseOptionsList from './BaseOptionsList';
import withWindowDimensions from '../withWindowDimensions';
import canUseTouchscreen from '../../libs/canUseTouchscreen';
import {propTypes, defaultProps} from './optionsListPropTypes';

const OptionsList = forwardRef((props, ref) => (
<BaseOptionsList
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={ref}
keyboardDismissMode={props.isSmallScreenWidth ? 'on-drag' : 'none'}
/>
));
class OptionsList extends Component {
constructor(props) {
super(props);

OptionsList.propTypes = propTypes;
this.userTouchStart = this.userTouchStart.bind(this);
this.userTouchEnd = this.userTouchEnd.bind(this);
mollfpr marked this conversation as resolved.
Show resolved Hide resolved
}

componentDidMount() {
if (!canUseTouchscreen()) {
return;
}

// According to https://github.com/Expensify/App/issues/11801
// `keyboardDismissMode` is dismissing the keyboard when the list is scrolled programmatically.
// In this listener we will set `isUserScreenTouched` when the user is touching the screen
// and `isUserScreenTouched` will be used to decide whether the list was scrolled by the user
// or programmatically so that the keyboard would be dismissed only when it was scrolled by the user.
mollfpr marked this conversation as resolved.
Show resolved Hide resolved
document.addEventListener('touchstart', this.userTouchStart);
document.addEventListener('touchend', this.userTouchEnd);
mollfpr marked this conversation as resolved.
Show resolved Hide resolved
}

componentWillUnmount() {
if (!canUseTouchscreen()) {
return;
}

document.removeEventListener('touchstart', this.userTouchStart);
document.removeEventListener('touchend', this.userTouchEnd);
mollfpr marked this conversation as resolved.
Show resolved Hide resolved
}

userTouchStart() {
this.isUserScreenTouched = true;
mollfpr marked this conversation as resolved.
Show resolved Hide resolved
}

userTouchEnd() {
this.isUserScreenTouched = false;
mollfpr marked this conversation as resolved.
Show resolved Hide resolved
}

render() {
return (
<BaseOptionsList
// eslint-disable-next-line react/jsx-props-no-spreading
{..._.omit(this.props, 'forwardedRef')}
ref={this.props.forwardedRef}
onScroll={() => {
// Only dismiss the keyboard whenever the user scrolls the screen
if (!this.isUserScreenTouched) {
mollfpr marked this conversation as resolved.
Show resolved Hide resolved
return;
}
Keyboard.dismiss();
}}
/>
);
}
}

OptionsList.propTypes = {
...propTypes,
};
OptionsList.defaultProps = defaultProps;
OptionsList.displayName = 'OptionsList';

export default withWindowDimensions(OptionsList);
export default withWindowDimensions(forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<OptionsList forwardedRef={ref} {...props} />
)));