aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src
diff options
context:
space:
mode:
authorSteve Klebanoff <steve.klebanoff@gmail.com>2018-11-02 08:29:59 +0800
committerSteve Klebanoff <steve.klebanoff@gmail.com>2018-11-02 08:29:59 +0800
commitf5623632d86504b4081ff6d102c2f9468e2dfa0d (patch)
tree0a8fa76c17ea5228ceb63a96608e0d7c67b11235 /packages/instant/src
parenta2e1bf0e6287e86c226b0801311f6f4498c893ed (diff)
downloaddexon-sol-tools-f5623632d86504b4081ff6d102c2f9468e2dfa0d.tar.gz
dexon-sol-tools-f5623632d86504b4081ff6d102c2f9468e2dfa0d.tar.zst
dexon-sol-tools-f5623632d86504b4081ff6d102c2f9468e2dfa0d.zip
Have ScalingAmountInput trigger onChange with MaybeBigNumber and keep string value as state
Diffstat (limited to 'packages/instant/src')
-rw-r--r--packages/instant/src/components/scaling_amount_input.tsx64
-rw-r--r--packages/instant/src/types.ts2
2 files changed, 54 insertions, 12 deletions
diff --git a/packages/instant/src/components/scaling_amount_input.tsx b/packages/instant/src/components/scaling_amount_input.tsx
index 4b046e8da..5d06c5242 100644
--- a/packages/instant/src/components/scaling_amount_input.tsx
+++ b/packages/instant/src/components/scaling_amount_input.tsx
@@ -3,6 +3,7 @@ import * as _ from 'lodash';
import * as React from 'react';
import { ColorOption } from '../style/theme';
+import { MaybeBigNumber } from '../types';
import { util } from '../util/util';
import { ScalingInput } from './scaling_input';
@@ -16,13 +17,52 @@ export interface ScalingAmountInputProps {
onChange: (value?: BigNumber) => void;
onFontSizeChange: (fontSizePx: number) => void;
}
+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;
+};
-export class ScalingAmountInput extends React.Component<ScalingAmountInputProps> {
+const areMaybeBigNumbersEqual = (val1: MaybeBigNumber, val2: MaybeBigNumber): boolean => {
+ if (!_.isUndefined(val1) && !_.isUndefined(val2)) {
+ return val1.equals(val2);
+ }
+ return _.isUndefined(val1) && _.isUndefined(val2);
+};
+
+export class ScalingAmountInput extends React.Component<ScalingAmountInputProps, ScalingAmountInputState> {
public static defaultProps = {
onChange: util.boundNoop,
onFontSizeChange: util.boundNoop,
isDisabled: false,
};
+ public constructor(props: ScalingAmountInputProps) {
+ super(props);
+ this.state = {
+ stringValue: _.isUndefined(props.value) ? '' : props.value.toString(),
+ };
+ }
+ public componentDidUpdate(prevProps: ScalingAmountInputProps): void {
+ const parsedStateValue = stringToMaybeBigNumber(this.state.stringValue);
+ const currentValue = this.props.value;
+
+ 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
+ this.setState({
+ stringValue: _.isUndefined(currentValue) ? '' : currentValue.toString(),
+ });
+ }
+ }
+
public render(): React.ReactNode {
const { textLengthThreshold, fontColor, maxFontSizePx, value, onFontSizeChange } = this.props;
return (
@@ -32,7 +72,7 @@ export class ScalingAmountInput extends React.Component<ScalingAmountInputProps>
onFontSizeChange={onFontSizeChange}
fontColor={fontColor}
onChange={this._handleChange}
- value={!_.isUndefined(value) ? value.toString() : ''}
+ value={this.state.stringValue}
placeholder="0.00"
emptyInputWidthCh={3.5}
isDisabled={this.props.isDisabled}
@@ -40,16 +80,16 @@ export class ScalingAmountInput extends React.Component<ScalingAmountInputProps>
);
}
private readonly _handleChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
- const value = event.target.value;
- let bigNumberValue;
- if (!_.isEmpty(value)) {
- try {
- bigNumberValue = new BigNumber(value);
- } catch {
- // We don't want to allow values that can't be a BigNumber, so don't even call onChange.
- return;
- }
- }
+ const sanitizedValue = event.target.value.replace(/[^0-9.]/g, ''); // only allow numbers and "."
+ this.setState({
+ stringValue: sanitizedValue,
+ });
+
+ // Trigger onChange with a valid BigNumber, or undefined if the sanitizedValue is invalid or empty
+ const bigNumberValue: MaybeBigNumber = _.isEmpty(sanitizedValue)
+ ? undefined
+ : stringToMaybeBigNumber(sanitizedValue);
+
this.props.onChange(bigNumberValue);
};
}
diff --git a/packages/instant/src/types.ts b/packages/instant/src/types.ts
index 336465e43..e92e96e9a 100644
--- a/packages/instant/src/types.ts
+++ b/packages/instant/src/types.ts
@@ -1,6 +1,8 @@
import { AssetProxyId, ObjectMap } from '@0x/types';
+import { BigNumber } from '@0x/utils';
// Reusable
+export type MaybeBigNumber = BigNumber | undefined;
export enum AsyncProcessState {
NONE = 'None',
PENDING = 'Pending',