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
|
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;
maxFontSizePx: number;
value?: string;
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
onFontSizeChange: (fontSizePx: number) => void;
fontColor?: ColorOption;
placeholder?: string;
maxLength?: number;
}
export interface ScalingInputState {
fixedWidthInPxIfExists?: number;
}
export interface ScalingInputSnapshot {
inputWidthPx: number;
}
// This is a magic number that was determined experimentally.
const percentageToReduceByPerCharacter = 0.18;
export class ScalingInput extends React.Component<ScalingInputProps, ScalingInputState> {
public static defaultProps = {
onChange: util.boundNoop,
onFontSizeChange: util.boundNoop,
maxLength: 9,
};
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 static calculateFontSize(
endWidthCh: number,
maxFontSizePx: number,
phase: ScalingInputPhase,
value?: string,
): number {
if (_.isUndefined(value) || phase !== ScalingInputPhase.End) {
return maxFontSizePx;
}
const charactersOverMax = value.length - endWidthCh;
const scalingFactor = (1 - percentageToReduceByPerCharacter) ** charactersOverMax;
const fontSize = scalingFactor * maxFontSizePx;
return fontSize;
}
public static calculateFontSizeFromProps(props: ScalingInputProps, phase: ScalingInputPhase): number {
const { endWidthCh, value, maxFontSizePx } = props;
return ScalingInput.calculateFontSize(endWidthCh, maxFontSizePx, phase, value);
}
public getSnapshotBeforeUpdate(): ScalingInputSnapshot {
return {
inputWidthPx: this._getInputWidthInPx(),
};
}
public componentDidUpdate(
prevProps: ScalingInputProps,
prevState: ScalingInputState,
snapshot: ScalingInputSnapshot,
): void {
const prevPhase = ScalingInput.getPhaseFromProps(prevProps);
const curPhase = ScalingInput.getPhaseFromProps(this.props);
const prevFontSize = ScalingInput.calculateFontSizeFromProps(prevProps, prevPhase);
const curFontSize = ScalingInput.calculateFontSizeFromProps(this.props, curPhase);
// 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: snapshot.inputWidthPx,
});
}
// if we end from end to to anything else, un-fix the width
if (prevPhase === ScalingInputPhase.End && curPhase !== ScalingInputPhase.End) {
this.setState({
fixedWidthInPxIfExists: undefined,
});
}
// If font size has changed, notify.
if (prevFontSize !== curFontSize) {
this.props.onFontSizeChange(curFontSize);
}
}
public render(): React.ReactNode {
const { fontColor, onChange, placeholder, value, maxLength } = this.props;
const phase = ScalingInput.getPhaseFromProps(this.props);
return (
<Input
ref={this._inputRef as any}
fontColor={fontColor}
onChange={onChange}
value={value}
placeholder={placeholder}
fontSize={`${this._calculateFontSize(phase)}px`}
width={this._calculateWidth(phase)}
maxLength={maxLength}
/>
);
}
private readonly _calculateWidth = (phase: ScalingInputPhase): string => {
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 _calculateFontSize = (phase: ScalingInputPhase): number => {
return ScalingInput.calculateFontSizeFromProps(this.props, phase);
};
private readonly _getInputWidthInPx = (): number => {
const ref = this._inputRef.current;
if (!ref) {
return 0;
}
return (ref as any).getBoundingClientRect().width;
};
}
|