Skip to content

Commit 3f19662

Browse files
authored
refactor!: render upload file elements in light DOM (#4870)
1 parent 7abde2b commit 3f19662

11 files changed

Lines changed: 340 additions & 109 deletions
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2016 - 2022 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import './vaadin-upload-file.js';
7+
import { html as legacyHtml, PolymerElement } from '@polymer/polymer/polymer-element.js';
8+
import { html, render } from 'lit';
9+
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
10+
11+
/**
12+
* An element used internally by `<vaadin-upload>`. Not intended to be used separately.
13+
*
14+
* @extends HTMLElement
15+
* @mixes FocusMixin
16+
* @private
17+
*/
18+
class UploadFileList extends ThemableMixin(PolymerElement) {
19+
static get is() {
20+
return 'vaadin-upload-file-list';
21+
}
22+
23+
static get template() {
24+
return legacyHtml`
25+
<style>
26+
:host {
27+
display: block;
28+
}
29+
30+
:host([hidden]) {
31+
display: none !important;
32+
}
33+
34+
[part='list'] {
35+
padding: 0;
36+
margin: 0;
37+
list-style-type: none;
38+
}
39+
</style>
40+
<ul part="list">
41+
<slot></slot>
42+
</ul>
43+
`;
44+
}
45+
46+
static get properties() {
47+
return {
48+
/**
49+
* The array of files being processed, or already uploaded.
50+
*/
51+
items: {
52+
type: Array,
53+
},
54+
55+
/**
56+
* The object used to localize upload files.
57+
*/
58+
i18n: {
59+
type: Object,
60+
},
61+
};
62+
}
63+
64+
static get observers() {
65+
return ['__updateItems(items, i18n)'];
66+
}
67+
68+
/** @private */
69+
__updateItems(items, i18n) {
70+
if (items && i18n) {
71+
this.requestContentUpdate();
72+
}
73+
}
74+
75+
/**
76+
* Requests an update for the `vaadin-upload-file` elements.
77+
*
78+
* It is not guaranteed that the update happens immediately (synchronously) after it is requested.
79+
*/
80+
requestContentUpdate() {
81+
const { items, i18n } = this;
82+
83+
render(
84+
html`
85+
${items.map(
86+
(file) => html`
87+
<li>
88+
<vaadin-upload-file
89+
.file="${file}"
90+
.complete="${file.complete}"
91+
.errorMessage="${file.error}"
92+
.fileName="${file.name}"
93+
.held="${file.held}"
94+
.indeterminate="${file.indeterminate}"
95+
.progress="${file.progress}"
96+
.status="${file.status}"
97+
.uploading="${file.uploading}"
98+
.i18n="${i18n}"
99+
></vaadin-upload-file>
100+
</li>
101+
`,
102+
)}
103+
`,
104+
this,
105+
);
106+
}
107+
}
108+
109+
customElements.define(UploadFileList.is, UploadFileList);
110+
111+
export { UploadFileList };

‎packages/upload/src/vaadin-upload-file.js‎

Lines changed: 90 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ class UploadFile extends FocusMixin(ThemableMixin(ControllerMixin(PolymerElement
8282
8383
<div part="row">
8484
<div part="info">
85-
<div part="done-icon" hidden$="[[!file.complete]]" aria-hidden="true"></div>
86-
<div part="warning-icon" hidden$="[[!file.error]]" aria-hidden="true"></div>
85+
<div part="done-icon" hidden$="[[!complete]]" aria-hidden="true"></div>
86+
<div part="warning-icon" hidden$="[[!errorMessage]]" aria-hidden="true"></div>
8787
8888
<div part="meta">
89-
<div part="name" id="name">[[file.name]]</div>
90-
<div part="status" hidden$="[[!file.status]]" id="status">[[file.status]]</div>
91-
<div part="error" id="error" hidden$="[[!file.error]]">[[file.error]]</div>
89+
<div part="name" id="name">[[fileName]]</div>
90+
<div part="status" hidden$="[[!status]]" id="status">[[status]]</div>
91+
<div part="error" id="error" hidden$="[[!errorMessage]]">[[errorMessage]]</div>
9292
</div>
9393
</div>
9494
<div part="commands">
@@ -97,7 +97,7 @@ class UploadFile extends FocusMixin(ThemableMixin(ControllerMixin(PolymerElement
9797
part="start-button"
9898
file-event="file-start"
9999
on-click="_fireFileEvent"
100-
hidden$="[[!file.held]]"
100+
hidden$="[[!held]]"
101101
aria-label$="[[i18n.file.start]]"
102102
aria-describedby="name"
103103
></button>
@@ -106,7 +106,7 @@ class UploadFile extends FocusMixin(ThemableMixin(ControllerMixin(PolymerElement
106106
part="retry-button"
107107
file-event="file-retry"
108108
on-click="_fireFileEvent"
109-
hidden$="[[!file.error]]"
109+
hidden$="[[!errorMessage]]"
110110
aria-label$="[[i18n.file.retry]]"
111111
aria-describedby="name"
112112
></button>
@@ -131,9 +131,75 @@ class UploadFile extends FocusMixin(ThemableMixin(ControllerMixin(PolymerElement
131131

132132
static get properties() {
133133
return {
134-
file: Object,
134+
/**
135+
* True if uploading is completed, false otherwise.
136+
*/
137+
complete: {
138+
type: Boolean,
139+
value: false,
140+
reflectToAttribute: true,
141+
},
142+
143+
/**
144+
* Error message returned by the server, if any.
145+
*/
146+
errorMessage: {
147+
type: String,
148+
value: '',
149+
observer: '_errorMessageChanged',
150+
},
151+
152+
/**
153+
* The object representing a file.
154+
*/
155+
file: {
156+
type: Object,
157+
},
158+
159+
/**
160+
* Name of the uploading file.
161+
*/
162+
fileName: {
163+
type: String,
164+
},
165+
166+
/**
167+
* True if uploading is not started, false otherwise.
168+
*/
169+
held: {
170+
type: Boolean,
171+
value: false,
172+
},
173+
174+
/**
175+
* True if remaining time is unknown, false otherwise.
176+
*/
177+
indeterminate: {
178+
type: Boolean,
179+
value: false,
180+
reflectToAttribute: true,
181+
},
135182

136-
i18n: Object,
183+
/**
184+
* The object used to localize this component.
185+
*/
186+
i18n: {
187+
type: Object,
188+
},
189+
190+
/**
191+
* Number representing the uploading progress.
192+
*/
193+
progress: {
194+
type: Number,
195+
},
196+
197+
/**
198+
* Uploading status.
199+
*/
200+
status: {
201+
type: String,
202+
},
137203

138204
/**
139205
* Indicates whether the element can be focused and where it participates in sequential keyboard navigation.
@@ -145,6 +211,15 @@ class UploadFile extends FocusMixin(ThemableMixin(ControllerMixin(PolymerElement
145211
reflectToAttribute: true,
146212
},
147213

214+
/**
215+
* True if uploading is in progress, false otherwise.
216+
*/
217+
uploading: {
218+
type: Boolean,
219+
value: false,
220+
reflectToAttribute: true,
221+
},
222+
148223
/** @private */
149224
_progress: {
150225
type: Object,
@@ -153,13 +228,7 @@ class UploadFile extends FocusMixin(ThemableMixin(ControllerMixin(PolymerElement
153228
}
154229

155230
static get observers() {
156-
return [
157-
'_toggleHostAttribute(file.error, "error")',
158-
'_toggleHostAttribute(file.indeterminate, "indeterminate")',
159-
'_toggleHostAttribute(file.uploading, "uploading")',
160-
'_toggleHostAttribute(file.complete, "complete")',
161-
'__updateProgress(_progress, file.progress, file.indeterminate)',
162-
];
231+
return ['__updateProgress(_progress, progress, indeterminate)'];
163232
}
164233

165234
/** @protected */
@@ -205,6 +274,11 @@ class UploadFile extends FocusMixin(ThemableMixin(ControllerMixin(PolymerElement
205274
return event.composedPath()[0] === this;
206275
}
207276

277+
/** @private */
278+
_errorMessageChanged(errorMessage) {
279+
this.toggleAttribute('error', Boolean(errorMessage));
280+
}
281+
208282
/** @private */
209283
__updateProgress(progress, value, indeterminate) {
210284
if (progress) {
@@ -225,19 +299,6 @@ class UploadFile extends FocusMixin(ThemableMixin(ControllerMixin(PolymerElement
225299
);
226300
}
227301

228-
/** @private */
229-
_toggleHostAttribute(value, attributeName) {
230-
const shouldHave = Boolean(value);
231-
const has = this.hasAttribute(attributeName);
232-
if (has !== shouldHave) {
233-
if (shouldHave) {
234-
this.setAttribute(attributeName, '');
235-
} else {
236-
this.removeAttribute(attributeName);
237-
}
238-
}
239-
}
240-
241302
/**
242303
* Fired when the retry button is pressed. It is listened by `vaadin-upload`
243304
* which will start a new upload process of this file.

‎packages/upload/src/vaadin-upload.d.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ export interface UploadEventMap extends HTMLElementEventMap, UploadCustomEventMa
185185
* -------------------|-------------------------------------
186186
* `primary-buttons` | Upload container
187187
* `drop-label` | Element wrapping drop label and icon
188-
* `file-list` | File list container
189188
*
190189
* The following state attributes are available for styling:
191190
*

0 commit comments

Comments
 (0)