@nanahoshi/mona-ui 0.32.3 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/api/file-upload.md +174 -0
- package/docs/api/grid.md +46 -13
- package/docs/api/i18n.md +46 -0
- package/docs/api/locales.md +14 -14
- package/docs/file-upload.md +516 -0
- package/docs/forms.md +2 -0
- package/docs/grid-columns.md +121 -17
- package/docs/grid-state-export.md +24 -7
- package/docs/grid.md +1 -1
- package/fesm2022/nanahoshi-mona-ui-color-gradient.mjs +2 -2
- package/fesm2022/nanahoshi-mona-ui-command.mjs +4 -3
- package/fesm2022/nanahoshi-mona-ui-dropdowns.mjs +2 -2
- package/fesm2022/nanahoshi-mona-ui-file-upload.mjs +1137 -0
- package/fesm2022/{nanahoshi-mona-ui-grid-grid-colgroup.component-NhONCVIX.mjs → nanahoshi-mona-ui-grid-grid-colgroup.component-Lv3GunpV.mjs} +2 -2
- package/fesm2022/{nanahoshi-mona-ui-grid-grid-footer-row.component-Djv8ZnT5.mjs → nanahoshi-mona-ui-grid-grid-footer-row.component-BYH9XbCV.mjs} +8 -3
- package/fesm2022/{nanahoshi-mona-ui-grid-grid-list.component-C1qjq2s7.mjs → nanahoshi-mona-ui-grid-grid-list.component-S2iCn8TV.mjs} +10 -6
- package/fesm2022/{nanahoshi-mona-ui-grid-grid-row.component-CgOt2A6O.mjs → nanahoshi-mona-ui-grid-grid-row.component-BUJvgihp.mjs} +23 -8
- package/fesm2022/{nanahoshi-mona-ui-grid-grid-virtual-list.component-tguG2LpN.mjs → nanahoshi-mona-ui-grid-grid-virtual-list.component-Wndy-_Bt.mjs} +5 -5
- package/fesm2022/{nanahoshi-mona-ui-grid-grid.styles-BmLU-3zP.mjs → nanahoshi-mona-ui-grid-grid.styles-aPD3c2A6.mjs} +230 -18
- package/fesm2022/nanahoshi-mona-ui-grid.mjs +425 -31
- package/fesm2022/nanahoshi-mona-ui-i18n-icu.mjs +74 -0
- package/fesm2022/nanahoshi-mona-ui-internal-live-announcer.mjs +10 -0
- package/fesm2022/nanahoshi-mona-ui-internal-tree.mjs +3 -2
- package/fesm2022/nanahoshi-mona-ui-list-box.mjs +2 -2
- package/fesm2022/nanahoshi-mona-ui-locales-ar-sa.mjs +50 -0
- package/fesm2022/nanahoshi-mona-ui-locales-de-de.mjs +38 -0
- package/fesm2022/nanahoshi-mona-ui-locales-es-es.mjs +38 -0
- package/fesm2022/nanahoshi-mona-ui-locales-fr-fr.mjs +38 -0
- package/fesm2022/nanahoshi-mona-ui-locales-id-id.mjs +29 -0
- package/fesm2022/nanahoshi-mona-ui-locales-it-it.mjs +38 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ja-jp.mjs +29 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ko-kr.mjs +29 -0
- package/fesm2022/nanahoshi-mona-ui-locales-pl-pl.mjs +44 -0
- package/fesm2022/nanahoshi-mona-ui-locales-pt-br.mjs +38 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ru-ru.mjs +44 -0
- package/fesm2022/nanahoshi-mona-ui-locales-tr-tr.mjs +29 -0
- package/fesm2022/nanahoshi-mona-ui-locales-zh-cn.mjs +29 -0
- package/fesm2022/nanahoshi-mona-ui-locales-zh-tw.mjs +29 -0
- package/fesm2022/nanahoshi-mona-ui-notification.mjs +2 -2
- package/fesm2022/nanahoshi-mona-ui-spinner.mjs +2 -2
- package/fesm2022/nanahoshi-mona-ui-tree-view.mjs +2 -2
- package/file-upload/package.json +4 -0
- package/i18n/catalogs/en-US.messages.json +29 -0
- package/internal/live-announcer/package.json +4 -0
- package/llms.txt +6 -4
- package/locales/ar-sa/ar-sa.messages.json +29 -0
- package/locales/de-de/de-de.messages.json +29 -0
- package/locales/es-es/es-es.messages.json +29 -0
- package/locales/fr-fr/fr-fr.messages.json +29 -0
- package/locales/id-id/id-id.messages.json +29 -0
- package/locales/it-it/it-it.messages.json +29 -0
- package/locales/ja-jp/ja-jp.messages.json +29 -0
- package/locales/ko-kr/ko-kr.messages.json +29 -0
- package/locales/pl-pl/pl-pl.messages.json +29 -0
- package/locales/pt-br/pt-br.messages.json +29 -0
- package/locales/ru-ru/ru-ru.messages.json +29 -0
- package/locales/tr-tr/tr-tr.messages.json +29 -0
- package/locales/zh-cn/zh-cn.messages.json +29 -0
- package/locales/zh-tw/zh-tw.messages.json +29 -0
- package/package.json +9 -1
- package/types/nanahoshi-mona-ui-file-upload.d.ts +366 -0
- package/types/nanahoshi-mona-ui-grid.d.ts +142 -64
- package/types/nanahoshi-mona-ui-i18n.d.ts +60 -0
- package/types/nanahoshi-mona-ui-internal-live-announcer.d.ts +12 -0
- package/types/nanahoshi-mona-ui-locales-ar-sa.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-de-de.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-es-es.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-fr-fr.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-id-id.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-it-it.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-ja-jp.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-ko-kr.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-pl-pl.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-pt-br.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-ru-ru.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-tr-tr.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-zh-cn.d.ts +29 -0
- package/types/nanahoshi-mona-ui-locales-zh-tw.d.ts +29 -0
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
# File Upload
|
|
2
|
+
|
|
3
|
+
> Picks files from a drop zone or the file dialog, checks them, and uploads them with progress.
|
|
4
|
+
|
|
5
|
+
Entry point: `@nanahoshi/mona-ui/file-upload`
|
|
6
|
+
|
|
7
|
+
**Selector:** `mona-file-upload`
|
|
8
|
+
|
|
9
|
+
`FileUploadComponent` picks files from a drop zone or the browser's file dialog, checks their type, size and count, and lists them. Given an upload function, it uploads each file with a progress bar, and lets people cancel an upload or retry one that failed.
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
Use `FileUploadComponent` wherever a form takes files: attachments, receipts, a profile photo, an import. It works two ways:
|
|
14
|
+
|
|
15
|
+
- **Picking only.** Without an `upload` function, the value holds the files that passed the checks, and the form sends them itself, for example in a `FormData` body.
|
|
16
|
+
- **Uploading.** With an `upload` function, each file is sent as it's added, or when the upload button is pressed. The list shows each file's progress, and the value holds the files that finished uploading.
|
|
17
|
+
|
|
18
|
+
The component doesn't talk to a server itself. The upload function does, with HttpClient, `fetch` or a storage SDK, so it works with any backend.
|
|
19
|
+
|
|
20
|
+
## Import & Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { FileUploadComponent } from "@nanahoshi/mona-ui/file-upload";
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```html
|
|
27
|
+
<mona-form-field label="Attachments">
|
|
28
|
+
<mona-file-upload [(value)]="attachments"></mona-file-upload>
|
|
29
|
+
</mona-form-field>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
protected readonly attachments = signal<File[]>([]);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
To upload each file as it's added, pass an upload function. With HttpClient:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { HttpClient } from "@angular/common/http";
|
|
40
|
+
import type { FileUploadHandler } from "@nanahoshi/mona-ui/file-upload";
|
|
41
|
+
|
|
42
|
+
readonly #http = inject(HttpClient);
|
|
43
|
+
|
|
44
|
+
protected readonly upload: FileUploadHandler = file => {
|
|
45
|
+
const body = new FormData();
|
|
46
|
+
body.append("file", file);
|
|
47
|
+
return this.#http.post<{ id: string }>("/api/files", body, { observe: "events", reportProgress: true });
|
|
48
|
+
};
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```html
|
|
52
|
+
<mona-file-upload [upload]="upload" (uploadSuccess)="onUploaded($event)"></mona-file-upload>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Anatomy
|
|
56
|
+
|
|
57
|
+
| Part | What it is |
|
|
58
|
+
|---|---|
|
|
59
|
+
| Drop zone | A box with a dashed border, its text, the checks in effect, and the browse button. Files can be dropped anywhere on it, and a click on it opens the file dialog. |
|
|
60
|
+
| Browse button | Opens the browser's file dialog. It's the way in for the keyboard. |
|
|
61
|
+
| File list | One row per file: an icon, the file's name, its size and status, a progress bar while it uploads, and its buttons. |
|
|
62
|
+
| Row buttons | Retry, on a failed upload. Remove, or cancel while the file uploads. |
|
|
63
|
+
| Upload and clear buttons | Below the list while files wait for the upload button, when `autoUpload` is off. |
|
|
64
|
+
|
|
65
|
+
The native `<input type="file">` is in the component, hidden. The browse button and the drop zone use it, so the dialog filters by `accept` and allows `multiple`, as the browser's own does.
|
|
66
|
+
|
|
67
|
+
## Examples
|
|
68
|
+
|
|
69
|
+
### Attachments that go with the form
|
|
70
|
+
|
|
71
|
+
Without an upload function, the value is the list of files, and the form sends them on submit.
|
|
72
|
+
|
|
73
|
+
`basic-example.component.ts`
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { Component, inject, signal } from "@angular/core";
|
|
77
|
+
import { ButtonDirective } from "@nanahoshi/mona-ui/button";
|
|
78
|
+
import { FileUploadComponent, formatFileSize } from "@nanahoshi/mona-ui/file-upload";
|
|
79
|
+
import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
|
|
80
|
+
import { MonaI18nService } from "@nanahoshi/mona-ui/i18n";
|
|
81
|
+
|
|
82
|
+
@Component({
|
|
83
|
+
selector: "app-file-upload-basic-example",
|
|
84
|
+
imports: [ButtonDirective, FileUploadComponent, FormFieldComponent],
|
|
85
|
+
templateUrl: "./basic-example.component.html"
|
|
86
|
+
})
|
|
87
|
+
export class FileUploadBasicExampleComponent {
|
|
88
|
+
readonly #i18n = inject(MonaI18nService);
|
|
89
|
+
protected readonly attachments = signal<File[]>([]);
|
|
90
|
+
protected readonly sent = signal("");
|
|
91
|
+
|
|
92
|
+
protected onSubmit(event: Event): void {
|
|
93
|
+
event.preventDefault();
|
|
94
|
+
const files = this.attachments();
|
|
95
|
+
// The files go with the rest of the form, as a multipart request would send them.
|
|
96
|
+
const body = new FormData();
|
|
97
|
+
for (const file of files) {
|
|
98
|
+
body.append("attachments", file);
|
|
99
|
+
}
|
|
100
|
+
const size = formatFileSize(
|
|
101
|
+
files.reduce((total, file) => total + file.size, 0),
|
|
102
|
+
this.#i18n.localeId()
|
|
103
|
+
);
|
|
104
|
+
this.sent.set(`Ready to send ${files.length} ${files.length === 1 ? "file" : "files"}, ${size} in all.`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`basic-example.component.html`
|
|
110
|
+
|
|
111
|
+
```html
|
|
112
|
+
<form class="flex max-w-md flex-col gap-4" novalidate (submit)="onSubmit($event)">
|
|
113
|
+
<mona-form-field label="Attachments" hint="They're sent with the message.">
|
|
114
|
+
<mona-file-upload [(value)]="attachments" />
|
|
115
|
+
</mona-form-field>
|
|
116
|
+
<div class="flex items-center gap-3">
|
|
117
|
+
<button monaButton look="primary" type="submit" [disabled]="attachments().length === 0">Send</button>
|
|
118
|
+
<span class="text-sm" role="status">{{ sent() }}</span>
|
|
119
|
+
</div>
|
|
120
|
+
</form>
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Uploading with progress
|
|
124
|
+
|
|
125
|
+
The upload function here stands in for a server, and reports its progress as it goes. Check "Server offline" to see a failed upload, then uncheck it and retry. The `remove` event carries what the upload returned, to delete the file from the server.
|
|
126
|
+
|
|
127
|
+
`upload-example.component.ts`
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { Component, signal } from "@angular/core";
|
|
131
|
+
import { CheckBoxComponent } from "@nanahoshi/mona-ui/check-box";
|
|
132
|
+
import { FileUploadComponent, type FileUploadHandler, type FileUploadItem } from "@nanahoshi/mona-ui/file-upload";
|
|
133
|
+
import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
|
|
134
|
+
|
|
135
|
+
interface StoredFile {
|
|
136
|
+
readonly id: string;
|
|
137
|
+
readonly name: string;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Stands in for a server: it takes about two seconds per file, and fails while `offline()` is true. */
|
|
141
|
+
function simulatedServer(offline: () => boolean): FileUploadHandler {
|
|
142
|
+
return (file, { progress, signal }) =>
|
|
143
|
+
new Promise<StoredFile>((resolve, reject) => {
|
|
144
|
+
let step = 0;
|
|
145
|
+
const timer = setInterval(() => {
|
|
146
|
+
step++;
|
|
147
|
+
progress(step, 10);
|
|
148
|
+
if (offline() && step === 4) {
|
|
149
|
+
clearInterval(timer);
|
|
150
|
+
reject(new Error("The server didn't answer."));
|
|
151
|
+
} else if (step === 10) {
|
|
152
|
+
clearInterval(timer);
|
|
153
|
+
resolve({ id: crypto.randomUUID().slice(0, 8), name: file.name });
|
|
154
|
+
}
|
|
155
|
+
}, 200);
|
|
156
|
+
signal.addEventListener("abort", () => clearInterval(timer));
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
@Component({
|
|
161
|
+
selector: "app-file-upload-upload-example",
|
|
162
|
+
imports: [CheckBoxComponent, FileUploadComponent, FormFieldComponent],
|
|
163
|
+
templateUrl: "./upload-example.component.html"
|
|
164
|
+
})
|
|
165
|
+
export class FileUploadUploadExampleComponent {
|
|
166
|
+
protected readonly offline = signal(false);
|
|
167
|
+
protected readonly stored = signal<readonly StoredFile[]>([]);
|
|
168
|
+
protected readonly upload = simulatedServer(() => this.offline());
|
|
169
|
+
|
|
170
|
+
protected onRemove(item: FileUploadItem): void {
|
|
171
|
+
// An uploaded file comes with what the upload returned, here its id, to delete it from the server.
|
|
172
|
+
const removed = item.response as StoredFile | null;
|
|
173
|
+
this.stored.update(stored => stored.filter(file => file.id !== removed?.id));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
protected onUploadSuccess(item: FileUploadItem): void {
|
|
177
|
+
this.stored.update(stored => [...stored, item.response as StoredFile]);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
`upload-example.component.html`
|
|
183
|
+
|
|
184
|
+
```html
|
|
185
|
+
<div class="flex max-w-md flex-col gap-4">
|
|
186
|
+
<mona-form-field label="Documents">
|
|
187
|
+
<mona-file-upload [upload]="upload" (remove)="onRemove($event)" (uploadSuccess)="onUploadSuccess($event)" />
|
|
188
|
+
</mona-form-field>
|
|
189
|
+
<mona-check-box label="Server offline" [(checked)]="offline" />
|
|
190
|
+
<p class="text-sm text-muted-foreground">
|
|
191
|
+
On the server:
|
|
192
|
+
@for (file of stored(); track file.id) {
|
|
193
|
+
<code>{{ file.name }} ({{ file.id }})</code>{{ $last ? "" : ", " }}
|
|
194
|
+
} @empty {
|
|
195
|
+
nothing yet
|
|
196
|
+
}
|
|
197
|
+
</p>
|
|
198
|
+
</div>
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Type, size and count
|
|
202
|
+
|
|
203
|
+
`accept` takes the same list as the native attribute. `maxFileSize` and `maxFiles` limit the size of each file and the number of files. Files that don't pass are listed with the reason, and stay out of the value.
|
|
204
|
+
|
|
205
|
+
`restrictions-example.component.ts`
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
import { Component, signal } from "@angular/core";
|
|
209
|
+
import { FileUploadComponent } from "@nanahoshi/mona-ui/file-upload";
|
|
210
|
+
import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
|
|
211
|
+
|
|
212
|
+
@Component({
|
|
213
|
+
selector: "app-file-upload-restrictions-example",
|
|
214
|
+
imports: [FileUploadComponent, FormFieldComponent],
|
|
215
|
+
templateUrl: "./restrictions-example.component.html"
|
|
216
|
+
})
|
|
217
|
+
export class FileUploadRestrictionsExampleComponent {
|
|
218
|
+
protected readonly maxFileSize = 2 * 1024 * 1024;
|
|
219
|
+
protected readonly photos = signal<File[]>([]);
|
|
220
|
+
}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
`restrictions-example.component.html`
|
|
224
|
+
|
|
225
|
+
```html
|
|
226
|
+
<mona-form-field class="max-w-md" label="Photos">
|
|
227
|
+
<mona-file-upload accept="image/*" [maxFileSize]="maxFileSize" [maxFiles]="3" [(value)]="photos" />
|
|
228
|
+
</mona-form-field>
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Upload on request
|
|
232
|
+
|
|
233
|
+
With `autoUpload` off, files wait in the list until the upload button is pressed, so people can check what they picked first.
|
|
234
|
+
|
|
235
|
+
`manual-upload-example.component.ts`
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
import { Component } from "@angular/core";
|
|
239
|
+
import { FileUploadComponent, type FileUploadHandler } from "@nanahoshi/mona-ui/file-upload";
|
|
240
|
+
import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
|
|
241
|
+
|
|
242
|
+
/** Stands in for a server that takes about a second and a half per file. */
|
|
243
|
+
const simulatedUpload: FileUploadHandler = (_file, { progress, signal }) =>
|
|
244
|
+
new Promise<void>(resolve => {
|
|
245
|
+
let step = 0;
|
|
246
|
+
const timer = setInterval(() => {
|
|
247
|
+
progress(++step, 6);
|
|
248
|
+
if (step === 6) {
|
|
249
|
+
clearInterval(timer);
|
|
250
|
+
resolve();
|
|
251
|
+
}
|
|
252
|
+
}, 250);
|
|
253
|
+
signal.addEventListener("abort", () => clearInterval(timer));
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
@Component({
|
|
257
|
+
selector: "app-file-upload-manual-upload-example",
|
|
258
|
+
imports: [FileUploadComponent, FormFieldComponent],
|
|
259
|
+
templateUrl: "./manual-upload-example.component.html"
|
|
260
|
+
})
|
|
261
|
+
export class FileUploadManualUploadExampleComponent {
|
|
262
|
+
protected readonly upload = simulatedUpload;
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
`manual-upload-example.component.html`
|
|
267
|
+
|
|
268
|
+
```html
|
|
269
|
+
<mona-form-field class="max-w-md" label="Invoices" hint="Check the list, then upload.">
|
|
270
|
+
<mona-file-upload accept=".pdf" [autoUpload]="false" [upload]="upload" />
|
|
271
|
+
</mona-form-field>
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### Required in a signal form
|
|
275
|
+
|
|
276
|
+
The value holds the uploaded files, so the field stays invalid until one upload has finished. The `uploading()` signal keeps the submit button off while files are on their way.
|
|
277
|
+
|
|
278
|
+
`signal-forms-example.component.ts`
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
import { Component, signal } from "@angular/core";
|
|
282
|
+
import { form, FormField, required, requiredError, submit, validate } from "@angular/forms/signals";
|
|
283
|
+
import { ButtonDirective } from "@nanahoshi/mona-ui/button";
|
|
284
|
+
import { FileUploadComponent, type FileUploadHandler } from "@nanahoshi/mona-ui/file-upload";
|
|
285
|
+
import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
|
|
286
|
+
|
|
287
|
+
interface ExpenseClaim {
|
|
288
|
+
receipts: File[];
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Stands in for a server that takes about a second per file. */
|
|
292
|
+
const simulatedUpload: FileUploadHandler = (_file, { progress, signal }) =>
|
|
293
|
+
new Promise<void>(resolve => {
|
|
294
|
+
let step = 0;
|
|
295
|
+
const timer = setInterval(() => {
|
|
296
|
+
progress(++step, 5);
|
|
297
|
+
if (step === 5) {
|
|
298
|
+
clearInterval(timer);
|
|
299
|
+
resolve();
|
|
300
|
+
}
|
|
301
|
+
}, 200);
|
|
302
|
+
signal.addEventListener("abort", () => clearInterval(timer));
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
@Component({
|
|
306
|
+
selector: "app-file-upload-signal-forms-example",
|
|
307
|
+
imports: [ButtonDirective, FileUploadComponent, FormField, FormFieldComponent],
|
|
308
|
+
templateUrl: "./signal-forms-example.component.html"
|
|
309
|
+
})
|
|
310
|
+
export class FileUploadSignalFormsExampleComponent {
|
|
311
|
+
readonly #model = signal<ExpenseClaim>({ receipts: [] });
|
|
312
|
+
protected readonly claim = form(this.#model, path => {
|
|
313
|
+
required(path.receipts);
|
|
314
|
+
// Signal Forms' required doesn't count an empty list as missing, so this reports it.
|
|
315
|
+
validate(path.receipts, ({ value }) => (value().length === 0 ? requiredError() : undefined));
|
|
316
|
+
});
|
|
317
|
+
protected readonly sent = signal(false);
|
|
318
|
+
protected readonly upload = simulatedUpload;
|
|
319
|
+
|
|
320
|
+
protected onSubmit(event: Event): void {
|
|
321
|
+
event.preventDefault();
|
|
322
|
+
void submit(this.claim, async () => {
|
|
323
|
+
this.sent.set(true);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
`signal-forms-example.component.html`
|
|
330
|
+
|
|
331
|
+
```html
|
|
332
|
+
<form class="flex max-w-md flex-col gap-4" novalidate (submit)="onSubmit($event)">
|
|
333
|
+
<mona-form-field label="Receipts" hint="A photo or a PDF of each receipt.">
|
|
334
|
+
<mona-file-upload
|
|
335
|
+
#receipts="monaFileUpload"
|
|
336
|
+
accept="image/*,.pdf"
|
|
337
|
+
[formField]="claim.receipts"
|
|
338
|
+
[upload]="upload" />
|
|
339
|
+
</mona-form-field>
|
|
340
|
+
<div class="flex items-center gap-3">
|
|
341
|
+
<button monaButton look="primary" type="submit" [disabled]="receipts.uploading()">Submit claim</button>
|
|
342
|
+
@if (sent()) {
|
|
343
|
+
<span class="text-sm" role="status">Claim submitted.</span>
|
|
344
|
+
}
|
|
345
|
+
</div>
|
|
346
|
+
</form>
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
## Appearance & Styling
|
|
350
|
+
|
|
351
|
+
The component fills its container's width. `class` adds classes to the host, which lays out the drop zone, the list and the buttons in a column.
|
|
352
|
+
|
|
353
|
+
| Part | How it looks |
|
|
354
|
+
|---|---|
|
|
355
|
+
| Drop zone | A dashed `input-border`, on the control background. Its border turns `focus-indicator` on hover. |
|
|
356
|
+
| Dragging over | The border turns solid in `focus-indicator`, and the zone takes the `selected` tint with `selected-foreground` text. In forced colors the border is drawn in `Highlight`, since the tint doesn't paint there. |
|
|
357
|
+
| Invalid | The drop zone's border turns `error`. |
|
|
358
|
+
| Disabled | The drop zone takes the disabled border, background and text. |
|
|
359
|
+
| A row | A `border` outline on the control background. A failed or rejected row has an `error` border, and its status is written in `error`. |
|
|
360
|
+
|
|
361
|
+
## Behavior
|
|
362
|
+
|
|
363
|
+
### Picking files
|
|
364
|
+
|
|
365
|
+
People pick files with the browse button, by dropping them on the drop zone, or by clicking the zone. With `multiple`, the default, each pick adds to the list. A file picked twice, with the same name, size, type and modification time, is skipped. With `multiple` off, a new file replaces the one in the list, and a running upload of the old one stops.
|
|
366
|
+
|
|
367
|
+
### Checks
|
|
368
|
+
|
|
369
|
+
Each file is checked as it's added:
|
|
370
|
+
|
|
371
|
+
| Check | Rejected when | Reason shown |
|
|
372
|
+
|---|---|---|
|
|
373
|
+
| `accept` | The file's extension or MIME type isn't in it | "This file type isn't accepted." |
|
|
374
|
+
| `maxFileSize` | The file is larger, in bytes | "The file is larger than 5 MB." |
|
|
375
|
+
| `minFileSize` | The file is smaller, in bytes | "The file is smaller than 1 byte." |
|
|
376
|
+
| `maxFiles` | The list already holds that many files, or one without `multiple` | "No more than 3 files can be added." |
|
|
377
|
+
|
|
378
|
+
`accept` matches extensions such as `.pdf` and MIME types such as `image/png` or `image/*`, like the dialog does. Dropped files are checked too, since the dialog's filter doesn't apply to them. Sizes are shown in multiples of 1024, so `5 * 1024 * 1024` reads as "5 MB".
|
|
379
|
+
|
|
380
|
+
A rejected file stays in the list with its reason until the next pick, or until it's removed. It isn't in the value, and isn't uploaded. The drop zone states the checks in effect, such as "File types: PDF or DOCX · Max. file size: 5 MB · Up to 3 files".
|
|
381
|
+
|
|
382
|
+
### Uploading
|
|
383
|
+
|
|
384
|
+
`upload` takes a function that uploads one file. It gets the file and a context, and returns a promise or an observable:
|
|
385
|
+
|
|
386
|
+
| It returns | Progress | The file's `response` | Failure |
|
|
387
|
+
|---|---|---|---|
|
|
388
|
+
| HttpClient's request with `observe: "events"` and `reportProgress: true` | Read from its upload progress events | The response's body | Its error |
|
|
389
|
+
| Any other observable | `context.progress(loaded, total)` | Its last value | Its error |
|
|
390
|
+
| A promise | `context.progress(loaded, total)` | What it resolves to | Its rejection, or an exception thrown by the function |
|
|
391
|
+
|
|
392
|
+
Until progress is reported, the progress bar runs without a value. `total` defaults to the file's size.
|
|
393
|
+
|
|
394
|
+
An upload stops when the file is removed, the list is cleared, the value is set from outside or the component is destroyed. An observable is unsubscribed, which cancels HttpClient's request, and `context.signal` is aborted. With `fetch`, pass the signal on:
|
|
395
|
+
|
|
396
|
+
```typescript
|
|
397
|
+
protected readonly upload: FileUploadHandler = async (file, { signal }) => {
|
|
398
|
+
const response = await fetch(`/api/files?name=${encodeURIComponent(file.name)}`, {
|
|
399
|
+
body: file,
|
|
400
|
+
method: "PUT",
|
|
401
|
+
signal
|
|
402
|
+
});
|
|
403
|
+
if (!response.ok) {
|
|
404
|
+
throw new Error(response.statusText);
|
|
405
|
+
}
|
|
406
|
+
return response.json();
|
|
407
|
+
};
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
`fetch` can't report upload progress, so the progress bar runs without a value until the file is uploaded.
|
|
411
|
+
|
|
412
|
+
Uploads run side by side, one per file. A failed file shows a retry button, and `uploadError` carries the error. `uploadSuccess` carries the item with its `response`.
|
|
413
|
+
|
|
414
|
+
`autoUpload` is on by default. When it's off, files wait in the list as "Ready to upload", below which an upload button and a clear button show. `uploadFiles()` starts the waiting uploads too.
|
|
415
|
+
|
|
416
|
+
### The value
|
|
417
|
+
|
|
418
|
+
| Mode | `value` holds |
|
|
419
|
+
|---|---|
|
|
420
|
+
| No `upload` function | The files that passed the checks |
|
|
421
|
+
| With an `upload` function | The files that finished uploading |
|
|
422
|
+
|
|
423
|
+
Rejected files are never in the value, and with an upload function, neither are files that are waiting, uploading or failed. Setting `value` from outside replaces the list: without an upload function the files are listed as picked, and with one as uploaded, which suits files that are already on the server. A form reset empties the list the same way.
|
|
424
|
+
|
|
425
|
+
`items()` lists every file with its status, progress, response and error, as `FileUploadItem`s. `uploading()` is `true` while any file uploads.
|
|
426
|
+
|
|
427
|
+
### Removing files
|
|
428
|
+
|
|
429
|
+
Each row's button removes its file, or cancels the upload while it runs. The `remove` event carries the item as it was, with its `response` if it was uploaded, so the server's copy can be deleted. `clearFiles()`, and the clear button, remove every file and emit `remove` for each but the rejected.
|
|
430
|
+
|
|
431
|
+
### Disabled and read-only
|
|
432
|
+
|
|
433
|
+
`disabled` turns off the drop zone, the browse button and the row buttons. Files dropped on a disabled zone are ignored, and the browser doesn't open them in the tab. `readonly` keeps the list as it is: the browse button is off, drops are ignored, and the rows have no buttons. A `mona-fieldset` with `disabled` disables the upload too.
|
|
434
|
+
|
|
435
|
+
## Forms Integration
|
|
436
|
+
|
|
437
|
+
`FileUploadComponent` implements `FormValueControl<File[]>` from `@angular/forms/signals`. The `FormField` directive writes `disabled`, `invalid`, `readonly`, `required` and `touched`.
|
|
438
|
+
|
|
439
|
+
Signal Forms' `required` doesn't count an empty list as missing. Pair it with a `validate` that reports `requiredError()`, so the form is invalid and the form field shows "This field is required.":
|
|
440
|
+
|
|
441
|
+
```typescript
|
|
442
|
+
import { form, required, requiredError, validate } from "@angular/forms/signals";
|
|
443
|
+
|
|
444
|
+
protected readonly claim = form(signal({ receipts: [] as File[] }), path => {
|
|
445
|
+
required(path.receipts);
|
|
446
|
+
validate(path.receipts, ({ value }) => (value().length === 0 ? requiredError() : undefined));
|
|
447
|
+
});
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
```html
|
|
451
|
+
<mona-form-field label="Receipts">
|
|
452
|
+
<mona-file-upload [formField]="claim.receipts" [upload]="upload"></mona-file-upload>
|
|
453
|
+
</mona-form-field>
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
The `touch` output fires when focus leaves the component, and when files are dropped on it. Moving between its buttons doesn't fire it, and neither does the file dialog taking the window's focus.
|
|
457
|
+
|
|
458
|
+
## Accessibility
|
|
459
|
+
|
|
460
|
+
The component is a `role="group"`, named by its label. The browse button is a native `<button>`, described by the checks under the drop zone, and by a form field's hint and errors. The native file input is hidden from assistive technology and from `Tab`, since the browse button opens it.
|
|
461
|
+
|
|
462
|
+
The list is named by the `files` message ("Files"). Each row's buttons are named for their file, such as "Remove report.pdf", "Cancel the upload of report.pdf" and "Retry the upload of report.pdf". A file's progress bar is a `role="progressbar"` named by the file's name.
|
|
463
|
+
|
|
464
|
+
| Attribute | Where | Value |
|
|
465
|
+
|---|---|---|
|
|
466
|
+
| `role` | Host | `"group"` |
|
|
467
|
+
| `aria-label`, `aria-labelledby` | Host | The matching inputs, or the label of a `mona-label` or `mona-form-field` around it |
|
|
468
|
+
| `aria-describedby` | Browse button | The checks under the drop zone, the component's `aria-describedby`, and a form field's hint and errors |
|
|
469
|
+
| `aria-hidden`, `tabindex` | Native file input | `"true"` and `-1` |
|
|
470
|
+
| `aria-label` | List | The `files` message |
|
|
471
|
+
| `aria-label` | Row buttons | The `remove`, `cancel` or `retry` message, with the file's name |
|
|
472
|
+
| `aria-describedby` | Retry button | The row's status, "The upload failed." |
|
|
473
|
+
| `role`, `aria-label` | Progress bar | `"progressbar"`, the file's name |
|
|
474
|
+
|
|
475
|
+
What happens is announced through a polite live region: the files added and the ones rejected with their reasons ("2 files added. setup.exe wasn't added. This file type isn't accepted."), each upload that finishes or fails, and each file removed.
|
|
476
|
+
|
|
477
|
+
When a row goes, focus moves to the row now in its place, the one before it, or the browse button. After a retry, focus moves to the row's cancel button, which takes the retry button's place. After the upload or clear button, focus moves to the browse button.
|
|
478
|
+
|
|
479
|
+
Dropping files isn't possible from the keyboard, so the browse button does everything the drop zone does.
|
|
480
|
+
|
|
481
|
+
A click on the caption of a `mona-label` or `mona-form-field` around it, or of a `mona-label` pointing at it with `[for]`, moves focus to the browse button. It doesn't open the dialog.
|
|
482
|
+
|
|
483
|
+
**Consumer responsibilities**
|
|
484
|
+
|
|
485
|
+
- Label the component. Put it in a `mona-form-field` or a `<mona-label text="...">`, point a label at it with `[for]`, or set `aria-labelledby` or `aria-label`.
|
|
486
|
+
- State checks the component doesn't know, such as a server's own limits, in a hint.
|
|
487
|
+
|
|
488
|
+
### Keyboard Interaction
|
|
489
|
+
|
|
490
|
+
| Key | Where | Action |
|
|
491
|
+
|---|---|---|
|
|
492
|
+
| `Tab` | Anywhere | Moves between the browse button, the row buttons and the upload and clear buttons |
|
|
493
|
+
| `Enter` / `Space` | Browse button | Opens the file dialog |
|
|
494
|
+
| `Enter` / `Space` | A row button | Removes the file, cancels its upload, or retries it |
|
|
495
|
+
|
|
496
|
+
## API
|
|
497
|
+
|
|
498
|
+
Everything below is imported from `@nanahoshi/mona-ui/file-upload`. Each name links to its reference page, with every input, output, property and method.
|
|
499
|
+
|
|
500
|
+
| Name | Kind | Description |
|
|
501
|
+
| --- | --- | --- |
|
|
502
|
+
| [`FileUploadComponent`](api/file-upload.md#fileuploadcomponent) | Component | Picks files from a drop zone or the browser's file dialog, checks their type, size and count, and lists them. |
|
|
503
|
+
| [`formatFileSize`](api/file-upload.md#formatfilesize) | Function | A file size in the locale, in the largest unit that keeps it at 1 or more: "512 bytes", "1.5 kB", "20 MB". |
|
|
504
|
+
|
|
505
|
+
### Types
|
|
506
|
+
|
|
507
|
+
| Type | Kind | Description |
|
|
508
|
+
| --- | --- | --- |
|
|
509
|
+
| [`FILE_UPLOAD_DEFAULT_MESSAGES`](api/file-upload.md#file-upload-default-messages) | Constant | |
|
|
510
|
+
| [`FileUploadContext`](api/file-upload.md#fileuploadcontext) | Interface | What the upload function gets with each file. |
|
|
511
|
+
| [`FileUploadItem`](api/file-upload.md#fileuploaditem) | Interface | A file in the upload's list, and where it stands. |
|
|
512
|
+
| [`FileUploadHandler`](api/file-upload.md#fileuploadhandler) | Type alias | Uploads one file, and returns a promise or an observable. |
|
|
513
|
+
| [`FileUploadRejection`](api/file-upload.md#fileuploadrejection) | Type alias | Why a file wasn't added: its type isn't in `accept` (`type`), it's larger than `maxFileSize` (`maxFileSize`) or smaller than `minFileSize` (`minFileSize`), or the list already holds as many files as it can (`maxFiles`). |
|
|
514
|
+
| [`FileUploadStatus`](api/file-upload.md#fileuploadstatus) | Type alias | Where a file in the list stands. |
|
|
515
|
+
|
|
516
|
+
**Related types:** [`MonaFileUploadMessages`](api/i18n.md#monafileuploadmessages)
|
package/docs/forms.md
CHANGED
|
@@ -103,6 +103,7 @@ If more than one applies, an explicit `aria-labelledby` wins, and then `aria-lab
|
|
|
103
103
|
| `mona-date-picker` | Yes | Focus only | Yes | Yes | No |
|
|
104
104
|
| `mona-date-range-picker` | Yes, as a group | Focus only | Yes, on the group | No | No |
|
|
105
105
|
| `mona-dropdown-list` | Yes | Focus only | Yes | No | No |
|
|
106
|
+
| `mona-file-upload` | Yes, as a group | Yes | Yes, on the group | No | No |
|
|
106
107
|
|
|
107
108
|
A check box and a radio button render their own `<label>`, and its text has to stay their name, so a wrapping `mona-label` doesn't name them. Use their `label` input, or `[for]` from outside.
|
|
108
109
|
|
|
@@ -132,6 +133,7 @@ Some controls can't offer everything:
|
|
|
132
133
|
- **Switch, rating, sliders, color picker and color gradient:** no `name`, since they have no native input to give it to.
|
|
133
134
|
- **Check box and radio button directives:** `input[monaCheckbox]` and `input[monaRadioButton]` have no `readonly`, because native check boxes and radios ignore it.
|
|
134
135
|
- **Rating:** `required` shows the error state at `0` once touched, but the signal forms `required` rule doesn't treat `0` as empty. To fail the form, use `min(path, 1)`.
|
|
136
|
+
- **File upload:** no `name`, since its files reach the form through the value. `required` shows the error state with no files once touched, but the signal forms `required` rule doesn't treat an empty list as empty. To fail the form, add a `validate` that returns `requiredError()` for an empty list.
|
|
135
137
|
|
|
136
138
|
---
|
|
137
139
|
|