File size: 3,165 Bytes
cf86710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import type { DateLibOptions } from "../classes/DateLib.js";
import * as defaultLabels from "../labels/index.js";
import type { DayPickerProps, Labels } from "../types/index.js";

// Use a bivariant callable type to accept both the exact label signatures and
// compatible overrides. This matches how React types event handlers, allowing
// consumers to pass narrower or slightly different function shapes without
// losing type safety.
type BivariantLabel<Args extends unknown[]> = {
  bivarianceHack(...args: Args): string;
}["bivarianceHack"];

const resolveLabel = <Args extends unknown[], T extends BivariantLabel<Args>>(
  defaultLabel: T,
  customLabel: T | undefined,
  localeLabel: string | T | undefined,
): T => {
  if (customLabel) return customLabel;
  if (localeLabel) {
    return (
      typeof localeLabel === "function"
        ? localeLabel
        : (..._args: Args) => localeLabel
    ) as T;
  }
  return defaultLabel;
};

/**
 * Merges custom labels from the props with the default labels.
 *
 * When available, uses the locale-provided translation for `labelNext`.
 *
 * @param customLabels The custom labels provided in the DayPicker props.
 * @param options Options from the date library, used to resolve locale
 *   translations.
 * @returns The merged labels object with locale-aware defaults.
 */
export function getLabels(
  customLabels: DayPickerProps["labels"],
  options: DateLibOptions,
): Labels {
  const localeLabels = options.locale?.labels ?? {};

  return {
    ...defaultLabels,
    ...(customLabels ?? {}),
    labelDayButton: resolveLabel(
      defaultLabels.labelDayButton,
      customLabels?.labelDayButton,
      localeLabels.labelDayButton,
    ),
    labelMonthDropdown: resolveLabel(
      defaultLabels.labelMonthDropdown,
      customLabels?.labelMonthDropdown,
      localeLabels.labelMonthDropdown,
    ),
    labelNext: resolveLabel(
      defaultLabels.labelNext,
      customLabels?.labelNext,
      localeLabels.labelNext,
    ),
    labelPrevious: resolveLabel(
      defaultLabels.labelPrevious,
      customLabels?.labelPrevious,
      localeLabels.labelPrevious,
    ),
    labelWeekNumber: resolveLabel(
      defaultLabels.labelWeekNumber,
      customLabels?.labelWeekNumber,
      localeLabels.labelWeekNumber,
    ),
    labelYearDropdown: resolveLabel(
      defaultLabels.labelYearDropdown,
      customLabels?.labelYearDropdown,
      localeLabels.labelYearDropdown,
    ),
    labelGrid: resolveLabel(
      defaultLabels.labelGrid,
      customLabels?.labelGrid,
      localeLabels.labelGrid,
    ),
    labelGridcell: resolveLabel(
      defaultLabels.labelGridcell,
      customLabels?.labelGridcell,
      localeLabels.labelGridcell,
    ),
    labelNav: resolveLabel(
      defaultLabels.labelNav,
      customLabels?.labelNav,
      localeLabels.labelNav,
    ),
    labelWeekNumberHeader: resolveLabel(
      defaultLabels.labelWeekNumberHeader,
      customLabels?.labelWeekNumberHeader,
      localeLabels.labelWeekNumberHeader,
    ),
    labelWeekday: resolveLabel(
      defaultLabels.labelWeekday,
      customLabels?.labelWeekday,
      localeLabels.labelWeekday,
    ),
  };
}