-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseGenericPressable.js
176 lines (157 loc) · 6.13 KB
/
BaseGenericPressable.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import React, {useCallback, useEffect, useState, useMemo, forwardRef} from 'react';
import {Pressable} from 'react-native';
import _ from 'underscore';
import Accessibility from '../../../libs/Accessibility';
import HapticFeedback from '../../../libs/HapticFeedback';
import KeyboardShortcut from '../../../libs/KeyboardShortcut';
import styles from '../../../styles/styles';
import genericPressablePropTypes from './PropTypes';
import CONST from '../../../CONST';
import * as StyleUtils from '../../../styles/StyleUtils';
/**
* Returns the cursor style based on the state of Pressable
* @param {Boolean} isDisabled
* @param {Boolean} isText
* @returns {Object}
*/
const getCursorStyle = (isDisabled, isText) => {
if (isDisabled) {
return styles.cursorDisabled;
}
if (isText) {
return styles.cursorText;
}
return styles.cursorPointer;
};
const GenericPressable = forwardRef((props, ref) => {
const {
children,
onPress,
onLongPress,
onKeyPress,
disabled,
style,
shouldUseHapticsOnLongPress,
shouldUseHapticsOnPress,
nextFocusRef,
keyboardShortcut,
shouldUseAutoHitSlop,
enableInScreenReaderStates,
onPressIn,
onPressOut,
...rest
} = props;
const isScreenReaderActive = Accessibility.useScreenReaderStatus();
const [hitSlop, onLayout] = Accessibility.useAutoHitSlop();
const isDisabled = useMemo(() => {
let shouldBeDisabledByScreenReader = false;
if (enableInScreenReaderStates === CONST.SCREEN_READER_STATES.ACTIVE) {
shouldBeDisabledByScreenReader = !isScreenReaderActive;
}
if (enableInScreenReaderStates === CONST.SCREEN_READER_STATES.DISABLED) {
shouldBeDisabledByScreenReader = isScreenReaderActive;
}
return props.disabled || shouldBeDisabledByScreenReader;
}, [isScreenReaderActive, enableInScreenReaderStates, props.disabled]);
const [shouldUseDisabledCursor, setShouldUseDisabledCursor] = useState(isDisabled);
const onLongPressHandler = useCallback(
(event) => {
if (isDisabled) {
return;
}
if (!onLongPress) {
return;
}
if (shouldUseHapticsOnLongPress) {
HapticFeedback.longPress();
}
if (ref && ref.current) {
ref.current.blur();
}
onLongPress(event);
Accessibility.moveAccessibilityFocus(nextFocusRef);
},
[shouldUseHapticsOnLongPress, onLongPress, nextFocusRef, ref, isDisabled],
);
const onPressHandler = useCallback(
(event) => {
if (isDisabled) {
return;
}
if (!onPress) {
return;
}
if (shouldUseHapticsOnPress) {
HapticFeedback.press();
}
if (ref && ref.current) {
ref.current.blur();
}
onPress(event);
Accessibility.moveAccessibilityFocus(nextFocusRef);
},
[shouldUseHapticsOnPress, onPress, nextFocusRef, ref, isDisabled],
);
const onKeyPressHandler = useCallback(
(event) => {
if (event.key !== 'Enter') {
return;
}
onPressHandler(event);
},
[onPressHandler],
);
useEffect(() => {
if (isDisabled) {
const timer = setTimeout(() => setShouldUseDisabledCursor(true), 1000);
return () => clearTimeout(timer);
}
setShouldUseDisabledCursor(false);
}, [isDisabled]);
useEffect(() => {
if (!keyboardShortcut) {
return () => {};
}
const {shortcutKey, descriptionKey, modifiers} = keyboardShortcut;
return KeyboardShortcut.subscribe(shortcutKey, onPressHandler, descriptionKey, modifiers, true, false, 0, false);
}, [keyboardShortcut, onPressHandler]);
return (
<Pressable
hitSlop={shouldUseAutoHitSlop ? hitSlop : undefined}
onLayout={onLayout}
ref={ref}
onPress={!isDisabled ? onPressHandler : undefined}
onLongPress={!isDisabled && onLongPress ? onLongPressHandler : undefined}
onKeyPress={!isDisabled ? onKeyPressHandler : undefined}
onPressIn={!isDisabled ? onPressIn : undefined}
onPressOut={!isDisabled ? onPressOut : undefined}
style={(state) => [
getCursorStyle(shouldUseDisabledCursor, [props.accessibilityRole, props.role].includes('text')),
StyleUtils.parseStyleFromFunction(props.style, state),
isScreenReaderActive && StyleUtils.parseStyleFromFunction(props.screenReaderActiveStyle, state),
state.focused && StyleUtils.parseStyleFromFunction(props.focusStyle, state),
state.hovered && StyleUtils.parseStyleFromFunction(props.hoverStyle, state),
state.pressed && StyleUtils.parseStyleFromFunction(props.pressStyle, state),
isDisabled && [...StyleUtils.parseStyleFromFunction(props.disabledStyle, state), styles.noSelect],
]}
// accessibility props
accessibilityState={{
disabled: isDisabled,
...props.accessibilityState,
}}
aria-disabled={isDisabled}
aria-keyshortcuts={keyboardShortcut && `${keyboardShortcut.modifiers}+${keyboardShortcut.shortcutKey}`}
// ios-only form of inputs
onMagicTap={!isDisabled && onPressHandler}
onAccessibilityTap={!isDisabled && onPressHandler}
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
>
{(state) => (_.isFunction(props.children) ? props.children({...state, isScreenReaderActive, isDisabled}) : props.children)}
</Pressable>
);
});
GenericPressable.displayName = 'GenericPressable';
GenericPressable.propTypes = genericPressablePropTypes.pressablePropTypes;
GenericPressable.defaultProps = genericPressablePropTypes.defaultProps;
export default GenericPressable;