-
Notifications
You must be signed in to change notification settings - Fork 14
/
FileItem.tsx
187 lines (171 loc) · 5.79 KB
/
FileItem.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
import React from "react";
import styled, { ThemeProvider } from "styled-components";
import { DxcFlex } from "../main";
import useTheme from "../useTheme";
import useTranslatedLabels from "../useTranslatedLabels";
import { FileItemProps } from "./types";
const deleteIcon = (
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
<path d="M0 0h24v24H0V0z" fill="none" />
<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z" />
</svg>
);
const errorIcon = (
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z" />
</svg>
);
const FileItem = ({
fileName = "",
error = "",
singleFileMode,
showPreview,
preview,
type,
onDelete,
tabIndex,
}: FileItemProps): JSX.Element => {
const colorsTheme = useTheme();
const translatedLabels = useTranslatedLabels();
const getIconAriaLabel = () => {
if (type.includes("video")) return "video";
else if (type.includes("audio")) return "audio";
else return "file";
};
return (
<ThemeProvider theme={colorsTheme.fileInput}>
<MainContainer error={error} singleFileMode={singleFileMode} showPreview={showPreview}>
{showPreview &&
(type.includes("image") ? (
<ImagePreview src={preview} alt={fileName} />
) : (
<IconPreview error={error} aria-label={getIconAriaLabel()}>
{preview}
</IconPreview>
))}
<FileItemContent>
<FileName>{fileName}</FileName>
<DxcFlex gap="0.25rem">
{error && <ErrorIcon>{errorIcon}</ErrorIcon>}
<DeleteFileAction
onClick={() => {
onDelete(fileName);
}}
type="button"
title={translatedLabels.fileInput.deleteFileActionTitle}
aria-label={translatedLabels.fileInput.deleteFileActionTitle}
tabIndex={tabIndex}
>
{deleteIcon}
</DeleteFileAction>
</DxcFlex>
{error && !singleFileMode && <ErrorMessage>{error}</ErrorMessage>}
</FileItemContent>
</MainContainer>
</ThemeProvider>
);
};
const MainContainer = styled.div<{ error: string; singleFileMode: boolean; showPreview: boolean }>`
box-sizing: border-box;
display: flex;
justify-content: center;
gap: 0.75rem;
width: ${(props) => (props.singleFileMode ? "230px" : "320px")};
padding: ${(props) =>
props.showPreview
? `calc(8px - ${props.theme.fileItemBorderThickness})`
: `calc(8px - ${props.theme.fileItemBorderThickness}) calc(8px - ${props.theme.fileItemBorderThickness}) calc(8px - ${props.theme.fileItemBorderThickness}) 16px`};
${(props) => (props.error ? `background-color: ${props.theme.errorFileItemBackgroundColor};` : "")};
border-color: ${(props) => (props.error ? props.theme.errorFileItemBorderColor : props.theme.fileItemBorderColor)};
border-width: ${(props) => props.theme.fileItemBorderThickness};
border-style: ${(props) => props.theme.fileItemBorderStyle};
border-radius: ${(props) => props.theme.fileItemBorderRadius};
`;
const ImagePreview = styled.img`
width: 48px;
height: 48px;
border-radius: 2px;
object-fit: contain;
`;
const IconPreview = styled.span<{ error: string }>`
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
background-color: ${(props) =>
props.error ? props.theme.errorFilePreviewBackgroundColor : props.theme.filePreviewBackgroundColor};
width: 48px;
height: 48px;
padding: 15px;
border-radius: 2px;
color: ${(props) => (props.error ? props.theme.errorFilePreviewIconColor : props.theme.filePreviewIconColor)};
svg {
height: 18px;
width: 18px;
}
`;
const FileItemContent = styled.div`
flex-grow: 1;
display: grid;
grid-template-columns: auto min-content;
grid-template-rows: min-content auto;
column-gap: 0.25rem;
`;
const FileName = styled.span`
align-self: center;
color: ${(props) => props.theme.fileNameFontColor};
font-family: ${(props) => props.theme.fileItemFontFamily};
font-size: ${(props) => props.theme.fileItemFontSize};
font-weight: ${(props) => props.theme.fileItemFontWeight};
line-height: ${(props) => props.theme.fileItemLineHeight};
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
`;
const ErrorIcon = styled.span`
display: flex;
flex-wrap: wrap;
align-content: center;
padding: 3px;
height: 18px;
width: 18px;
color: #d0011b;
`;
const DeleteFileAction = styled.button`
display: flex;
flex-wrap: wrap;
align-content: center;
height: 24px;
width: 24px;
font-size: 1rem;
font-family: ${(props) => props.theme.fontFamily};
border: 1px solid transparent;
border-radius: 2px;
background-color: transparent;
box-shadow: 0 0 0 2px transparent;
padding: 3px;
cursor: pointer;
color: ${(props) => props.theme.deleteFileItemColor};
svg {
line-height: 18px;
}
&:hover {
background-color: ${(props) => props.theme.hoverDeleteFileItemBackgroundColor};
}
&:focus,
&:focus-visible {
outline: none;
box-shadow: 0 0 0 2px ${(props) => props.theme.focusDeleteFileItemBorderColor};
}
&:active {
background-color: ${(props) => props.theme.activeDeleteFileItemBackgroundColor};
}
`;
const ErrorMessage = styled.span`
color: ${(props) => props.theme.errorMessageFontColor};
font-family: ${(props) => props.theme.errorMessageFontFamily};
font-size: ${(props) => props.theme.errorMessageFontSize};
font-weight: ${(props) => props.theme.errorMessageFontWeight};
line-height: ${(props) => props.theme.errorMessageLineHeight};
`;
export default React.memo(FileItem);