@efolusi/meridian 1.6.0 → 1.8.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/ai/Conversation.js +2 -1
- package/ai/Player.js +18 -1
- package/ai/SelectionQuote.js +2 -1
- package/ai/Transcript.js +2 -1
- package/ai/UsageMeter.js +2 -2
- package/code/Console.js +4 -1
- package/code/Terminal.js +2 -0
- package/components.css +69 -2
- package/dates/Calendar.d.ts +18 -2
- package/dates/Calendar.js +44 -15
- package/dates/DateRangePicker.d.ts +21 -0
- package/dates/DateRangePicker.js +148 -0
- package/dates/TimePicker.d.ts +25 -0
- package/dates/TimePicker.js +205 -0
- package/display/AspectRatio.js +1 -1
- package/display/Avatar.js +12 -3
- package/display/Carousel.js +2 -1
- package/display/Tag.js +30 -7
- package/feedback/Progress.js +4 -1
- package/feedback/Tooltip.js +2 -1
- package/forms/Button.js +15 -0
- package/forms/FormField.d.ts +43 -0
- package/forms/FormField.js +92 -1
- package/forms/NumberInput.d.ts +39 -0
- package/forms/NumberInput.js +177 -0
- package/index.d.ts +3 -0
- package/index.js +3 -0
- package/navigation/Menubar.js +4 -1
- package/navigation/Tabs.js +2 -2
- package/overlay/ContextMenu.js +2 -1
- package/overlay/HoverCard.js +11 -0
- package/overlay/Portal.js +3 -2
- package/package.json +3 -1
- package/tailwind.preset.cjs +125 -0
- package/tokens.json +622 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { Icon } from "../icons/Icon.js";
|
|
5
|
+
import { useFieldProps } from "../forms/FormField.js";
|
|
6
|
+
import { Portal, useAnchoredStyle } from "../overlay/Portal.js";
|
|
7
|
+
const toMin = t => {
|
|
8
|
+
const p = t.split(':');
|
|
9
|
+
return Number(p[0]) * 60 + Number(p[1] || 0);
|
|
10
|
+
};
|
|
11
|
+
const toHM = n => String(Math.floor(n / 60)).padStart(2, '0') + ':' + String(n % 60).padStart(2, '0');
|
|
12
|
+
const label12 = t => {
|
|
13
|
+
const p = t.split(':').map(Number);
|
|
14
|
+
const ap = p[0] < 12 ? 'AM' : 'PM';
|
|
15
|
+
return (p[0] % 12 || 12) + ':' + String(p[1]).padStart(2, '0') + ' ' + ap;
|
|
16
|
+
};
|
|
17
|
+
// '9:5' -> '09:05', '14' -> '14:00', '9:05 pm' -> '21:05'; null when not a time.
|
|
18
|
+
const parseTyped = raw => {
|
|
19
|
+
const m = /^\s*(\d{1,2})(?::(\d{1,2}))?\s*(am|pm)?\s*$/i.exec(raw);
|
|
20
|
+
if (!m) return null;
|
|
21
|
+
let h = Number(m[1]);
|
|
22
|
+
const mm = Number(m[2] || 0);
|
|
23
|
+
const ap = m[3] ? m[3].toLowerCase() : null;
|
|
24
|
+
if (ap) {
|
|
25
|
+
if (h < 1 || h > 12) return null;
|
|
26
|
+
if (ap === 'pm' && h !== 12) h += 12;
|
|
27
|
+
if (ap === 'am' && h === 12) h = 0;
|
|
28
|
+
}
|
|
29
|
+
if (h > 23 || mm > 59) return null;
|
|
30
|
+
return toHM(h * 60 + mm);
|
|
31
|
+
};
|
|
32
|
+
export function TimePicker({
|
|
33
|
+
label,
|
|
34
|
+
hint,
|
|
35
|
+
value,
|
|
36
|
+
onChange,
|
|
37
|
+
stepMinutes = 30,
|
|
38
|
+
minTime,
|
|
39
|
+
maxTime,
|
|
40
|
+
format24 = true,
|
|
41
|
+
placeholder = 'Pick a time',
|
|
42
|
+
disabled,
|
|
43
|
+
invalid,
|
|
44
|
+
style,
|
|
45
|
+
className,
|
|
46
|
+
...rest
|
|
47
|
+
}) {
|
|
48
|
+
// Picks up id / aria wiring when nested in a FormField; standalone this is a no-op.
|
|
49
|
+
const field = useFieldProps({
|
|
50
|
+
invalid,
|
|
51
|
+
id: rest.id,
|
|
52
|
+
'aria-describedby': rest['aria-describedby']
|
|
53
|
+
});
|
|
54
|
+
const bad = field.invalid;
|
|
55
|
+
const [open, setOpen] = React.useState(false);
|
|
56
|
+
const [q, setQ] = React.useState(null); // null = not editing, input mirrors `value`
|
|
57
|
+
const [hi, setHi] = React.useState(0);
|
|
58
|
+
const listId = React.useId();
|
|
59
|
+
const ref = React.useRef(null);
|
|
60
|
+
const panelRef = React.useRef(null);
|
|
61
|
+
const {
|
|
62
|
+
style: anchored
|
|
63
|
+
} = useAnchoredStyle(ref, panelRef, {
|
|
64
|
+
open,
|
|
65
|
+
placement: 'bottom',
|
|
66
|
+
align: 'start',
|
|
67
|
+
matchWidth: true
|
|
68
|
+
});
|
|
69
|
+
const lo = toMin(minTime || '00:00');
|
|
70
|
+
const end = toMin(maxTime || '23:59');
|
|
71
|
+
const step = Math.max(1, stepMinutes);
|
|
72
|
+
const slots = [];
|
|
73
|
+
for (let t = lo; t <= end; t += step) slots.push(toHM(t));
|
|
74
|
+
const fmtSlot = t => format24 ? t : label12(t);
|
|
75
|
+
const query = q ? q.trim().toLowerCase() : '';
|
|
76
|
+
const shown = query ? slots.filter(s => fmtSlot(s).toLowerCase().includes(query) || s.includes(query)) : slots;
|
|
77
|
+
// Keep the highlighted option visible to sighted users the same way
|
|
78
|
+
// aria-activedescendant keeps it visible to screen readers.
|
|
79
|
+
React.useEffect(() => {
|
|
80
|
+
if (!open) return;
|
|
81
|
+
const el = document.getElementById(`${listId}-opt-${hi}`);
|
|
82
|
+
if (el && el.scrollIntoView) el.scrollIntoView({
|
|
83
|
+
block: 'nearest'
|
|
84
|
+
});
|
|
85
|
+
}, [open, hi, listId]);
|
|
86
|
+
React.useEffect(() => {
|
|
87
|
+
if (!open) return;
|
|
88
|
+
const away = e => {
|
|
89
|
+
const inControl = ref.current && ref.current.contains(e.target);
|
|
90
|
+
const inPanel = panelRef.current && panelRef.current.contains(e.target);
|
|
91
|
+
if (!inControl && !inPanel) setOpen(false);
|
|
92
|
+
};
|
|
93
|
+
document.addEventListener('mousedown', away);
|
|
94
|
+
return () => document.removeEventListener('mousedown', away);
|
|
95
|
+
}, [open]);
|
|
96
|
+
const openList = () => {
|
|
97
|
+
if (disabled) return;
|
|
98
|
+
setOpen(true);
|
|
99
|
+
const idx = value ? slots.indexOf(value) : -1;
|
|
100
|
+
setHi(idx >= 0 ? idx : 0);
|
|
101
|
+
};
|
|
102
|
+
const pick = t => {
|
|
103
|
+
if (onChange) onChange(t);
|
|
104
|
+
setQ(null);
|
|
105
|
+
setOpen(false);
|
|
106
|
+
};
|
|
107
|
+
// A typed valid time commits on blur even when it is not a listed slot,
|
|
108
|
+
// clamped into [minTime, maxTime]; an emptied input commits null.
|
|
109
|
+
const commitTyped = () => {
|
|
110
|
+
if (q == null) return;
|
|
111
|
+
if (q.trim() === '') {
|
|
112
|
+
if (onChange) onChange(null);
|
|
113
|
+
} else {
|
|
114
|
+
const t = parseTyped(q);
|
|
115
|
+
if (t) {
|
|
116
|
+
if (onChange) onChange(toHM(Math.min(Math.max(toMin(t), lo), end)));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
setQ(null);
|
|
120
|
+
};
|
|
121
|
+
const onBlur = () => {
|
|
122
|
+
commitTyped();
|
|
123
|
+
setOpen(false);
|
|
124
|
+
};
|
|
125
|
+
const onKey = e => {
|
|
126
|
+
if (e.key === 'ArrowDown') {
|
|
127
|
+
e.preventDefault();
|
|
128
|
+
if (!open) openList();else setHi(h => Math.min(h + 1, shown.length - 1));
|
|
129
|
+
} else if (e.key === 'ArrowUp') {
|
|
130
|
+
e.preventDefault();
|
|
131
|
+
setHi(h => Math.max(h - 1, 0));
|
|
132
|
+
} else if (e.key === 'Enter' && open && shown[hi]) {
|
|
133
|
+
e.preventDefault();
|
|
134
|
+
pick(shown[hi]);
|
|
135
|
+
} else if (e.key === 'Escape') {
|
|
136
|
+
setOpen(false);
|
|
137
|
+
setQ(null);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const control = /*#__PURE__*/React.createElement("div", {
|
|
141
|
+
ref: ref,
|
|
142
|
+
className: `ef-timepicker${bad ? ' ef-timepicker--invalid' : ''}`
|
|
143
|
+
}, /*#__PURE__*/React.createElement("input", _extends({}, field.controlProps, {
|
|
144
|
+
className: "ef-timepicker__input",
|
|
145
|
+
role: "combobox",
|
|
146
|
+
"aria-expanded": open,
|
|
147
|
+
"aria-controls": listId,
|
|
148
|
+
"aria-autocomplete": "list",
|
|
149
|
+
"aria-invalid": bad || undefined,
|
|
150
|
+
"aria-activedescendant": open && shown.length ? `${listId}-opt-${hi}` : undefined,
|
|
151
|
+
placeholder: placeholder,
|
|
152
|
+
disabled: disabled,
|
|
153
|
+
value: q != null ? q : value ? fmtSlot(value) : '',
|
|
154
|
+
onChange: e => {
|
|
155
|
+
setQ(e.target.value);
|
|
156
|
+
setOpen(true);
|
|
157
|
+
setHi(0);
|
|
158
|
+
},
|
|
159
|
+
onClick: openList,
|
|
160
|
+
onFocus: openList,
|
|
161
|
+
onBlur: onBlur,
|
|
162
|
+
onKeyDown: onKey
|
|
163
|
+
})), /*#__PURE__*/React.createElement("span", {
|
|
164
|
+
className: "ef-timepicker__icon"
|
|
165
|
+
}, /*#__PURE__*/React.createElement(Icon, {
|
|
166
|
+
name: "clock",
|
|
167
|
+
size: 15
|
|
168
|
+
})), open && /*#__PURE__*/React.createElement(Portal, null, /*#__PURE__*/React.createElement("div", {
|
|
169
|
+
ref: panelRef,
|
|
170
|
+
className: "ef-timepicker__panel",
|
|
171
|
+
role: "listbox",
|
|
172
|
+
id: listId,
|
|
173
|
+
style: anchored,
|
|
174
|
+
onMouseDown: e => e.preventDefault()
|
|
175
|
+
}, shown.length === 0 && /*#__PURE__*/React.createElement("div", {
|
|
176
|
+
className: "ef-timepicker__empty"
|
|
177
|
+
}, "No matching time."), shown.map((t, i) => /*#__PURE__*/React.createElement("button", {
|
|
178
|
+
key: t,
|
|
179
|
+
type: "button",
|
|
180
|
+
role: "option",
|
|
181
|
+
id: `${listId}-opt-${i}`,
|
|
182
|
+
tabIndex: -1,
|
|
183
|
+
"aria-selected": t === value,
|
|
184
|
+
className: `ef-timepicker__opt${i === hi ? ' ef-timepicker__opt--hi' : ''}`,
|
|
185
|
+
onMouseEnter: () => setHi(i),
|
|
186
|
+
onClick: () => pick(t)
|
|
187
|
+
}, fmtSlot(t), t === value ? /*#__PURE__*/React.createElement("span", {
|
|
188
|
+
className: "ef-timepicker__opt__check"
|
|
189
|
+
}, /*#__PURE__*/React.createElement(Icon, {
|
|
190
|
+
name: "check",
|
|
191
|
+
size: 15
|
|
192
|
+
})) : null)))));
|
|
193
|
+
if (!label && !hint) return /*#__PURE__*/React.createElement("div", _extends({}, rest, {
|
|
194
|
+
className: className,
|
|
195
|
+
style: style
|
|
196
|
+
}), control);
|
|
197
|
+
return /*#__PURE__*/React.createElement("div", _extends({}, rest, {
|
|
198
|
+
className: `ef-field${className ? ' ' + className : ''}`,
|
|
199
|
+
style: style
|
|
200
|
+
}), label ? /*#__PURE__*/React.createElement("span", {
|
|
201
|
+
className: "ef-field__label"
|
|
202
|
+
}, label) : null, control, hint ? /*#__PURE__*/React.createElement("span", {
|
|
203
|
+
className: "ef-field__hint"
|
|
204
|
+
}, hint) : null);
|
|
205
|
+
}
|
package/display/AspectRatio.js
CHANGED
package/display/Avatar.js
CHANGED
|
@@ -10,6 +10,14 @@ export function Avatar({
|
|
|
10
10
|
className,
|
|
11
11
|
...rest
|
|
12
12
|
}) {
|
|
13
|
+
// A dead avatar URL falls back to the initials instead of a broken image.
|
|
14
|
+
// The reset must be an effect: remounting the img would not clear state that
|
|
15
|
+
// lives up here in Avatar.
|
|
16
|
+
const [broken, setBroken] = React.useState(false);
|
|
17
|
+
React.useEffect(() => {
|
|
18
|
+
setBroken(false);
|
|
19
|
+
}, [src]);
|
|
20
|
+
const showImg = !!src && !broken;
|
|
13
21
|
const initials = name.trim().split(/\s+/).map(w => w[0]).slice(0, 2).join('').toUpperCase();
|
|
14
22
|
let h = 0;
|
|
15
23
|
for (let i = 0; i < name.length; i++) h = h * 31 + name.charCodeAt(i) >>> 0;
|
|
@@ -21,13 +29,14 @@ export function Avatar({
|
|
|
21
29
|
width: size,
|
|
22
30
|
height: size,
|
|
23
31
|
fontSize: Math.round(size * 0.38),
|
|
24
|
-
background:
|
|
32
|
+
background: showImg ? 'var(--sand-100)' : bg,
|
|
25
33
|
color: fg,
|
|
26
34
|
...style
|
|
27
35
|
}
|
|
28
|
-
}, rest),
|
|
36
|
+
}, rest), showImg ? /*#__PURE__*/React.createElement("img", {
|
|
29
37
|
src: src,
|
|
30
|
-
alt: name
|
|
38
|
+
alt: name,
|
|
39
|
+
onError: () => setBroken(true)
|
|
31
40
|
}) : initials || '•');
|
|
32
41
|
}
|
|
33
42
|
export function AvatarGroup({
|
package/display/Carousel.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|
3
3
|
import React from 'react';
|
|
4
|
+
import { prefersReducedMotion } from "../forms/Button.js";
|
|
4
5
|
import { Icon } from "../icons/Icon.js";
|
|
5
6
|
export function Carousel({
|
|
6
7
|
children,
|
|
@@ -25,7 +26,7 @@ export function Carousel({
|
|
|
25
26
|
const el = track.current;
|
|
26
27
|
if (el) el.scrollTo({
|
|
27
28
|
left: i * step(),
|
|
28
|
-
behavior: 'smooth'
|
|
29
|
+
behavior: prefersReducedMotion() ? 'auto' : 'smooth'
|
|
29
30
|
});
|
|
30
31
|
};
|
|
31
32
|
const onScroll = () => {
|
package/display/Tag.js
CHANGED
|
@@ -11,14 +11,12 @@ export function Tag({
|
|
|
11
11
|
className,
|
|
12
12
|
...rest
|
|
13
13
|
}) {
|
|
14
|
-
|
|
15
|
-
className: `ef-tag${onClick ? ' ef-tag--interactive' : ''}${className ? ' ' + className : ''}`,
|
|
16
|
-
onClick: onClick,
|
|
17
|
-
style: style
|
|
18
|
-
}, rest), icon ? /*#__PURE__*/React.createElement(Icon, {
|
|
14
|
+
const body = /*#__PURE__*/React.createElement(React.Fragment, null, icon ? /*#__PURE__*/React.createElement(Icon, {
|
|
19
15
|
name: icon,
|
|
20
16
|
size: 13
|
|
21
|
-
}) : null, children
|
|
17
|
+
}) : null, children);
|
|
18
|
+
const removeBtn = onRemove ? /*#__PURE__*/React.createElement("button", {
|
|
19
|
+
type: "button",
|
|
22
20
|
className: "ef-tag__x",
|
|
23
21
|
"aria-label": "Remove",
|
|
24
22
|
onClick: e => {
|
|
@@ -28,5 +26,30 @@ export function Tag({
|
|
|
28
26
|
}, /*#__PURE__*/React.createElement(Icon, {
|
|
29
27
|
name: "x",
|
|
30
28
|
size: 12
|
|
31
|
-
})) : null
|
|
29
|
+
})) : null;
|
|
30
|
+
// A clickable tag has to be a real button, or the keyboard never reaches it.
|
|
31
|
+
// With onRemove present the root cannot be one (interactive content must not
|
|
32
|
+
// nest), so the label becomes an inner button beside the remove button.
|
|
33
|
+
if (onClick && !onRemove) {
|
|
34
|
+
return /*#__PURE__*/React.createElement("button", _extends({
|
|
35
|
+
type: "button",
|
|
36
|
+
className: `ef-tag ef-tag--interactive${className ? ' ' + className : ''}`,
|
|
37
|
+
onClick: onClick,
|
|
38
|
+
style: style
|
|
39
|
+
}, rest), body);
|
|
40
|
+
}
|
|
41
|
+
if (onClick) {
|
|
42
|
+
return /*#__PURE__*/React.createElement("span", _extends({
|
|
43
|
+
className: `ef-tag ef-tag--interactive${className ? ' ' + className : ''}`,
|
|
44
|
+
style: style
|
|
45
|
+
}, rest), /*#__PURE__*/React.createElement("button", {
|
|
46
|
+
type: "button",
|
|
47
|
+
className: "ef-tag__body",
|
|
48
|
+
onClick: onClick
|
|
49
|
+
}, body), removeBtn);
|
|
50
|
+
}
|
|
51
|
+
return /*#__PURE__*/React.createElement("span", _extends({
|
|
52
|
+
className: `ef-tag${className ? ' ' + className : ''}`,
|
|
53
|
+
style: style
|
|
54
|
+
}, rest), body, removeBtn);
|
|
32
55
|
}
|
package/feedback/Progress.js
CHANGED
|
@@ -13,16 +13,19 @@ export function Progress({
|
|
|
13
13
|
...rest
|
|
14
14
|
}) {
|
|
15
15
|
const pct = Math.min(100, Math.max(0, value / max * 100));
|
|
16
|
+
const labelId = React.useId();
|
|
16
17
|
return /*#__PURE__*/React.createElement("div", _extends({}, rest, {
|
|
17
18
|
className: `ef-progress ef-progress--${tone}${className ? ' ' + className : ''}`,
|
|
18
19
|
style: style,
|
|
19
20
|
role: "progressbar",
|
|
20
21
|
"aria-valuenow": value,
|
|
21
22
|
"aria-valuemin": 0,
|
|
22
|
-
"aria-valuemax": max
|
|
23
|
+
"aria-valuemax": max,
|
|
24
|
+
"aria-labelledby": label ? labelId : undefined
|
|
23
25
|
}), (label || showValue) && /*#__PURE__*/React.createElement("div", {
|
|
24
26
|
className: "ef-progress__head"
|
|
25
27
|
}, /*#__PURE__*/React.createElement("span", {
|
|
28
|
+
id: labelId,
|
|
26
29
|
className: "ef-progress__label"
|
|
27
30
|
}, label), showValue ? /*#__PURE__*/React.createElement("span", {
|
|
28
31
|
className: "ef-progress__val"
|
package/feedback/Tooltip.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|
3
3
|
import React from 'react';
|
|
4
|
+
import { useIsoLayoutEffect } from "../forms/Button.js";
|
|
4
5
|
import { Portal, useAnchoredStyle } from "../overlay/Portal.js";
|
|
5
6
|
export function Tooltip({
|
|
6
7
|
label,
|
|
@@ -29,7 +30,7 @@ export function Tooltip({
|
|
|
29
30
|
// Keep the arrow over the trigger's centre even when the bubble has been
|
|
30
31
|
// shifted to stay on screen, so it never points at empty space.
|
|
31
32
|
const [arrowX, setArrowX] = React.useState(null);
|
|
32
|
-
|
|
33
|
+
useIsoLayoutEffect(() => {
|
|
33
34
|
if (!open || !ref.current || !bubbleRef.current) {
|
|
34
35
|
setArrowX(null);
|
|
35
36
|
return;
|
package/forms/Button.js
CHANGED
|
@@ -37,6 +37,21 @@ export function cssPct(value) {
|
|
|
37
37
|
* anchor an overlay, trap focus, or measure. Lowercase, so it stays an internal
|
|
38
38
|
* helper rather than part of the public namespace.
|
|
39
39
|
*/
|
|
40
|
+
/**
|
|
41
|
+
* useLayoutEffect that is silent on the server.
|
|
42
|
+
*
|
|
43
|
+
* React warns when useLayoutEffect renders under renderToString; the effect
|
|
44
|
+
* body is positioning work that only means anything with a DOM, so downgrading
|
|
45
|
+
* to useEffect on the server is behaviour-identical. Lowercase: internal.
|
|
46
|
+
*/
|
|
47
|
+
export const useIsoLayoutEffect = typeof window === 'undefined' ? React.useEffect : React.useLayoutEffect;
|
|
48
|
+
/**
|
|
49
|
+
* The CSS kill-switch cannot reach JS-driven motion — rAF count-ups and
|
|
50
|
+
* scrollTo({behavior:'smooth'}) — so those call sites ask here. Lowercase: internal.
|
|
51
|
+
*/
|
|
52
|
+
export function prefersReducedMotion() {
|
|
53
|
+
return typeof window !== 'undefined' && !!window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
54
|
+
}
|
|
40
55
|
export function mergeRefs(...refs) {
|
|
41
56
|
return node => {
|
|
42
57
|
for (const r of refs) {
|
package/forms/FormField.d.ts
CHANGED
|
@@ -37,3 +37,46 @@ export interface FormFieldProps {
|
|
|
37
37
|
* anything else, pass a function and spread `controlProps` on the control.
|
|
38
38
|
*/
|
|
39
39
|
export declare function FormField(props: FormFieldProps): React.JSX.Element;
|
|
40
|
+
|
|
41
|
+
/** What `field(name)` returns: spread it on a control, or wire prop-by-prop. */
|
|
42
|
+
export interface FormFieldControl<V = unknown> {
|
|
43
|
+
value: V;
|
|
44
|
+
/** Accepts a change event (reads `target.value`, or `target.checked` for checkboxes) or a raw value. */
|
|
45
|
+
onChange: (eventOrValue: unknown) => void;
|
|
46
|
+
/** Marks the field touched, which lets its error surface. */
|
|
47
|
+
onBlur: () => void;
|
|
48
|
+
invalid: boolean;
|
|
49
|
+
/** Only set after the field was touched or a submit attempt (guidelines/forms.md timing). */
|
|
50
|
+
error: string | undefined;
|
|
51
|
+
}
|
|
52
|
+
export interface UseFormStateOptions<T extends Record<string, any>> {
|
|
53
|
+
/** Seed values. Read once, on the first render. */
|
|
54
|
+
initial: T;
|
|
55
|
+
/** Returns `{ field: 'message' }` for invalid fields; empty object = valid. */
|
|
56
|
+
validate?: (values: T) => Partial<Record<keyof T & string, string>>;
|
|
57
|
+
}
|
|
58
|
+
export interface UseFormStateReturn<T extends Record<string, any>> {
|
|
59
|
+
values: T;
|
|
60
|
+
/** The raw `validate(values)` result — not gated by touch. `field().error` is. */
|
|
61
|
+
errors: Partial<Record<keyof T & string, string>>;
|
|
62
|
+
touched: Partial<Record<keyof T & string, boolean>>;
|
|
63
|
+
set: <K extends keyof T & string>(name: K, value: T[K]) => void;
|
|
64
|
+
field: <K extends keyof T & string>(name: K) => FormFieldControl<T[K]>;
|
|
65
|
+
/** True while an async submit handler is in flight. */
|
|
66
|
+
submitting: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Wraps a submit handler: validates everything, marks all fields touched,
|
|
69
|
+
* and only calls `fn(values)` when clean. An async `fn` sets `submitting`
|
|
70
|
+
* around its promise.
|
|
71
|
+
*/
|
|
72
|
+
handleSubmit: (fn?: (values: T) => unknown | Promise<unknown>) => (e?: React.SyntheticEvent) => unknown;
|
|
73
|
+
/** Back to `initial`, untouched, not submitting. */
|
|
74
|
+
reset: () => void;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Zero-dependency form state. Published as a static — `FormField.useFormState()`
|
|
78
|
+
* — because only capitalised exports reach the global namespace.
|
|
79
|
+
*/
|
|
80
|
+
export declare namespace FormField {
|
|
81
|
+
function useFormState<T extends Record<string, any>>(options: UseFormStateOptions<T>): UseFormStateReturn<T>;
|
|
82
|
+
}
|
package/forms/FormField.js
CHANGED
|
@@ -107,6 +107,7 @@ export function FormField({
|
|
|
107
107
|
value: ctx
|
|
108
108
|
}, typeof children === 'function' ? children(wiring) : children), hasError ? /*#__PURE__*/React.createElement("span", {
|
|
109
109
|
id: noteId,
|
|
110
|
+
role: "alert",
|
|
110
111
|
className: "ef-field__error"
|
|
111
112
|
}, /*#__PURE__*/React.createElement(Icon, {
|
|
112
113
|
name: "circle-alert",
|
|
@@ -115,4 +116,94 @@ export function FormField({
|
|
|
115
116
|
id: noteId,
|
|
116
117
|
className: "ef-field__hint"
|
|
117
118
|
}, hint) : null);
|
|
118
|
-
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Zero-dependency form state: values, per-field wiring, validation timing.
|
|
123
|
+
*
|
|
124
|
+
* `validate(values)` returns `{ field: 'message' }` for whatever is currently
|
|
125
|
+
* invalid; an empty object means clean. A field's error only surfaces once
|
|
126
|
+
* that field has blurred or a submit was attempted — the timing
|
|
127
|
+
* guidelines/forms.md prescribes (blur, then re-validate on change, then
|
|
128
|
+
* everything on submit) — while `errors` always carries the raw result.
|
|
129
|
+
*
|
|
130
|
+
* Lowercase, so the compiler files it under unexposedExports and it never
|
|
131
|
+
* reaches the global namespace directly. Consumers get it as
|
|
132
|
+
* `FormField.useFormState` (assigned at the bottom of this file), the same
|
|
133
|
+
* publication Toaster.useToast uses.
|
|
134
|
+
*/
|
|
135
|
+
export function useFormState({
|
|
136
|
+
initial,
|
|
137
|
+
validate
|
|
138
|
+
} = {}) {
|
|
139
|
+
// The first render's `initial` wins for the life of the hook, so an inline
|
|
140
|
+
// object literal does not re-seed the form on every render.
|
|
141
|
+
const initialRef = React.useRef(initial || {});
|
|
142
|
+
const [values, setValues] = React.useState(initialRef.current);
|
|
143
|
+
const [touched, setTouched] = React.useState({});
|
|
144
|
+
const [submitted, setSubmitted] = React.useState(false);
|
|
145
|
+
const [submitting, setSubmitting] = React.useState(false);
|
|
146
|
+
const errors = validate ? validate(values) || {} : {};
|
|
147
|
+
const set = React.useCallback((name, val) => {
|
|
148
|
+
setValues(prev => ({
|
|
149
|
+
...prev,
|
|
150
|
+
[name]: val
|
|
151
|
+
}));
|
|
152
|
+
}, []);
|
|
153
|
+
const field = name => {
|
|
154
|
+
const err = touched[name] || submitted ? errors[name] : undefined;
|
|
155
|
+
return {
|
|
156
|
+
value: values[name],
|
|
157
|
+
onChange: eventOrValue => {
|
|
158
|
+
const t = eventOrValue && typeof eventOrValue === 'object' ? eventOrValue.target : null;
|
|
159
|
+
set(name, t && 'value' in t ? t.type === 'checkbox' ? t.checked : t.value : eventOrValue);
|
|
160
|
+
},
|
|
161
|
+
onBlur: () => setTouched(prev => prev[name] ? prev : {
|
|
162
|
+
...prev,
|
|
163
|
+
[name]: true
|
|
164
|
+
}),
|
|
165
|
+
invalid: err != null && err !== '',
|
|
166
|
+
error: err != null && err !== '' ? err : undefined
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
const handleSubmit = fn => e => {
|
|
170
|
+
if (e && typeof e.preventDefault === 'function') e.preventDefault();
|
|
171
|
+
setSubmitted(true);
|
|
172
|
+
const errs = validate ? validate(values) || {} : {};
|
|
173
|
+
const all = {};
|
|
174
|
+
Object.keys(values).forEach(k => {
|
|
175
|
+
all[k] = true;
|
|
176
|
+
});
|
|
177
|
+
Object.keys(errs).forEach(k => {
|
|
178
|
+
all[k] = true;
|
|
179
|
+
});
|
|
180
|
+
setTouched(prev => ({
|
|
181
|
+
...prev,
|
|
182
|
+
...all
|
|
183
|
+
}));
|
|
184
|
+
if (Object.keys(errs).length > 0) return;
|
|
185
|
+
const out = fn ? fn(values) : undefined;
|
|
186
|
+
if (out && typeof out.then === 'function') {
|
|
187
|
+
setSubmitting(true);
|
|
188
|
+
return Promise.resolve(out).finally(() => setSubmitting(false));
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
};
|
|
192
|
+
const reset = React.useCallback(() => {
|
|
193
|
+
setValues(initialRef.current);
|
|
194
|
+
setTouched({});
|
|
195
|
+
setSubmitted(false);
|
|
196
|
+
setSubmitting(false);
|
|
197
|
+
}, []);
|
|
198
|
+
return {
|
|
199
|
+
values,
|
|
200
|
+
errors,
|
|
201
|
+
touched,
|
|
202
|
+
set,
|
|
203
|
+
field,
|
|
204
|
+
submitting,
|
|
205
|
+
handleSubmit,
|
|
206
|
+
reset
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
FormField.useFormState = useFormState;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface NumberInputProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size' | 'value' | 'defaultValue' | 'onChange' | 'min' | 'max' | 'step'> {
|
|
2
|
+
/** Field label rendered above */
|
|
3
|
+
label?: string;
|
|
4
|
+
/** Muted helper text below */
|
|
5
|
+
hint?: string;
|
|
6
|
+
/** Error message below — also sets invalid styling */
|
|
7
|
+
error?: string;
|
|
8
|
+
/** Invalid styling with no message. @default false */
|
|
9
|
+
invalid?: boolean;
|
|
10
|
+
/** Controlled value; null is "empty" */
|
|
11
|
+
value?: number | null;
|
|
12
|
+
defaultValue?: number | null;
|
|
13
|
+
/** Fires on commit (blur, arrow keys, steppers). null when cleared. */
|
|
14
|
+
onChange?: (value: number | null, e: React.SyntheticEvent) => void;
|
|
15
|
+
/** Lower rail; commits clamp to it, Home jumps to it when finite */
|
|
16
|
+
min?: number;
|
|
17
|
+
/** Upper rail; commits clamp to it, End jumps to it when finite */
|
|
18
|
+
max?: number;
|
|
19
|
+
/** Arrow/stepper increment; commits snap to its grid. @default 1 */
|
|
20
|
+
step?: number;
|
|
21
|
+
/** @default 'md' */
|
|
22
|
+
size?: 'sm' | 'md' | 'lg';
|
|
23
|
+
disabled?: boolean;
|
|
24
|
+
placeholder?: string;
|
|
25
|
+
/** Display formatting for the committed value, e.g. n => n + ' GB' */
|
|
26
|
+
format?: (value: number) => string;
|
|
27
|
+
/** Turn typed text back into a number; pair it with `format`. Return null for unparseable. */
|
|
28
|
+
parse?: (text: string) => number | null;
|
|
29
|
+
style?: React.CSSProperties;
|
|
30
|
+
className?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Numeric field with stepper buttons. A text input with inputmode="decimal" —
|
|
34
|
+
* not type=number — so typing stays free-form; the value commits on blur:
|
|
35
|
+
* parse, clamp to min/max, snap to the step grid. Empty commits null.
|
|
36
|
+
* ArrowUp/Down step, Shift+Arrow steps by 10× step, Home/End jump to finite
|
|
37
|
+
* rails. Steppers disable at the rails.
|
|
38
|
+
*/
|
|
39
|
+
export declare function NumberInput(props: NumberInputProps): React.JSX.Element;
|