-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextField.tsx
More file actions
135 lines (123 loc) · 4.52 KB
/
Copy pathTextField.tsx
File metadata and controls
135 lines (123 loc) · 4.52 KB
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
import React, { KeyboardEventHandler, RefObject } from "react";
import {
Classes as BlueprintClassNames,
HTMLInputProps,
InputGroup as BlueprintInputGroup,
InputGroupProps as BlueprintInputGroupProps,
MaybeElement,
} from "@blueprintjs/core";
import { Definitions as IntentDefinitions, IntentBlueprint, IntentTypes } from "../../common/Intent";
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
import { ValidIconName } from "../Icon/canonicalIconNames";
import Icon from "../Icon/Icon";
import { InvisibleCharacterWarningProps, useTextValidation } from "./useTextValidation";
export interface TextFieldProps extends Partial<
Omit<BlueprintInputGroupProps, "intent" | "leftIcon" | "leftElement"> & HTMLInputProps
> {
/**
* Intent state of the text field.
*/
intent?: IntentTypes | "edited" | "removed";
/**
* The input element uses the full horizontal width of the parent container.
*/
fullWidth?: boolean;
/**
* Left aligned icon, can be a canonical icon name or an `Icon` element.
*/
leftIcon?: ValidIconName | MaybeElement;
/**
* If set, allows to be informed of invisible, hard to spot characters in the string value.
*/
invisibleCharacterWarning?: InvisibleCharacterWarningProps;
/** If true pressing the Escape key will blur/de-focus the input field. Default: false */
escapeToBlur?: boolean;
}
/**
* Text input field.
*/
export const TextField = ({
className = "",
fullWidth = true,
leftIcon,
invisibleCharacterWarning,
escapeToBlur = false,
intent,
...otherBlueprintInputGroupProps
}: TextFieldProps) => {
const inputRef = React.useRef<HTMLInputElement | null>(null);
const handleLabelEscape = React.useCallback(() => {
inputRef.current?.blur();
if (otherBlueprintInputGroupProps.inputRef) {
const otherInputRef = otherBlueprintInputGroupProps.inputRef as RefObject<HTMLInputElement>;
if (otherInputRef.current) {
otherInputRef.current.blur();
}
}
}, []);
const onKeyDown: KeyboardEventHandler<HTMLInputElement> = React.useCallback(
(event) => {
if (escapeToBlur && event.key === "Escape") {
event.preventDefault();
handleLabelEscape();
return false;
}
return otherBlueprintInputGroupProps.onKeyDown?.(event);
},
[otherBlueprintInputGroupProps.onKeyDown, escapeToBlur],
);
let iconIntent;
switch (intent) {
case "edited":
iconIntent = IntentDefinitions.INFO;
break;
case "removed":
iconIntent = IntentDefinitions.DANGER;
break;
default:
iconIntent = intent as IntentTypes;
break;
}
const maybeWrappedOnChange = useTextValidation({ ...otherBlueprintInputGroupProps, invisibleCharacterWarning });
if (
(otherBlueprintInputGroupProps.readOnly || otherBlueprintInputGroupProps.disabled) &&
!!otherBlueprintInputGroupProps.value &&
!otherBlueprintInputGroupProps.title
) {
otherBlueprintInputGroupProps["title"] = otherBlueprintInputGroupProps.value;
}
return (
<BlueprintInputGroup
inputRef={inputRef}
className={
`${eccgui}-textfield` +
(intent ? ` ${eccgui}-intent--${intent}` : "") +
(className ? ` ${className}` : "")
}
intent={
intent && !["info", "edited", "removed", "neutral", "accent"].includes(intent)
? (intent as IntentBlueprint)
: undefined
}
fill={fullWidth}
{...otherBlueprintInputGroupProps}
leftElement={
leftIcon != null && leftIcon !== false ? (
typeof leftIcon === "string" ? (
<Icon
name={leftIcon as ValidIconName}
className={BlueprintClassNames.ICON}
intent={iconIntent as IntentTypes | undefined}
/>
) : (
<span className={BlueprintClassNames.ICON}>{leftIcon}</span>
)
) : undefined
}
dir={"auto"}
onChange={maybeWrappedOnChange}
onKeyDown={otherBlueprintInputGroupProps.onKeyDown || escapeToBlur ? onKeyDown : undefined}
/>
);
};
export default TextField;