aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/components/scaling_amount_input.tsx
blob: 655ae2b749dcc56c9fb10576a81a43273446e182 (plain) (blame)
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
import * as _ from 'lodash';
import * as React from 'react';

import { ColorOption } from '../style/theme';
import { BigNumberInput } from '../util/big_number_input';
import { util } from '../util/util';

import { ScalingInput } from './scaling_input';

export interface ScalingAmountInputProps {
    maxFontSizePx: number;
    textLengthThreshold: number;
    fontColor?: ColorOption;
    value?: BigNumberInput;
    onChange: (value?: BigNumberInput) => void;
    onFontSizeChange: (fontSizePx: number) => void;
}

export class ScalingAmountInput extends React.Component<ScalingAmountInputProps> {
    public static defaultProps = {
        onChange: util.boundNoop,
        onFontSizeChange: util.boundNoop,
    };
    public render(): React.ReactNode {
        const { textLengthThreshold, fontColor, maxFontSizePx, value, onFontSizeChange } = this.props;
        return (
            <ScalingInput
                maxFontSizePx={maxFontSizePx}
                textLengthThreshold={textLengthThreshold}
                onFontSizeChange={onFontSizeChange}
                fontColor={fontColor}
                onChange={this._handleChange}
                value={!_.isUndefined(value) ? value.toDisplayString() : ''}
                placeholder="0.00"
                emptyInputWidthCh={3.5}
            />
        );
    }
    private readonly _handleChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
        const value = event.target.value;
        let bigNumberValue;
        if (!_.isEmpty(value)) {
            try {
                bigNumberValue = new BigNumberInput(value);
            } catch {
                // We don't want to allow values that can't be a BigNumber, so don't even call onChange.
                return;
            }
        }
        this.props.onChange(bigNumberValue);
    };
}