-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextInput.jsx
408 lines (376 loc) · 9.86 KB
/
TextInput.jsx
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* TEAM: frontend_infra
* WATCHERS: uforic
* @flow strict
*/
import {StyleSheet, css} from "aphrodite";
import * as React from "react";
import InputGroupContext, {
CENTER_INPUT,
LEFT_INPUT,
RIGHT_INPUT,
} from "./context/InputGroupContext";
import latitudeColors from "./colors";
import {LabelContext} from "./Label";
import IconButton from "./button/IconButton";
import Icon, {type IconNames, type StandardIconSizes} from "./Icon";
import {type Size} from "./sizes";
import {
getInputStyles,
HORIZONTAL_INPUT_PADDING_BASE_PX,
HORIZONTAL_INPUT_PADDING_LARGE_PX,
HORIZONTAL_INPUT_PADDING_SMALL_PX,
} from "./styles/input";
import iconSizes from "./iconSizes";
import {getWidthOfText} from "./styles/fontWidths";
import {typeScale} from "./styles/typography";
export type TextAlignment = "left" | "right" | "center";
export type TextInputType = "text" | "password" | "email";
export type InputPrefixSuffix =
| ?string
| {|iconName: IconNames, onClick?: () => void, onFocus?: () => void|};
export type TextInputProps = {|
/** note that this must be a string, and cannot be null or undefined. */
+value: string,
/** the placeholder text that will be displayed when the input is empty */
+placeholder: string,
/** whether the input is disabled or not */
+disabled: boolean,
/** whether the input is readonly or not */
+readOnly: boolean,
/** whether the input is invalid or not */
+isInvalid: boolean,
/** whether the input is prefilled or not */
+isPrefilled: boolean,
/** whether the input should show ellipsis and a tooltip on overflow */
+textOverflow?: "clip" | "ellipsis",
/** the size of the input */
+size: Size,
/** forwards a ref to the inputfield of the input */
+inputRef?: ((HTMLElement | null) => void) | {|current: ?HTMLElement|},
/** called when the value of the textinput changes */
+onChange: string => void,
/** called on the mouseDown event */
+onMouseDown?: Event => void,
/** called when the keydown event is trigged on the input */
+onClick?: Event => void,
/** called when the input is focused */
+onFocus?: Event => void,
/** called when the input is blurred */
+onBlur?: Event => void,
/** called when the input is clicked */
+onKeyDown?: KeyboardEvent => void,
/** most Flexport forms do not use the browser FormData API, which requires
* named inputs; only use this if you need the form data API to work. */
+name?: string,
/** sets the alignment of the text in the input field */
+textAlign: TextAlignment,
/** the HTML type of this text input, see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input for a complete list */
+type: TextInputType,
/** the maximum number of characters the textinput will accept */
+maxLength?: number,
/** prefix sigil that will appear within and at the start of the input field */
+prefix?: InputPrefixSuffix,
/** suffix sigil that will appear within and at the end of the input field */
+suffix?: InputPrefixSuffix,
|};
type SigilProps = {|
+side: "left" | "right",
+paddingDefaultTextInput: number,
+sigil: InputPrefixSuffix,
+size: Size,
+disabled: boolean,
|};
/**
* Sigil returns the appropriate prefix/suffix label based on the type of context obj
*/
function Sigil({
side,
paddingDefaultTextInput,
sigil,
size,
disabled,
}: SigilProps) {
let innerContent = null;
if (sigil == null) {
return null;
}
if (typeof sigil === "string") {
innerContent = (
<span
className={css(
styles.sigilText,
disabled ? styles.sigilTextDisabled : undefined
)}
style={{
...getTextTypeScale(size),
}}
>
{sigil}
</span>
);
} else if (sigil.onClick) {
innerContent = (
<IconButton
{...sigil}
disabled={disabled}
kind="blank"
intent="none"
type="button"
size={size}
/>
);
} else {
innerContent = (
<Icon
iconName={sigil.iconName}
size={getIconSizeFromTextInputSize(size)}
color="grey50"
/>
);
}
return (
<span
className={css(styles.sigil)}
style={{
[(side: string)]: `${paddingDefaultTextInput}px`,
...(typeof sigil === "string" || !sigil.onClick
? {
pointerEvents: "none",
}
: {}),
}}
>
{innerContent}
</span>
);
}
/**
* @category Data Entry
* @short Collect simple text input from the user.
* @brandStatus V2
* @status Stable
* You may want to use TextInput with InputError or TextInputPrefixSuffix, decorators that
* modify the look and feel of TextInput. If you need a multiline input, think about using
* TextareaInput.
*/
function TextInput(props: TextInputProps) {
const labelContext = React.useContext(LabelContext);
const inputGroupContext = React.useContext(InputGroupContext);
const handleFocus = (event: Event) => {
if (labelContext.labelOnFocus) {
labelContext.labelOnFocus();
}
if (props.onFocus) {
props.onFocus(event);
}
};
const handleBlur = (event: Event) => {
if (labelContext.labelOnBlur) {
labelContext.labelOnBlur();
}
if (props.onBlur) {
props.onBlur(event);
}
};
const {
size,
disabled,
isInvalid,
isPrefilled,
textOverflow = "clip",
onKeyDown,
onChange,
onMouseDown,
placeholder,
textAlign,
name,
value,
maxLength,
onClick,
readOnly,
prefix = null,
suffix = null,
} = props;
const paddingDefaultTextInput = getPaddingFromTextInputSize(size);
let prefixPadding = paddingDefaultTextInput;
let prefixSigil = null;
if (prefix !== null) {
prefixPadding = paddingDefaultTextInput * 2 + getWidth(prefix, size);
prefixSigil = (
<Sigil
side="left"
sigil={prefix}
size={size}
paddingDefaultTextInput={paddingDefaultTextInput}
disabled={disabled}
/>
);
}
let suffixPadding = paddingDefaultTextInput;
let suffixSigil = null;
if (suffix !== null) {
suffixPadding = paddingDefaultTextInput * 2 + getWidth(suffix, size);
suffixSigil = (
<Sigil
side="right"
sigil={suffix}
size={size}
paddingDefaultTextInput={paddingDefaultTextInput}
disabled={disabled}
/>
);
}
const textFieldStyles = {
textAlign,
paddingLeft: prefixPadding,
paddingRight: suffixPadding,
};
const inputStyle = css(
...getInputStyles({
size,
readOnly,
disabled,
isInvalid,
isPrefilled,
applyEllipsis: textOverflow === "ellipsis",
noPadding: true,
}),
inputGroupContext === CENTER_INPUT && styles.noBorders,
inputGroupContext === LEFT_INPUT && styles.noRightBorders,
inputGroupContext === RIGHT_INPUT && styles.noLeftBorders
);
const passwordAtts =
props.type === "password"
? {autoComplete: "off", "data-sensitve": true}
: undefined;
return (
<div className={css(styles.wrapper)}>
<input
className={inputStyle}
disabled={disabled}
onKeyDown={onKeyDown}
onFocus={handleFocus}
onBlur={handleBlur}
onChange={evt => onChange(evt.target.value)}
onMouseDown={onMouseDown}
ref={props.inputRef}
onClick={onClick}
type={props.type}
placeholder={placeholder}
value={value}
name={name}
maxLength={maxLength}
style={textFieldStyles}
readOnly={readOnly}
{...passwordAtts}
/>
{prefixSigil}
{suffixSigil}
</div>
);
}
TextInput.defaultProps = {
disabled: false,
placeholder: "",
size: "m",
isPrefilled: false,
isInvalid: false,
textAlign: "left",
maxLength: 255,
readOnly: false,
type: "text",
};
export default TextInput;
/**
* getWidth returns the appropriate width for text or icon depending on the text, icon name and size we provide since icons and text varies in size
*/
const getWidth = (sigil: InputPrefixSuffix, size: Size) => {
switch (typeof sigil) {
case "string": {
return getWidthOfText(sigil);
}
default: {
return getSvgSizeFromTextInputSize(size);
}
}
};
const getTextTypeScale = (size: Size) => {
switch (size) {
case "s":
return typeScale.subtext;
case "m":
return typeScale.base;
case "l":
// Need to make custom font size here due to the original font size not existing in constants
return styles.largeFontSize;
default:
return typeScale.base;
}
};
const getIconSizeFromTextInputSize = (size: Size): StandardIconSizes => {
// eslint-disable-next-line default-case
switch (size) {
case "s":
case "m":
return "xs";
case "l":
return "s";
}
return "xs";
};
const getPaddingFromTextInputSize = (size: Size): number => {
// eslint-disable-next-line default-case
switch (size) {
case "s":
return HORIZONTAL_INPUT_PADDING_SMALL_PX;
case "m":
return HORIZONTAL_INPUT_PADDING_BASE_PX;
case "l":
return HORIZONTAL_INPUT_PADDING_LARGE_PX;
}
return HORIZONTAL_INPUT_PADDING_BASE_PX;
};
const getSvgSizeFromTextInputSize = (size: Size): number => {
// eslint-disable-next-line default-case
switch (size) {
case "s":
return iconSizes.xxs;
case "m":
return iconSizes.xs;
case "l":
return iconSizes.s;
}
return iconSizes.xs;
};
const styles = StyleSheet.create({
largeFontSize: {
fontSize: "16px",
lineHeight: "18px",
},
wrapper: {
display: "flex",
position: "relative",
},
sigil: {
position: "absolute",
top: "50%",
transform: "translateY(-50%)",
},
sigilTextDisabled: {
color: latitudeColors.grey40,
},
sigilText: {
lineHeight: "100%",
color: latitudeColors.grey50,
},
noBorders: {
borderRadius: "0px 0px 0px 0px",
},
noLeftBorders: {
borderRadius: "0px 3px 3px 0px",
},
noRightBorders: {
borderRadius: "3px 0px 0px 3px",
},
});