@efolusi/meridian 1.7.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/components.css +63 -0
- package/dates/Calendar.d.ts +18 -2
- package/dates/Calendar.js +33 -9
- 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/forms/FormField.d.ts +43 -0
- package/forms/FormField.js +91 -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/package.json +1 -1
package/components.css
CHANGED
|
@@ -612,6 +612,8 @@ a.ef-sourcecard:focus-visible{outline:none;box-shadow:var(--focus-ring)}
|
|
|
612
612
|
.ef-cal__day--today{border-color:var(--sand-400)}
|
|
613
613
|
.ef-cal__day--sel{background:var(--accent);color:var(--accent-contrast);font-weight:var(--weight-semibold)}
|
|
614
614
|
.ef-cal__day--sel:hover{background:var(--sand-900)}
|
|
615
|
+
.ef-cal__day--band{background:var(--brand-100);color:var(--brand-950);border-radius:0}
|
|
616
|
+
.ef-cal__day--band:hover{background:var(--brand-200)}
|
|
615
617
|
/* components/dates/DatePicker.jsx */
|
|
616
618
|
.ef-datepicker{position:relative;display:inline-flex;flex-direction:column;gap:6px}
|
|
617
619
|
.ef-datepicker__label{font-size:var(--text-sm);font-weight:var(--weight-semibold);color:var(--text-primary)}
|
|
@@ -621,6 +623,41 @@ a.ef-sourcecard:focus-visible{outline:none;box-shadow:var(--focus-ring)}
|
|
|
621
623
|
.ef-datepicker__btn--empty{color:var(--text-muted)}
|
|
622
624
|
.ef-datepicker__pop{position:absolute;top:calc(100% + 6px);left:0;background:var(--surface-card);border:1px solid var(--border-strong);border-radius:var(--radius-md);box-shadow:var(--shadow-md);padding:12px;z-index:var(--z-dropdown);animation:ef-dp-in var(--dur-fast) var(--ease-out)}
|
|
623
625
|
@keyframes ef-dp-in{from{opacity:0;transform:translateY(-3px)}}
|
|
626
|
+
/* components/dates/DateRangePicker.jsx */
|
|
627
|
+
.ef-daterange{position:relative;display:inline-flex;flex-direction:column;gap:6px}
|
|
628
|
+
.ef-daterange__label{font-size:var(--text-sm);font-weight:var(--weight-semibold);color:var(--text-primary)}
|
|
629
|
+
.ef-daterange__hint{font-size:var(--text-xs);color:var(--text-muted)}
|
|
630
|
+
.ef-daterange__wrap{position:relative;display:inline-flex}
|
|
631
|
+
.ef-daterange__btn{flex:1;min-width:0;display:inline-flex;align-items:center;gap:8px;height:var(--control-h-md);padding:0 32px 0 12px;border:1px solid var(--border-strong);border-radius:var(--radius-sm);background:var(--surface-card);font-family:var(--font-sans);font-size:var(--text-md);color:var(--text-primary);cursor:pointer;white-space:nowrap;transition:border-color var(--dur-fast) var(--ease-out)}
|
|
632
|
+
.ef-daterange__btn:hover:not(:disabled){border-color:var(--sand-400)}
|
|
633
|
+
.ef-daterange__btn:focus-visible{outline:none;border-color:var(--accent);box-shadow:var(--focus-ring)}
|
|
634
|
+
.ef-daterange__btn:disabled{background:var(--surface-sunken);color:var(--text-muted);cursor:not-allowed}
|
|
635
|
+
.ef-daterange__btn--empty{color:var(--text-muted)}
|
|
636
|
+
.ef-daterange__chevron{position:absolute;right:10px;top:50%;transform:translateY(-50%);display:inline-flex;color:var(--text-muted);pointer-events:none}
|
|
637
|
+
.ef-daterange__clear{position:absolute;right:6px;top:50%;transform:translateY(-50%);display:inline-flex;align-items:center;justify-content:center;width:22px;height:22px;border:none;border-radius:var(--radius-sm);background:none;color:var(--text-muted);cursor:pointer}
|
|
638
|
+
.ef-daterange__clear:hover{background:var(--surface-sunken);color:var(--text-primary)}
|
|
639
|
+
.ef-daterange__clear:focus-visible{outline:none;box-shadow:var(--focus-ring)}
|
|
640
|
+
.ef-daterange__pop{background:var(--surface-card);border:1px solid var(--border-strong);border-radius:var(--radius-md);box-shadow:var(--shadow-md);padding:12px;z-index:var(--z-dropdown);animation:ef-drp-in var(--dur-fast) var(--ease-out)}
|
|
641
|
+
@keyframes ef-drp-in{from{opacity:0;transform:translateY(-3px)}}
|
|
642
|
+
/* components/dates/TimePicker.jsx */
|
|
643
|
+
.ef-field{display:flex;flex-direction:column;gap:6px}
|
|
644
|
+
.ef-field__label{font-size:var(--text-sm);font-weight:var(--weight-semibold);color:var(--text-primary)}
|
|
645
|
+
.ef-field__hint{font-size:var(--text-xs);color:var(--text-muted)}
|
|
646
|
+
.ef-timepicker{position:relative;display:flex;align-items:center}
|
|
647
|
+
.ef-timepicker__input{width:100%;height:var(--control-h-md);padding:0 34px 0 12px;border:1px solid var(--border-strong);border-radius:var(--radius-sm);background:var(--surface-card);font-family:var(--font-sans);font-size:var(--text-md);color:var(--text-primary);transition:border-color var(--dur-fast) var(--ease-out),box-shadow var(--dur-fast) var(--ease-out)}
|
|
648
|
+
.ef-timepicker__input::placeholder{color:var(--text-muted)}
|
|
649
|
+
.ef-timepicker__input:hover:not(:disabled){border-color:var(--sand-400)}
|
|
650
|
+
.ef-timepicker__input:focus{outline:none;border-color:var(--accent);box-shadow:var(--focus-ring)}
|
|
651
|
+
.ef-timepicker__input:disabled{background:var(--surface-sunken);color:var(--text-muted);cursor:not-allowed}
|
|
652
|
+
.ef-timepicker--invalid .ef-timepicker__input{border-color:var(--danger-600)}
|
|
653
|
+
.ef-timepicker--invalid .ef-timepicker__input:focus{box-shadow:var(--focus-ring-danger)}
|
|
654
|
+
.ef-timepicker__icon{position:absolute;right:11px;color:var(--text-muted);display:inline-flex;pointer-events:none}
|
|
655
|
+
.ef-timepicker__panel{max-height:240px;overflow-y:auto;background:var(--surface-card);border:1px solid var(--border-strong);border-radius:var(--radius-md);box-shadow:var(--shadow-md);padding:4px;z-index:var(--z-dropdown);animation:ef-tp-in var(--dur-fast) var(--ease-out)}
|
|
656
|
+
@keyframes ef-tp-in{from{opacity:0;transform:translateY(-3px)}}
|
|
657
|
+
.ef-timepicker__opt{display:flex;align-items:center;gap:9px;width:100%;padding:7px 10px;border:none;border-radius:var(--radius-sm);background:none;cursor:pointer;text-align:left;font-family:var(--font-sans);font-size:var(--text-sm);color:var(--text-primary)}
|
|
658
|
+
.ef-timepicker__opt--hi{background:var(--surface-sunken)}
|
|
659
|
+
.ef-timepicker__opt__check{margin-left:auto;color:var(--accent);display:inline-flex}
|
|
660
|
+
.ef-timepicker__empty{padding:14px 10px;font-size:var(--text-sm);color:var(--text-muted);text-align:center}
|
|
624
661
|
/* components/display/Accordion.jsx */
|
|
625
662
|
.ef-acc{display:flex;flex-direction:column}
|
|
626
663
|
.ef-acc__item{border-bottom:1px solid var(--border-default)}
|
|
@@ -1002,6 +1039,32 @@ button.ef-listitem:focus-visible{outline:none;box-shadow:var(--focus-ring)}
|
|
|
1002
1039
|
.ef-label{display:inline-flex;align-items:baseline;gap:5px;font-family:var(--font-sans);font-size:var(--text-sm);font-weight:600;color:var(--text-primary)}
|
|
1003
1040
|
.ef-label__req{color:var(--danger-600);font-weight:600}
|
|
1004
1041
|
.ef-label__hint{font-weight:400;color:var(--text-muted);font-size:12.5px}
|
|
1042
|
+
/* components/forms/NumberInput.jsx */
|
|
1043
|
+
.ef-field{display:flex;flex-direction:column;gap:6px}
|
|
1044
|
+
.ef-field__label{font-size:var(--text-sm);font-weight:var(--weight-semibold);color:var(--text-primary)}
|
|
1045
|
+
.ef-field__hint{font-size:var(--text-xs);color:var(--text-muted)}
|
|
1046
|
+
.ef-field__error{font-size:var(--text-xs);color:var(--danger-600);display:flex;align-items:center;gap:4px}
|
|
1047
|
+
.ef-number{display:flex;align-items:stretch;overflow:hidden;border:1px solid var(--border-strong);border-radius:var(--radius-sm);background:var(--surface-card);transition:border-color var(--dur-fast) var(--ease-out),box-shadow var(--dur-fast) var(--ease-out)}
|
|
1048
|
+
.ef-number:hover:not(.ef-number--disabled){border-color:var(--sand-400)}
|
|
1049
|
+
.ef-number:focus-within{border-color:var(--accent);box-shadow:var(--focus-ring)}
|
|
1050
|
+
.ef-number.ef-number--invalid{border-color:var(--danger-600)}
|
|
1051
|
+
.ef-number.ef-number--invalid:focus-within{box-shadow:var(--focus-ring-danger)}
|
|
1052
|
+
.ef-number--disabled{background:var(--surface-sunken)}
|
|
1053
|
+
.ef-number__el{flex:1;min-width:0;border:none;background:transparent;color:var(--text-primary);font-family:var(--font-sans);font-variant-numeric:tabular-nums}
|
|
1054
|
+
.ef-number__el:focus{outline:none}
|
|
1055
|
+
.ef-number__el::placeholder{color:var(--text-muted)}
|
|
1056
|
+
.ef-number__el:disabled{color:var(--text-muted);cursor:not-allowed}
|
|
1057
|
+
.ef-number--sm .ef-number__el{height:calc(var(--control-h-sm) - 2px);padding:0 10px;font-size:var(--text-sm)}
|
|
1058
|
+
.ef-number--md .ef-number__el{height:calc(var(--control-h-md) - 2px);padding:0 12px;font-size:var(--text-md)}
|
|
1059
|
+
.ef-number--lg .ef-number__el{height:calc(var(--control-h-lg) - 2px);padding:0 14px;font-size:var(--text-lg)}
|
|
1060
|
+
.ef-number__steps{display:flex;flex-direction:column;flex:none;align-self:stretch;border-left:1px solid var(--border-default)}
|
|
1061
|
+
.ef-number__step{flex:1 1 0;display:flex;align-items:center;justify-content:center;width:24px;padding:0;border:none;background:transparent;color:var(--text-muted);cursor:pointer;transition:background var(--dur-fast) var(--ease-out),color var(--dur-fast) var(--ease-out)}
|
|
1062
|
+
.ef-number__step:hover:not(:disabled){background:var(--surface-sunken);color:var(--text-primary)}
|
|
1063
|
+
.ef-number__step:disabled{opacity:.45;cursor:not-allowed}
|
|
1064
|
+
.ef-number__step:focus-visible{outline:none;box-shadow:var(--focus-ring)}
|
|
1065
|
+
.ef-number__step+.ef-number__step{border-top:1px solid var(--border-default)}
|
|
1066
|
+
.ef-number--sm .ef-number__step{width:22px}
|
|
1067
|
+
.ef-number--lg .ef-number__step{width:28px}
|
|
1005
1068
|
/* components/forms/Radio.jsx */
|
|
1006
1069
|
.ef-radio{display:inline-flex;align-items:flex-start;gap:10px;cursor:pointer;user-select:none}
|
|
1007
1070
|
.ef-radio--disabled{opacity:.45;cursor:not-allowed}
|
package/dates/Calendar.d.ts
CHANGED
|
@@ -1,8 +1,24 @@
|
|
|
1
|
-
export interface
|
|
1
|
+
export interface DateRange {
|
|
2
|
+
/** Range start as YYYY-MM-DD, or null while unset */
|
|
3
|
+
from: string | null;
|
|
4
|
+
/** Range end as YYYY-MM-DD, or null while unset */
|
|
5
|
+
to: string | null;
|
|
6
|
+
}
|
|
7
|
+
export interface CalendarProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
|
|
8
|
+
/** Single-date mode (the default) */
|
|
9
|
+
range?: false;
|
|
2
10
|
/** Selected date as YYYY-MM-DD */
|
|
3
11
|
value?: string;
|
|
4
12
|
onChange?: (date: string) => void;
|
|
5
13
|
style?: React.CSSProperties;
|
|
6
14
|
className?: string;
|
|
7
15
|
}
|
|
8
|
-
export
|
|
16
|
+
export interface CalendarRangeProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
|
|
17
|
+
/** Range mode: value/onChange carry a {from,to} pair; first pick sets from, second sets to (swapped when earlier), third starts over */
|
|
18
|
+
range: true;
|
|
19
|
+
value?: DateRange;
|
|
20
|
+
onChange?: (range: DateRange) => void;
|
|
21
|
+
style?: React.CSSProperties;
|
|
22
|
+
className?: string;
|
|
23
|
+
}
|
|
24
|
+
export declare function Calendar(props: CalendarProps | CalendarRangeProps): React.JSX.Element;
|
package/dates/Calendar.js
CHANGED
|
@@ -9,13 +9,18 @@ const iso = d => d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0
|
|
|
9
9
|
export function Calendar({
|
|
10
10
|
value,
|
|
11
11
|
onChange,
|
|
12
|
+
range,
|
|
12
13
|
style,
|
|
13
14
|
className,
|
|
14
15
|
...rest
|
|
15
16
|
}) {
|
|
16
|
-
|
|
17
|
+
// In range mode value is { from, to } of ISO strings (either may be null);
|
|
18
|
+
// without the prop everything below behaves exactly as before.
|
|
19
|
+
const from = range && value ? value.from || null : null;
|
|
20
|
+
const to = range && value ? value.to || null : null;
|
|
21
|
+
const sel = !range && value ? new Date(value + 'T00:00:00') : null;
|
|
17
22
|
const [view, setView] = React.useState(() => {
|
|
18
|
-
const b = sel || new Date();
|
|
23
|
+
const b = sel || (from ? new Date(from + 'T00:00:00') : new Date());
|
|
19
24
|
return [b.getFullYear(), b.getMonth()];
|
|
20
25
|
});
|
|
21
26
|
const [y, m] = view;
|
|
@@ -27,7 +32,7 @@ export function Calendar({
|
|
|
27
32
|
length: 42
|
|
28
33
|
}, (_, i) => new Date(start.getFullYear(), start.getMonth(), start.getDate() + i));
|
|
29
34
|
const gridRef = React.useRef(null);
|
|
30
|
-
const [focusIso, setFocusIso] = React.useState(() => sel ? iso(sel) : today);
|
|
35
|
+
const [focusIso, setFocusIso] = React.useState(() => sel ? iso(sel) : from || today);
|
|
31
36
|
const focusTarget = cells.some(d => iso(d) === focusIso) ? focusIso : iso(first);
|
|
32
37
|
const move = days => {
|
|
33
38
|
const base = new Date(focusTarget + 'T00:00:00');
|
|
@@ -73,6 +78,26 @@ export function Calendar({
|
|
|
73
78
|
setView([ty, tm]);
|
|
74
79
|
}
|
|
75
80
|
};
|
|
81
|
+
// Range selection: first pick opens a range, the second closes it (swapped
|
|
82
|
+
// when it lands earlier — ISO strings compare correctly), a third starts over.
|
|
83
|
+
const pick = id => {
|
|
84
|
+
setFocusIso(id);
|
|
85
|
+
if (!onChange) return;
|
|
86
|
+
if (!range) {
|
|
87
|
+
onChange(id);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (!from || to) onChange({
|
|
91
|
+
from: id,
|
|
92
|
+
to: null
|
|
93
|
+
});else if (id < from) onChange({
|
|
94
|
+
from: id,
|
|
95
|
+
to: from
|
|
96
|
+
});else onChange({
|
|
97
|
+
from,
|
|
98
|
+
to: id
|
|
99
|
+
});
|
|
100
|
+
};
|
|
76
101
|
const rows = Array.from({
|
|
77
102
|
length: 6
|
|
78
103
|
}, (_, r) => cells.slice(r * 7, r * 7 + 7));
|
|
@@ -114,19 +139,18 @@ export function Calendar({
|
|
|
114
139
|
key: ri
|
|
115
140
|
}, row.map(d => {
|
|
116
141
|
const id = iso(d);
|
|
142
|
+
const isEnd = range && (id === from || id === to);
|
|
143
|
+
const inBand = range && from && to && id > from && id < to;
|
|
117
144
|
return /*#__PURE__*/React.createElement("button", {
|
|
118
145
|
key: id,
|
|
119
146
|
role: "gridcell",
|
|
120
147
|
"data-iso": id,
|
|
121
148
|
tabIndex: id === focusTarget ? 0 : -1,
|
|
122
|
-
"aria-selected": sel ? id === iso(sel) : undefined,
|
|
149
|
+
"aria-selected": range ? from || to ? isEnd : undefined : sel ? id === iso(sel) : undefined,
|
|
123
150
|
"aria-current": id === today ? 'date' : undefined,
|
|
124
151
|
"aria-label": `${d.getDate()} ${MONTHS[d.getMonth()]} ${d.getFullYear()}`,
|
|
125
|
-
className: `ef-cal__day${d.getMonth() !== m ? ' ef-cal__day--out' : ''}${id === today ? ' ef-cal__day--today' : ''}${sel && id === iso(sel) ? ' ef-cal__day--sel' : ''}`,
|
|
126
|
-
onClick: () =>
|
|
127
|
-
setFocusIso(id);
|
|
128
|
-
if (onChange) onChange(id);
|
|
129
|
-
}
|
|
152
|
+
className: `ef-cal__day${d.getMonth() !== m ? ' ef-cal__day--out' : ''}${id === today ? ' ef-cal__day--today' : ''}${(range ? isEnd : sel && id === iso(sel)) ? ' ef-cal__day--sel' : ''}${inBand ? ' ef-cal__day--band' : ''}`,
|
|
153
|
+
onClick: () => pick(id)
|
|
130
154
|
}, d.getDate());
|
|
131
155
|
})))));
|
|
132
156
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { DateRange } from './Calendar.js';
|
|
2
|
+
|
|
3
|
+
export interface DateRangePickerProps extends Omit<React.HTMLAttributes<HTMLSpanElement>, 'onChange'> {
|
|
4
|
+
/** Field label rendered above the trigger */
|
|
5
|
+
label?: string;
|
|
6
|
+
/** Muted helper line rendered below */
|
|
7
|
+
hint?: string;
|
|
8
|
+
/** Controlled {from,to} of YYYY-MM-DD strings; either may be null */
|
|
9
|
+
value?: DateRange;
|
|
10
|
+
onChange?: (range: DateRange) => void;
|
|
11
|
+
/** Earliest pickable day as YYYY-MM-DD; earlier picks are ignored */
|
|
12
|
+
min?: string;
|
|
13
|
+
/** Latest pickable day as YYYY-MM-DD; later picks are ignored */
|
|
14
|
+
max?: string;
|
|
15
|
+
/** @default 'Pick a date range' */
|
|
16
|
+
placeholder?: string;
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
style?: React.CSSProperties;
|
|
19
|
+
className?: string;
|
|
20
|
+
}
|
|
21
|
+
export declare function DateRangePicker(props: DateRangePickerProps): React.JSX.Element;
|
|
@@ -0,0 +1,148 @@
|
|
|
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 { Calendar } from "./Calendar.js";
|
|
6
|
+
import { useFieldProps } from "../forms/FormField.js";
|
|
7
|
+
import { Portal, useAnchoredStyle } from "../overlay/Portal.js";
|
|
8
|
+
const fmt = v => {
|
|
9
|
+
if (!v) return null;
|
|
10
|
+
const d = new Date(v + 'T00:00:00');
|
|
11
|
+
return d.toLocaleDateString('en-US', {
|
|
12
|
+
month: 'short',
|
|
13
|
+
day: 'numeric',
|
|
14
|
+
year: 'numeric'
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
const EMPTY = {
|
|
18
|
+
from: null,
|
|
19
|
+
to: null
|
|
20
|
+
};
|
|
21
|
+
export function DateRangePicker({
|
|
22
|
+
label,
|
|
23
|
+
hint,
|
|
24
|
+
value,
|
|
25
|
+
onChange,
|
|
26
|
+
min,
|
|
27
|
+
max,
|
|
28
|
+
placeholder = 'Pick a date range',
|
|
29
|
+
disabled,
|
|
30
|
+
style,
|
|
31
|
+
className,
|
|
32
|
+
...rest
|
|
33
|
+
}) {
|
|
34
|
+
// Picks up id / aria wiring when nested in a FormField; standalone this is a no-op.
|
|
35
|
+
const field = useFieldProps({
|
|
36
|
+
id: rest.id,
|
|
37
|
+
'aria-describedby': rest['aria-describedby']
|
|
38
|
+
});
|
|
39
|
+
const [inner, setInner] = React.useState(EMPTY);
|
|
40
|
+
const v = value != null ? value : inner;
|
|
41
|
+
const from = v && v.from ? v.from : null;
|
|
42
|
+
const to = v && v.to ? v.to : null;
|
|
43
|
+
const [open, setOpen] = React.useState(false);
|
|
44
|
+
const rootRef = React.useRef(null);
|
|
45
|
+
const wrapRef = React.useRef(null);
|
|
46
|
+
const btnRef = React.useRef(null);
|
|
47
|
+
const panelRef = React.useRef(null);
|
|
48
|
+
const {
|
|
49
|
+
style: anchored
|
|
50
|
+
} = useAnchoredStyle(wrapRef, panelRef, {
|
|
51
|
+
open,
|
|
52
|
+
placement: 'bottom',
|
|
53
|
+
align: 'start'
|
|
54
|
+
});
|
|
55
|
+
React.useEffect(() => {
|
|
56
|
+
if (!open) return;
|
|
57
|
+
// the calendar is portaled out of `rootRef`, so look for it in the panel
|
|
58
|
+
const day = panelRef.current && panelRef.current.querySelector('[role="gridcell"][tabindex="0"]');
|
|
59
|
+
if (day) day.focus();
|
|
60
|
+
const away = e => {
|
|
61
|
+
const inTrigger = rootRef.current && rootRef.current.contains(e.target);
|
|
62
|
+
const inPanel = panelRef.current && panelRef.current.contains(e.target);
|
|
63
|
+
if (!inTrigger && !inPanel) setOpen(false);
|
|
64
|
+
};
|
|
65
|
+
const key = e => {
|
|
66
|
+
if (e.key === 'Escape') {
|
|
67
|
+
setOpen(false);
|
|
68
|
+
if (btnRef.current) btnRef.current.focus();
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
document.addEventListener('mousedown', away);
|
|
72
|
+
document.addEventListener('keydown', key);
|
|
73
|
+
return () => {
|
|
74
|
+
document.removeEventListener('mousedown', away);
|
|
75
|
+
document.removeEventListener('keydown', key);
|
|
76
|
+
};
|
|
77
|
+
}, [open]);
|
|
78
|
+
const commit = r => {
|
|
79
|
+
if (value == null) setInner(r);
|
|
80
|
+
if (onChange) onChange(r);
|
|
81
|
+
};
|
|
82
|
+
// The Calendar itself has no bounds, so a pick outside [min, max] is dropped
|
|
83
|
+
// here: any change whose ends stray out of the window never commits.
|
|
84
|
+
const inBounds = d => !d || (!min || d >= min) && (!max || d <= max);
|
|
85
|
+
const handlePick = r => {
|
|
86
|
+
if (!inBounds(r.from) || !inBounds(r.to)) return;
|
|
87
|
+
commit(r);
|
|
88
|
+
if (r.from && r.to) {
|
|
89
|
+
setOpen(false);
|
|
90
|
+
if (btnRef.current) btnRef.current.focus();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const clear = () => {
|
|
94
|
+
commit(EMPTY);
|
|
95
|
+
if (btnRef.current) btnRef.current.focus();
|
|
96
|
+
};
|
|
97
|
+
const text = from || to ? `${fmt(from) || '…'} — ${fmt(to) || '…'}` : null;
|
|
98
|
+
return /*#__PURE__*/React.createElement("span", _extends({}, rest, {
|
|
99
|
+
ref: rootRef,
|
|
100
|
+
className: `ef-daterange${className ? ' ' + className : ''}`,
|
|
101
|
+
style: style
|
|
102
|
+
}), label ? /*#__PURE__*/React.createElement("span", {
|
|
103
|
+
className: "ef-daterange__label"
|
|
104
|
+
}, label) : null, /*#__PURE__*/React.createElement("span", {
|
|
105
|
+
ref: wrapRef,
|
|
106
|
+
className: "ef-daterange__wrap"
|
|
107
|
+
}, /*#__PURE__*/React.createElement("button", _extends({}, field.controlProps, {
|
|
108
|
+
type: "button",
|
|
109
|
+
ref: btnRef,
|
|
110
|
+
disabled: disabled,
|
|
111
|
+
"aria-haspopup": "dialog",
|
|
112
|
+
"aria-expanded": open,
|
|
113
|
+
className: `ef-daterange__btn${text ? '' : ' ef-daterange__btn--empty'}`,
|
|
114
|
+
onClick: () => setOpen(o => !o)
|
|
115
|
+
}), /*#__PURE__*/React.createElement(Icon, {
|
|
116
|
+
name: "calendar",
|
|
117
|
+
size: 15,
|
|
118
|
+
style: {
|
|
119
|
+
color: 'var(--text-muted)'
|
|
120
|
+
}
|
|
121
|
+
}), text || placeholder), text && !disabled ? /*#__PURE__*/React.createElement("button", {
|
|
122
|
+
type: "button",
|
|
123
|
+
className: "ef-daterange__clear",
|
|
124
|
+
"aria-label": "Clear date range",
|
|
125
|
+
onClick: clear
|
|
126
|
+
}, /*#__PURE__*/React.createElement(Icon, {
|
|
127
|
+
name: "x",
|
|
128
|
+
size: 13
|
|
129
|
+
})) : /*#__PURE__*/React.createElement("span", {
|
|
130
|
+
className: "ef-daterange__chevron"
|
|
131
|
+
}, /*#__PURE__*/React.createElement(Icon, {
|
|
132
|
+
name: "chevron-down",
|
|
133
|
+
size: 14
|
|
134
|
+
}))), hint ? /*#__PURE__*/React.createElement("span", {
|
|
135
|
+
className: "ef-daterange__hint"
|
|
136
|
+
}, hint) : null, open && /*#__PURE__*/React.createElement(Portal, null, /*#__PURE__*/React.createElement("div", {
|
|
137
|
+
ref: panelRef,
|
|
138
|
+
className: "ef-daterange__pop",
|
|
139
|
+
style: anchored
|
|
140
|
+
}, /*#__PURE__*/React.createElement(Calendar, {
|
|
141
|
+
range: true,
|
|
142
|
+
value: {
|
|
143
|
+
from,
|
|
144
|
+
to
|
|
145
|
+
},
|
|
146
|
+
onChange: handlePick
|
|
147
|
+
}))));
|
|
148
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface TimePickerProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
|
|
2
|
+
/** Field label rendered above the input */
|
|
3
|
+
label?: string;
|
|
4
|
+
/** Muted helper line rendered below */
|
|
5
|
+
hint?: string;
|
|
6
|
+
/** Controlled time as 24h 'HH:MM', or null when empty (always 24h, whatever `format24` shows) */
|
|
7
|
+
value?: string | null;
|
|
8
|
+
onChange?: (time: string | null) => void;
|
|
9
|
+
/** Minutes between listed slots @default 30 */
|
|
10
|
+
stepMinutes?: number;
|
|
11
|
+
/** First listed slot as 'HH:MM' @default '00:00' */
|
|
12
|
+
minTime?: string;
|
|
13
|
+
/** Slots stop at this 'HH:MM' @default '23:59' */
|
|
14
|
+
maxTime?: string;
|
|
15
|
+
/** 24h labels; false renders 12h with AM/PM (value stays 24h) @default true */
|
|
16
|
+
format24?: boolean;
|
|
17
|
+
/** @default 'Pick a time' */
|
|
18
|
+
placeholder?: string;
|
|
19
|
+
disabled?: boolean;
|
|
20
|
+
/** Danger border + aria-invalid */
|
|
21
|
+
invalid?: boolean;
|
|
22
|
+
style?: React.CSSProperties;
|
|
23
|
+
className?: string;
|
|
24
|
+
}
|
|
25
|
+
export declare function TimePicker(props: TimePickerProps): React.JSX.Element;
|
|
@@ -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/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
|
@@ -116,4 +116,94 @@ export function FormField({
|
|
|
116
116
|
id: noteId,
|
|
117
117
|
className: "ef-field__hint"
|
|
118
118
|
}, hint) : null);
|
|
119
|
-
}
|
|
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;
|
|
@@ -0,0 +1,177 @@
|
|
|
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 "./FormField.js";
|
|
6
|
+
// How many decimal places a number carries, so a commit can round away float
|
|
7
|
+
// noise: 0.1 + 0.2 must land as 0.3, not 0.30000000000000004.
|
|
8
|
+
function decimalsOf(n) {
|
|
9
|
+
if (!Number.isFinite(n)) return 0;
|
|
10
|
+
const s = String(n);
|
|
11
|
+
const e = s.search(/e-/i);
|
|
12
|
+
const i = s.indexOf('.');
|
|
13
|
+
if (e !== -1) return (+s.slice(e + 2) || 0) + (i !== -1 ? e - i - 1 : 0);
|
|
14
|
+
return i === -1 ? 0 : s.length - i - 1;
|
|
15
|
+
}
|
|
16
|
+
const defaultParse = t => {
|
|
17
|
+
const n = Number(t.replace(/,/g, ''));
|
|
18
|
+
return Number.isFinite(n) ? n : null;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* A numeric field with stepper buttons — a text input with inputmode="decimal"
|
|
22
|
+
* rather than type=number, so typing stays free-form and the value only
|
|
23
|
+
* commits (parse → clamp → snap to step) on blur. Empty commits null.
|
|
24
|
+
* ArrowUp/Down step, Shift multiplies by 10, Home/End jump to finite rails.
|
|
25
|
+
*/
|
|
26
|
+
export function NumberInput({
|
|
27
|
+
label,
|
|
28
|
+
hint,
|
|
29
|
+
error,
|
|
30
|
+
invalid,
|
|
31
|
+
value,
|
|
32
|
+
defaultValue,
|
|
33
|
+
onChange,
|
|
34
|
+
min,
|
|
35
|
+
max,
|
|
36
|
+
step = 1,
|
|
37
|
+
size = 'md',
|
|
38
|
+
disabled,
|
|
39
|
+
placeholder,
|
|
40
|
+
format,
|
|
41
|
+
parse,
|
|
42
|
+
style,
|
|
43
|
+
className,
|
|
44
|
+
...rest
|
|
45
|
+
}) {
|
|
46
|
+
// Picks up id / aria wiring when nested in a FormField; standalone this is a no-op.
|
|
47
|
+
const field = useFieldProps({
|
|
48
|
+
invalid,
|
|
49
|
+
error,
|
|
50
|
+
id: rest.id,
|
|
51
|
+
'aria-describedby': rest['aria-describedby']
|
|
52
|
+
});
|
|
53
|
+
const uid = React.useId();
|
|
54
|
+
const inputId = field.id || (label ? uid + 'n' : undefined);
|
|
55
|
+
const bad = field.invalid;
|
|
56
|
+
const [inner, setInner] = React.useState(defaultValue !== undefined ? defaultValue : null);
|
|
57
|
+
const v = value !== undefined ? value : inner;
|
|
58
|
+
// The text being edited; null means "not editing, show the formatted value".
|
|
59
|
+
const [draft, setDraft] = React.useState(null);
|
|
60
|
+
const fmt = n => format ? format(n) : String(n);
|
|
61
|
+
const parseText = t => {
|
|
62
|
+
const s = t.trim();
|
|
63
|
+
if (s === '') return null;
|
|
64
|
+
const n = (parse || defaultParse)(s);
|
|
65
|
+
return n != null && Number.isFinite(n) ? n : undefined; // undefined = unparseable
|
|
66
|
+
};
|
|
67
|
+
const normalize = n => {
|
|
68
|
+
let x = n;
|
|
69
|
+
if (step > 0) {
|
|
70
|
+
const base = Number.isFinite(min) ? min : 0;
|
|
71
|
+
const d = Math.max(decimalsOf(step), decimalsOf(base));
|
|
72
|
+
x = base + Math.round((x - base) / step) * step;
|
|
73
|
+
x = Number(x.toFixed(Math.min(d, 20)));
|
|
74
|
+
}
|
|
75
|
+
if (Number.isFinite(min) && x < min) x = min;
|
|
76
|
+
if (Number.isFinite(max) && x > max) x = max;
|
|
77
|
+
return x;
|
|
78
|
+
};
|
|
79
|
+
const emit = (next, e) => {
|
|
80
|
+
if (value === undefined) setInner(next);
|
|
81
|
+
if (onChange && !Object.is(next, v)) onChange(next, e);
|
|
82
|
+
};
|
|
83
|
+
const commit = e => {
|
|
84
|
+
if (draft == null) return;
|
|
85
|
+
const n = parseText(draft);
|
|
86
|
+
setDraft(null);
|
|
87
|
+
if (n === undefined) return; // unparseable: revert to the last committed value
|
|
88
|
+
emit(n === null ? null : normalize(n), e);
|
|
89
|
+
};
|
|
90
|
+
const stepBy = (dir, mult, e) => {
|
|
91
|
+
const parsed = draft != null ? parseText(draft) : v;
|
|
92
|
+
const cur = parsed === undefined ? v : parsed;
|
|
93
|
+
let next;
|
|
94
|
+
if (cur == null) next = Number.isFinite(min) ? min : Number.isFinite(max) && dir < 0 ? max : 0;else next = cur + dir * step * mult;
|
|
95
|
+
setDraft(null);
|
|
96
|
+
emit(normalize(next), e);
|
|
97
|
+
};
|
|
98
|
+
const keys = e => {
|
|
99
|
+
if (disabled) return;
|
|
100
|
+
if (e.key === 'ArrowUp') {
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
stepBy(1, e.shiftKey ? 10 : 1, e);
|
|
103
|
+
} else if (e.key === 'ArrowDown') {
|
|
104
|
+
e.preventDefault();
|
|
105
|
+
stepBy(-1, e.shiftKey ? 10 : 1, e);
|
|
106
|
+
} else if (e.key === 'Home' && Number.isFinite(min)) {
|
|
107
|
+
e.preventDefault();
|
|
108
|
+
setDraft(null);
|
|
109
|
+
emit(normalize(min), e);
|
|
110
|
+
} else if (e.key === 'End' && Number.isFinite(max)) {
|
|
111
|
+
e.preventDefault();
|
|
112
|
+
setDraft(null);
|
|
113
|
+
emit(normalize(max), e);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
const shown = draft != null ? draft : v == null ? '' : fmt(v);
|
|
117
|
+
const upStuck = disabled || v != null && Number.isFinite(max) && v >= max;
|
|
118
|
+
const downStuck = disabled || v != null && Number.isFinite(min) && v <= min;
|
|
119
|
+
const control = /*#__PURE__*/React.createElement("span", {
|
|
120
|
+
className: `ef-number ef-number--${size}${bad ? ' ef-number--invalid' : ''}${disabled ? ' ef-number--disabled' : ''}`
|
|
121
|
+
}, /*#__PURE__*/React.createElement("input", _extends({}, rest, field.controlProps, {
|
|
122
|
+
id: inputId,
|
|
123
|
+
className: "ef-number__el",
|
|
124
|
+
type: "text",
|
|
125
|
+
inputMode: "decimal",
|
|
126
|
+
autoComplete: "off",
|
|
127
|
+
placeholder: placeholder,
|
|
128
|
+
disabled: disabled,
|
|
129
|
+
value: shown,
|
|
130
|
+
"aria-invalid": bad || undefined,
|
|
131
|
+
onChange: e => setDraft(e.target.value),
|
|
132
|
+
onKeyDown: keys,
|
|
133
|
+
onBlur: commit
|
|
134
|
+
})), /*#__PURE__*/React.createElement("span", {
|
|
135
|
+
className: "ef-number__steps"
|
|
136
|
+
}, /*#__PURE__*/React.createElement("button", {
|
|
137
|
+
type: "button",
|
|
138
|
+
className: "ef-number__step",
|
|
139
|
+
"aria-label": "Increase",
|
|
140
|
+
tabIndex: -1,
|
|
141
|
+
disabled: upStuck,
|
|
142
|
+
onMouseDown: e => e.preventDefault(),
|
|
143
|
+
onClick: e => stepBy(1, 1, e)
|
|
144
|
+
}, /*#__PURE__*/React.createElement(Icon, {
|
|
145
|
+
name: "chevron-up",
|
|
146
|
+
size: size === 'lg' ? 14 : 12
|
|
147
|
+
})), /*#__PURE__*/React.createElement("button", {
|
|
148
|
+
type: "button",
|
|
149
|
+
className: "ef-number__step",
|
|
150
|
+
"aria-label": "Decrease",
|
|
151
|
+
tabIndex: -1,
|
|
152
|
+
disabled: downStuck,
|
|
153
|
+
onMouseDown: e => e.preventDefault(),
|
|
154
|
+
onClick: e => stepBy(-1, 1, e)
|
|
155
|
+
}, /*#__PURE__*/React.createElement(Icon, {
|
|
156
|
+
name: "chevron-down",
|
|
157
|
+
size: size === 'lg' ? 14 : 12
|
|
158
|
+
}))));
|
|
159
|
+
if (!label && !hint && !error) return /*#__PURE__*/React.cloneElement(control, {
|
|
160
|
+
style,
|
|
161
|
+
className: control.props.className + (className ? ' ' + className : '')
|
|
162
|
+
});
|
|
163
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
164
|
+
className: `ef-field${className ? ' ' + className : ''}`,
|
|
165
|
+
style: style
|
|
166
|
+
}, label ? /*#__PURE__*/React.createElement("label", {
|
|
167
|
+
htmlFor: inputId,
|
|
168
|
+
className: "ef-field__label"
|
|
169
|
+
}, label) : null, control, error ? /*#__PURE__*/React.createElement("span", {
|
|
170
|
+
className: "ef-field__error"
|
|
171
|
+
}, /*#__PURE__*/React.createElement(Icon, {
|
|
172
|
+
name: "circle-alert",
|
|
173
|
+
size: 12
|
|
174
|
+
}), error) : hint ? /*#__PURE__*/React.createElement("span", {
|
|
175
|
+
className: "ef-field__hint"
|
|
176
|
+
}, hint) : null);
|
|
177
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -41,6 +41,8 @@ export * from './data/StatusDot.js';
|
|
|
41
41
|
export * from './data/Table.js';
|
|
42
42
|
export * from './dates/Calendar.js';
|
|
43
43
|
export * from './dates/DatePicker.js';
|
|
44
|
+
export * from './dates/DateRangePicker.js';
|
|
45
|
+
export * from './dates/TimePicker.js';
|
|
44
46
|
export * from './display/Accordion.js';
|
|
45
47
|
export * from './display/AspectRatio.js';
|
|
46
48
|
export * from './display/Avatar.js';
|
|
@@ -81,6 +83,7 @@ export * from './forms/IconButton.js';
|
|
|
81
83
|
export * from './forms/Input.js';
|
|
82
84
|
export * from './forms/InputGroup.js';
|
|
83
85
|
export * from './forms/Label.js';
|
|
86
|
+
export * from './forms/NumberInput.js';
|
|
84
87
|
export * from './forms/Radio.js';
|
|
85
88
|
export * from './forms/Select.js';
|
|
86
89
|
export * from './forms/Slider.js';
|
package/index.js
CHANGED
|
@@ -41,6 +41,8 @@ export { StatusDot } from './data/StatusDot.js';
|
|
|
41
41
|
export { Table } from './data/Table.js';
|
|
42
42
|
export { Calendar } from './dates/Calendar.js';
|
|
43
43
|
export { DatePicker } from './dates/DatePicker.js';
|
|
44
|
+
export { DateRangePicker } from './dates/DateRangePicker.js';
|
|
45
|
+
export { TimePicker } from './dates/TimePicker.js';
|
|
44
46
|
export { Accordion } from './display/Accordion.js';
|
|
45
47
|
export { AspectRatio } from './display/AspectRatio.js';
|
|
46
48
|
export { Avatar, AvatarGroup } from './display/Avatar.js';
|
|
@@ -81,6 +83,7 @@ export { IconButton } from './forms/IconButton.js';
|
|
|
81
83
|
export { Input } from './forms/Input.js';
|
|
82
84
|
export { InputGroup } from './forms/InputGroup.js';
|
|
83
85
|
export { Label } from './forms/Label.js';
|
|
86
|
+
export { NumberInput } from './forms/NumberInput.js';
|
|
84
87
|
export { Radio } from './forms/Radio.js';
|
|
85
88
|
export { Select } from './forms/Select.js';
|
|
86
89
|
export { Slider } from './forms/Slider.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@efolusi/meridian",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "A general-purpose open-source design system by Efolusi: accessible React components, design tokens, blocks and example apps.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://meridian.efolusi.com",
|