-
Notifications
You must be signed in to change notification settings - Fork 3k
/
PressableWithoutFocus.js
68 lines (57 loc) · 2.13 KB
/
PressableWithoutFocus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from 'react';
import _ from 'underscore';
import PropTypes from 'prop-types';
import GenericPressable from './GenericPressable';
import genericPressablePropTypes from './GenericPressable/PropTypes';
const propTypes = {
/** Element that should be clickable */
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
/** Callback for onPress event */
onPress: PropTypes.func.isRequired,
/** Callback for onLongPress event */
onLongPress: PropTypes.func,
/** Styles that should be passed to touchable container */
// eslint-disable-next-line react/forbid-prop-types
style: PropTypes.arrayOf(PropTypes.object),
/** Proptypes of pressable component used for implementation */
...genericPressablePropTypes.pressablePropTypes,
};
const defaultProps = {
style: [],
onLongPress: undefined,
};
/**
* This component prevents the tapped element from capturing focus.
* We need to blur this element when clicked as it opens modal that implements focus-trapping.
* When the modal is closed it focuses back to the last active element.
* Therefore it shifts the element to bring it back to focus.
* https://github.com/Expensify/App/issues/6806
*/
class PressableWithoutFocus extends React.Component {
constructor(props) {
super(props);
this.pressAndBlur = this.pressAndBlur.bind(this);
}
pressAndBlur() {
this.pressableRef.blur();
this.props.onPress();
}
render() {
const restProps = _.omit(this.props, ['children', 'onPress', 'onLongPress', 'style']);
return (
<GenericPressable
onPress={this.pressAndBlur}
onLongPress={this.props.onLongPress}
ref={(el) => (this.pressableRef = el)}
style={this.props.style}
// eslint-disable-next-line react/jsx-props-no-spreading
{...restProps}
>
{this.props.children}
</GenericPressable>
);
}
}
PressableWithoutFocus.propTypes = propTypes;
PressableWithoutFocus.defaultProps = defaultProps;
export default PressableWithoutFocus;