@entur/datepicker 9.2.0 → 9.3.1

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.
@@ -1,9 +1,10 @@
1
1
  import React from 'react';
2
2
  import { CalendarDate, DateValue } from '@internationalized/date';
3
+ import { MappedDateValue } from '@react-types/datepicker';
3
4
  import './Calendar.scss';
4
- export type CalendarProps = {
5
- selectedDate: DateValue | null;
6
- onChange: (SelectedDate: DateValue | null) => void;
5
+ export type CalendarProps<DateType extends DateValue> = {
6
+ selectedDate: DateType | null;
7
+ onChange: (SelectedDate: MappedDateValue<DateType> | null) => void | React.Dispatch<React.SetStateAction<DateType | null>>;
7
8
  navigationDescription?: string;
8
9
  style?: React.CSSProperties;
9
10
  /** Ekstra klassenavn */
@@ -23,6 +24,13 @@ export type CalendarProps = {
23
24
  * Gyldig til og med den tiden som legges inn som maxDate.
24
25
  * Dato uten tid vil være gyldig hele maxDate-dagen */
25
26
  maxDate?: DateValue;
27
+ /** Slå på visning av ukenummere i kalenderen. Overskriften for ukenummer-kolonnen
28
+ * kan endres med prop-en 'weekNumberHeader'
29
+ * @default false */
30
+ showWeekNumbers?: boolean;
31
+ /** Overskrift som vises for ukenummer-kolonnen. Vises kun hvis 'showWeekNumbers' er true.
32
+ * @default 'uke' */
33
+ weekNumberHeader?: string;
26
34
  /** Brukes for å legge til klassenavn på spesifikke datoer i kalenderen.
27
35
  * Tar inn en dato og skal returnere klassenavnet som skal legges til den datoen.
28
36
  * @default undefined
@@ -38,6 +46,7 @@ export type CalendarProps = {
38
46
  * @example (date) => isWeekend(date, 'no-NO') ? 'helgedag' : ''
39
47
  */
40
48
  ariaLabelForDate?: (date: CalendarDate) => string;
41
- [key: string]: any;
49
+ locale?: string;
50
+ calendarRef?: React.MutableRefObject<HTMLDivElement | null>;
42
51
  };
43
- export declare const Calendar: React.ForwardRefExoticComponent<Omit<CalendarProps, "ref"> & React.RefAttributes<HTMLDivElement>>;
52
+ export declare const Calendar: <DateType extends DateValue>({ locale: localOverride, ...rest }: CalendarProps<DateType>) => React.JSX.Element;
@@ -4,9 +4,10 @@ import { CalendarDate } from '@internationalized/date';
4
4
  type CalendarCellProps = {
5
5
  state: CalendarState;
6
6
  date: CalendarDate;
7
+ weekNumberString: string;
7
8
  onSelectedCellClick?: () => void;
8
9
  classNameForDate?: (date: CalendarDate) => string;
9
10
  ariaLabelForDate?: (date: CalendarDate) => string;
10
11
  };
11
- export declare const CalendarCell: ({ state, date, onSelectedCellClick, classNameForDate, ariaLabelForDate, ...rest }: CalendarCellProps) => React.JSX.Element;
12
+ export declare const CalendarCell: ({ state, date, onSelectedCellClick, weekNumberString, classNameForDate, ariaLabelForDate, ...rest }: CalendarCellProps) => React.JSX.Element;
12
13
  export {};
@@ -4,9 +4,11 @@ import { CalendarDate } from '@internationalized/date';
4
4
  type CalendarGridProps = {
5
5
  state: CalendarState;
6
6
  navigationDescription?: string;
7
+ showWeekNumbers: boolean;
8
+ weekNumberHeader: string;
7
9
  onSelectedCellClick?: () => void;
8
10
  classNameForDate?: (date: CalendarDate) => string;
9
11
  ariaLabelForDate?: (date: CalendarDate) => string;
10
12
  };
11
- export declare const CalendarGrid: ({ state, navigationDescription, onSelectedCellClick, classNameForDate, ariaLabelForDate, ...rest }: CalendarGridProps) => React.JSX.Element;
13
+ export declare const CalendarGrid: ({ state, navigationDescription, onSelectedCellClick, showWeekNumbers, weekNumberHeader, classNameForDate, ariaLabelForDate, ...rest }: CalendarGridProps) => React.JSX.Element;
12
14
  export {};
@@ -52,6 +52,13 @@ export type DatePickerProps<DateType extends DateValue> = {
52
52
  */
53
53
  validationVariant?: VariantType;
54
54
  disabled?: boolean;
55
+ /** Slå på visning av ukenummere i kalenderen. Overskriften for ukenummer-kolonnen
56
+ * kan endres med prop-en 'weekNumberHeader'
57
+ * @default false */
58
+ showWeekNumbers?: boolean;
59
+ /** Overskrift som vises for ukenummer-kolonnen. Vises kun hvis 'showWeekNumbers' er true.
60
+ * @default '#' */
61
+ weekNumberHeader?: string;
55
62
  /** Hvis true vil kalenderen alltid vises i en popover når den åpnes
56
63
  * @default false
57
64
  */
@@ -94,4 +101,4 @@ export type DatePickerProps<DateType extends DateValue> = {
94
101
  className?: string;
95
102
  style?: React.CSSProperties;
96
103
  } & Omit<AriaDatePickerProps<DateType>, 'value' | 'onChange' | 'label' | 'hideTimeZone' | 'placeholder' | 'minValue' | 'maxValue'>;
97
- export declare const DatePicker: <DateType extends DateValue>({ selectedDate, onChange, locale, disabled, showTime, showTimeZone, classNameForDate, className, style, variant, feedback, validationVariant, validationFeedback, disableModal, labelTooltip, navigationDescription, minDate, maxDate, modalTreshold, forcedReturnType, ariaLabelForDate, ...rest }: DatePickerProps<DateType>) => React.JSX.Element;
104
+ export declare const DatePicker: <DateType extends DateValue>({ selectedDate, onChange, locale, disabled, showTime, showTimeZone, classNameForDate, className, style, variant, feedback, validationVariant, validationFeedback, showWeekNumbers, weekNumberHeader, disableModal, labelTooltip, navigationDescription, minDate, maxDate, modalTreshold, forcedReturnType, ariaLabelForDate, ...rest }: DatePickerProps<DateType>) => React.JSX.Element;
@@ -197,6 +197,21 @@ var focusSegment = function focusSegment(ref, segment) {
197
197
  }
198
198
  }
199
199
  };
200
+ /** Based on code from https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php */
201
+ function getWeekNumberForDate(date$1) {
202
+ if (date$1 === null) return -1;
203
+ var calendarDate = convertValueToType({
204
+ value: date$1,
205
+ type: 'CalendarDate'
206
+ });
207
+ var firstDayOfWeek = date.startOfWeek(calendarDate, 'no-NO');
208
+ var thursdayOfWeek = firstDayOfWeek.add({
209
+ days: 3
210
+ });
211
+ var firstDayOfYearForThursday = date.startOfYear(thursdayOfWeek);
212
+ var weekNumber = Math.ceil((thursdayOfWeek.compare(firstDayOfYearForThursday) + 1) / 7);
213
+ return weekNumber;
214
+ }
200
215
 
201
216
  var _excluded$a = ["selectedDate", "onChange", "label", "locale", "showTimeZone", "showTime", "granularity", "disabled", "isDisabled", "variant", "feedback", "validationVariant", "validationFeedback", "labelTooltip", "minDate", "maxDate", "style", "className", "labelProps", "append"];
202
217
  var DateField = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
@@ -293,7 +308,7 @@ var CalendarButton = function CalendarButton(_ref) {
293
308
  }), children);
