@calcifer-design/ui 0.4.0 → 0.5.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/dist/components/Breadcrumb/Breadcrumb.d.ts +24 -0
- package/dist/components/Breadcrumb/Breadcrumb.js +48 -0
- package/dist/components/Breadcrumb/Breadcrumb.module.js +10 -0
- package/dist/components/Breadcrumb/Breadcrumb_module.css +49 -0
- package/dist/components/CardList/CardList.d.ts +30 -0
- package/dist/components/CardList/CardList.js +35 -0
- package/dist/components/CardList/CardList.module.js +7 -0
- package/dist/components/CardList/CardList_module.css +20 -0
- package/dist/components/DatePicker/Calendar.d.ts +19 -0
- package/dist/components/DatePicker/Calendar.js +207 -0
- package/dist/components/DatePicker/DatePicker.d.ts +72 -0
- package/dist/components/DatePicker/DatePicker.js +116 -0
- package/dist/components/DatePicker/DatePicker.module.js +14 -0
- package/dist/components/DatePicker/DatePicker_module.css +112 -0
- package/dist/components/DatePicker/calendarDate.d.ts +46 -0
- package/dist/components/DatePicker/calendarDate.js +117 -0
- package/dist/components/Descriptions/Descriptions.d.ts +25 -0
- package/dist/components/Descriptions/Descriptions.js +44 -0
- package/dist/components/Descriptions/Descriptions.module.js +8 -0
- package/dist/components/Descriptions/Descriptions_module.css +47 -0
- package/dist/components/Field/Field.d.ts +1 -1
- package/dist/components/Field/Field.js +7 -1
- package/dist/components/Field/PickerField.d.ts +41 -0
- package/dist/components/Field/PickerField.js +115 -0
- package/dist/components/Field/PickerField.module.js +8 -0
- package/dist/components/Field/PickerField_module.css +36 -0
- package/dist/components/FilterBar/FilterBar.d.ts +55 -0
- package/dist/components/FilterBar/FilterBar.js +138 -0
- package/dist/components/FilterBar/FilterBar.module.js +13 -0
- package/dist/components/FilterBar/FilterBar_module.css +98 -0
- package/dist/components/Form/Form.d.ts +48 -0
- package/dist/components/Form/Form.js +38 -0
- package/dist/components/Form/Form.module.js +5 -0
- package/dist/components/Form/FormContext.d.ts +15 -0
- package/dist/components/Form/FormContext.js +20 -0
- package/dist/components/Form/Form_module.css +6 -0
- package/dist/components/PageShell/PageShell.d.ts +27 -0
- package/dist/components/PageShell/PageShell.js +35 -0
- package/dist/components/PageShell/PageShell.module.js +9 -0
- package/dist/components/PageShell/PageShell_module.css +56 -0
- package/dist/components/Segmented/Segmented.d.ts +49 -0
- package/dist/components/Segmented/Segmented.js +55 -0
- package/dist/components/Segmented/Segmented.module.js +7 -0
- package/dist/components/Segmented/Segmented_module.css +75 -0
- package/dist/components/Steps/Steps.d.ts +30 -0
- package/dist/components/Steps/Steps.js +63 -0
- package/dist/components/Steps/Steps.module.js +10 -0
- package/dist/components/Steps/Steps_module.css +126 -0
- package/dist/components/TextArea/TextArea.d.ts +16 -0
- package/dist/components/TextArea/TextArea.js +28 -0
- package/dist/components/TextArea/TextArea.module.js +5 -0
- package/dist/components/TextArea/TextArea_module.css +12 -0
- package/dist/components/TimePicker/TimePicker.d.ts +19 -0
- package/dist/components/TimePicker/TimePicker.js +17 -0
- package/dist/components/Timeline/Timeline.d.ts +39 -0
- package/dist/components/Timeline/Timeline.js +109 -0
- package/dist/components/Timeline/Timeline.module.js +12 -0
- package/dist/components/Timeline/Timeline_module.css +99 -0
- package/dist/components/Toolbar/InsideToolbarContext.d.ts +6 -0
- package/dist/components/Toolbar/InsideToolbarContext.js +3 -0
- package/dist/components/Toolbar/Toolbar.d.ts +46 -0
- package/dist/components/Toolbar/Toolbar.js +43 -0
- package/dist/components/Toolbar/Toolbar.module.js +7 -0
- package/dist/components/Toolbar/Toolbar_module.css +26 -0
- package/dist/components/Tree/Tree.d.ts +81 -0
- package/dist/components/Tree/Tree.js +232 -0
- package/dist/components/Tree/Tree.module.js +12 -0
- package/dist/components/Tree/Tree_module.css +124 -0
- package/dist/components/Tree/treeIndex.d.ts +23 -0
- package/dist/components/Tree/treeIndex.js +33 -0
- package/dist/components/TreeSelect/TreeSelect.d.ts +61 -0
- package/dist/components/TreeSelect/TreeSelect.js +117 -0
- package/dist/components/TreeSelect/TreeSelect.module.js +7 -0
- package/dist/components/TreeSelect/TreeSelect_module.css +22 -0
- package/dist/components/Upload/Upload.d.ts +66 -0
- package/dist/components/Upload/Upload.js +287 -0
- package/dist/components/Upload/Upload.module.js +12 -0
- package/dist/components/Upload/Upload_module.css +96 -0
- package/dist/components/ViewField/ViewField.d.ts +26 -0
- package/dist/components/ViewField/ViewField.js +29 -0
- package/dist/components/ViewField/ViewField.module.js +6 -0
- package/dist/components/ViewField/ViewField_module.css +17 -0
- package/dist/hooks/useFormReset.d.ts +10 -0
- package/dist/hooks/useFormReset.js +34 -0
- package/dist/icons/CalendarIcon.d.ts +2 -0
- package/dist/icons/CalendarIcon.js +26 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +17 -0
- package/dist/styles/pair.module.js +6 -0
- package/dist/styles/pair_module.css +18 -0
- package/package.json +2 -2
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { TreeItem } from './Tree.js';
|
|
2
|
+
/** One item in its place. Internal, as `TreeSelect` and `Tree` share it. */
|
|
3
|
+
export interface TreeNode {
|
|
4
|
+
id: string;
|
|
5
|
+
item: TreeItem;
|
|
6
|
+
/** Nearest first. */
|
|
7
|
+
ancestorIds: string[];
|
|
8
|
+
children: TreeNode[];
|
|
9
|
+
/** Its place in the whole tree, which an element id can hold where any `id` string could not. */
|
|
10
|
+
ordinal: number;
|
|
11
|
+
}
|
|
12
|
+
export interface TreeIndex {
|
|
13
|
+
roots: TreeNode[];
|
|
14
|
+
nodes: Map<string, TreeNode>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The tree with each id in one place. An id seen before is left out with everything it holds:
|
|
18
|
+
* selection, expansion and focus all speak in ids, so two items sharing one could not be told apart.
|
|
19
|
+
* That also stops an item that holds itself from going round for ever.
|
|
20
|
+
*/
|
|
21
|
+
export declare function indexItems(items: TreeItem[]): TreeIndex;
|
|
22
|
+
/** What is shown, top to bottom: every root, and what each open parent holds. */
|
|
23
|
+
export declare function visibleNodesOf(roots: TreeNode[], expanded: ReadonlySet<string>): TreeNode[];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
function indexItems(items) {
|
|
2
|
+
const nodes = new Map();
|
|
3
|
+
const collect = (level, ancestorIds)=>{
|
|
4
|
+
const collected = [];
|
|
5
|
+
for (const item of level)if (!nodes.has(item.id)) {
|
|
6
|
+
const node = {
|
|
7
|
+
id: item.id,
|
|
8
|
+
item,
|
|
9
|
+
ancestorIds,
|
|
10
|
+
children: [],
|
|
11
|
+
ordinal: nodes.size
|
|
12
|
+
};
|
|
13
|
+
nodes.set(item.id, node);
|
|
14
|
+
node.children = collect(item.children ?? [], [
|
|
15
|
+
item.id,
|
|
16
|
+
...ancestorIds
|
|
17
|
+
]);
|
|
18
|
+
collected.push(node);
|
|
19
|
+
}
|
|
20
|
+
return collected;
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
roots: collect(items, []),
|
|
24
|
+
nodes
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function visibleNodesOf(roots, expanded) {
|
|
28
|
+
return roots.flatMap((node)=>[
|
|
29
|
+
node,
|
|
30
|
+
...expanded.has(node.id) ? visibleNodesOf(node.children, expanded) : []
|
|
31
|
+
]);
|
|
32
|
+
}
|
|
33
|
+
export { indexItems, visibleNodesOf };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import type { FieldFrameProps } from '../Field/Field.js';
|
|
3
|
+
import { type TreeItem } from '../Tree/Tree.js';
|
|
4
|
+
export interface TreeSelectLabels {
|
|
5
|
+
/** Joins the trigger's accessible name when the field is `required`. */
|
|
6
|
+
required: string;
|
|
7
|
+
/** Shown when `Form` submits a `required` field with nothing chosen. */
|
|
8
|
+
requiredMessage: string;
|
|
9
|
+
/** The clear button's name. Defaults to "Clear" and the field's own label. */
|
|
10
|
+
clear: string;
|
|
11
|
+
/** Shown in the popup, in place of the tree, while `items` is empty. */
|
|
12
|
+
empty: string;
|
|
13
|
+
}
|
|
14
|
+
export interface TreeSelectProps extends FieldFrameProps {
|
|
15
|
+
/** The visible label, the start of the trigger's accessible name and the name of the tree. */
|
|
16
|
+
label: string;
|
|
17
|
+
/**
|
|
18
|
+
* The hierarchy, as `Tree` takes it. A new array of the same ids changes no state. While it is
|
|
19
|
+
* empty the popup still opens, says `labels.empty` and takes focus itself.
|
|
20
|
+
*/
|
|
21
|
+
items: TreeItem[];
|
|
22
|
+
/** Sits under the trigger and joins its `aria-describedby`. */
|
|
23
|
+
description?: ReactNode;
|
|
24
|
+
/**
|
|
25
|
+
* Draws the required mark and adds the word to the trigger's name. `Form` refuses to submit while
|
|
26
|
+
* nothing is chosen, with `labels.requiredMessage` on the frame. A plain `<form>` is not blocked:
|
|
27
|
+
* the value travels in a hidden input, which has no constraints, so the app validates.
|
|
28
|
+
*/
|
|
29
|
+
required?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Controlled: a node's `id`, or `null` for nothing chosen. An id no node holds reads as nothing
|
|
32
|
+
* chosen, and nothing is reported for it. A node that is `disabled` can still be the value, since
|
|
33
|
+
* it is the app's to set: it shows and submits, and only cannot be chosen by the user.
|
|
34
|
+
*/
|
|
35
|
+
value?: string | null;
|
|
36
|
+
/**
|
|
37
|
+
* Uncontrolled: the first value, and the one a native reset of the surrounding form goes back to,
|
|
38
|
+
* with `onValueChange` told. A controlled `value` stays through a reset, as the parent's to change.
|
|
39
|
+
*/
|
|
40
|
+
defaultValue?: string | null;
|
|
41
|
+
/** The chosen node's `id`, or `null` once cleared, in one argument, and only for a real change. */
|
|
42
|
+
onValueChange?: (value: string | null) => void;
|
|
43
|
+
/** Called when focus leaves the trigger, for a form library's "touched". */
|
|
44
|
+
onBlur?: () => void;
|
|
45
|
+
/** Shown on the trigger while nothing is chosen. */
|
|
46
|
+
placeholder?: string;
|
|
47
|
+
/** Shows a button that clears the choice, while there is one. */
|
|
48
|
+
clearable?: boolean;
|
|
49
|
+
/** The words the control says itself, for an app that is not in English. */
|
|
50
|
+
labels?: Partial<TreeSelectLabels>;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Choose one node from a hierarchy: a button showing the choice opens a `Tree`, with focus on the
|
|
54
|
+
* chosen node and the branches around it opened. Choosing closes it and returns focus, as Escape
|
|
55
|
+
* does. Submits the node's `id` through a hidden input.
|
|
56
|
+
*
|
|
57
|
+
* The popup does not trap focus, as `DatePicker`'s dialog does: there is one tab stop in it, so Tab
|
|
58
|
+
* closes it and moves on. Shift+Tab goes to the trigger and leaves it open, and Escape or a second
|
|
59
|
+
* Shift+Tab closes it from there.
|
|
60
|
+
*/
|
|
61
|
+
export declare function TreeSelect({ label, name, items, description, error, required, disabled, value, defaultValue, onValueChange, onBlur, placeholder, clearable, labels, }: TreeSelectProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Popover } from "@base-ui/react/popover";
|
|
3
|
+
import { useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { ChevronDownIcon } from "../../icons/ChevronDownIcon.js";
|
|
5
|
+
import popup_module from "../../styles/popup.module.js";
|
|
6
|
+
import { PickerField } from "../Field/PickerField.js";
|
|
7
|
+
import { TreeView } from "../Tree/Tree.js";
|
|
8
|
+
import { indexItems } from "../Tree/treeIndex.js";
|
|
9
|
+
import TreeSelect_module from "./TreeSelect.module.js";
|
|
10
|
+
const DEFAULT_LABELS = {
|
|
11
|
+
required: 'required',
|
|
12
|
+
requiredMessage: 'Choose an option.',
|
|
13
|
+
empty: 'No options'
|
|
14
|
+
};
|
|
15
|
+
function TreeSelect({ label, name, items, description, error, required = false, disabled = false, value, defaultValue, onValueChange, onBlur, placeholder = 'Select…', clearable = false, labels }) {
|
|
16
|
+
const popupRef = useRef(null);
|
|
17
|
+
const [heldValue, setHeldValue] = useState(defaultValue ?? null);
|
|
18
|
+
const [expandedIds, setExpandedIds] = useState([]);
|
|
19
|
+
const [open, setOpen] = useState(false);
|
|
20
|
+
if (open && disabled) setOpen(false);
|
|
21
|
+
const index = useMemo(()=>indexItems(items), [
|
|
22
|
+
items
|
|
23
|
+
]);
|
|
24
|
+
const nodeOf = (id)=>null === id ? void 0 : index.nodes.get(id);
|
|
25
|
+
const chosenNode = nodeOf(void 0 === value ? heldValue : value);
|
|
26
|
+
const chosen = chosenNode?.id ?? null;
|
|
27
|
+
const words = {
|
|
28
|
+
...DEFAULT_LABELS,
|
|
29
|
+
clear: `Clear ${label}`,
|
|
30
|
+
...labels
|
|
31
|
+
};
|
|
32
|
+
const hasItems = index.roots.length > 0;
|
|
33
|
+
const commit = (nextValue)=>{
|
|
34
|
+
setHeldValue(nextValue);
|
|
35
|
+
const nextChosen = nodeOf(nextValue)?.id ?? null;
|
|
36
|
+
if (nextChosen !== chosen) onValueChange?.(nextChosen);
|
|
37
|
+
};
|
|
38
|
+
const changeOpen = (nextOpen)=>{
|
|
39
|
+
if (nextOpen && void 0 !== chosenNode) setExpandedIds([
|
|
40
|
+
...new Set([
|
|
41
|
+
...expandedIds,
|
|
42
|
+
...chosenNode.ancestorIds
|
|
43
|
+
])
|
|
44
|
+
]);
|
|
45
|
+
setOpen(nextOpen);
|
|
46
|
+
};
|
|
47
|
+
useLayoutEffect(()=>{
|
|
48
|
+
if (open && !hasItems && document.activeElement === document.body) popupRef.current?.focus();
|
|
49
|
+
}, [
|
|
50
|
+
open,
|
|
51
|
+
hasItems
|
|
52
|
+
]);
|
|
53
|
+
return /*#__PURE__*/ jsx(PickerField, {
|
|
54
|
+
label: label,
|
|
55
|
+
name: name,
|
|
56
|
+
description: description,
|
|
57
|
+
error: error,
|
|
58
|
+
required: required,
|
|
59
|
+
disabled: disabled,
|
|
60
|
+
value: chosen,
|
|
61
|
+
valueText: void 0 === chosenNode ? placeholder : chosenNode.item.label,
|
|
62
|
+
icon: /*#__PURE__*/ jsx(ChevronDownIcon, {}),
|
|
63
|
+
iconClassName: TreeSelect_module.chevron,
|
|
64
|
+
requiredWord: words.required,
|
|
65
|
+
validate: ()=>required && null === chosen ? words.requiredMessage : null,
|
|
66
|
+
onReset: ()=>{
|
|
67
|
+
if (void 0 === value) commit(defaultValue ?? null);
|
|
68
|
+
},
|
|
69
|
+
clearable: clearable,
|
|
70
|
+
clearLabel: words.clear,
|
|
71
|
+
onClear: ()=>commit(null),
|
|
72
|
+
onBlur: onBlur,
|
|
73
|
+
onTriggerKeyDown: (event)=>{
|
|
74
|
+
if ('ArrowDown' === event.key || 'ArrowUp' === event.key) {
|
|
75
|
+
event.preventDefault();
|
|
76
|
+
if (!open) changeOpen(true);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
open: open,
|
|
80
|
+
onOpenChange: changeOpen,
|
|
81
|
+
children: /*#__PURE__*/ jsx(Popover.Portal, {
|
|
82
|
+
children: /*#__PURE__*/ jsx(Popover.Positioner, {
|
|
83
|
+
className: popup_module.layer,
|
|
84
|
+
align: "start",
|
|
85
|
+
sideOffset: 8,
|
|
86
|
+
children: /*#__PURE__*/ jsx(Popover.Popup, {
|
|
87
|
+
ref: popupRef,
|
|
88
|
+
className: [
|
|
89
|
+
popup_module.surface,
|
|
90
|
+
TreeSelect_module.popup
|
|
91
|
+
].join(' '),
|
|
92
|
+
"data-popup": "select",
|
|
93
|
+
"aria-label": label,
|
|
94
|
+
initialFocus: ()=>popupRef.current?.querySelector('[tabindex="0"]') ?? true,
|
|
95
|
+
children: hasItems ? /*#__PURE__*/ jsx(TreeView, {
|
|
96
|
+
items: items,
|
|
97
|
+
"aria-label": label,
|
|
98
|
+
selectionMode: "single",
|
|
99
|
+
selectedIds: null === chosen ? [] : [
|
|
100
|
+
chosen
|
|
101
|
+
],
|
|
102
|
+
expandedIds: expandedIds,
|
|
103
|
+
onExpandedChange: setExpandedIds,
|
|
104
|
+
onItemChoose: (id)=>{
|
|
105
|
+
commit(id);
|
|
106
|
+
setOpen(false);
|
|
107
|
+
}
|
|
108
|
+
}) : /*#__PURE__*/ jsx(Popover.Description, {
|
|
109
|
+
className: TreeSelect_module.empty,
|
|
110
|
+
children: words.empty
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
export { TreeSelect };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
.chevron-HJ0ky {
|
|
2
|
+
transition-property: transform;
|
|
3
|
+
transition-duration: var(--motion-duration-1);
|
|
4
|
+
transition-timing-function: var(--motion-ease);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
[data-popup-open] > .chevron-HJ0ky {
|
|
8
|
+
transform: rotate(180deg);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.popup-KUQxt[data-popup] {
|
|
12
|
+
width: max(var(--anchor-width), 18rem);
|
|
13
|
+
max-height: min(24rem, var(--available-height));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.empty-RdOM_ {
|
|
17
|
+
padding: var(--space-2) var(--space-3);
|
|
18
|
+
color: var(--color-text-muted);
|
|
19
|
+
font-size: var(--text-sm);
|
|
20
|
+
margin: 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type FieldFrameProps } from '../Field/Field.js';
|
|
3
|
+
export type UploadRejectionReason = 'type' | 'size' | 'count' | 'duplicate';
|
|
4
|
+
export interface UploadRejection {
|
|
5
|
+
file: File;
|
|
6
|
+
/**
|
|
7
|
+
* `type`: not covered by `accept`. `size`: over `maxSizeBytes`. `count`: a second file when
|
|
8
|
+
* `multiple` is off. `duplicate`: with `multiple`, a file the list already holds. Checked in
|
|
9
|
+
* that order, and a file gets the first reason that applies.
|
|
10
|
+
*/
|
|
11
|
+
reason: UploadRejectionReason;
|
|
12
|
+
}
|
|
13
|
+
export interface UploadLabels {
|
|
14
|
+
/** The words on the button. */
|
|
15
|
+
choose: string;
|
|
16
|
+
/** Beside the button. */
|
|
17
|
+
dropHint: string;
|
|
18
|
+
/** The name of one file's remove button. */
|
|
19
|
+
remove: (fileName: string) => string;
|
|
20
|
+
/** Announced when files join the list. */
|
|
21
|
+
added: (fileNames: string) => string;
|
|
22
|
+
/** Announced when a file leaves the list. */
|
|
23
|
+
removed: (fileName: string) => string;
|
|
24
|
+
/** Shown and announced for a file turned away. */
|
|
25
|
+
rejected: (fileName: string, reason: UploadRejectionReason) => string;
|
|
26
|
+
}
|
|
27
|
+
export interface UploadProps extends FieldFrameProps {
|
|
28
|
+
/** The visible label, and the file input's accessible name. */
|
|
29
|
+
label: string;
|
|
30
|
+
/** Sits under the drop zone and joins the input's `aria-describedby`. */
|
|
31
|
+
description?: ReactNode;
|
|
32
|
+
/** Controlled. Pair it with `onFilesChange`; use `defaultFiles` for an uncontrolled one. */
|
|
33
|
+
files?: File[];
|
|
34
|
+
/**
|
|
35
|
+
* Uncontrolled: the first list, and the one a native reset of the surrounding form goes back to,
|
|
36
|
+
* with `onFilesChange` told. A controlled `files` stays through a reset, and goes back in the
|
|
37
|
+
* input the browser just emptied.
|
|
38
|
+
*/
|
|
39
|
+
defaultFiles?: File[];
|
|
40
|
+
/**
|
|
41
|
+
* The whole new list. With `multiple` a choice adds to the list, and the same file again goes to
|
|
42
|
+
* `onReject` as a `duplicate`. Without it a choice replaces the file there was, and choosing that
|
|
43
|
+
* same file again is no change: this is not called and nothing is announced.
|
|
44
|
+
*/
|
|
45
|
+
onFilesChange?: (files: File[]) => void;
|
|
46
|
+
/**
|
|
47
|
+
* The native `accept` syntax: `.pdf`, `image/*`, `application/pdf`. It is checked here as well,
|
|
48
|
+
* for every file, because a browser only filters its picker: a drop would get anything through.
|
|
49
|
+
*/
|
|
50
|
+
accept?: string;
|
|
51
|
+
multiple?: boolean;
|
|
52
|
+
/** The largest file allowed. A file of exactly this size is allowed. */
|
|
53
|
+
maxSizeBytes?: number;
|
|
54
|
+
/** The files turned away from one choice or drop, each with its reason. */
|
|
55
|
+
onReject?: (rejections: UploadRejection[]) => void;
|
|
56
|
+
/** Called when focus leaves the file input, for a form library's "touched". */
|
|
57
|
+
onBlur?: () => void;
|
|
58
|
+
/** The words the control says itself, for an app that is not in English. */
|
|
59
|
+
labels?: Partial<UploadLabels>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Selects files; it does not transfer them. The real file input lies over the whole zone, so the
|
|
63
|
+
* keyboard, a screen reader and a pointer all use it, and what looks like a button is a picture of
|
|
64
|
+
* one. Dropping is an enhancement over that, and goes through the same checks.
|
|
65
|
+
*/
|
|
66
|
+
export declare function Upload({ label, name, description, error, required, disabled, files, defaultFiles, onFilesChange, accept, multiple, maxSizeBytes, onReject, onBlur, labels, }: UploadProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Field } from "@base-ui/react/field";
|
|
3
|
+
import { useLayoutEffect, useRef, useState } from "react";
|
|
4
|
+
import { useFormReset } from "../../hooks/useFormReset.js";
|
|
5
|
+
import { CloseIcon } from "../../icons/CloseIcon.js";
|
|
6
|
+
import field_module from "../../styles/field.module.js";
|
|
7
|
+
import { Button } from "../Button/Button.js";
|
|
8
|
+
import { FieldMessages, RequiredMark } from "../Field/Field.js";
|
|
9
|
+
import { useFormValue } from "../Form/FormContext.js";
|
|
10
|
+
import { IconButton } from "../IconButton/IconButton.js";
|
|
11
|
+
import { LiveRegion } from "../LiveRegion/LiveRegion.js";
|
|
12
|
+
import Upload_module from "./Upload.module.js";
|
|
13
|
+
const REJECTION_REASONS = {
|
|
14
|
+
type: 'that type of file is not allowed',
|
|
15
|
+
size: 'it is too large',
|
|
16
|
+
count: 'only one file can be chosen',
|
|
17
|
+
duplicate: 'it is already in the list'
|
|
18
|
+
};
|
|
19
|
+
const DEFAULT_LABELS = {
|
|
20
|
+
remove: (fileName)=>`Remove ${fileName}`,
|
|
21
|
+
added: (fileNames)=>`Added ${fileNames}.`,
|
|
22
|
+
removed: (fileName)=>`Removed ${fileName}.`,
|
|
23
|
+
rejected: (fileName, reason)=>`${fileName} was not added: ${REJECTION_REASONS[reason]}.`
|
|
24
|
+
};
|
|
25
|
+
function acceptsFile(file, accept) {
|
|
26
|
+
const tokens = (accept ?? '').split(',').map((token)=>token.trim().toLowerCase()).filter(Boolean);
|
|
27
|
+
if (0 === tokens.length) return true;
|
|
28
|
+
const fileName = file.name.toLowerCase();
|
|
29
|
+
const fileType = file.type.toLowerCase();
|
|
30
|
+
return tokens.some((token)=>{
|
|
31
|
+
if (token.startsWith('.')) return fileName.endsWith(token);
|
|
32
|
+
if ('*/*' === token) return true;
|
|
33
|
+
if (token.endsWith('/*')) return fileType.startsWith(token.slice(0, -1));
|
|
34
|
+
return fileType === token;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function signatureOf(file) {
|
|
38
|
+
return [
|
|
39
|
+
file.name,
|
|
40
|
+
file.size,
|
|
41
|
+
file.lastModified
|
|
42
|
+
].join(':');
|
|
43
|
+
}
|
|
44
|
+
function signaturesOf(files) {
|
|
45
|
+
return files.map(signatureOf).join('|');
|
|
46
|
+
}
|
|
47
|
+
function withoutPartners(list, other) {
|
|
48
|
+
const unmatched = other.map(signatureOf);
|
|
49
|
+
return list.filter((file)=>{
|
|
50
|
+
const partner = unmatched.indexOf(signatureOf(file));
|
|
51
|
+
if (-1 === partner) return true;
|
|
52
|
+
unmatched.splice(partner, 1);
|
|
53
|
+
return false;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function carriesFiles(event) {
|
|
57
|
+
return Array.from(event.dataTransfer.types ?? []).includes('Files');
|
|
58
|
+
}
|
|
59
|
+
function Upload({ label, name, description, error, required = false, disabled = false, files, defaultFiles, onFilesChange, accept, multiple = false, maxSizeBytes, onReject, onBlur, labels }) {
|
|
60
|
+
const inputRef = useRef(null);
|
|
61
|
+
const listRef = useRef(null);
|
|
62
|
+
const focusAfterRemoval = useRef(null);
|
|
63
|
+
const announcedFiles = useRef(files ?? defaultFiles ?? []);
|
|
64
|
+
const turnedAwayThisCommit = useRef('');
|
|
65
|
+
const [heldFiles, setHeldFiles] = useState(defaultFiles ?? []);
|
|
66
|
+
const [isDragging, setIsDragging] = useState(false);
|
|
67
|
+
const [rejectionMessages, setRejectionMessages] = useState([]);
|
|
68
|
+
const [announcement, setAnnouncement] = useState({
|
|
69
|
+
message: '',
|
|
70
|
+
count: 0
|
|
71
|
+
});
|
|
72
|
+
const currentFiles = files ?? heldFiles;
|
|
73
|
+
const words = {
|
|
74
|
+
...DEFAULT_LABELS,
|
|
75
|
+
choose: multiple ? 'Choose files' : 'Choose a file',
|
|
76
|
+
dropHint: multiple ? 'or drop them here' : 'or drop it here',
|
|
77
|
+
...labels
|
|
78
|
+
};
|
|
79
|
+
useFormValue(disabled ? void 0 : name, currentFiles);
|
|
80
|
+
const announce = (message)=>setAnnouncement((previous)=>({
|
|
81
|
+
message,
|
|
82
|
+
count: previous.count + 1
|
|
83
|
+
}));
|
|
84
|
+
const showInInput = (list)=>{
|
|
85
|
+
if (null === inputRef.current || "u" < typeof DataTransfer) return;
|
|
86
|
+
const transfer = new DataTransfer();
|
|
87
|
+
list.forEach((file)=>transfer.items.add(file));
|
|
88
|
+
inputRef.current.files = transfer.files;
|
|
89
|
+
};
|
|
90
|
+
useLayoutEffect(()=>{
|
|
91
|
+
showInInput(currentFiles);
|
|
92
|
+
}, [
|
|
93
|
+
currentFiles
|
|
94
|
+
]);
|
|
95
|
+
useLayoutEffect(()=>{
|
|
96
|
+
if (null === focusAfterRemoval.current) return;
|
|
97
|
+
const buttons = listRef.current?.querySelectorAll('button') ?? [];
|
|
98
|
+
const target = buttons[Math.min(focusAfterRemoval.current, buttons.length - 1)];
|
|
99
|
+
(target ?? inputRef.current)?.focus();
|
|
100
|
+
focusAfterRemoval.current = null;
|
|
101
|
+
});
|
|
102
|
+
useLayoutEffect(()=>{
|
|
103
|
+
const addedNames = withoutPartners(currentFiles, announcedFiles.current).map((file)=>file.name).join(', ');
|
|
104
|
+
const changes = [
|
|
105
|
+
'' === addedNames ? null : words.added(addedNames),
|
|
106
|
+
...withoutPartners(announcedFiles.current, currentFiles).map((file)=>words.removed(file.name))
|
|
107
|
+
].filter(Boolean);
|
|
108
|
+
announcedFiles.current = currentFiles;
|
|
109
|
+
if (changes.length > 0) announce([
|
|
110
|
+
...changes,
|
|
111
|
+
turnedAwayThisCommit.current
|
|
112
|
+
].filter(Boolean).join(' '));
|
|
113
|
+
turnedAwayThisCommit.current = '';
|
|
114
|
+
});
|
|
115
|
+
const commit = (nextFiles)=>{
|
|
116
|
+
if (signaturesOf(nextFiles) !== signaturesOf(currentFiles)) {
|
|
117
|
+
setHeldFiles(nextFiles);
|
|
118
|
+
onFilesChange?.(nextFiles);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const receive = (incoming)=>{
|
|
122
|
+
if (0 === incoming.length) return;
|
|
123
|
+
const rejections = [];
|
|
124
|
+
const accepted = [];
|
|
125
|
+
const known = new Set(multiple ? currentFiles.map(signatureOf) : []);
|
|
126
|
+
for (const file of incoming)if (acceptsFile(file, accept)) if (void 0 !== maxSizeBytes && file.size > maxSizeBytes) rejections.push({
|
|
127
|
+
file,
|
|
128
|
+
reason: 'size'
|
|
129
|
+
});
|
|
130
|
+
else if (!multiple && accepted.length > 0) rejections.push({
|
|
131
|
+
file,
|
|
132
|
+
reason: 'count'
|
|
133
|
+
});
|
|
134
|
+
else if (known.has(signatureOf(file))) rejections.push({
|
|
135
|
+
file,
|
|
136
|
+
reason: 'duplicate'
|
|
137
|
+
});
|
|
138
|
+
else {
|
|
139
|
+
known.add(signatureOf(file));
|
|
140
|
+
accepted.push(file);
|
|
141
|
+
}
|
|
142
|
+
else rejections.push({
|
|
143
|
+
file,
|
|
144
|
+
reason: 'type'
|
|
145
|
+
});
|
|
146
|
+
if (accepted.length > 0) commit(multiple ? [
|
|
147
|
+
...currentFiles,
|
|
148
|
+
...accepted
|
|
149
|
+
] : accepted);
|
|
150
|
+
const messages = rejections.map(({ file, reason })=>words.rejected(file.name, reason));
|
|
151
|
+
setRejectionMessages(messages);
|
|
152
|
+
if (rejections.length > 0) {
|
|
153
|
+
onReject?.(rejections);
|
|
154
|
+
turnedAwayThisCommit.current = messages.join(' ');
|
|
155
|
+
announce(turnedAwayThisCommit.current);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
useFormReset(inputRef, ()=>{
|
|
159
|
+
const restored = files ?? defaultFiles ?? [];
|
|
160
|
+
setRejectionMessages([]);
|
|
161
|
+
commit(restored);
|
|
162
|
+
showInInput(restored);
|
|
163
|
+
});
|
|
164
|
+
const remove = (index)=>{
|
|
165
|
+
focusAfterRemoval.current = index;
|
|
166
|
+
setRejectionMessages([]);
|
|
167
|
+
commit(currentFiles.filter((_file, fileIndex)=>fileIndex !== index));
|
|
168
|
+
};
|
|
169
|
+
return /*#__PURE__*/ jsxs(Field.Root, {
|
|
170
|
+
className: field_module.frame,
|
|
171
|
+
name: name,
|
|
172
|
+
disabled: disabled,
|
|
173
|
+
invalid: void 0 !== error,
|
|
174
|
+
children: [
|
|
175
|
+
/*#__PURE__*/ jsxs(Field.Label, {
|
|
176
|
+
className: field_module.label,
|
|
177
|
+
children: [
|
|
178
|
+
label,
|
|
179
|
+
/*#__PURE__*/ jsx(RequiredMark, {
|
|
180
|
+
when: required
|
|
181
|
+
})
|
|
182
|
+
]
|
|
183
|
+
}),
|
|
184
|
+
/*#__PURE__*/ jsxs("div", {
|
|
185
|
+
className: Upload_module.zone,
|
|
186
|
+
"data-dragging": isDragging ? '' : void 0,
|
|
187
|
+
"data-disabled": disabled ? '' : void 0,
|
|
188
|
+
onDragOver: (event)=>{
|
|
189
|
+
if (!carriesFiles(event)) return;
|
|
190
|
+
event.preventDefault();
|
|
191
|
+
event.dataTransfer.dropEffect = disabled ? 'none' : 'copy';
|
|
192
|
+
setIsDragging(!disabled);
|
|
193
|
+
},
|
|
194
|
+
onDragLeave: (event)=>{
|
|
195
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setIsDragging(false);
|
|
196
|
+
},
|
|
197
|
+
onDrop: (event)=>{
|
|
198
|
+
if (!carriesFiles(event)) return;
|
|
199
|
+
event.preventDefault();
|
|
200
|
+
setIsDragging(false);
|
|
201
|
+
if (!disabled) receive(Array.from(event.dataTransfer.files));
|
|
202
|
+
},
|
|
203
|
+
children: [
|
|
204
|
+
/*#__PURE__*/ jsx(Field.Control, {
|
|
205
|
+
ref: inputRef,
|
|
206
|
+
className: Upload_module.input,
|
|
207
|
+
value: signaturesOf(currentFiles),
|
|
208
|
+
onBlur: onBlur,
|
|
209
|
+
required: required && 0 === currentFiles.length,
|
|
210
|
+
onChange: (event)=>{
|
|
211
|
+
const picked = Array.from(event.currentTarget.files ?? []);
|
|
212
|
+
event.currentTarget.value = '';
|
|
213
|
+
showInInput(currentFiles);
|
|
214
|
+
receive(picked);
|
|
215
|
+
},
|
|
216
|
+
render: (controlProps)=>{
|
|
217
|
+
const { value: _signature, ...inputProps } = controlProps;
|
|
218
|
+
return /*#__PURE__*/ jsx("input", {
|
|
219
|
+
...inputProps,
|
|
220
|
+
type: "file",
|
|
221
|
+
accept: accept,
|
|
222
|
+
multiple: multiple,
|
|
223
|
+
title: ""
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}),
|
|
227
|
+
/*#__PURE__*/ jsxs("div", {
|
|
228
|
+
className: [
|
|
229
|
+
field_module.control,
|
|
230
|
+
Upload_module.face
|
|
231
|
+
].join(' '),
|
|
232
|
+
"aria-hidden": "true",
|
|
233
|
+
children: [
|
|
234
|
+
/*#__PURE__*/ jsx(Button, {
|
|
235
|
+
variant: "secondary",
|
|
236
|
+
size: "sm",
|
|
237
|
+
tabIndex: -1,
|
|
238
|
+
disabled: disabled,
|
|
239
|
+
children: words.choose
|
|
240
|
+
}),
|
|
241
|
+
/*#__PURE__*/ jsx("span", {
|
|
242
|
+
className: Upload_module.hint,
|
|
243
|
+
children: words.dropHint
|
|
244
|
+
})
|
|
245
|
+
]
|
|
246
|
+
})
|
|
247
|
+
]
|
|
248
|
+
}),
|
|
249
|
+
0 === currentFiles.length ? null : /*#__PURE__*/ jsx("ul", {
|
|
250
|
+
ref: listRef,
|
|
251
|
+
className: Upload_module.list,
|
|
252
|
+
role: "list",
|
|
253
|
+
children: currentFiles.map((file, index)=>/*#__PURE__*/ jsxs("li", {
|
|
254
|
+
className: Upload_module.file,
|
|
255
|
+
children: [
|
|
256
|
+
/*#__PURE__*/ jsx("span", {
|
|
257
|
+
className: Upload_module.fileName,
|
|
258
|
+
children: file.name
|
|
259
|
+
}),
|
|
260
|
+
/*#__PURE__*/ jsx(IconButton, {
|
|
261
|
+
size: "sm",
|
|
262
|
+
label: words.remove(file.name),
|
|
263
|
+
disabled: disabled,
|
|
264
|
+
onClick: ()=>remove(index),
|
|
265
|
+
children: /*#__PURE__*/ jsx(CloseIcon, {})
|
|
266
|
+
})
|
|
267
|
+
]
|
|
268
|
+
}, `${signatureOf(file)}-${index}`))
|
|
269
|
+
}),
|
|
270
|
+
rejectionMessages.map((message, index)=>/*#__PURE__*/ jsx("p", {
|
|
271
|
+
className: [
|
|
272
|
+
field_module.error,
|
|
273
|
+
Upload_module.rejection
|
|
274
|
+
].join(' '),
|
|
275
|
+
children: message
|
|
276
|
+
}, index)),
|
|
277
|
+
/*#__PURE__*/ jsx(FieldMessages, {
|
|
278
|
+
description: description,
|
|
279
|
+
error: error
|
|
280
|
+
}),
|
|
281
|
+
/*#__PURE__*/ jsx(LiveRegion, {
|
|
282
|
+
message: `${announcement.message}${announcement.count % 2 ? ' ' : ''}`
|
|
283
|
+
})
|
|
284
|
+
]
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
export { Upload };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import "./Upload_module.css";
|
|
2
|
+
const Upload_module = {
|
|
3
|
+
zone: "zone-ZaTfF",
|
|
4
|
+
input: "input-nfdVe",
|
|
5
|
+
face: "face-ZjTYq",
|
|
6
|
+
hint: "hint-UVfUW",
|
|
7
|
+
list: "list-jDElN",
|
|
8
|
+
file: "file-Ui1KG",
|
|
9
|
+
fileName: "fileName-CHIc8",
|
|
10
|
+
rejection: "rejection-mI7_p"
|
|
11
|
+
};
|
|
12
|
+
export default Upload_module;
|