-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathuui-input-file.element.ts
306 lines (263 loc) · 7.63 KB
/
uui-input-file.element.ts
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
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
import { property, query, state } from 'lit/decorators.js';
import { css, html, LitElement, nothing } from 'lit';
import { UUIFileDropzoneElement } from '@umbraco-ui/uui-file-dropzone/lib';
import { UUIFormControlMixin } from '@umbraco-ui/uui-base/lib/mixins';
import { demandCustomElement } from '@umbraco-ui/uui-base/lib/utils';
import { iconDelete } from '@umbraco-ui/uui-icon-registry-essential/lib/svgs';
import { repeat } from 'lit/directives/repeat.js';
// TODO: Missing change event, when files are changed.
/**
* @element uui-input-file
* @description - A form associated file input that supports multiple files.
* @extends UUIFormControlMixin
*/
@defineElement('uui-input-file')
export class UUIInputFileElement extends UUIFormControlMixin(LitElement) {
@query('#dropzone')
private _dropzone!: UUIFileDropzoneElement;
@query('#dropzone')
private _dropZone: UUIFileDropzoneElement | undefined;
/**
* Accepted filetypes. Will allow all types if empty.
* @type {string}
* @attr
* @default false
*/
@property({ type: String })
public accept: string = '';
/**
* Allows for multiple files to be selected.
* @type {boolean}
* @attr
* @default false
*/
@property({ type: Boolean })
public multiple: boolean = false;
get value() {
return super.value;
}
set value(newValue) {
super.value = newValue;
if (newValue instanceof FormData) {
const data = (
this.multiple ? newValue.getAll(this.name) : [newValue.get(this.name)]
) as Array<File>;
this._updateFileWrappers(data);
return;
}
if (newValue instanceof File) {
this._updateFileWrappers([newValue]);
return;
}
}
@state()
private _files: File[] = [];
constructor() {
super();
this.addEventListener('dragenter', () => this._setShowDropzone(true));
this.addEventListener('dragleave', () => this._setShowDropzone(false));
this.addEventListener('drop', () => this._setShowDropzone(false));
}
connectedCallback(): void {
super.connectedCallback();
demandCustomElement(this, 'uui-icon');
demandCustomElement(this, 'uui-file-dropzone');
demandCustomElement(this, 'uui-button');
demandCustomElement(this, 'uui-action-bar');
demandCustomElement(this, 'uui-file-preview');
}
protected getFormElement(): HTMLElement {
return this._dropZone! as HTMLElement;
}
/**
* Removes focus from the input.
*/
async blur() {
await this.updateComplete;
this._dropzone.blur();
}
/**
* This method enables <label for="..."> to focus the input
*/
async focus() {
await this.updateComplete;
this._dropzone.focus();
}
async click() {
await this.updateComplete;
this._dropzone.browse();
}
private _handleClick(e: Event) {
e.stopImmediatePropagation();
this._dropzone.browse();
}
private _updateFileWrappers = (data: Array<File>) => {
let newFileWrappers: Array<File> = [];
for (const file of data) {
if (this.multiple) {
newFileWrappers.push(file);
} else {
newFileWrappers = [file];
}
}
this._files = newFileWrappers;
};
private async _handleFilesChange(event: CustomEvent) {
const entries = event.detail.files as (File | FileSystemFileEntry)[];
const files = entries.filter(
entry => entry instanceof File || entry.isFile,
);
// TODO: implement folder preview + remove folder including children
if (!this.multiple) {
const entry = files[0];
const isFile = entry instanceof File;
const file = isFile ? entry : await this._getFile(entry);
if (this.value instanceof File) {
this.value = file;
return;
}
if (this.value instanceof FormData) {
this.value.delete(this.name);
this.value.append(this.name, file);
this._updateFileWrappers([file]);
return;
}
}
let newValue = this.value;
if (files.length > 0 && !(this.value instanceof FormData)) {
newValue = new FormData();
}
if (newValue instanceof FormData) {
for (const entry of files) {
const isFile = entry instanceof File;
newValue.append(this.name, isFile ? entry : await this._getFile(entry));
}
}
this.value = newValue;
}
private async _getFile(fileEntry: FileSystemFileEntry): Promise<File> {
return await new Promise<File>((resolve, reject) =>
fileEntry.file(resolve, reject),
);
}
private _removeFile(index: number) {
const fileToRemove = this._files[index];
if (this.value instanceof FormData) {
const files = this.value.getAll(this.name) as Array<File>;
const filteredFiles = files.filter(file => file !== fileToRemove);
if (filteredFiles.length === 0) {
this.value = '';
} else {
this.value.delete(this.name);
for (const file of filteredFiles) {
this.value.append(this.name, file);
}
}
this._updateFileWrappers(filteredFiles);
}
if (this.value instanceof File) {
this.value = '';
this._updateFileWrappers([]);
}
}
private _setShowDropzone(show: boolean) {
if (show) {
this._dropZone!.style.display = 'flex';
} else {
this._dropZone!.style.display = 'none';
}
}
private _renderFileItem(file: File, index: number) {
return html`<uui-file-preview .file="${file}">
<uui-action-bar slot="actions">
<uui-button
@click=${() => this._removeFile(index)}
color="danger"
label=${`Delete ${file.name}`}>
<uui-icon name="delete" .fallback=${iconDelete.strings[0]}></uui-icon>
</uui-button>
</uui-action-bar>
</uui-file-preview>`;
}
private _renderFiles() {
return html`${repeat(
this._files,
(file: File) => file.name + file.size,
(file: File, index: number) => this._renderFileItem(file, index),
)}`;
}
render() {
return html`
<uui-file-dropzone
id="dropzone"
?multiple=${this.multiple}
.accept=${this.accept}
@change=${this._handleFilesChange}
label="Drop files here"></uui-file-dropzone>
<div id="files">
${this._renderFiles()}
${this._files.length === 0 || this.multiple
? html`<uui-button
@click=${this._handleClick}
id="add-button"
look="placeholder"
label="Add"></uui-button>`
: nothing}
</div>
`;
}
static styles = [
css`
:host {
width: 100%;
height: 100%;
position: relative;
display: flex;
box-sizing: border-box;
border: 1px solid var(--uui-color-border);
}
#input {
position: absolute;
width: 0px;
height: 0px;
opacity: 0;
display: none;
}
#files {
display: grid;
box-sizing: border-box;
justify-items: center;
width: 100%;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
grid-auto-rows: min-content;
gap: 16px;
padding: 16px;
overflow: auto;
max-height: 100%;
}
#dropzone {
display: none;
position: absolute;
inset: 0px;
z-index: 10;
justify-content: center;
align-items: center;
}
#add-button {
width: 150px;
height: 150px;
display: flex;
padding: 16px;
box-sizing: border-box;
justify-content: center;
align-items: stretch;
}
`,
];
}
declare global {
interface HTMLElementTagNameMap {
'uui-input-file': UUIInputFileElement;
}
}