-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathIOUAmountPage.js
executable file
·258 lines (226 loc) · 8.46 KB
/
IOUAmountPage.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import React from 'react';
import {
View,
InteractionManager,
} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import _ from 'underscore';
import ONYXKEYS from '../../../ONYXKEYS';
import styles from '../../../styles/styles';
import BigNumberPad from '../../../components/BigNumberPad';
import Navigation from '../../../libs/Navigation/Navigation';
import ROUTES from '../../../ROUTES';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import compose from '../../../libs/compose';
import Button from '../../../components/Button';
import CONST from '../../../CONST';
import canUseTouchScreen from '../../../libs/canUseTouchscreen';
import TextInputWithCurrencySymbol from '../../../components/TextInputWithCurrencySymbol';
const propTypes = {
/** Whether or not this IOU has multiple participants */
hasMultipleParticipants: PropTypes.bool.isRequired,
/** The ID of the report this screen should display */
reportID: PropTypes.string.isRequired,
/** Callback to inform parent modal of success */
onStepComplete: PropTypes.func.isRequired,
/** Previously selected amount to show if the user comes back to this screen */
selectedAmount: PropTypes.string.isRequired,
/* Onyx Props */
/** Holds data related to IOU view state, rather than the underlying IOU data. */
iou: PropTypes.shape({
/** Whether or not the IOU step is loading (retrieving users preferred currency) */
loading: PropTypes.bool,
/** Selected Currency Code of the current IOU */
selectedCurrencyCode: PropTypes.string,
}),
...withLocalizePropTypes,
};
const defaultProps = {
iou: {
selectedCurrencyCode: CONST.CURRENCY.USD,
},
};
class IOUAmountPage extends React.Component {
constructor(props) {
super(props);
this.updateAmountNumberPad = this.updateAmountNumberPad.bind(this);
this.updateAmount = this.updateAmount.bind(this);
this.stripCommaFromAmount = this.stripCommaFromAmount.bind(this);
this.focusTextInput = this.focusTextInput.bind(this);
this.navigateToCurrencySelectionPage = this.navigateToCurrencySelectionPage.bind(this);
this.state = {
amount: props.selectedAmount,
};
}
componentDidMount() {
this.focusTextInput();
}
componentDidUpdate(prevProps) {
if (this.props.iou.selectedCurrencyCode === prevProps.iou.selectedCurrencyCode) {
return;
}
this.focusTextInput();
}
/**
* Focus text input
*/
focusTextInput() {
// Component may not initialized due to navigation transitions
// Wait until interactions are complete before trying to focus
InteractionManager.runAfterInteractions(() => {
// Focus text input
if (!this.textInput) {
return;
}
this.textInput.focus();
});
}
/**
* @param {String} amount
* @returns {Number}
*/
calculateAmountLength(amount) {
const leadingZeroes = amount.match(/^0+/);
const leadingZeroesLength = lodashGet(leadingZeroes, '[0].length', 0);
const absAmount = parseFloat((amount * 100).toFixed(2)).toString();
/*
Return the sum of leading zeroes length and absolute amount length(including fraction digits).
When the absolute amount is 0, add 2 to the leading zeroes length to represent fraction digits.
*/
return leadingZeroesLength + (absAmount === '0' ? 2 : absAmount.length);
}
/**
* Check if amount is a decimal upto 3 digits
*
* @param {String} amount
* @returns {Boolean}
*/
validateAmount(amount) {
const decimalNumberRegex = new RegExp(/^\d+(,\d+)*(\.\d{0,2})?$/, 'i');
return amount === '' || (decimalNumberRegex.test(amount) && this.calculateAmountLength(amount) <= CONST.IOU.AMOUNT_MAX_LENGTH);
}
/**
* Strip comma from the amount
*
* @param {String} amount
* @returns {String}
*/
stripCommaFromAmount(amount) {
return amount.replace(/,/g, '');
}
/**
* Update amount with number or Backspace pressed for BigNumberPad.
* Validate new amount with decimal number regex up to 6 digits and 2 decimal digit to enable Next button
*
* @param {String} key
*/
updateAmountNumberPad(key) {
// Backspace button is pressed
if (key === '<' || key === 'Backspace') {
if (this.state.amount.length > 0) {
this.setState(prevState => ({
amount: prevState.amount.slice(0, -1),
}));
}
return;
}
this.setState((prevState) => {
const amount = `${prevState.amount}${key}`;
return this.validateAmount(amount) ? {amount: this.stripCommaFromAmount(amount)} : prevState;
});
}
/**
* Update amount on amount change
* Validate new amount with decimal number regex up to 6 digits and 2 decimal digit
*
* @param {String} text - Changed text from user input
*/
updateAmount(text) {
this.setState((prevState) => {
const amount = this.replaceAllDigits(text, this.props.fromLocaleDigit);
return this.validateAmount(amount)
? {amount: this.stripCommaFromAmount(amount)}
: prevState;
});
}
/**
* Replaces each character by calling `convertFn`. If `convertFn` throws an error, then
* the original character will be preserved.
*
* @param {String} text
* @param {Function} convertFn - `this.props.fromLocaleDigit` or `this.props.toLocaleDigit`
* @returns {String}
*/
replaceAllDigits(text, convertFn) {
return _.chain([...text])
.map((char) => {
try {
return convertFn(char);
} catch {
return char;
}
})
.join('')
.value();
}
navigateToCurrencySelectionPage() {
if (this.props.hasMultipleParticipants) {
return Navigation.navigate(ROUTES.getIouBillCurrencyRoute(this.props.reportID));
}
if (this.props.iouType === CONST.IOU.IOU_TYPE.SEND) {
return Navigation.navigate(ROUTES.getIouSendCurrencyRoute(this.props.reportID));
}
return Navigation.navigate(ROUTES.getIouRequestCurrencyRoute(this.props.reportID));
}
render() {
const formattedAmount = this.replaceAllDigits(this.state.amount, this.props.toLocaleDigit);
return (
<>
<View style={[
styles.flex1,
styles.flexRow,
styles.w100,
styles.alignItemsCenter,
styles.justifyContentCenter,
]}
>
<TextInputWithCurrencySymbol
formattedAmount={formattedAmount}
onChangeAmount={this.updateAmount}
onCurrencyButtonPress={this.navigateToCurrencySelectionPage}
placeholder={this.props.numberFormat(0)}
preferredLocale={this.props.preferredLocale}
ref={el => this.textInput = el}
selectedCurrencyCode={this.props.iou.selectedCurrencyCode}
/>
</View>
<View style={[styles.w100, styles.justifyContentEnd]}>
{canUseTouchScreen()
? (
<BigNumberPad
numberPressed={this.updateAmountNumberPad}
/>
) : <View />}
<Button
success
style={[styles.w100, styles.mt5]}
onPress={() => this.props.onStepComplete(this.state.amount)}
pressOnEnter
isDisabled={!this.state.amount.length || parseFloat(this.state.amount) < 0.01}
text={this.props.translate('common.next')}
/>
</View>
</>
);
}
}
IOUAmountPage.propTypes = propTypes;
IOUAmountPage.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
iou: {key: ONYXKEYS.IOU},
}),
)(IOUAmountPage);