aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--packages/instant/src/components/scaling_amount_input.tsx21
-rw-r--r--packages/instant/src/util/maybe_big_number.ts25
2 files changed, 29 insertions, 17 deletions
diff --git a/packages/instant/src/components/scaling_amount_input.tsx b/packages/instant/src/components/scaling_amount_input.tsx
index 5d06c5242..a30c64b3a 100644
--- a/packages/instant/src/components/scaling_amount_input.tsx
+++ b/packages/instant/src/components/scaling_amount_input.tsx
@@ -4,6 +4,7 @@ import * as React from 'react';
import { ColorOption } from '../style/theme';
import { MaybeBigNumber } from '../types';
+import { maybeBigNumberUtil } from '../util/maybe_big_number';
import { util } from '../util/util';
import { ScalingInput } from './scaling_input';
@@ -21,23 +22,7 @@ interface ScalingAmountInputState {
stringValue: string;
}
-const stringToMaybeBigNumber = (stringValue: string): MaybeBigNumber => {
- let maybeBigNumber: MaybeBigNumber;
- try {
- maybeBigNumber = new BigNumber(stringValue);
- } catch {
- maybeBigNumber = undefined;
- }
- return _.isNaN(maybeBigNumber) ? undefined : maybeBigNumber;
-};
-
-const areMaybeBigNumbersEqual = (val1: MaybeBigNumber, val2: MaybeBigNumber): boolean => {
- if (!_.isUndefined(val1) && !_.isUndefined(val2)) {
- return val1.equals(val2);
- }
- return _.isUndefined(val1) && _.isUndefined(val2);
-};
-
+const { stringToMaybeBigNumber, areMaybeBigNumbersEqual } = maybeBigNumberUtil;
export class ScalingAmountInput extends React.Component<ScalingAmountInputProps, ScalingAmountInputState> {
public static defaultProps = {
onChange: util.boundNoop,
@@ -57,6 +42,8 @@ export class ScalingAmountInput extends React.Component<ScalingAmountInputProps,
if (!areMaybeBigNumbersEqual(parsedStateValue, currentValue)) {
// we somehow got into the state in which the value passed in and the string value
// in state have differed, reset state
+ // we dont expect to ever get into this state, but let's make sure
+ // we reset if we do since we're dealing with important numbers
this.setState({
stringValue: _.isUndefined(currentValue) ? '' : currentValue.toString(),
});
diff --git a/packages/instant/src/util/maybe_big_number.ts b/packages/instant/src/util/maybe_big_number.ts
new file mode 100644
index 000000000..113ba552f
--- /dev/null
+++ b/packages/instant/src/util/maybe_big_number.ts
@@ -0,0 +1,25 @@
+import { BigNumber } from '@0x/utils';
+import * as _ from 'lodash';
+
+import { MaybeBigNumber } from '../types';
+
+export const maybeBigNumberUtil = {
+ // converts a string to a MaybeBigNumber
+ // if string is a NaN, considered undefined
+ stringToMaybeBigNumber: (stringValue: string): MaybeBigNumber => {
+ let validBigNumber: BigNumber;
+ try {
+ validBigNumber = new BigNumber(stringValue);
+ } catch {
+ return undefined;
+ }
+
+ return validBigNumber.isNaN() ? undefined : validBigNumber;
+ },
+ areMaybeBigNumbersEqual: (val1: MaybeBigNumber, val2: MaybeBigNumber): boolean => {
+ if (!_.isUndefined(val1) && !_.isUndefined(val2)) {
+ return val1.equals(val2);
+ }
+ return _.isUndefined(val1) && _.isUndefined(val2);
+ },
+};