@prismora/elements 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +21 -2
- package/components.json +15600 -0
- package/custom-elements.json +56320 -0
- package/dist/base.d.ts +42 -0
- package/dist/base.js +62 -0
- package/dist/catalog.d.ts +33 -0
- package/dist/catalog.js +803 -0
- package/dist/components/accordion.d.ts +31 -0
- package/dist/components/accordion.js +63 -0
- package/dist/components/alert.d.ts +27 -0
- package/dist/components/alert.js +40 -0
- package/dist/components/avatar-group.d.ts +23 -0
- package/dist/components/avatar-group.js +43 -0
- package/dist/components/avatar.d.ts +24 -0
- package/dist/components/avatar.js +43 -0
- package/dist/components/back-to-top.d.ts +32 -0
- package/dist/components/back-to-top.js +50 -0
- package/dist/components/badge.d.ts +17 -0
- package/dist/components/badge.js +20 -0
- package/dist/components/banner.d.ts +34 -0
- package/dist/components/banner.js +54 -0
- package/dist/components/breadcrumb.d.ts +42 -0
- package/dist/components/breadcrumb.js +63 -0
- package/dist/components/button-group.d.ts +14 -0
- package/dist/components/button-group.js +28 -0
- package/dist/components/button.d.ts +59 -0
- package/dist/components/button.js +162 -0
- package/dist/components/calendar.d.ts +48 -0
- package/dist/components/calendar.js +296 -0
- package/dist/components/card.d.ts +23 -0
- package/dist/components/card.js +44 -0
- package/dist/components/carousel.d.ts +41 -0
- package/dist/components/carousel.js +156 -0
- package/dist/components/cascader.d.ts +70 -0
- package/dist/components/cascader.js +220 -0
- package/dist/components/chart.d.ts +59 -0
- package/dist/components/chart.js +267 -0
- package/dist/components/chat.d.ts +98 -0
- package/dist/components/chat.js +134 -0
- package/dist/components/checkbox-group.d.ts +48 -0
- package/dist/components/checkbox-group.js +111 -0
- package/dist/components/checkbox.d.ts +23 -0
- package/dist/components/checkbox.js +70 -0
- package/dist/components/code-block.d.ts +46 -0
- package/dist/components/code-block.js +86 -0
- package/dist/components/color-picker.d.ts +46 -0
- package/dist/components/color-picker.js +292 -0
- package/dist/components/combobox.d.ts +89 -0
- package/dist/components/combobox.js +384 -0
- package/dist/components/command.d.ts +96 -0
- package/dist/components/command.js +289 -0
- package/dist/components/context-menu.d.ts +23 -0
- package/dist/components/context-menu.js +110 -0
- package/dist/components/copy-button.d.ts +33 -0
- package/dist/components/copy-button.js +62 -0
- package/dist/components/countdown.d.ts +36 -0
- package/dist/components/countdown.js +56 -0
- package/dist/components/data-table.d.ts +180 -0
- package/dist/components/data-table.js +1060 -0
- package/dist/components/date-picker.d.ts +47 -0
- package/dist/components/date-picker.js +267 -0
- package/dist/components/description-list.d.ts +38 -0
- package/dist/components/description-list.js +41 -0
- package/dist/components/dialog.d.ts +47 -0
- package/dist/components/dialog.js +189 -0
- package/dist/components/divider.d.ts +16 -0
- package/dist/components/divider.js +23 -0
- package/dist/components/drawer.d.ts +19 -0
- package/dist/components/drawer.js +27 -0
- package/dist/components/editor.d.ts +50 -0
- package/dist/components/editor.js +474 -0
- package/dist/components/empty-state.d.ts +24 -0
- package/dist/components/empty-state.js +27 -0
- package/dist/components/file-upload.d.ts +56 -0
- package/dist/components/file-upload.js +176 -0
- package/dist/components/form-control.d.ts +69 -0
- package/dist/components/form-control.js +134 -0
- package/dist/components/form.d.ts +38 -0
- package/dist/components/form.js +161 -0
- package/dist/components/format-number.d.ts +37 -0
- package/dist/components/format-number.js +86 -0
- package/dist/components/icon.d.ts +34 -0
- package/dist/components/icon.js +47 -0
- package/dist/components/image.d.ts +44 -0
- package/dist/components/image.js +53 -0
- package/dist/components/indicator.d.ts +41 -0
- package/dist/components/indicator.js +65 -0
- package/dist/components/inline-edit.d.ts +45 -0
- package/dist/components/inline-edit.js +129 -0
- package/dist/components/input.d.ts +55 -0
- package/dist/components/input.js +186 -0
- package/dist/components/kbd.d.ts +15 -0
- package/dist/components/kbd.js +18 -0
- package/dist/components/list.d.ts +68 -0
- package/dist/components/list.js +180 -0
- package/dist/components/menu.d.ts +22 -0
- package/dist/components/menu.js +182 -0
- package/dist/components/menubar.d.ts +20 -0
- package/dist/components/menubar.js +120 -0
- package/dist/components/meter.d.ts +38 -0
- package/dist/components/meter.js +48 -0
- package/dist/components/nav.d.ts +91 -0
- package/dist/components/nav.js +228 -0
- package/dist/components/number-input.d.ts +26 -0
- package/dist/components/number-input.js +70 -0
- package/dist/components/pagination.d.ts +44 -0
- package/dist/components/pagination.js +94 -0
- package/dist/components/pin-input.d.ts +25 -0
- package/dist/components/pin-input.js +128 -0
- package/dist/components/popover.d.ts +23 -0
- package/dist/components/popover.js +140 -0
- package/dist/components/progress.d.ts +26 -0
- package/dist/components/progress.js +66 -0
- package/dist/components/pull-refresh.d.ts +41 -0
- package/dist/components/pull-refresh.js +101 -0
- package/dist/components/qr-code.d.ts +33 -0
- package/dist/components/qr-code.js +54 -0
- package/dist/components/radio-group.d.ts +37 -0
- package/dist/components/radio-group.js +82 -0
- package/dist/components/range-slider.d.ts +46 -0
- package/dist/components/range-slider.js +95 -0
- package/dist/components/rating.d.ts +38 -0
- package/dist/components/rating.js +80 -0
- package/dist/components/relative-time.d.ts +31 -0
- package/dist/components/relative-time.js +51 -0
- package/dist/components/scheduler.d.ts +84 -0
- package/dist/components/scheduler.js +187 -0
- package/dist/components/segmented.d.ts +58 -0
- package/dist/components/segmented.js +129 -0
- package/dist/components/select.d.ts +62 -0
- package/dist/components/select.js +140 -0
- package/dist/components/share-button.d.ts +37 -0
- package/dist/components/share-button.js +60 -0
- package/dist/components/signature.d.ts +54 -0
- package/dist/components/signature.js +152 -0
- package/dist/components/skeleton.d.ts +21 -0
- package/dist/components/skeleton.js +26 -0
- package/dist/components/slider.d.ts +31 -0
- package/dist/components/slider.js +80 -0
- package/dist/components/sortable.d.ts +22 -0
- package/dist/components/sortable.js +196 -0
- package/dist/components/sparkline.d.ts +28 -0
- package/dist/components/sparkline.js +46 -0
- package/dist/components/spinner.d.ts +16 -0
- package/dist/components/spinner.js +22 -0
- package/dist/components/splitter.d.ts +28 -0
- package/dist/components/splitter.js +89 -0
- package/dist/components/stat.d.ts +28 -0
- package/dist/components/stat.js +33 -0
- package/dist/components/steps.d.ts +53 -0
- package/dist/components/steps.js +91 -0
- package/dist/components/switch.d.ts +12 -0
- package/dist/components/switch.js +18 -0
- package/dist/components/table.d.ts +58 -0
- package/dist/components/table.js +96 -0
- package/dist/components/tabs.d.ts +62 -0
- package/dist/components/tabs.js +310 -0
- package/dist/components/tag.d.ts +32 -0
- package/dist/components/tag.js +58 -0
- package/dist/components/textarea.d.ts +25 -0
- package/dist/components/textarea.js +56 -0
- package/dist/components/theme-switch.d.ts +51 -0
- package/dist/components/theme-switch.js +85 -0
- package/dist/components/time-picker.d.ts +39 -0
- package/dist/components/time-picker.js +257 -0
- package/dist/components/timeline.d.ts +38 -0
- package/dist/components/timeline.js +56 -0
- package/dist/components/toaster.d.ts +25 -0
- package/dist/components/toaster.js +44 -0
- package/dist/components/toggle-group.d.ts +70 -0
- package/dist/components/toggle-group.js +172 -0
- package/dist/components/toolbar.d.ts +23 -0
- package/dist/components/toolbar.js +81 -0
- package/dist/components/tooltip.d.ts +18 -0
- package/dist/components/tooltip.js +92 -0
- package/dist/components/tour.d.ts +41 -0
- package/dist/components/tour.js +197 -0
- package/dist/components/transfer.d.ts +57 -0
- package/dist/components/transfer.js +161 -0
- package/dist/components/tree-select.d.ts +66 -0
- package/dist/components/tree-select.js +282 -0
- package/dist/components/tree.d.ts +129 -0
- package/dist/components/tree.js +427 -0
- package/dist/components/virtual-list.d.ts +41 -0
- package/dist/components/virtual-list.js +77 -0
- package/dist/confirm.d.ts +21 -0
- package/dist/confirm.js +69 -0
- package/dist/count.d.ts +5 -0
- package/dist/count.js +12 -0
- package/dist/data-table/icons.d.ts +8 -0
- package/dist/data-table/icons.js +10 -0
- package/dist/data-table/logic.d.ts +33 -0
- package/dist/data-table/logic.js +141 -0
- package/dist/data-table/strings.d.ts +37 -0
- package/dist/data-table/strings.js +36 -0
- package/dist/data-table/types.d.ts +76 -0
- package/dist/data-table/types.js +1 -0
- package/dist/define.d.ts +2 -0
- package/dist/define.js +2 -0
- package/dist/field-validity.d.ts +42 -0
- package/dist/field-validity.js +58 -0
- package/dist/floating.d.ts +13 -0
- package/dist/floating.js +65 -0
- package/dist/highlight.d.ts +11 -0
- package/dist/highlight.js +103 -0
- package/dist/id.d.ts +2 -0
- package/dist/id.js +3 -0
- package/dist/index.d.ts +102 -0
- package/dist/index.js +102 -0
- package/dist/manifest.d.ts +51 -0
- package/dist/manifest.js +11 -0
- package/dist/named-icon.d.ts +8 -0
- package/dist/named-icon.js +8 -0
- package/dist/patterns/account.d.ts +6 -0
- package/dist/patterns/account.js +137 -0
- package/dist/patterns/app.d.ts +7 -0
- package/dist/patterns/app.js +351 -0
- package/dist/patterns/commerce.d.ts +4 -0
- package/dist/patterns/commerce.js +200 -0
- package/dist/patterns/flows.d.ts +3 -0
- package/dist/patterns/flows.js +100 -0
- package/dist/patterns/index.d.ts +9 -0
- package/dist/patterns/index.js +29 -0
- package/dist/patterns/marketing.d.ts +4 -0
- package/dist/patterns/marketing.js +173 -0
- package/dist/patterns/more.d.ts +15 -0
- package/dist/patterns/more.js +581 -0
- package/dist/patterns/shell.d.ts +9 -0
- package/dist/patterns/shell.js +48 -0
- package/dist/patterns/types.d.ts +19 -0
- package/dist/patterns/types.js +1 -0
- package/dist/position.d.ts +2 -0
- package/dist/position.js +33 -0
- package/dist/prismora.standalone.js +5222 -0
- package/dist/prismora.standalone.js.map +199 -0
- package/dist/qr.d.ts +14 -0
- package/dist/qr.js +258 -0
- package/dist/register.d.ts +8 -0
- package/dist/register.js +21 -0
- package/dist/registry.d.ts +203 -0
- package/dist/registry.js +203 -0
- package/dist/remote.d.ts +58 -0
- package/dist/remote.js +130 -0
- package/dist/sheet.d.ts +34 -0
- package/dist/sheet.js +153 -0
- package/dist/styles/accordion.d.ts +2 -0
- package/dist/styles/accordion.js +3 -0
- package/dist/styles/alert.d.ts +2 -0
- package/dist/styles/alert.js +3 -0
- package/dist/styles/avatar.d.ts +2 -0
- package/dist/styles/avatar.js +3 -0
- package/dist/styles/badge.d.ts +2 -0
- package/dist/styles/badge.js +3 -0
- package/dist/styles/banner.d.ts +2 -0
- package/dist/styles/banner.js +3 -0
- package/dist/styles/breadcrumb.d.ts +2 -0
- package/dist/styles/breadcrumb.js +3 -0
- package/dist/styles/button.d.ts +2 -0
- package/dist/styles/button.js +3 -0
- package/dist/styles/calendar.d.ts +2 -0
- package/dist/styles/calendar.js +3 -0
- package/dist/styles/card.d.ts +2 -0
- package/dist/styles/card.js +3 -0
- package/dist/styles/carousel.d.ts +2 -0
- package/dist/styles/carousel.js +3 -0
- package/dist/styles/chart.d.ts +2 -0
- package/dist/styles/chart.js +3 -0
- package/dist/styles/chat.d.ts +2 -0
- package/dist/styles/chat.js +3 -0
- package/dist/styles/checkbox.d.ts +2 -0
- package/dist/styles/checkbox.js +3 -0
- package/dist/styles/color-picker.d.ts +2 -0
- package/dist/styles/color-picker.js +3 -0
- package/dist/styles/combobox.d.ts +2 -0
- package/dist/styles/combobox.js +3 -0
- package/dist/styles/command.d.ts +2 -0
- package/dist/styles/command.js +3 -0
- package/dist/styles/data-table.d.ts +2 -0
- package/dist/styles/data-table.js +3 -0
- package/dist/styles/description-list.d.ts +2 -0
- package/dist/styles/description-list.js +3 -0
- package/dist/styles/dialog.d.ts +2 -0
- package/dist/styles/dialog.js +3 -0
- package/dist/styles/drawer.d.ts +2 -0
- package/dist/styles/drawer.js +3 -0
- package/dist/styles/editor.d.ts +2 -0
- package/dist/styles/editor.js +3 -0
- package/dist/styles/empty-state.d.ts +2 -0
- package/dist/styles/empty-state.js +3 -0
- package/dist/styles/extras.d.ts +2 -0
- package/dist/styles/extras.js +3 -0
- package/dist/styles/field.d.ts +2 -0
- package/dist/styles/field.js +3 -0
- package/dist/styles/file-upload.d.ts +2 -0
- package/dist/styles/file-upload.js +3 -0
- package/dist/styles/foundation.d.ts +2 -0
- package/dist/styles/foundation.js +3 -0
- package/dist/styles/helpers.d.ts +2 -0
- package/dist/styles/helpers.js +3 -0
- package/dist/styles/icon.d.ts +2 -0
- package/dist/styles/icon.js +3 -0
- package/dist/styles/image.d.ts +2 -0
- package/dist/styles/image.js +3 -0
- package/dist/styles/indicator.d.ts +2 -0
- package/dist/styles/indicator.js +3 -0
- package/dist/styles/input.d.ts +2 -0
- package/dist/styles/input.js +3 -0
- package/dist/styles/kbd.d.ts +2 -0
- package/dist/styles/kbd.js +3 -0
- package/dist/styles/list.d.ts +2 -0
- package/dist/styles/list.js +3 -0
- package/dist/styles/listbox.d.ts +2 -0
- package/dist/styles/listbox.js +3 -0
- package/dist/styles/meter.d.ts +2 -0
- package/dist/styles/meter.js +3 -0
- package/dist/styles/nav.d.ts +2 -0
- package/dist/styles/nav.js +3 -0
- package/dist/styles/number-input.d.ts +2 -0
- package/dist/styles/number-input.js +3 -0
- package/dist/styles/overlay.d.ts +2 -0
- package/dist/styles/overlay.js +3 -0
- package/dist/styles/pagination.d.ts +2 -0
- package/dist/styles/pagination.js +3 -0
- package/dist/styles/pickers.d.ts +2 -0
- package/dist/styles/pickers.js +3 -0
- package/dist/styles/pin-input.d.ts +2 -0
- package/dist/styles/pin-input.js +3 -0
- package/dist/styles/popover.d.ts +2 -0
- package/dist/styles/popover.js +3 -0
- package/dist/styles/progress.d.ts +2 -0
- package/dist/styles/progress.js +3 -0
- package/dist/styles/pull-refresh.d.ts +2 -0
- package/dist/styles/pull-refresh.js +3 -0
- package/dist/styles/range-slider.d.ts +2 -0
- package/dist/styles/range-slider.js +3 -0
- package/dist/styles/rating.d.ts +2 -0
- package/dist/styles/rating.js +3 -0
- package/dist/styles/scheduler.d.ts +2 -0
- package/dist/styles/scheduler.js +3 -0
- package/dist/styles/segmented.d.ts +2 -0
- package/dist/styles/segmented.js +3 -0
- package/dist/styles/select.d.ts +2 -0
- package/dist/styles/select.js +3 -0
- package/dist/styles/separator.d.ts +2 -0
- package/dist/styles/separator.js +3 -0
- package/dist/styles/sheet.d.ts +2 -0
- package/dist/styles/sheet.js +3 -0
- package/dist/styles/skeleton.d.ts +2 -0
- package/dist/styles/skeleton.js +3 -0
- package/dist/styles/slider.d.ts +2 -0
- package/dist/styles/slider.js +3 -0
- package/dist/styles/sortable.d.ts +2 -0
- package/dist/styles/sortable.js +3 -0
- package/dist/styles/spinner.d.ts +2 -0
- package/dist/styles/spinner.js +3 -0
- package/dist/styles/splitter.d.ts +2 -0
- package/dist/styles/splitter.js +3 -0
- package/dist/styles/stat.d.ts +2 -0
- package/dist/styles/stat.js +3 -0
- package/dist/styles/steps.d.ts +2 -0
- package/dist/styles/steps.js +3 -0
- package/dist/styles/surface.d.ts +2 -0
- package/dist/styles/surface.js +3 -0
- package/dist/styles/switch.d.ts +2 -0
- package/dist/styles/switch.js +3 -0
- package/dist/styles/table.d.ts +2 -0
- package/dist/styles/table.js +3 -0
- package/dist/styles/tabs.d.ts +2 -0
- package/dist/styles/tabs.js +3 -0
- package/dist/styles/tag.d.ts +2 -0
- package/dist/styles/tag.js +3 -0
- package/dist/styles/time-picker.d.ts +2 -0
- package/dist/styles/time-picker.js +3 -0
- package/dist/styles/timeline.d.ts +2 -0
- package/dist/styles/timeline.js +3 -0
- package/dist/styles/toast.d.ts +2 -0
- package/dist/styles/toast.js +3 -0
- package/dist/styles/toggle.d.ts +2 -0
- package/dist/styles/toggle.js +3 -0
- package/dist/styles/toolbar.d.ts +2 -0
- package/dist/styles/toolbar.js +3 -0
- package/dist/styles/tooltip.d.ts +2 -0
- package/dist/styles/tooltip.js +3 -0
- package/dist/styles/tree.d.ts +2 -0
- package/dist/styles/tree.js +3 -0
- package/dist/table-core.d.ts +75 -0
- package/dist/table-core.js +205 -0
- package/dist/toast.d.ts +42 -0
- package/dist/toast.js +206 -0
- package/dist/top-layer.d.ts +4 -0
- package/dist/top-layer.js +33 -0
- package/package.json +101 -4
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type PropertyDeclarations } from 'lit';
|
|
2
|
+
import { PmElement } from '../base.js';
|
|
3
|
+
/** Does a file match an `accept` list (".pdf,image/*")? */
|
|
4
|
+
export declare function acceptsFile(file: File, accept?: string): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* File upload: drag-and-drop zone, file picker and paste (Ctrl/⌘+V on the focused zone), with type/size/count
|
|
7
|
+
* checks, image previews and a removable file list.
|
|
8
|
+
*
|
|
9
|
+
* Show upload progress per file with `setProgress(file, %)` and `setStatus(file, 'done' | 'error', message)`.
|
|
10
|
+
* `capture` opens the camera on phones. Form-associated: submits the chosen files under `name`.
|
|
11
|
+
* @tag pm-file-upload
|
|
12
|
+
* @slot - Custom drop-zone text.
|
|
13
|
+
* @fires pm-change - Files added or removed. detail: { files }
|
|
14
|
+
* @fires pm-reject - A file was refused. detail: { file, reason: 'type' | 'size' | 'count' }
|
|
15
|
+
* @example <pm-file-upload label="Attachments" accept="image/*,.pdf" multiple max-size="5242880"></pm-file-upload>
|
|
16
|
+
*/
|
|
17
|
+
export declare class PmFileUpload extends PmElement {
|
|
18
|
+
#private;
|
|
19
|
+
static componentName: string;
|
|
20
|
+
static formAssociated: boolean;
|
|
21
|
+
static styles: import("lit").CSSResult[];
|
|
22
|
+
static properties: PropertyDeclarations;
|
|
23
|
+
/** Phones: open the camera directly (`user` front, `environment` back). */
|
|
24
|
+
capture?: 'user' | 'environment';
|
|
25
|
+
/** Upload progress 0–100 for a file (shows a bar; 100 marks it done). */
|
|
26
|
+
setProgress(file: File, percent: number): void;
|
|
27
|
+
/** Mark a file done or failed (with a message shown under it). */
|
|
28
|
+
setStatus(file: File, status: 'uploading' | 'done' | 'error', message?: string): void;
|
|
29
|
+
name?: string;
|
|
30
|
+
label?: string;
|
|
31
|
+
hint?: string;
|
|
32
|
+
/** Allowed types, like the native attribute: ".pdf,image/*". */
|
|
33
|
+
accept?: string;
|
|
34
|
+
multiple: boolean;
|
|
35
|
+
/** Maximum size per file, in bytes. */
|
|
36
|
+
maxSize?: number;
|
|
37
|
+
/** Most files accepted when `multiple` is set. */
|
|
38
|
+
maxFiles?: number;
|
|
39
|
+
disabled: boolean;
|
|
40
|
+
/** Custom error message (e.g. a server-side rejection). Setting it marks the field invalid. */
|
|
41
|
+
error?: string;
|
|
42
|
+
required: boolean;
|
|
43
|
+
/** Selected files. */
|
|
44
|
+
files: File[];
|
|
45
|
+
_dragging: boolean;
|
|
46
|
+
_message: string;
|
|
47
|
+
constructor();
|
|
48
|
+
formResetCallback(): void;
|
|
49
|
+
formDisabledCallback(d: boolean): void;
|
|
50
|
+
disconnectedCallback(): void;
|
|
51
|
+
protected updated(): void;
|
|
52
|
+
/** Add files (validated); returns the accepted ones. */
|
|
53
|
+
addFiles(list: Iterable<File>): File[];
|
|
54
|
+
removeFile(file: File): void;
|
|
55
|
+
render(): import("lit").TemplateResult<1>;
|
|
56
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { css, html, nothing } from 'lit';
|
|
2
|
+
import { PmElement, defineElement } from '../base.js';
|
|
3
|
+
import { FieldValidity } from '../field-validity.js';
|
|
4
|
+
import { nextId } from '../id.js';
|
|
5
|
+
import fieldCss from '../styles/field.js';
|
|
6
|
+
import fileCss from '../styles/file-upload.js';
|
|
7
|
+
const formatBytes = (n) => (n < 1024 ? `${n} B` : n < 1048576 ? `${(n / 1024).toFixed(1)} KB` : `${(n / 1048576).toFixed(1)} MB`);
|
|
8
|
+
/** Does a file match an `accept` list (".pdf,image/*")? */
|
|
9
|
+
export function acceptsFile(file, accept) {
|
|
10
|
+
if (!accept)
|
|
11
|
+
return true;
|
|
12
|
+
return accept.split(',').map((a) => a.trim().toLowerCase()).filter(Boolean).some((a) => {
|
|
13
|
+
if (a.startsWith('.'))
|
|
14
|
+
return file.name.toLowerCase().endsWith(a);
|
|
15
|
+
if (a.endsWith('/*'))
|
|
16
|
+
return file.type.toLowerCase().startsWith(a.slice(0, -1));
|
|
17
|
+
return file.type.toLowerCase() === a;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* File upload: drag-and-drop zone, file picker and paste (Ctrl/⌘+V on the focused zone), with type/size/count
|
|
22
|
+
* checks, image previews and a removable file list.
|
|
23
|
+
*
|
|
24
|
+
* Show upload progress per file with `setProgress(file, %)` and `setStatus(file, 'done' | 'error', message)`.
|
|
25
|
+
* `capture` opens the camera on phones. Form-associated: submits the chosen files under `name`.
|
|
26
|
+
* @tag pm-file-upload
|
|
27
|
+
* @slot - Custom drop-zone text.
|
|
28
|
+
* @fires pm-change - Files added or removed. detail: { files }
|
|
29
|
+
* @fires pm-reject - A file was refused. detail: { file, reason: 'type' | 'size' | 'count' }
|
|
30
|
+
* @example <pm-file-upload label="Attachments" accept="image/*,.pdf" multiple max-size="5242880"></pm-file-upload>
|
|
31
|
+
*/
|
|
32
|
+
export class PmFileUpload extends PmElement {
|
|
33
|
+
static { this.componentName = 'file-upload'; }
|
|
34
|
+
static { this.formAssociated = true; }
|
|
35
|
+
static { this.styles = [...PmElement.styles, fieldCss, fileCss, css `:host { display: block; } :host([hidden]) { display: none; } input[type=file] { display: none; }`]; }
|
|
36
|
+
static { this.properties = {
|
|
37
|
+
name: { reflect: true }, label: {}, hint: {}, error: {}, accept: {}, multiple: { type: Boolean }, maxSize: { type: Number, attribute: 'max-size' },
|
|
38
|
+
maxFiles: { type: Number, attribute: 'max-files' }, disabled: { type: Boolean, reflect: true }, required: { type: Boolean, reflect: true },
|
|
39
|
+
files: { attribute: false }, capture: {}, _dragging: { state: true }, _message: { state: true },
|
|
40
|
+
}; }
|
|
41
|
+
#state = new Map();
|
|
42
|
+
/** Upload progress 0–100 for a file (shows a bar; 100 marks it done). */
|
|
43
|
+
setProgress(file, percent) {
|
|
44
|
+
const p = Math.max(0, Math.min(100, percent));
|
|
45
|
+
this.#state.set(file, { ...this.#state.get(file), progress: p, status: p >= 100 ? 'done' : 'uploading' });
|
|
46
|
+
this.requestUpdate();
|
|
47
|
+
}
|
|
48
|
+
/** Mark a file done or failed (with a message shown under it). */
|
|
49
|
+
setStatus(file, status, message) {
|
|
50
|
+
this.#state.set(file, { ...this.#state.get(file), status, message });
|
|
51
|
+
this.requestUpdate();
|
|
52
|
+
}
|
|
53
|
+
#onPaste = (e) => {
|
|
54
|
+
const files = [...(e.clipboardData?.files ?? [])];
|
|
55
|
+
if (!files.length || this.disabled)
|
|
56
|
+
return;
|
|
57
|
+
e.preventDefault();
|
|
58
|
+
this.addFiles(files);
|
|
59
|
+
};
|
|
60
|
+
#internals = this.attachInternals();
|
|
61
|
+
#field = new FieldValidity(this, this.#internals, {
|
|
62
|
+
hint: () => this.hint, error: () => this.error, missing: () => this.required && !this.files.length && !this.disabled,
|
|
63
|
+
missingMessage: 'Please choose a file.', anchor: () => this.renderRoot.querySelector('.pm-dropzone'),
|
|
64
|
+
});
|
|
65
|
+
#id = nextId('file');
|
|
66
|
+
#urls = new Map();
|
|
67
|
+
constructor() { super(); this.multiple = false; this.disabled = false; this.required = false; this.files = []; this._dragging = false; this._message = ''; }
|
|
68
|
+
formResetCallback() { this.files = []; this.#field.reset(); }
|
|
69
|
+
formDisabledCallback(d) { this.disabled = d; }
|
|
70
|
+
disconnectedCallback() { super.disconnectedCallback(); for (const u of this.#urls.values())
|
|
71
|
+
URL.revokeObjectURL(u); this.#urls.clear(); }
|
|
72
|
+
updated() {
|
|
73
|
+
const data = new FormData();
|
|
74
|
+
for (const f of this.files)
|
|
75
|
+
data.append(this.name ?? 'files', f);
|
|
76
|
+
this.#internals.setFormValue(this.files.length ? data : null);
|
|
77
|
+
}
|
|
78
|
+
/** Add files (validated); returns the accepted ones. */
|
|
79
|
+
addFiles(list) {
|
|
80
|
+
const accepted = [];
|
|
81
|
+
const reasons = [];
|
|
82
|
+
for (const file of list) {
|
|
83
|
+
const max = this.multiple ? (this.maxFiles ?? Infinity) : 1;
|
|
84
|
+
let reason;
|
|
85
|
+
if (!acceptsFile(file, this.accept))
|
|
86
|
+
reason = 'type';
|
|
87
|
+
else if (this.maxSize && file.size > this.maxSize)
|
|
88
|
+
reason = 'size';
|
|
89
|
+
else if (this.multiple && this.files.length + accepted.length >= max)
|
|
90
|
+
reason = 'count';
|
|
91
|
+
if (reason) {
|
|
92
|
+
reasons.push(`${file.name}: ${reason === 'type' ? 'file type not allowed' : reason === 'size' ? `larger than ${formatBytes(this.maxSize)}` : `limit of ${max} files`}`);
|
|
93
|
+
this.emit('pm-reject', { file, reason });
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
accepted.push(file);
|
|
97
|
+
if (!this.multiple)
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
if (accepted.length) {
|
|
101
|
+
this.files = this.multiple ? [...this.files, ...accepted] : accepted.slice(0, 1);
|
|
102
|
+
this.emit('pm-change', { files: this.files });
|
|
103
|
+
}
|
|
104
|
+
this._message = reasons.join('. ');
|
|
105
|
+
return accepted;
|
|
106
|
+
}
|
|
107
|
+
removeFile(file) {
|
|
108
|
+
this.files = this.files.filter((f) => f !== file);
|
|
109
|
+
this.#state.delete(file);
|
|
110
|
+
const u = this.#urls.get(file);
|
|
111
|
+
if (u) {
|
|
112
|
+
URL.revokeObjectURL(u);
|
|
113
|
+
this.#urls.delete(file);
|
|
114
|
+
}
|
|
115
|
+
this.emit('pm-change', { files: this.files });
|
|
116
|
+
}
|
|
117
|
+
#browse() { if (!this.disabled)
|
|
118
|
+
this.renderRoot.querySelector('input[type=file]')?.click(); }
|
|
119
|
+
#onPick(e) { const i = e.target; if (i.files)
|
|
120
|
+
this.addFiles(i.files); i.value = ''; }
|
|
121
|
+
#onDrop(e) {
|
|
122
|
+
e.preventDefault();
|
|
123
|
+
this._dragging = false;
|
|
124
|
+
if (!this.disabled && e.dataTransfer?.files)
|
|
125
|
+
this.addFiles(e.dataTransfer.files);
|
|
126
|
+
}
|
|
127
|
+
#onDrag(e, on) {
|
|
128
|
+
e.preventDefault();
|
|
129
|
+
if (!this.disabled)
|
|
130
|
+
this._dragging = on;
|
|
131
|
+
}
|
|
132
|
+
#thumb(f) {
|
|
133
|
+
if (!f.type.startsWith('image/'))
|
|
134
|
+
return html `<span class="pm-file-thumb" aria-hidden="true">${f.name.split('.').pop()?.slice(0, 4).toUpperCase()}</span>`;
|
|
135
|
+
let u = this.#urls.get(f);
|
|
136
|
+
if (!u) {
|
|
137
|
+
u = URL.createObjectURL(f);
|
|
138
|
+
this.#urls.set(f, u);
|
|
139
|
+
}
|
|
140
|
+
return html `<img class="pm-file-thumb" src=${u} alt="" />`;
|
|
141
|
+
}
|
|
142
|
+
render() {
|
|
143
|
+
const hasCustom = this.childElementCount > 0 || !!this.textContent?.trim();
|
|
144
|
+
// One error line: validation (custom error / required) first, else the last rejected-file message.
|
|
145
|
+
const error = this.#field.message || this._message;
|
|
146
|
+
return html `<div class="pm-field" part="field">
|
|
147
|
+
${this.label ? html `<span class="pm-label" id=${`${this.#id}-l`} ?data-required=${this.required}>${this.label}</span>` : nothing}
|
|
148
|
+
<div class="pm-dropzone" part="dropzone" role="button" tabindex=${this.disabled ? -1 : 0} ?data-dragging=${this._dragging} ?data-disabled=${this.disabled}
|
|
149
|
+
aria-labelledby=${[this.label ? `${this.#id}-l` : '', `${this.#id}-z`, this.accept || this.maxSize ? `${this.#id}-a` : ''].filter(Boolean).join(' ')} aria-describedby=${[this.hint ? this.#field.hintId : '', error ? this.#field.errorId : ''].filter(Boolean).join(' ') || nothing} aria-invalid=${this.#field.invalid ? 'true' : nothing} aria-disabled=${this.disabled ? 'true' : nothing}
|
|
150
|
+
@click=${this.#browse} @paste=${this.#onPaste} @keydown=${(e) => { if (e.key === 'Enter' || e.key === ' ') {
|
|
151
|
+
e.preventDefault();
|
|
152
|
+
this.#browse();
|
|
153
|
+
} }}
|
|
154
|
+
@dragenter=${(e) => this.#onDrag(e, true)} @dragover=${(e) => this.#onDrag(e, true)} @dragleave=${(e) => this.#onDrag(e, false)} @drop=${this.#onDrop}>
|
|
155
|
+
<span class="pm-dropzone-icon" aria-hidden="true"><svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 15V4M7 9l5-5 5 5M5 20h14"/></svg></span>
|
|
156
|
+
<span id=${`${this.#id}-z`}>${hasCustom ? html `<slot></slot>` : html `<strong>Choose ${this.multiple ? 'files' : 'a file'}</strong> or drag ${this.multiple ? 'them' : 'it'} here`}</span>
|
|
157
|
+
${this.accept || this.maxSize ? html `<span class="pm-text-xs" id=${`${this.#id}-a`}>${[this.accept?.replace(/,/g, ', '), this.maxSize ? `up to ${formatBytes(this.maxSize)}` : ''].filter(Boolean).join(' · ')}</span>` : nothing}
|
|
158
|
+
</div>
|
|
159
|
+
<input type="file" accept=${this.accept ?? nothing} capture=${this.capture ?? nothing} ?multiple=${this.multiple} ?disabled=${this.disabled} tabindex="-1" aria-hidden="true" @change=${this.#onPick} />
|
|
160
|
+
${this.hint ? html `<span class="pm-hint" id=${this.#field.hintId} part="hint">${this.hint}</span>` : nothing}
|
|
161
|
+
<span class="pm-error" id=${this.#field.errorId} part="error" role="alert" ?hidden=${!error}>${error}</span>
|
|
162
|
+
${this.files.length ? html `<ul class="pm-file-list" part="list" aria-label="Selected files">
|
|
163
|
+
${this.files.map((f) => {
|
|
164
|
+
const st = this.#state.get(f);
|
|
165
|
+
return html `<li class="pm-file" part="file" data-status=${st?.status ?? nothing}>${this.#thumb(f)}
|
|
166
|
+
<span style="min-inline-size: 0; display: grid; gap: 2px"><span class="pm-file-name">${f.name}</span>
|
|
167
|
+
<span class="pm-file-meta">${formatBytes(f.size)}${st?.status === 'done' ? ' · Uploaded' : st?.status === 'error' ? html ` · <span class="pm-file-error">${st.message ?? 'Upload failed'}</span>` : st?.progress !== undefined ? ` · ${Math.round(st.progress)}%` : ''}</span>
|
|
168
|
+
${st?.status === 'uploading' && st.progress !== undefined ? html `<span class="pm-file-progress" role="progressbar" aria-label=${`Uploading ${f.name}`} aria-valuenow=${Math.round(st.progress)} aria-valuemin="0" aria-valuemax="100"><span style=${`inline-size:${st.progress}%`}></span></span>` : nothing}</span>
|
|
169
|
+
<button type="button" class="pm-input-action pm-tag-remove" style="position: static; margin: 0; inline-size: 2rem; block-size: 2rem; border: 0; background: none; color: inherit; cursor: pointer; border-radius: var(--pm-radius-sm)"
|
|
170
|
+
aria-label=${`Remove ${f.name}`} @click=${() => this.removeFile(f)}>✕</button></li>`;
|
|
171
|
+
})}
|
|
172
|
+
</ul>` : nothing}
|
|
173
|
+
</div>`;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
defineElement('pm-file-upload', PmFileUpload);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type PropertyDeclarations, type PropertyValues } from 'lit';
|
|
2
|
+
import { PmElement, type Size } from '../base.js';
|
|
3
|
+
export type NativeControl = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
|
4
|
+
/**
|
|
5
|
+
* Form-associated base: label/hint/error wiring, validity mirroring, form reset/restore.
|
|
6
|
+
* @fires pm-input - On every value change while editing. detail: { value }
|
|
7
|
+
* @fires pm-change - When the value is committed. detail: { value }
|
|
8
|
+
* Label, hint and error render inside the same shadow root as the control so ARIA ids resolve.
|
|
9
|
+
*/
|
|
10
|
+
export declare class PmFormControl extends PmElement {
|
|
11
|
+
#private;
|
|
12
|
+
static formAssociated: boolean;
|
|
13
|
+
static shadowRootOptions: {
|
|
14
|
+
delegatesFocus: boolean;
|
|
15
|
+
clonable?: boolean;
|
|
16
|
+
customElementRegistry?: CustomElementRegistry;
|
|
17
|
+
mode: ShadowRootMode;
|
|
18
|
+
serializable?: boolean;
|
|
19
|
+
slotAssignment?: SlotAssignmentMode;
|
|
20
|
+
};
|
|
21
|
+
static styles: import("lit").CSSResult[];
|
|
22
|
+
static properties: PropertyDeclarations;
|
|
23
|
+
/** Form field name: the value is submitted under it. */
|
|
24
|
+
name?: string;
|
|
25
|
+
/** Current value (submitted with the form). */
|
|
26
|
+
value: string;
|
|
27
|
+
/** Visible label (required for accessibility; use `hide-label` to visually hide it). */
|
|
28
|
+
label?: string;
|
|
29
|
+
/** Help text under the field, linked to it for screen readers. */
|
|
30
|
+
hint?: string;
|
|
31
|
+
/** Custom error message. Setting it marks the control invalid. */
|
|
32
|
+
error?: string;
|
|
33
|
+
/** Control size. */
|
|
34
|
+
size?: Size;
|
|
35
|
+
/** The form cannot be submitted until this has a value. */
|
|
36
|
+
required: boolean;
|
|
37
|
+
/** Not interactive and not submitted. */
|
|
38
|
+
disabled: boolean;
|
|
39
|
+
/** Focusable and submitted, but not editable. */
|
|
40
|
+
readonly: boolean;
|
|
41
|
+
/** Hide the label visually; it stays the accessible name. */
|
|
42
|
+
hideLabel: boolean;
|
|
43
|
+
protected internals: ElementInternals;
|
|
44
|
+
protected controlId: string;
|
|
45
|
+
constructor();
|
|
46
|
+
connectedCallback(): void;
|
|
47
|
+
protected get control(): NativeControl | null;
|
|
48
|
+
get form(): HTMLFormElement | null;
|
|
49
|
+
get validity(): ValidityState;
|
|
50
|
+
get validationMessage(): string;
|
|
51
|
+
checkValidity(): boolean;
|
|
52
|
+
reportValidity(): boolean;
|
|
53
|
+
/** Check the value and show the error state now (as after leaving the field), without the browser's popup. */
|
|
54
|
+
validate(): boolean;
|
|
55
|
+
focus(options?: FocusOptions): void;
|
|
56
|
+
formResetCallback(): void;
|
|
57
|
+
formStateRestoreCallback(state: string): void;
|
|
58
|
+
formDisabledCallback(disabled: boolean): void;
|
|
59
|
+
protected updated(changed: PropertyValues<this>): void;
|
|
60
|
+
protected syncFormValue(): void;
|
|
61
|
+
protected syncValidity(): void;
|
|
62
|
+
protected get showInvalid(): boolean;
|
|
63
|
+
protected onInput(e: Event): void;
|
|
64
|
+
protected onChange(): void;
|
|
65
|
+
protected onBlur(): void;
|
|
66
|
+
/** Field chrome around a control template. */
|
|
67
|
+
protected field(control: unknown): import("lit").TemplateResult<1>;
|
|
68
|
+
protected describedBy(): string | undefined;
|
|
69
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { css, html, nothing } from 'lit';
|
|
2
|
+
import { PmElement, nextId } from '../base.js';
|
|
3
|
+
import fieldCss from '../styles/field.js';
|
|
4
|
+
/**
|
|
5
|
+
* Form-associated base: label/hint/error wiring, validity mirroring, form reset/restore.
|
|
6
|
+
* @fires pm-input - On every value change while editing. detail: { value }
|
|
7
|
+
* @fires pm-change - When the value is committed. detail: { value }
|
|
8
|
+
* Label, hint and error render inside the same shadow root as the control so ARIA ids resolve.
|
|
9
|
+
*/
|
|
10
|
+
export class PmFormControl extends PmElement {
|
|
11
|
+
static { this.formAssociated = true; }
|
|
12
|
+
static { this.shadowRootOptions = { ...PmElement.shadowRootOptions, delegatesFocus: true }; }
|
|
13
|
+
static { this.styles = [...PmElement.styles, fieldCss,
|
|
14
|
+
css `
|
|
15
|
+
:host { display: block; }
|
|
16
|
+
:host([hidden]) { display: none; }
|
|
17
|
+
`,
|
|
18
|
+
]; }
|
|
19
|
+
static { this.properties = {
|
|
20
|
+
name: { reflect: true },
|
|
21
|
+
value: {},
|
|
22
|
+
label: {},
|
|
23
|
+
hint: {},
|
|
24
|
+
error: {},
|
|
25
|
+
size: { reflect: true },
|
|
26
|
+
required: { type: Boolean, reflect: true },
|
|
27
|
+
disabled: { type: Boolean, reflect: true },
|
|
28
|
+
readonly: { type: Boolean, reflect: true },
|
|
29
|
+
hideLabel: { type: Boolean, attribute: 'hide-label' },
|
|
30
|
+
}; }
|
|
31
|
+
#defaultValue;
|
|
32
|
+
#touched;
|
|
33
|
+
constructor() {
|
|
34
|
+
super();
|
|
35
|
+
this.internals = this.attachInternals();
|
|
36
|
+
this.controlId = nextId('ctl');
|
|
37
|
+
this.#defaultValue = '';
|
|
38
|
+
this.#touched = false;
|
|
39
|
+
this.value = '';
|
|
40
|
+
this.required = false;
|
|
41
|
+
this.disabled = false;
|
|
42
|
+
this.readonly = false;
|
|
43
|
+
this.hideLabel = false;
|
|
44
|
+
}
|
|
45
|
+
connectedCallback() {
|
|
46
|
+
super.connectedCallback();
|
|
47
|
+
this.#defaultValue = this.getAttribute('value') ?? '';
|
|
48
|
+
if (!this.value)
|
|
49
|
+
this.value = this.#defaultValue;
|
|
50
|
+
}
|
|
51
|
+
get control() {
|
|
52
|
+
return this.renderRoot?.querySelector('[data-control]') ?? null;
|
|
53
|
+
}
|
|
54
|
+
get form() { return this.internals.form; }
|
|
55
|
+
get validity() { return this.internals.validity; }
|
|
56
|
+
get validationMessage() { return this.internals.validationMessage; }
|
|
57
|
+
checkValidity() { return this.internals.checkValidity(); }
|
|
58
|
+
reportValidity() { this.#touched = true; this.requestUpdate(); return this.internals.reportValidity(); }
|
|
59
|
+
/** Check the value and show the error state now (as after leaving the field), without the browser's popup. */
|
|
60
|
+
validate() { this.#touched = true; this.requestUpdate(); return this.internals.checkValidity(); }
|
|
61
|
+
focus(options) { this.control?.focus(options); }
|
|
62
|
+
formResetCallback() {
|
|
63
|
+
this.value = this.#defaultValue;
|
|
64
|
+
this.#touched = false;
|
|
65
|
+
}
|
|
66
|
+
formStateRestoreCallback(state) { this.value = state; }
|
|
67
|
+
formDisabledCallback(disabled) { this.disabled = disabled; }
|
|
68
|
+
updated(changed) {
|
|
69
|
+
super.updated(changed);
|
|
70
|
+
this.syncFormValue();
|
|
71
|
+
this.syncValidity();
|
|
72
|
+
}
|
|
73
|
+
syncFormValue() {
|
|
74
|
+
this.internals.setFormValue(this.value, this.value);
|
|
75
|
+
}
|
|
76
|
+
syncValidity() {
|
|
77
|
+
const c = this.control;
|
|
78
|
+
if (!c)
|
|
79
|
+
return;
|
|
80
|
+
if (this.error) {
|
|
81
|
+
this.internals.setValidity({ customError: true }, this.error, c);
|
|
82
|
+
}
|
|
83
|
+
else if (!c.validity.valid) {
|
|
84
|
+
// Browsers can report an invalid state before they have a message (e.g. a control inside a hidden or
|
|
85
|
+
// re-rendering subtree); setValidity throws on an empty message, so fall back to a generic one.
|
|
86
|
+
this.internals.setValidity(c.validity, c.validationMessage || (c.validity.valueMissing ? 'Please fill in this field.' : 'Please check this value.'), c);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
this.internals.setValidity({});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
get showInvalid() {
|
|
93
|
+
return Boolean(this.error) || (this.#touched && !this.internals.validity.valid);
|
|
94
|
+
}
|
|
95
|
+
onInput(e) {
|
|
96
|
+
this.value = e.target.value;
|
|
97
|
+
this.emit('pm-input', { value: this.value });
|
|
98
|
+
}
|
|
99
|
+
onChange() {
|
|
100
|
+
this.#touched = true;
|
|
101
|
+
this.requestUpdate();
|
|
102
|
+
this.emit('pm-change', { value: this.value });
|
|
103
|
+
}
|
|
104
|
+
onBlur() {
|
|
105
|
+
this.#touched = true;
|
|
106
|
+
this.requestUpdate();
|
|
107
|
+
}
|
|
108
|
+
/** Field chrome around a control template. */
|
|
109
|
+
field(control) {
|
|
110
|
+
const hintId = `${this.controlId}-hint`;
|
|
111
|
+
const errId = `${this.controlId}-err`;
|
|
112
|
+
const message = this.error || (this.showInvalid ? this.internals.validationMessage : '');
|
|
113
|
+
return html `<div class="pm-field" part="field">
|
|
114
|
+
${this.label
|
|
115
|
+
? html `<label for=${this.controlId} part="label" class=${this.hideLabel ? 'pm-visually-hidden' : ''}
|
|
116
|
+
>${this.label}</label
|
|
117
|
+
>`
|
|
118
|
+
: nothing}
|
|
119
|
+
${control}
|
|
120
|
+
${this.hint ? html `<span class="pm-hint" id=${hintId} part="hint">${this.hint}</span>` : nothing}
|
|
121
|
+
<span class="pm-error" id=${errId} part="error" ?hidden=${!message} role=${message ? 'alert' : nothing}
|
|
122
|
+
>${message}</span
|
|
123
|
+
>
|
|
124
|
+
</div>`;
|
|
125
|
+
}
|
|
126
|
+
describedBy() {
|
|
127
|
+
const ids = [];
|
|
128
|
+
if (this.hint)
|
|
129
|
+
ids.push(`${this.controlId}-hint`);
|
|
130
|
+
if (this.showInvalid)
|
|
131
|
+
ids.push(`${this.controlId}-err`);
|
|
132
|
+
return ids.length ? ids.join(' ') : undefined;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { SafeHTMLElement } from '../base.js';
|
|
2
|
+
export interface FormSubmitDetail {
|
|
3
|
+
form: HTMLFormElement;
|
|
4
|
+
data: FormData;
|
|
5
|
+
/** Field values by name; a name used more than once becomes an array. */
|
|
6
|
+
values: Record<string, FormDataEntryValue | FormDataEntryValue[]>;
|
|
7
|
+
/** Keep the form busy until the promise settles. Resolve or reject with `{ errors: { field: 'message' } }` to show server errors. */
|
|
8
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Form helper around a native `<form>`: validation summary, busy submit button and server errors.
|
|
12
|
+
*
|
|
13
|
+
* On submit it validates every field and lists the problems in an error summary that takes focus (each entry jumps
|
|
14
|
+
* to its field). It keeps the submit button busy while the request runs, and shows errors returned by the server on
|
|
15
|
+
* the right fields.
|
|
16
|
+
*
|
|
17
|
+
* Listen for `pm-submit`; call `event.detail.waitUntil(promise)` for async work. A form without an `action` never
|
|
18
|
+
* reloads the page.
|
|
19
|
+
* @tag pm-form
|
|
20
|
+
* @slot - One `<form>` with Prismora or native controls.
|
|
21
|
+
* @attr {boolean} no-summary - Do not show the error summary; focus the first invalid field instead.
|
|
22
|
+
* @attr {string} summary-title - Heading of the error summary (default "Please fix the following").
|
|
23
|
+
* @fires pm-submit - The form is valid and was submitted. detail: { form, data, values, waitUntil }
|
|
24
|
+
* @fires pm-invalid - Submit was blocked. detail: { fields: [{ name, label, message }] }
|
|
25
|
+
* @example <pm-form><form><pm-input label="Email" name="email" type="email" required></pm-input><pm-button type="submit">Subscribe</pm-button></form></pm-form>
|
|
26
|
+
*/
|
|
27
|
+
export declare class PmForm extends SafeHTMLElement {
|
|
28
|
+
#private;
|
|
29
|
+
get form(): HTMLFormElement | null;
|
|
30
|
+
/** True while a `waitUntil` promise is pending. */
|
|
31
|
+
get busy(): boolean;
|
|
32
|
+
connectedCallback(): void;
|
|
33
|
+
disconnectedCallback(): void;
|
|
34
|
+
/** Validate every field and show the summary. Returns true when the form is valid. */
|
|
35
|
+
validate(): boolean;
|
|
36
|
+
/** Show errors from the server: `{ fieldName: 'message' }`. Unknown names are listed in the summary only. */
|
|
37
|
+
setErrors(errors: Record<string, string>): void;
|
|
38
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { SafeHTMLElement, defineElement } from '../base.js';
|
|
2
|
+
import helpersCss from '../styles/helpers.js';
|
|
3
|
+
/**
|
|
4
|
+
* Form helper around a native `<form>`: validation summary, busy submit button and server errors.
|
|
5
|
+
*
|
|
6
|
+
* On submit it validates every field and lists the problems in an error summary that takes focus (each entry jumps
|
|
7
|
+
* to its field). It keeps the submit button busy while the request runs, and shows errors returned by the server on
|
|
8
|
+
* the right fields.
|
|
9
|
+
*
|
|
10
|
+
* Listen for `pm-submit`; call `event.detail.waitUntil(promise)` for async work. A form without an `action` never
|
|
11
|
+
* reloads the page.
|
|
12
|
+
* @tag pm-form
|
|
13
|
+
* @slot - One `<form>` with Prismora or native controls.
|
|
14
|
+
* @attr {boolean} no-summary - Do not show the error summary; focus the first invalid field instead.
|
|
15
|
+
* @attr {string} summary-title - Heading of the error summary (default "Please fix the following").
|
|
16
|
+
* @fires pm-submit - The form is valid and was submitted. detail: { form, data, values, waitUntil }
|
|
17
|
+
* @fires pm-invalid - Submit was blocked. detail: { fields: [{ name, label, message }] }
|
|
18
|
+
* @example <pm-form><form><pm-input label="Email" name="email" type="email" required></pm-input><pm-button type="submit">Subscribe</pm-button></form></pm-form>
|
|
19
|
+
*/
|
|
20
|
+
export class PmForm extends SafeHTMLElement {
|
|
21
|
+
#form = null;
|
|
22
|
+
#busy = false;
|
|
23
|
+
#server = new Set();
|
|
24
|
+
get form() { return this.#form ?? (this.#form = this.querySelector('form')); }
|
|
25
|
+
/** True while a `waitUntil` promise is pending. */
|
|
26
|
+
get busy() { return this.#busy; }
|
|
27
|
+
connectedCallback() {
|
|
28
|
+
if (!this.style.display)
|
|
29
|
+
this.style.display = 'block';
|
|
30
|
+
this.addEventListener('submit', this.#onSubmit, true);
|
|
31
|
+
this.addEventListener('reset', this.#clear);
|
|
32
|
+
this.addEventListener('pm-input', this.#onEdit);
|
|
33
|
+
this.addEventListener('input', this.#onEdit);
|
|
34
|
+
queueMicrotask(() => this.form?.setAttribute('novalidate', ''));
|
|
35
|
+
}
|
|
36
|
+
disconnectedCallback() {
|
|
37
|
+
this.removeEventListener('submit', this.#onSubmit, true);
|
|
38
|
+
this.removeEventListener('reset', this.#clear);
|
|
39
|
+
this.removeEventListener('pm-input', this.#onEdit);
|
|
40
|
+
this.removeEventListener('input', this.#onEdit);
|
|
41
|
+
}
|
|
42
|
+
#fields() { return this.form ? [...this.form.elements].filter((el) => typeof el.checkValidity === 'function' && el.willValidate !== false) : []; }
|
|
43
|
+
#labelOf(el) {
|
|
44
|
+
return el.label || el.getAttribute('aria-label') || el.labels?.[0]?.textContent?.trim() || el.getAttribute('name') || 'Field';
|
|
45
|
+
}
|
|
46
|
+
/** A server error disappears as soon as its field is edited. */
|
|
47
|
+
#onEdit = (e) => {
|
|
48
|
+
const el = e.target;
|
|
49
|
+
if (this.#server.delete(el)) {
|
|
50
|
+
el.error = undefined;
|
|
51
|
+
el.removeAttribute('error');
|
|
52
|
+
el.setCustomValidity?.('');
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
#clear = () => { this.querySelector('.pm-form-summary')?.remove(); for (const el of this.#server) {
|
|
56
|
+
el.error = undefined;
|
|
57
|
+
el.removeAttribute('error');
|
|
58
|
+
} this.#server.clear(); };
|
|
59
|
+
#summarise(problems) {
|
|
60
|
+
this.querySelector('.pm-form-summary')?.remove();
|
|
61
|
+
if (!problems.length)
|
|
62
|
+
return;
|
|
63
|
+
this.dispatchEvent(new CustomEvent('pm-invalid', { bubbles: true, composed: true, detail: { fields: problems.map((p) => ({ name: p.el.getAttribute('name') ?? '', label: p.label, message: p.message })) } }));
|
|
64
|
+
if (this.hasAttribute('no-summary')) {
|
|
65
|
+
problems[0].el.focus();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const box = document.createElement('div');
|
|
69
|
+
box.className = 'pm-form-summary';
|
|
70
|
+
box.setAttribute('role', 'alert');
|
|
71
|
+
box.tabIndex = -1;
|
|
72
|
+
const title = document.createElement('strong');
|
|
73
|
+
title.textContent = this.getAttribute('summary-title') ?? (problems.length === 1 ? 'Please fix 1 field' : `Please fix ${problems.length} fields`);
|
|
74
|
+
const list = document.createElement('ul');
|
|
75
|
+
for (const p of problems) {
|
|
76
|
+
const li = document.createElement('li');
|
|
77
|
+
const a = document.createElement('a');
|
|
78
|
+
a.href = '#';
|
|
79
|
+
a.textContent = `${p.label}: ${p.message}`;
|
|
80
|
+
a.addEventListener('click', (e) => { e.preventDefault(); p.el.focus(); p.el.scrollIntoView({ block: 'center', behavior: 'auto' }); });
|
|
81
|
+
li.append(a);
|
|
82
|
+
list.append(li);
|
|
83
|
+
}
|
|
84
|
+
box.append(title, list);
|
|
85
|
+
this.form.prepend(box);
|
|
86
|
+
box.focus();
|
|
87
|
+
}
|
|
88
|
+
/** Validate every field and show the summary. Returns true when the form is valid. */
|
|
89
|
+
validate() {
|
|
90
|
+
const problems = this.#fields().filter((el) => !(el.validate ? el.validate() : el.checkValidity())).map((el) => ({ el, label: this.#labelOf(el), message: el.error || el.validationMessage || 'Please check this value.' }));
|
|
91
|
+
this.#summarise(problems);
|
|
92
|
+
return problems.length === 0;
|
|
93
|
+
}
|
|
94
|
+
/** Show errors from the server: `{ fieldName: 'message' }`. Unknown names are listed in the summary only. */
|
|
95
|
+
setErrors(errors) {
|
|
96
|
+
const problems = [];
|
|
97
|
+
for (const [name, message] of Object.entries(errors)) {
|
|
98
|
+
const el = this.#fields().find((f) => f.getAttribute('name') === name);
|
|
99
|
+
if (!el) {
|
|
100
|
+
problems.push({ el: this.form, label: name, message });
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if ('error' in el)
|
|
104
|
+
el.error = message;
|
|
105
|
+
else
|
|
106
|
+
el.setCustomValidity?.(message);
|
|
107
|
+
this.#server.add(el);
|
|
108
|
+
problems.push({ el, label: this.#labelOf(el), message });
|
|
109
|
+
}
|
|
110
|
+
this.#summarise(problems);
|
|
111
|
+
}
|
|
112
|
+
#setBusy(on) {
|
|
113
|
+
this.#busy = on;
|
|
114
|
+
this.form?.toggleAttribute('aria-busy', on);
|
|
115
|
+
if (on)
|
|
116
|
+
this.form?.setAttribute('aria-busy', 'true');
|
|
117
|
+
for (const b of this.querySelectorAll('pm-button[type="submit"], button[type="submit"], button:not([type]), input[type="submit"]')) {
|
|
118
|
+
if (b.localName === 'pm-button')
|
|
119
|
+
b.loading = on;
|
|
120
|
+
else
|
|
121
|
+
b.disabled = on;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
#onSubmit = (e) => {
|
|
125
|
+
const form = this.form;
|
|
126
|
+
if (!form || e.target !== form)
|
|
127
|
+
return;
|
|
128
|
+
if (this.#busy) {
|
|
129
|
+
e.preventDefault();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
this.#clear();
|
|
133
|
+
if (!this.validate()) {
|
|
134
|
+
e.preventDefault();
|
|
135
|
+
e.stopPropagation();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const data = new FormData(form);
|
|
139
|
+
const values = {};
|
|
140
|
+
for (const key of new Set(data.keys())) {
|
|
141
|
+
const all = data.getAll(key);
|
|
142
|
+
values[key] = all.length > 1 ? all : all[0];
|
|
143
|
+
}
|
|
144
|
+
let pending;
|
|
145
|
+
const ev = new CustomEvent('pm-submit', { bubbles: true, composed: true, cancelable: true, detail: { form, data, values, waitUntil: (p) => { pending = p; } } });
|
|
146
|
+
const go = this.dispatchEvent(ev);
|
|
147
|
+
if (!go || pending || !form.hasAttribute('action'))
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
if (!pending)
|
|
150
|
+
return;
|
|
151
|
+
this.#setBusy(true);
|
|
152
|
+
const done = (result) => {
|
|
153
|
+
this.#setBusy(false);
|
|
154
|
+
const errors = result?.errors;
|
|
155
|
+
if (errors && typeof errors === 'object')
|
|
156
|
+
this.setErrors(errors);
|
|
157
|
+
};
|
|
158
|
+
pending.then(done, done);
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
defineElement('pm-form', PmForm, [helpersCss]);
|