@ismailnakkar/ui-behaviours 0.1.0 → 0.3.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/README.md +366 -110
- package/package.json +9 -3
- package/src/helpers.js +4 -0
- package/src/index.d.ts +4 -1
- package/src/index.js +48 -8
- package/src/range-picker.css +410 -0
- package/src/range-picker.d.css.ts +2 -0
- package/src/range-picker.js +21 -17
- package/src/steps.js +197 -0
package/src/range-picker.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'cally';
|
|
2
|
-
import {closestTo, once} from './helpers.js';
|
|
2
|
+
import {closestTo, isRadio, once} from './helpers.js';
|
|
3
3
|
|
|
4
4
|
const PICKER = '[data-range-picker]';
|
|
5
5
|
const CALENDAR = 'calendar-range';
|
|
@@ -11,12 +11,13 @@ const State = Object.freeze({preset: 'preset', custom: 'custom', pending: 'pendi
|
|
|
11
11
|
/** data-range-dates="compact" leaves out a year both ends share. */
|
|
12
12
|
const COMPACT = 'compact';
|
|
13
13
|
|
|
14
|
-
/** Between the two days of a Cally range value: "2026-11-14/2026-11-20". */
|
|
14
|
+
/** Between the two days of a Cally range value, and of a preset's data-range-preset: "2026-11-14/2026-11-20". */
|
|
15
15
|
const VALUE_SEPARATOR = '/';
|
|
16
16
|
|
|
17
17
|
/** A spaced en dash: "14 Nov – 20 Nov 2026". */
|
|
18
18
|
const DATES_SEPARATOR = ' – ';
|
|
19
19
|
|
|
20
|
+
// In UTC, as dayOf() reads each day as its UTC midnight.
|
|
20
21
|
const DAY = {day: 'numeric', month: 'short', timeZone: 'UTC'};
|
|
21
22
|
const DAY_AND_YEAR = {...DAY, year: 'numeric'};
|
|
22
23
|
|
|
@@ -37,18 +38,15 @@ function isCalendar(element) {
|
|
|
37
38
|
return element.localName === CALENDAR;
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
function isRadio(element) {
|
|
41
|
-
return element instanceof HTMLInputElement && element.type === 'radio';
|
|
42
|
-
}
|
|
43
|
-
|
|
44
41
|
function pickRange(event) {
|
|
45
42
|
const picker = closestTo(event, PICKER);
|
|
46
|
-
const target = event.target;
|
|
47
43
|
|
|
48
44
|
if (!picker) {
|
|
49
45
|
return;
|
|
50
46
|
}
|
|
51
47
|
|
|
48
|
+
const target = event.target;
|
|
49
|
+
|
|
52
50
|
if (isCalendar(target)) {
|
|
53
51
|
const [from, to] = target.value.split(VALUE_SEPARATOR);
|
|
54
52
|
|
|
@@ -77,17 +75,22 @@ function pickStart(event) {
|
|
|
77
75
|
}
|
|
78
76
|
|
|
79
77
|
const start = isoOf(event.detail);
|
|
78
|
+
const range = {state: State.pending, name: nameOf(customRadioOf(picker)), from: start, to: start};
|
|
80
79
|
|
|
81
|
-
showIn(picker,
|
|
80
|
+
showIn(picker, range, localeOf(picker));
|
|
82
81
|
}
|
|
83
82
|
|
|
84
|
-
/** A preset radio's days, worked out by the server; null for Custom range. */
|
|
83
|
+
/** A preset radio's days, worked out by the server, from its data-range-preset="from/to"; null for Custom range. */
|
|
85
84
|
function presetOf(radio) {
|
|
86
|
-
const
|
|
85
|
+
const [from, to] = radio?.dataset.rangePreset?.split(VALUE_SEPARATOR) ?? [];
|
|
87
86
|
|
|
88
87
|
return from && to ? {from, to} : null;
|
|
89
88
|
}
|
|
90
89
|
|
|
90
|
+
function calendarOf(picker) {
|
|
91
|
+
return picker.querySelector(CALENDAR);
|
|
92
|
+
}
|
|
93
|
+
|
|
91
94
|
function checkedRadioOf(picker) {
|
|
92
95
|
return picker.querySelector('input[type="radio"]:checked');
|
|
93
96
|
}
|
|
@@ -106,7 +109,7 @@ function checkCustom(picker) {
|
|
|
106
109
|
|
|
107
110
|
/** Drops a start still waiting for its end day. */
|
|
108
111
|
function dropStart(picker) {
|
|
109
|
-
const calendar = picker
|
|
112
|
+
const calendar = calendarOf(picker);
|
|
110
113
|
|
|
111
114
|
if (calendar) {
|
|
112
115
|
calendar.tentative = '';
|
|
@@ -114,7 +117,7 @@ function dropStart(picker) {
|
|
|
114
117
|
}
|
|
115
118
|
|
|
116
119
|
function moveCalendar(picker, from, to) {
|
|
117
|
-
const calendar = picker
|
|
120
|
+
const calendar = calendarOf(picker);
|
|
118
121
|
|
|
119
122
|
if (calendar) {
|
|
120
123
|
calendar.value = from + VALUE_SEPARATOR + to;
|
|
@@ -124,11 +127,11 @@ function moveCalendar(picker, from, to) {
|
|
|
124
127
|
|
|
125
128
|
/**
|
|
126
129
|
* Ends the two months on the end day's and focuses that day. Cally pages to a far day by whole pages, which can leave
|
|
127
|
-
* it in the first month
|
|
128
|
-
*
|
|
130
|
+
* it in the first month. So the view goes to the month before first, then, a task later once Cally has redrawn, to the
|
|
131
|
+
* day itself: one page on with page-by="single".
|
|
129
132
|
*/
|
|
130
133
|
function viewEndDay(picker, to) {
|
|
131
|
-
const calendar = picker
|
|
134
|
+
const calendar = calendarOf(picker);
|
|
132
135
|
const monthBefore = dayOf(to);
|
|
133
136
|
|
|
134
137
|
if (!calendar || !monthBefore) {
|
|
@@ -204,12 +207,13 @@ function openersOf(picker) {
|
|
|
204
207
|
return [];
|
|
205
208
|
}
|
|
206
209
|
|
|
207
|
-
return [...document.querySelectorAll('[popovertarget]')]
|
|
210
|
+
return [...document.querySelectorAll('[popovertarget]')]
|
|
211
|
+
.filter((opener) => opener.getAttribute('popovertarget') === picker.id);
|
|
208
212
|
}
|
|
209
213
|
|
|
210
214
|
/** The calendar's locale, so the labels match its months; else the page's lang. */
|
|
211
215
|
function localeOf(picker) {
|
|
212
|
-
return picker
|
|
216
|
+
return calendarOf(picker)?.getAttribute('locale') || picker.closest('[lang]')?.lang || undefined;
|
|
213
217
|
}
|
|
214
218
|
|
|
215
219
|
function isoOf(day) {
|
package/src/steps.js
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import {closestTo} from './helpers.js';
|
|
2
|
+
|
|
3
|
+
const STEPPER = '[data-steps]';
|
|
4
|
+
const STEP = '[data-step]';
|
|
5
|
+
const FIELDS = 'input, select, textarea';
|
|
6
|
+
const HEADING = 'h1, h2, h3, h4, h5, h6';
|
|
7
|
+
const BACK = '[data-step-back]';
|
|
8
|
+
const NEXT = '[data-step-next]';
|
|
9
|
+
const SUBMIT = '[data-step-submit]';
|
|
10
|
+
const MARK_STATE = 'data-step-state';
|
|
11
|
+
|
|
12
|
+
/** Steps are numbered from 1, in the order they come. */
|
|
13
|
+
const FIRST_STEP = 1;
|
|
14
|
+
|
|
15
|
+
/** The values of data-step-state on a progress mark: the steps behind are done, the one showing is current. */
|
|
16
|
+
const MarkState = Object.freeze({done: 'done', current: 'current'});
|
|
17
|
+
|
|
18
|
+
/** Listens for the steppers' buttons and Enter, and shows each stepper on the page at the step it opens at. */
|
|
19
|
+
export function installSteps() {
|
|
20
|
+
document.addEventListener('click', stepOnClick);
|
|
21
|
+
document.addEventListener('keydown', stepOnEnter);
|
|
22
|
+
|
|
23
|
+
for (const stepper of document.querySelectorAll(STEPPER)) {
|
|
24
|
+
openStepper(stepper);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// preventDefault(): a control that is a submit button moves the steps without sending the form.
|
|
29
|
+
function stepOnClick(event) {
|
|
30
|
+
const control = closestTo(event, `${BACK}, ${NEXT}, ${SUBMIT}`);
|
|
31
|
+
const stepper = control?.closest(STEPPER);
|
|
32
|
+
|
|
33
|
+
if (!stepper || event.defaultPrevented) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
event.preventDefault();
|
|
38
|
+
|
|
39
|
+
if (control.matches(BACK)) {
|
|
40
|
+
back(stepper);
|
|
41
|
+
} else if (control.matches(NEXT)) {
|
|
42
|
+
advance(stepper);
|
|
43
|
+
} else {
|
|
44
|
+
submit(stepper);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Enter in a field moves on, as Next does: the form's own submit would skip the steps' checks.
|
|
49
|
+
function stepOnEnter(event) {
|
|
50
|
+
const field = event.target;
|
|
51
|
+
const stepper = field instanceof HTMLInputElement ? field.closest(STEP)?.closest(STEPPER) : null;
|
|
52
|
+
|
|
53
|
+
if (event.key !== 'Enter' || event.isComposing || event.defaultPrevented || !stepper) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
event.preventDefault();
|
|
58
|
+
advance(stepper);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A refused submit comes back with its errors in the stepper: it opens at the earliest step with a refused or an empty
|
|
63
|
+
* field, as a field the server never hands back (a mailed code) is empty again. Otherwise at data-open-at-step, or
|
|
64
|
+
* shut at the step it shows.
|
|
65
|
+
*/
|
|
66
|
+
function openStepper(stepper) {
|
|
67
|
+
const opening = openingStepOf(stepper);
|
|
68
|
+
|
|
69
|
+
show(stepper, opening ?? currentStepOf(stepper));
|
|
70
|
+
|
|
71
|
+
if (opening !== null && stepper instanceof HTMLDialogElement && !stepper.open) {
|
|
72
|
+
stepper.showModal();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function openingStepOf(stepper) {
|
|
77
|
+
const fields = fieldsOf(stepper);
|
|
78
|
+
|
|
79
|
+
if (fields.some(isRefused)) {
|
|
80
|
+
return stepOf(fields.find((field) => isRefused(field) || !field.checkValidity()));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const asked = Number(stepper.dataset.openAtStep);
|
|
84
|
+
|
|
85
|
+
return sectionOf(stepper, asked) ? asked : null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** A field the server refused: aria-invalid="true" beside its message. */
|
|
89
|
+
function isRefused(field) {
|
|
90
|
+
return field.getAttribute('aria-invalid') === 'true';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function back(stepper) {
|
|
94
|
+
const step = currentStepOf(stepper);
|
|
95
|
+
|
|
96
|
+
if (step > FIRST_STEP) {
|
|
97
|
+
moveTo(stepper, step - 1);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Moves on once the step's fields pass, or submits from the last step. */
|
|
102
|
+
function advance(stepper) {
|
|
103
|
+
const step = currentStepOf(stepper);
|
|
104
|
+
|
|
105
|
+
if (isLast(stepper, step)) {
|
|
106
|
+
submit(stepper);
|
|
107
|
+
} else if (fieldsOf(sectionOf(stepper, step)).every((field) => field.reportValidity())) {
|
|
108
|
+
moveTo(stepper, step + 1);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// An empty field on another step would block the submit with nothing on screen to say why: show its step instead.
|
|
113
|
+
function submit(stepper) {
|
|
114
|
+
const invalid = fieldsOf(stepper).find((field) => !field.checkValidity());
|
|
115
|
+
|
|
116
|
+
if (invalid) {
|
|
117
|
+
show(stepper, stepOf(invalid));
|
|
118
|
+
invalid.reportValidity();
|
|
119
|
+
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
stepper.querySelector(STEP)?.closest('form')?.requestSubmit();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// To the new step's heading, so a screen reader starts where the step does.
|
|
127
|
+
function moveTo(stepper, step) {
|
|
128
|
+
show(stepper, step);
|
|
129
|
+
sectionOf(stepper, step)?.querySelector(HEADING)?.focus();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function show(stepper, step) {
|
|
133
|
+
for (const section of stepsOf(stepper)) {
|
|
134
|
+
section.hidden = stepOf(section) !== step;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
for (const mark of stepper.querySelectorAll('[data-step-mark]')) {
|
|
138
|
+
const state = markStateOf(Number(mark.dataset.stepMark), step);
|
|
139
|
+
|
|
140
|
+
if (state === null) {
|
|
141
|
+
mark.removeAttribute(MARK_STATE);
|
|
142
|
+
} else {
|
|
143
|
+
mark.setAttribute(MARK_STATE, state);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
for (const label of stepper.querySelectorAll('[data-step-label]')) {
|
|
148
|
+
label.hidden = Number(label.dataset.stepLabel) !== step;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
hideAll(stepper, BACK, step === FIRST_STEP);
|
|
152
|
+
hideAll(stepper, NEXT, isLast(stepper, step));
|
|
153
|
+
hideAll(stepper, SUBMIT, !isLast(stepper, step));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function markStateOf(mark, step) {
|
|
157
|
+
if (mark < step) {
|
|
158
|
+
return MarkState.done;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return mark === step ? MarkState.current : null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function hideAll(stepper, selector, hidden) {
|
|
165
|
+
for (const element of stepper.querySelectorAll(selector)) {
|
|
166
|
+
element.hidden = hidden;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function stepsOf(stepper) {
|
|
171
|
+
return [...stepper.querySelectorAll(STEP)];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function sectionOf(stepper, step) {
|
|
175
|
+
return stepsOf(stepper).find((section) => stepOf(section) === step);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** The step a section or a field belongs to. */
|
|
179
|
+
function stepOf(element) {
|
|
180
|
+
return Number(element.closest(STEP).dataset.step);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The step showing; the first when none does. */
|
|
184
|
+
function currentStepOf(stepper) {
|
|
185
|
+
const showing = stepsOf(stepper).find((section) => !section.hidden);
|
|
186
|
+
|
|
187
|
+
return showing ? stepOf(showing) : FIRST_STEP;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function isLast(stepper, step) {
|
|
191
|
+
return step >= stepsOf(stepper).length;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** The fields of a step, or of every step in a stepper. */
|
|
195
|
+
function fieldsOf(element) {
|
|
196
|
+
return [...element.querySelectorAll(FIELDS)].filter((field) => field.closest(STEP));
|
|
197
|
+
}
|