aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/components/scaling_input.tsx
blob: a2c4ba3426567d6c28689d2155f2bcd77785e7ab (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
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
import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

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

import { Container, Input } from './ui';

export enum ScalingInputPhase {
    Start,
    Scaling,
    End,
}

export interface ScalingInputProps {
    startWidthCh: number;
    endWidthCh: number;
    fontSizePx: number;
    value?: string;
    onChange: (event: React.ChangeEvent<HTMLInputElement>, fontSize: number) => void;
    fontColor?: ColorOption;
    placeholder?: string;
}

export interface ScalingInputProps {
    fixedWidthInPxIfExists?: number;
}

export class ScalingInput extends React.Component<ScalingInputProps> {
    public static defaultProps = {
        onChange: util.boundNoop,
        onFontSizeChange: util.boundNoop,
    };
    public state = {
        fixedWidthInPxIfExists: undefined,
    };
    private _inputRef = React.createRef();
    public static getPhase(startWidthCh: number, endWidthCh: number, value?: string): ScalingInputPhase {
        if (_.isUndefined(value) || value.length <= startWidthCh) {
            return ScalingInputPhase.Start;
        }
        if (value.length > startWidthCh && value.length <= endWidthCh) {
            return ScalingInputPhase.Scaling;
        }
        return ScalingInputPhase.End;
    }
    public static getPhaseFromProps(props: ScalingInputProps): ScalingInputPhase {
        const { value, startWidthCh, endWidthCh } = props;
        return ScalingInput.getPhase(startWidthCh, endWidthCh, value);
    }
    public componentDidUpdate(prevProps: ScalingInputProps): void {
        const prevPhase = ScalingInput.getPhaseFromProps(prevProps);
        const curPhase = ScalingInput.getPhaseFromProps(this.props);
        // if we went from anything else to end, fix to the current width as it shouldn't change as we grow
        if (prevPhase !== ScalingInputPhase.End && curPhase === ScalingInputPhase.End) {
            this.setState({
                fixedWidthInPxIfExists: this._getInputWidthInPx(),
            });
        }
        // if we end from end to to anything else, un-fix the width
        if (prevPhase === ScalingInputPhase.End && curPhase !== ScalingInputPhase.End) {
            this.setState({
                fixedWidthInPxIfExists: undefined,
            });
        }
    }
    public render(): React.ReactNode {
        const { fontColor, onChange, placeholder, value } = this.props;
        const phase = ScalingInput.getPhaseFromProps(this.props);
        return (
            <Input
                ref={this._inputRef as any}
                fontColor={fontColor}
                onChange={this._handleChange}
                value={value}
                placeholder={placeholder}
                fontSize={`${this.props.fontSizePx}px`}
                width={this._calculateWidth()}
            />
        );
    }
    private readonly _calculateWidth = (): string => {
        const phase = ScalingInput.getPhaseFromProps(this.props);
        const { value, startWidthCh, endWidthCh } = this.props;
        if (_.isUndefined(value)) {
            return `${startWidthCh}ch`;
        }
        if (!_.isUndefined(this.state.fixedWidthInPxIfExists)) {
            return `${this.state.fixedWidthInPxIfExists}px`;
        }
        switch (phase) {
            case ScalingInputPhase.Start:
                return `${startWidthCh}ch`;
            case ScalingInputPhase.Scaling:
                return `${value.length}ch`;
            case ScalingInputPhase.End:
                return `${endWidthCh}ch`;
            default:
                return `${startWidthCh}ch`;
        }
    };
    private readonly _getInputWidthInPx = (): number => {
        const ref = this._inputRef.current;
        if (!ref) {
            return 0;
        }
        return (ref as any).getBoundingClientRect().width;
    };
    private readonly _calculateNextFontSize = (
        currentFontSizePx: number,
        value: string,
        startWidthCh: number,
        endWidthCh: number,
    ): number => {
        const phase = ScalingInput.getPhase(startWidthCh, endWidthCh, value);
        if (_.isUndefined(value) || phase !== ScalingInputPhase.End) {
            return currentFontSizePx;
        }
        const charactersOverMax = value.length - endWidthCh;
        const pixelsToReduceFontSizeBy = charactersOverMax * 5;
        const fontSize = currentFontSizePx - pixelsToReduceFontSizeBy;
        return fontSize;
    };
    private readonly _handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        const value = event.target.value;
        const { fontSizePx, startWidthCh, endWidthCh } = this.props;
        this.props.onChange(event, this._calculateNextFontSize(fontSizePx, value, startWidthCh, endWidthCh));
    };
}