@spartan-ng/brain 1.10.0-beta.1 → 1.10.0-beta.2

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 CHANGED
@@ -89,7 +89,6 @@ spartan/ui requires Tailwind CSS v4.
89
89
 
90
90
  - Angular `>=21.0.0 <23.0.0` (core, common, forms, cdk)
91
91
  - `rxjs` `>=6.6.0`
92
- - `clsx` `>=2.0.0`
93
92
  - `tailwindcss` `>=4.0.0`
94
93
  - `tw-animate-css` `>=1.0.0` _(imported by the Tailwind preset)_
95
94
  - `luxon` `>=3.0.0` _(optional - only required for `@spartan-ng/brain/date-time-luxon`)_
@@ -119,6 +119,32 @@ const compareDays = (a, b, dateAdapter) => {
119
119
  return a.every((day, i) => dateAdapter.isSameDay(day, b[i]));
120
120
  };
121
121
 
122
+ /** Find the first enabled day from the target, moving in the requested direction. */
123
+ function findEnabledDate(start, direction, adapter, isDisabled, bounds) {
124
+ let date = start;
125
+ // Allow multi-year disabled ranges, but cap searches with no far boundary.
126
+ // Explicit calendar boundaries stop the search as soon as they are crossed.
127
+ for (let step = 0; step < 3660; step++) {
128
+ const beforeMin = bounds?.min !== undefined && adapter.isBefore(date, adapter.startOfDay(bounds.min));
129
+ const afterMax = bounds?.max !== undefined && adapter.isAfter(date, adapter.endOfDay(bounds.max));
130
+ // Only stop at a bound when moving farther out, not toward the allowed range.
131
+ if ((beforeMin && direction === -1) || (afterMax && direction === 1))
132
+ return undefined;
133
+ if (!beforeMin && !afterMax && !isDisabled(date))
134
+ return date;
135
+ date = direction === 1 ? adapter.add(date, { days: 1 }) : adapter.subtract(date, { days: 1 });
136
+ }
137
+ return undefined;
138
+ }
139
+ /** Find a usable initial tab stop if the requested focused day is disabled. */
140
+ function findInitialFocusedDate(date, adapter, isDisabled, bounds) {
141
+ if (!isDisabled(date))
142
+ return date;
143
+ return (findEnabledDate(date, 1, adapter, isDisabled, bounds) ??
144
+ findEnabledDate(date, -1, adapter, isDisabled, bounds) ??
145
+ date);
146
+ }
147
+
122
148
  class BrnCalendar {
123
149
  _i18n = injectBrnCalendarI18n();
124
150
  /** Access the date adapter */
@@ -150,7 +176,32 @@ class BrnCalendar {
150
176
  /**
151
177
  * The focused date.
152
178
  */
153
- focusedDate = linkedSignal(() => this.constrainDate(this.defaultFocusedDate() ?? this.date() ?? this._dateAdapter.now()), ...(ngDevMode ? [{ debugName: "focusedDate" }] : /* istanbul ignore next */ []));
179
+ focusedDate = linkedSignal({ ...(ngDevMode ? { debugName: "focusedDate" } : /* istanbul ignore next */ {}), source: () => ({
180
+ initialDate: this.constrainDate(this.defaultFocusedDate() ?? this.date() ?? this._dateAdapter.now()),
181
+ startingDate: this.defaultFocusedDate(),
182
+ selectedDate: this.date(),
183
+ min: this.min(),
184
+ max: this.max(),
185
+ disabled: this.disabled(),
186
+ dateDisabled: this.dateDisabled(),
187
+ }),
188
+ computation: (source, previous) => {
189
+ if (source.disabled)
190
+ return source.initialDate;
191
+ // Availability changes must not reset keyboard focus to the default date.
192
+ // A new initial date or changed limits should still reset it.
193
+ const preserveFocus = previous !== undefined &&
194
+ previous.source.startingDate === source.startingDate &&
195
+ previous.source.selectedDate === source.selectedDate &&
196
+ previous.source.min === source.min &&
197
+ previous.source.max === source.max &&
198
+ previous.source.disabled === source.disabled;
199
+ const target = preserveFocus ? previous.value : source.initialDate;
200
+ return findInitialFocusedDate(target, this._dateAdapter, (day) => this.isDateDisabled(day), {
201
+ min: source.min,
202
+ max: source.max,
203
+ });
204
+ } });
154
205
  /** Emits whenever the focused date changes. */
155
206
  focusedDateChange = output();
156
207
  _setFocusedDate(date) {
@@ -343,7 +394,7 @@ class BrnCalendarCellButton {
343
394
  this._destroyRef.onDestroy(() => this._calendar.unregisterCalendarCell(this));
344
395
  }
345
396
  /** Whether this date is focusable */
346
- focusable = computed(() => this._dateAdapter.isSameDay(this._calendar.focusedDate(), this.date()), ...(ngDevMode ? [{ debugName: "focusable" }] : /* istanbul ignore next */ []));
397
+ focusable = computed(() => !this.disabled() && this._dateAdapter.isSameDay(this._calendar.focusedDate(), this.date()), ...(ngDevMode ? [{ debugName: "focusable" }] : /* istanbul ignore next */ []));
347
398
  outside = computed(() => {
348
399
  const focusedDate = this._calendar.focusedDate();
349
400
  return !this._dateAdapter.isSameMonth(this.date(), focusedDate);
@@ -362,7 +413,7 @@ class BrnCalendarCellButton {
362
413
  const targetDate = this._dateAdapter.add(this._calendar.focusedDate(), {
363
414
  days: this.getDirection() === 'rtl' ? 1 : -1,
364
415
  });
365
- this._calendar.setFocusedDate(targetDate);
416
+ this.focusEnabledDate(targetDate, this.getDirection() === 'rtl' ? 1 : -1);
366
417
  }
367
418
  /**
368
419
  * Focus the next cell.
@@ -373,7 +424,7 @@ class BrnCalendarCellButton {
373
424
  const targetDate = this._dateAdapter.add(this._calendar.focusedDate(), {
374
425
  days: this.getDirection() === 'rtl' ? -1 : 1,
375
426
  });
376
- this._calendar.setFocusedDate(targetDate);
427
+ this.focusEnabledDate(targetDate, this.getDirection() === 'rtl' ? -1 : 1);
377
428
  }
378
429
  /**
379
430
  * Focus the above cell.
@@ -381,7 +432,7 @@ class BrnCalendarCellButton {
381
432
  focusAbove(event) {
382
433
  event.preventDefault();
383
434
  event.stopPropagation();
384
- this._calendar.setFocusedDate(this._dateAdapter.subtract(this._calendar.focusedDate(), { days: 7 }));
435
+ this.focusEnabledDate(this._dateAdapter.subtract(this._calendar.focusedDate(), { days: 7 }), -1);
385
436
  }
386
437
  /**
387
438
  * Focus the below cell.
@@ -389,7 +440,7 @@ class BrnCalendarCellButton {
389
440
  focusBelow(event) {
390
441
  event.preventDefault();
391
442
  event.stopPropagation();
392
- this._calendar.setFocusedDate(this._dateAdapter.add(this._calendar.focusedDate(), { days: 7 }));
443
+ this.focusEnabledDate(this._dateAdapter.add(this._calendar.focusedDate(), { days: 7 }), 1);
393
444
  }
394
445
  /**
395
446
  * Focus the first date of the month.
@@ -397,7 +448,7 @@ class BrnCalendarCellButton {
397
448
  focusFirst(event) {
398
449
  event.preventDefault();
399
450
  event.stopPropagation();
400
- this._calendar.setFocusedDate(this._dateAdapter.startOfMonth(this._calendar.focusedDate()));
451
+ this.focusEnabledDate(this._dateAdapter.startOfMonth(this._calendar.focusedDate()), 1, this._dateAdapter.endOfMonth(this._calendar.focusedDate()));
401
452
  }
402
453
  /**
403
454
  * Focus the last date of the month.
@@ -405,7 +456,7 @@ class BrnCalendarCellButton {
405
456
  focusLast(event) {
406
457
  event.preventDefault();
407
458
  event.stopPropagation();
408
- this._calendar.setFocusedDate(this._dateAdapter.endOfMonth(this._calendar.focusedDate()));
459
+ this.focusEnabledDate(this._dateAdapter.endOfMonth(this._calendar.focusedDate()), -1, this._dateAdapter.startOfMonth(this._calendar.focusedDate()));
409
460
  }
410
461
  /**
411
462
  * Focus the same date in the previous month.
@@ -419,10 +470,10 @@ class BrnCalendarCellButton {
419
470
  const lastDay = this._dateAdapter.endOfMonth(previousMonthTarget);
420
471
  // if we are on a date that does not exist in the previous month, we should focus the last day of the month.
421
472
  if (date > this._dateAdapter.getDate(lastDay)) {
422
- this._calendar.setFocusedDate(lastDay);
473
+ this.focusEnabledDate(lastDay, -1);
423
474
  }
424
475
  else {
425
- this._calendar.setFocusedDate(this._dateAdapter.set(previousMonthTarget, { day: date }));
476
+ this.focusEnabledDate(this._dateAdapter.set(previousMonthTarget, { day: date }), -1);
426
477
  }
427
478
  }
428
479
  /**
@@ -437,12 +488,28 @@ class BrnCalendarCellButton {
437
488
  const lastDay = this._dateAdapter.endOfMonth(nextMonthTarget);
438
489
  // if we are on a date that does not exist in the next month, we should focus the last day of the month.
439
490
  if (date > this._dateAdapter.getDate(lastDay)) {
440
- this._calendar.setFocusedDate(lastDay);
491
+ this.focusEnabledDate(lastDay, 1);
441
492
  }
442
493
  else {
443
- this._calendar.setFocusedDate(this._dateAdapter.set(nextMonthTarget, { day: date }));
494
+ this.focusEnabledDate(this._dateAdapter.set(nextMonthTarget, { day: date }), 1);
444
495
  }
445
496
  }
497
+ focusEnabledDate(target, direction, boundary) {
498
+ let min = this._calendar.min();
499
+ let max = this._calendar.max();
500
+ if (boundary !== undefined) {
501
+ if (direction === 1 && (max === undefined || this._dateAdapter.isBefore(boundary, max)))
502
+ max = boundary;
503
+ if (direction === -1 && (min === undefined || this._dateAdapter.isAfter(boundary, min)))
504
+ min = boundary;
505
+ }
506
+ const date = findEnabledDate(target, direction, this._dateAdapter, (day) => this._calendar.isDateDisabled(day), {
507
+ min,
508
+ max,
509
+ });
510
+ if (date !== undefined)
511
+ this._calendar.setFocusedDate(date);
512
+ }
446
513
  /**
447
514
  * Get the direction of the element.
448
515
  */
@@ -929,7 +996,32 @@ class BrnCalendarMulti {
929
996
  /**
930
997
  * The focused date.
931
998
  */
932
- focusedDate = linkedSignal(() => this.constrainDate(this.defaultFocusedDate() ?? this._dateAdapter.now()), ...(ngDevMode ? [{ debugName: "focusedDate" }] : /* istanbul ignore next */ []));
999
+ focusedDate = linkedSignal({ ...(ngDevMode ? { debugName: "focusedDate" } : /* istanbul ignore next */ {}), source: () => ({
1000
+ initialDate: this.constrainDate(this.defaultFocusedDate() ?? this._dateAdapter.now()),
1001
+ startingDate: this.defaultFocusedDate(),
1002
+ selectedDate: this.defaultFocusedDate(),
1003
+ min: this.min(),
1004
+ max: this.max(),
1005
+ disabled: this.disabled(),
1006
+ dateDisabled: this.dateDisabled(),
1007
+ }),
1008
+ computation: (source, previous) => {
1009
+ if (source.disabled)
1010
+ return source.initialDate;
1011
+ // Availability changes must not reset keyboard focus to the default date.
1012
+ // A new initial date or changed limits should still reset it.
1013
+ const preserveFocus = previous !== undefined &&
1014
+ previous.source.startingDate === source.startingDate &&
1015
+ previous.source.selectedDate === source.selectedDate &&
1016
+ previous.source.min === source.min &&
1017
+ previous.source.max === source.max &&
1018
+ previous.source.disabled === source.disabled;
1019
+ const target = preserveFocus ? previous.value : source.initialDate;
1020
+ return findInitialFocusedDate(target, this._dateAdapter, (day) => this.isDateDisabled(day), {
1021
+ min: source.min,
1022
+ max: source.max,
1023
+ });
1024
+ } });
933
1025
  /** Emits whenever the focused date changes. */
934
1026
  focusedDateChange = output();
935
1027
  _setFocusedDate(date) {
@@ -1101,7 +1193,32 @@ class BrnCalendarRange {
1101
1193
  /**
1102
1194
  * The focused date.
1103
1195
  */
1104
- focusedDate = linkedSignal(() => this.constrainDate(this.defaultFocusedDate() ?? this.startDate() ?? this._dateAdapter.now()), ...(ngDevMode ? [{ debugName: "focusedDate" }] : /* istanbul ignore next */ []));
1196
+ focusedDate = linkedSignal({ ...(ngDevMode ? { debugName: "focusedDate" } : /* istanbul ignore next */ {}), source: () => ({
1197
+ initialDate: this.constrainDate(this.defaultFocusedDate() ?? this.startDate() ?? this._dateAdapter.now()),
1198
+ startingDate: this.defaultFocusedDate(),
1199
+ selectedDate: this.startDate(),
1200
+ min: this.min(),
1201
+ max: this.max(),
1202
+ disabled: this.disabled(),
1203
+ dateDisabled: this.dateDisabled(),
1204
+ }),
1205
+ computation: (source, previous) => {
1206
+ if (source.disabled)
1207
+ return source.initialDate;
1208
+ // Availability changes must not reset keyboard focus to the default date.
1209
+ // A new initial date or changed limits should still reset it.
1210
+ const preserveFocus = previous !== undefined &&
1211
+ previous.source.startingDate === source.startingDate &&
1212
+ previous.source.selectedDate === source.selectedDate &&
1213
+ previous.source.min === source.min &&
1214
+ previous.source.max === source.max &&
1215
+ previous.source.disabled === source.disabled;
1216
+ const target = preserveFocus ? previous.value : source.initialDate;
1217
+ return findInitialFocusedDate(target, this._dateAdapter, (day) => this.isDateDisabled(day), {
1218
+ min: source.min,
1219
+ max: source.max,
1220
+ });
1221
+ } });
1105
1222
  /** Emits whenever the focused date changes. */
1106
1223
  focusedDateChange = output();
1107
1224
  _setFocusedDate(date) {