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,

/** Called when the user start scrolling */
Copy link
Contributor

Choose a reason for hiding this comment

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

/** 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
73 changes: 61 additions & 12 deletions src/components/OptionsList/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,68 @@
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;
}

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, 'innerRef')}
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to omit the innerRef?

ref={this.props.innerRef}
onScroll={() => {
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 innerRef={ref} {...props} />
Copy link
Contributor

Choose a reason for hiding this comment

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

let's call it forwardedRef so it's consistent with the rest of the app

)));