294
309
  };
295
310
 
296
- var _excluded$8 = ["state", "date", "onSelectedCellClick", "classNameForDate", "ariaLabelForDate"];
311
+ var _excluded$8 = ["state", "date", "onSelectedCellClick", "weekNumberString", "classNameForDate", "ariaLabelForDate"];
297
312
  var CalendarCell = function CalendarCell(_ref) {
298
313
  var _ariaLabelForDate, _classNameForDate, _state$timeZone, _classNames;
299
314
  var state = _ref.state,
@@ -302,6 +317,7 @@ var CalendarCell = function CalendarCell(_ref) {
302
317
  onSelectedCellClick = _ref$onSelectedCellCl === void 0 ? function () {
303
318
  return;
304
319
  } : _ref$onSelectedCellCl,
320
+ weekNumberString = _ref.weekNumberString,
305
321
  classNameForDate = _ref.classNameForDate,
306
322
  ariaLabelForDate = _ref.ariaLabelForDate,
307
323
  rest = _objectWithoutPropertiesLoose(_ref, _excluded$8);
@@ -316,11 +332,12 @@ var CalendarCell = function CalendarCell(_ref) {
316
332
  isDisabled = _useCalendarCell.isDisabled,
317
333
  isUnavailable = _useCalendarCell.isUnavailable,
318
334
  formattedDate = _useCalendarCell.formattedDate;
319
- var ariaLabel = buttonProps['aria-label'] + " " + ((_ariaLabelForDate = ariaLabelForDate == null ? void 0 : ariaLabelForDate(date$1)) != null ? _ariaLabelForDate : '');
335
+ var ariaLabel = "" + buttonProps['aria-label'] + weekNumberString + " " + ((_ariaLabelForDate = ariaLabelForDate == null ? void 0 : ariaLabelForDate(date$1)) != null ? _ariaLabelForDate : '');
320
336
  return React.createElement("td", _extends({}, cellProps, {
321
337
  className: "eds-datepicker__calendar__grid__cell__td"
322
338
  }), React.createElement("div", _extends({}, buttonProps, {
323
339
  "aria-label": ariaLabel,
340
+ "aria-hidden": isOutsideVisibleRange,
324
341
  ref: cellRef,
325
342
  hidden: isOutsideVisibleRange,
326
343
  className: classNames('eds-datepicker__calendar__grid__cell', (_classNames = {}, _classNames[(_classNameForDate = classNameForDate == null ? void 0 : classNameForDate(date$1)) != null ? _classNameForDate : ''] = !isOutsideVisibleRange, _classNames['eds-datepicker__calendar__grid__cell--selected'] = isSelected, _classNames['eds-datepicker__calendar__grid__cell--disabled'] = isDisabled || isUnavailable, _classNames['eds-datepicker__calendar__grid__cell--outside-month'] = isOutsideVisibleRange, _classNames['eds-datepicker__calendar__grid__cell--today'] = date.isEqualDay(date$1, date.now((_state$timeZone = state.timeZone) != null ? _state$timeZone : date.getLocalTimeZone())), _classNames))
@@ -336,7 +353,7 @@ var CalendarCell = function CalendarCell(_ref) {
336
353
  }), formattedDate));
337
354
  };
338
355
 
339
- var _excluded$7 = ["state", "navigationDescription", "onSelectedCellClick", "classNameForDate", "ariaLabelForDate"];
356
+ var _excluded$7 = ["state", "navigationDescription", "onSelectedCellClick", "showWeekNumbers", "weekNumberHeader", "classNameForDate", "ariaLabelForDate"];
340
357
  var CalendarGrid = function CalendarGrid(_ref) {
341
358
  var state = _ref.state,
342
359
  navigationDescription = _ref.navigationDescription,
@@ -344,6 +361,8 @@ var CalendarGrid = function CalendarGrid(_ref) {
344
361
  onSelectedCellClick = _ref$onSelectedCellCl === void 0 ? function () {
345
362
  return;
346
363
  } : _ref$onSelectedCellCl,
364
+ showWeekNumbers = _ref.showWeekNumbers,
365
+ weekNumberHeader = _ref.weekNumberHeader,
347
366
  classNameForDate = _ref.classNameForDate,
348
367
  ariaLabelForDate = _ref.ariaLabelForDate,
349
368
  rest = _objectWithoutPropertiesLoose(_ref, _excluded$7);
@@ -374,19 +393,26 @@ var CalendarGrid = function CalendarGrid(_ref) {
374
393
  return React.createElement(React.Fragment, null, React.createElement("table", _extends({}, gridProps, {
375
394
  cellSpacing: "0",
376
395
  className: "eds-datepicker__calendar__grid"
377
- }), React.createElement("thead", _extends({}, headerProps), React.createElement("tr", null, weekDaysMapped().map(function (day, index) {
396
+ }), React.createElement("thead", _extends({}, headerProps), React.createElement("tr", null, showWeekNumbers && React.createElement("th", {
397
+ className: "eds-datepicker__calendar__grid__weeknumber-header"
398
+ }, weekNumberHeader), weekDaysMapped().map(function (day, index) {
378
399
  return React.createElement("th", {
379
400
  key: index
380
401
  }, day);
381
402
  }))), React.createElement("tbody", null, weeksArray.map(function (weekIndex) {
403
+ var weekNumber = getWeekNumberForDate(state.getDatesInWeek(weekIndex)[0]);
382
404
  return React.createElement("tr", {
383
405
  key: weekIndex
384
- }, state.getDatesInWeek(weekIndex).map(function (date, i) {
406
+ }, showWeekNumbers && React.createElement("th", {
407
+ "aria-hidden": true,
408
+ className: "eds-datepicker__calendar__grid__weeknumber"
409
+ }, weekNumber), state.getDatesInWeek(weekIndex).map(function (date, i) {
385
410
  return date ? React.createElement(CalendarCell, {
386
411
  key: i,
387
412
  state: state,
388
413
  date: date,
389
414
  "aria-describedby": calendarGridId + 'description',
415
+ weekNumberString: showWeekNumbers ? ", " + weekNumberHeader + " " + weekNumber + "," : '',
390
416
  onSelectedCellClick: onSelectedCellClick,
391
417
  classNameForDate: classNameForDate,
392
418
  ariaLabelForDate: ariaLabelForDate
@@ -399,29 +425,43 @@ var CalendarGrid = function CalendarGrid(_ref) {
399
425
  }, getNavigationDescription()));
400
426
  };
401
427
 
402
- var _excluded$6 = ["selectedDate", "onChange", "locale", "minDate", "maxDate", "style", "className", "children", "navigationDescription", "onSelectedCellClick", "classNameForDate", "ariaLabelForDate"];
403
- var Calendar = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
404
- var selectedDate = _ref.selectedDate,
405
- onChange = _ref.onChange,
406
- customLocale = _ref.locale,
407
- minDate = _ref.minDate,
408
- maxDate = _ref.maxDate,
409
- style = _ref.style,
410
- className = _ref.className,
411
- navigationDescription = _ref.navigationDescription,
412
- _ref$onSelectedCellCl = _ref.onSelectedCellClick,
413
- onSelectedCellClick = _ref$onSelectedCellCl === void 0 ? function () {
414
- return;
415
- } : _ref$onSelectedCellCl,
416
- classNameForDate = _ref.classNameForDate,
417
- ariaLabelForDate = _ref.ariaLabelForDate,
428
+ var _excluded$6 = ["locale"],
429
+ _excluded2$3 = ["selectedDate", "onChange", "minDate", "maxDate", "showWeekNumbers", "weekNumberHeader", "style", "className", "navigationDescription", "onSelectedCellClick", "classNameForDate", "ariaLabelForDate", "calendarRef"];
430
+ var Calendar = function Calendar(_ref) {
431
+ var localOverride = _ref.locale,
418
432
  rest = _objectWithoutPropertiesLoose(_ref, _excluded$6);
419
433
  var _useLocale = i18n.useLocale(),
420
434
  locale = _useLocale.locale;
435
+ return React.createElement(i18n.I18nProvider, {
436
+ locale: localOverride != null ? localOverride : locale
437
+ }, React.createElement(CalendarBase, _extends({}, rest)));
438
+ };
439
+ var CalendarBase = function CalendarBase(_ref2) {
440
+ var selectedDate = _ref2.selectedDate,
441
+ onChange = _ref2.onChange,
442
+ minDate = _ref2.minDate,
443
+ maxDate = _ref2.maxDate,
444
+ _ref2$showWeekNumbers = _ref2.showWeekNumbers,
445
+ showWeekNumbers = _ref2$showWeekNumbers === void 0 ? false : _ref2$showWeekNumbers,
446
+ _ref2$weekNumberHeade = _ref2.weekNumberHeader,
447
+ weekNumberHeader = _ref2$weekNumberHeade === void 0 ? 'uke' : _ref2$weekNumberHeade,
448
+ style = _ref2.style,
449
+ className = _ref2.className,
450
+ navigationDescription = _ref2.navigationDescription,
451
+ _ref2$onSelectedCellC = _ref2.onSelectedCellClick,
452
+ onSelectedCellClick = _ref2$onSelectedCellC === void 0 ? function () {
453
+ return;
454
+ } : _ref2$onSelectedCellC,
455
+ classNameForDate = _ref2.classNameForDate,
456
+ ariaLabelForDate = _ref2.ariaLabelForDate,
457
+ calendarRef = _ref2.calendarRef,
458
+ rest = _objectWithoutPropertiesLoose(_ref2, _excluded2$3);
459
+ var _useLocale2 = i18n.useLocale(),
460
+ locale = _useLocale2.locale;
421
461
  var allProps = _extends({}, rest, {
422
462
  value: selectedDate,
423
463
  onChange: onChange,
424
- locale: customLocale != null ? customLocale : locale,
464
+ locale: locale,
425
465
  createCalendar: createCalendar,
426
466
  minValue: minDate,
427
467
  maxValue: maxDate
@@ -432,15 +472,8 @@ var Calendar = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
432
472
  prevButtonProps = _useCalendar.prevButtonProps,
433
473
  nextButtonProps = _useCalendar.nextButtonProps,
434
474
  title = _useCalendar.title;
435
- return React.createElement(utils.ConditionalWrapper, {
436
- condition: customLocale,
437
- wrapper: function wrapper(child) {
438
- return React.createElement(i18n.I18nProvider, {
439
- locale: customLocale
440
- }, child);
441
- }
442
- }, React.createElement("div", _extends({}, calendarProps, {
443
- ref: ref,
475
+ return React.createElement("div", _extends({}, calendarProps, {
476
+ ref: calendarRef,
444
477
  className: classNames('eds-datepicker__calendar', className),
445
478
  style: style
446
479
  }), React.createElement("div", {
@@ -458,11 +491,13 @@ var Calendar = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
458
491
  navigationDescription: navigationDescription,
459
492
  onSelectedCellClick: onSelectedCellClick,
460
493
  classNameForDate: classNameForDate,
461
- ariaLabelForDate: ariaLabelForDate
462
- })));
463
- });
494
+ ariaLabelForDate: ariaLabelForDate,
495
+ showWeekNumbers: showWeekNumbers,
496
+ weekNumberHeader: weekNumberHeader
497
+ }));
498
+ };
464
499
 
465
- var _excluded$5 = ["selectedDate", "onChange", "locale", "disabled", "showTime", "showTimeZone", "classNameForDate", "className", "style", "variant", "feedback", "validationVariant", "validationFeedback", "disableModal", "labelTooltip", "navigationDescription", "minDate", "maxDate", "modalTreshold", "forcedReturnType", "ariaLabelForDate"];
500
+ var _excluded$5 = ["selectedDate", "onChange", "locale", "disabled", "showTime", "showTimeZone", "classNameForDate", "className", "style", "variant", "feedback", "validationVariant", "validationFeedback", "showWeekNumbers", "weekNumberHeader", "disableModal", "labelTooltip", "navigationDescription", "minDate", "maxDate", "modalTreshold", "forcedReturnType", "ariaLabelForDate"];
466
501
  var DatePicker = function DatePicker(_ref) {
467
502
  var selectedDate = _ref.selectedDate,
468
503
  onChange = _ref.onChange,
@@ -477,6 +512,8 @@ var DatePicker = function DatePicker(_ref) {
477
512
  feedback = _ref.feedback,
478
513
  validationVariant = _ref.validationVariant,
479
514
  validationFeedback = _ref.validationFeedback,
515
+ showWeekNumbers = _ref.showWeekNumbers,
516
+ weekNumberHeader = _ref.weekNumberHeader,
480
517
  _ref$disableModal = _ref.disableModal,
481
518
  disableModal = _ref$disableModal === void 0 ? false : _ref$disableModal,
482
519
  labelTooltip = _ref.labelTooltip,
@@ -549,9 +586,11 @@ var DatePicker = function DatePicker(_ref) {
549
586
  onChange: handleOnChange,
550
587
  minDate: minDate,
551
588
  maxDate: maxDate,
552
- ref: calendarRef,
589
+ calendarRef: calendarRef,
553
590
  classNameForDate: classNameForDate,
554
- ariaLabelForDate: ariaLabelForDate
591
+ ariaLabelForDate: ariaLabelForDate,
592
+ showWeekNumbers: showWeekNumbers,
593
+ weekNumberHeader: weekNumberHeader
555
594
  });
556
595
  var useModal = typeof width !== 'undefined' && width <= CALENDAR_MODAL_MAX_SCREEN_WIDTH && !disableModal;
557
596
  var popoverCalendar = React.createElement("div", {
@@ -1135,6 +1174,7 @@ exports.ariaLabelIfNorwegian = ariaLabelIfNorwegian;
1135
1174
  exports.convertValueToType = convertValueToType;
1136
1175
  exports.createCalendar = createCalendar;
1137
1176
  exports.focusSegment = focusSegment;
1177
+ exports.getWeekNumberForDate = getWeekNumberForDate;
1138
1178
  exports.lastMillisecondOfDay = lastMillisecondOfDay;
1139
1179
  exports.modulo = modulo;
1140
1180
  exports.nativeDateToDateValue = nativeDateToDateValue;