-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BigNumberPad.tsx
104 lines (88 loc) · 3.67 KB
/
BigNumberPad.tsx
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
import React, {useState} from 'react';
import {View} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import ControlSelection from '@libs/ControlSelection';
import Button from './Button';
type BigNumberPadProps = {
/** Callback to inform parent modal with key pressed */
numberPressed: (key: string) => void;
/** Callback to inform parent modal whether user is long pressing the "<" (backspace) button */
longPressHandlerStateChanged?: (isUserLongPressingBackspace: boolean) => void;
/** Used to locate this view from native classes. */
id?: string;
/** Whether long press is disabled */
isLongPressDisabled?: boolean;
};
const padNumbers = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['.', '0', '<'],
] as const;
function BigNumberPad({numberPressed, longPressHandlerStateChanged = () => {}, id = 'numPadView', isLongPressDisabled = false}: BigNumberPadProps) {
const {toLocaleDigit} = useLocalize();
const styles = useThemeStyles();
const [timer, setTimer] = useState<NodeJS.Timeout | null>(null);
const {isExtraSmallScreenHeight} = useWindowDimensions();
/**
* Handle long press key on number pad.
* Only handles the '<' key and starts the continuous input timer.
*/
const handleLongPress = (key: string) => {
if (key !== '<') {
return;
}
longPressHandlerStateChanged(true);
const newTimer = setInterval(() => {
numberPressed(key);
}, 100);
setTimer(newTimer);
};
return (
<View
style={[styles.flexColumn, styles.w100]}
id={id}
>
{padNumbers.map((row) => (
<View
key={`NumberPadRow-${row[0]}`}
style={[styles.flexRow, styles.mt3]}
>
{row.map((column, columnIndex) => {
// Adding margin between buttons except first column to
// avoid unccessary space before the first column.
const marginLeft = columnIndex > 0 ? styles.ml3 : {};
return (
<Button
key={column}
medium={isExtraSmallScreenHeight}
large={!isExtraSmallScreenHeight}
shouldEnableHapticFeedback
style={[styles.flex1, marginLeft]}
text={column === '<' ? column : toLocaleDigit(column)}
onLongPress={() => handleLongPress(column)}
onPress={() => numberPressed(column)}
onPressIn={ControlSelection.block}
onPressOut={() => {
if (timer) {
clearInterval(timer);
}
ControlSelection.unblock();
longPressHandlerStateChanged(false);
}}
onMouseDown={(e) => {
e.preventDefault();
}}
isLongPressDisabled={isLongPressDisabled}
/>
);
})}
</View>
))}
</View>
);
}
BigNumberPad.displayName = 'BigNumberPad';
export default BigNumberPad;