/
index.tsx
220 lines (200 loc) · 5.85 KB
/
index.tsx
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
import * as React from "react";
import styled, { css } from "styled-components";
import type * as Common from "../common/types";
import defaultTheme from "../defaultTheme";
import Button from "../Button";
import ButtonLink from "../ButtonLink";
import FormLabel from "../FormLabel";
import ErrorFormTooltip from "../ErrorFormTooltip";
import Attachment from "../icons/Attachment";
import CloseCircle from "../icons/CloseCircle";
import { rtlSpacing } from "../utils/rtl";
import getSpacingToken from "../common/getSpacingToken";
import getFieldDataState from "../common/getFieldDataState";
import formElementFocus from "../InputField/helpers/formElementFocus";
import useErrorTooltip from "../ErrorFormTooltip/hooks/useErrorTooltip";
import mq from "../utils/mediaQuery";
import type { Props } from "./types";
const Field = styled.label<{ $width: Props["width"]; spaceAfter?: Common.SpaceAfterSizes }>`
${({ theme, $width }) => css`
font-family: ${theme.orbit.fontFamily};
display: block;
position: relative;
width: ${$width};
margin-bottom: ${getSpacingToken};
`}
`;
Field.defaultProps = {
theme: defaultTheme,
};
const FakeInput = styled(({ children, className }) => <div className={className}>{children}</div>)`
box-sizing: border-box;
display: flex;
align-items: center;
padding: ${({ theme }) => rtlSpacing(theme.orbit.paddingInputFile)};
height: ${({ theme }) => theme.orbit.heightInputNormal};
box-shadow: inset 0 0 0
${({ theme, error }) =>
`${theme.orbit.borderWidthInput} ${
error ? theme.orbit.borderColorInputError : theme.orbit.borderColorInput
}`};
background-color: ${({ theme }) => theme.backgroundInput};
transition: box-shadow ${({ theme }) => theme.orbit.durationFast} ease-in-out;
border-radius: 6px;
${mq.tablet(css`
border-radius: ${({ theme }) => theme.orbit.borderRadiusNormal};
`)};
&:hover {
box-shadow: inset 0 0 0
${({ theme, error }) =>
`${theme.orbit.borderWidthInput} ${
error ? theme.orbit.paletteRedNormalHover : theme.orbit.borderColorInputHover
}`};
}
`;
FakeInput.defaultProps = {
theme: defaultTheme,
};
// we need to hide the input, but not with display or visibility so we can trigger the focus
const Input = styled.input<{ error?: Props["error"] }>`
opacity: 0;
position: absolute;
height: 0;
&:focus ~ ${FakeInput} {
${formElementFocus}
}
`;
Input.defaultProps = {
theme: defaultTheme,
};
const getFileInputColor = ({ error, fileName }, theme) => {
if (error) {
return theme.orbit.paletteRedNormal;
}
if (fileName) {
return theme.orbit.colorTextInput;
}
return theme.orbit.paletteInkNormal;
};
const StyledFileInput = styled.div<Pick<Props, "error" | "fileName">>`
font-family: ${({ theme }) => theme.orbit.fontFamily};
color: ${({ error, fileName, theme }) => getFileInputColor({ error, fileName }, theme)};
width: 100%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding: ${({ theme }) => rtlSpacing(`0 0 0 ${theme.orbit.spaceSmall}`)};
`;
StyledFileInput.defaultProps = {
theme: defaultTheme,
};
const InputFile = React.forwardRef<HTMLDivElement, Props>((props, ref) => {
const {
placeholder = "No file selected",
buttonLabel = "Select file",
required,
onRemoveFile,
dataTest,
id,
helpClosable = true,
spaceAfter,
width = "100%",
help,
error,
onFocus,
onBlur,
name,
label,
onChange,
allowedFileTypes,
tabIndex,
fileName,
insideInputGroup,
} = props;
const hasTooltip = Boolean(error || help);
const {
tooltipShown,
tooltipShownHover,
setTooltipShown,
setTooltipShownHover,
labelRef,
iconRef,
handleFocus,
} = useErrorTooltip({ onFocus, hasTooltip });
const shown = tooltipShown || tooltipShownHover;
return (
<Field spaceAfter={spaceAfter} ref={labelRef} $width={width}>
<Input
data-test={dataTest}
id={id}
aria-required={required}
data-state={
insideInputGroup && typeof error === "undefined"
? undefined
: getFieldDataState(!!props.error)
}
type="file"
name={name}
error={error}
onChange={onChange}
onFocus={handleFocus}
onBlur={onBlur}
accept={Array.isArray(allowedFileTypes) ? allowedFileTypes.join(",") : allowedFileTypes}
tabIndex={tabIndex ? Number(tabIndex) : undefined}
/>
{label && (
<FormLabel
filled={!!fileName}
error={!!error}
help={!!help}
labelRef={labelRef}
iconRef={iconRef}
onMouseEnter={() => setTooltipShownHover(true)}
onMouseLeave={() => setTooltipShownHover(false)}
>
{label}
</FormLabel>
)}
<FakeInput error={error}>
<Button
type="secondary"
size="small"
iconLeft={<Attachment />}
asComponent="div"
role="button"
>
{buttonLabel}
</Button>
<StyledFileInput fileName={fileName} error={error} ref={ref}>
{fileName || placeholder}
</StyledFileInput>
{fileName && (
<ButtonLink
type="primary"
compact
iconLeft={<CloseCircle ariaLabel="remove" color="secondary" />}
onClick={ev => {
ev.preventDefault();
if (onRemoveFile) {
onRemoveFile();
}
}}
/>
)}
</FakeInput>
{!insideInputGroup && hasTooltip && (
<ErrorFormTooltip
help={help}
error={error}
helpClosable={helpClosable}
inputSize="normal"
referenceElement={labelRef}
shown={shown}
onShown={setTooltipShown}
/>
)}
</Field>
);
});
InputFile.displayName = "InputFile";
export default InputFile;