@gooddata/sdk-ui-filters 11.61.0-alpha.4 → 11.61.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.
Files changed (29) hide show
  1. package/esm/DateFilter/WeekRangeList/WeekRangeList.d.ts +28 -0
  2. package/esm/DateFilter/WeekRangeList/WeekRangeList.d.ts.map +1 -0
  3. package/esm/DateFilter/WeekRangeList/WeekRangeList.js +263 -0
  4. package/esm/DateFilter/WeekRangeList/weeks.d.ts +99 -0
  5. package/esm/DateFilter/WeekRangeList/weeks.d.ts.map +1 -0
  6. package/esm/DateFilter/WeekRangeList/weeks.js +229 -0
  7. package/esm/internal.d.ts +1 -0
  8. package/esm/internal.d.ts.map +1 -1
  9. package/esm/internal.js +3 -0
  10. package/package.json +12 -12
  11. package/styles/css/attributeFilter.css +1 -1
  12. package/styles/css/attributeFilterNext/attributeFilter.css +1 -1
  13. package/styles/css/attributeFilterNext/attributeFilterAddons.css +1 -1
  14. package/styles/css/attributeFilterNext/attributeFilterDropdownBody.css +1 -1
  15. package/styles/css/attributeFilterNext/attributeFilterDropdownButton.css +1 -1
  16. package/styles/css/attributeFilterNext/attributeFilterElementsSelect.css +1 -1
  17. package/styles/css/attributeFilterNext/attributeFilterEmptyResult.css +1 -1
  18. package/styles/css/attributeFilterNext/attributeFilterStatusBar.css +1 -1
  19. package/styles/css/attributeFilterNext.css +1 -1
  20. package/styles/css/components/WeekRangeList.css +70 -0
  21. package/styles/css/components/WeekRangeList.css.map +1 -0
  22. package/styles/css/dateFilter.css +70 -1
  23. package/styles/css/dateFilter.css.map +1 -1
  24. package/styles/css/main.css +70 -1
  25. package/styles/css/main.css.map +1 -1
  26. package/styles/css/measureValueFilter.css +1 -1
  27. package/styles/css/rankingFilter.css +1 -1
  28. package/styles/scss/components/WeekRangeList.scss +95 -0
  29. package/styles/scss/dateFilter.scss +2 -1
@@ -0,0 +1,28 @@
1
+ import { type ReactNode } from "react";
2
+ import { type WeekStart } from "@gooddata/sdk-model";
3
+ import { type IPeriodRange } from "../PeriodRangePicker/types.js";
4
+ /**
5
+ * @internal
6
+ */
7
+ export interface IWeekRangeListProps {
8
+ /** The selected weeks. Every range reported by `onRangeChange` has to come back here. */
9
+ range: IPeriodRange;
10
+ onRangeChange: (range: IPeriodRange) => void;
11
+ /** Defaults to "Sunday". */
12
+ weekStart?: WeekStart;
13
+ /** Date format (date-fns tokens) of the jump field and the range preview. */
14
+ dateFormat?: string;
15
+ isMobile: boolean;
16
+ customRangeHint?: ReactNode;
17
+ /** Reports whether a week range is selected. Pass a stable callback. */
18
+ onValidityChange?: (isValid: boolean) => void;
19
+ withoutApply?: boolean;
20
+ submitForm?: () => void;
21
+ }
22
+ /**
23
+ * A list of weeks grouped by month, with a two-click range selection and a field that jumps to a typed date.
24
+ *
25
+ * @internal
26
+ */
27
+ export declare function WeekRangeList({ range, onRangeChange, weekStart, dateFormat, isMobile, customRangeHint, onValidityChange, withoutApply, submitForm, }: IWeekRangeListProps): import("react/jsx-runtime").JSX.Element;
28
+ //# sourceMappingURL=WeekRangeList.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WeekRangeList.d.ts","sourceRoot":"","sources":["../../../src/DateFilter/WeekRangeList/WeekRangeList.tsx"],"names":[],"mappings":"AAEA,OAAO,EAGH,KAAK,SAAS,EAQjB,MAAM,OAAO,CAAC;AAMf,OAAO,EAAmB,KAAK,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAStE,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAyClE;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAChC,yFAAyF;IACzF,KAAK,EAAE,YAAY,CAAC;IACpB,aAAa,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC;IAC7C,4BAA4B;IAC5B,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,6EAA6E;IAC7E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;IAClB,eAAe,CAAC,EAAE,SAAS,CAAC;IAC5B,wEAAwE;IACxE,gBAAgB,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IAC9C,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;CAC3B;AA0ED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,EAC1B,KAAK,EACL,aAAa,EACb,SAAoB,EACpB,UAAgC,EAChC,QAAQ,EACR,eAAe,EACf,gBAAgB,EAChB,YAAoB,EACpB,UAAU,GACb,EAAE,mBAAmB,2CA8TrB"}
@@ -0,0 +1,263 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // (C) 2026 GoodData Corporation
3
+ import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, } from "react";
4
+ import cx from "classnames";
5
+ import { format } from "date-fns";
6
+ import { defineMessages, useIntl } from "react-intl";
7
+ import { useAutoupdateRef, useDebouncedState } from "@gooddata/sdk-ui";
8
+ import { Input, useIdPrefixed } from "@gooddata/sdk-ui-kit";
9
+ import { DEFAULT_DATE_FORMAT, platformDateFnsFormat } from "../constants/Platform.js";
10
+ import { InputErrorMessage } from "../DateRangePicker/InputErrorMessage.js";
11
+ import { ListHeading } from "../ListHeading/ListHeading.js";
12
+ import { toDisplayDateFormat } from "../PeriodRangePicker/displayDateFormat.js";
13
+ import { PreviewAnnouncer } from "../PeriodRangePicker/PreviewAnnouncer.js";
14
+ import { formatAbsoluteDateRange } from "../utils/FormattingUtils.js";
15
+ import { FIRST_LISTED_DAY, LAST_LISTED_DAY, createOpeningWeekWindow, extendWeekWindow, formatMonthHeading, formatWeekAccessibleName, formatWeekLabel, getOpeningDay, getWeek, getWeekRows, jumpWeekWindow, periodRangeToWeekSelection, readJumpText, selectWeek, trimWeekWindow, weekSelectionToPeriodRange, } from "./weeks.js";
16
+ const JUMP_DELAY = 300;
17
+ const PAGE_WEEKS = 4;
18
+ const PLACEHOLDER_EXAMPLE_DATE = new Date(2026, 11, 29);
19
+ // The two clicks that close a range follow each other quickly, so only the range they leave is announced.
20
+ const PREVIEW_ANNOUNCEMENT_DELAY = 1000;
21
+ const messages = defineMessages({
22
+ searchPlaceholder: { id: "filters.staticPeriod.weekList.searchPlaceholder" },
23
+ searchLabel: { id: "filters.staticPeriod.weekList.searchLabel" },
24
+ listLabel: { id: "filters.staticPeriod.weekList.listLabel" },
25
+ invalidDate: { id: "filters.staticPeriod.weekList.errors.invalidDate" },
26
+ dateOutOfRange: { id: "filters.staticPeriod.weekList.errors.dateOutOfRange" },
27
+ rangePreview: { id: "filters.staticPeriod.rangePreview" },
28
+ });
29
+ const getToday = () => format(new Date(), platformDateFnsFormat);
30
+ const isInRange = (week, range) => range !== undefined && week.start >= range.from && week.end <= range.to;
31
+ // Keeps the focus in the jump field when the list is clicked.
32
+ const preventFocusChange = (event) => event.preventDefault();
33
+ const WeekOption = memo(function WeekOption({ id, start, end, label, accessibleName, isActive, isSelected, isInShownRange, isShownRangeEdge, onSelect, onHover, }) {
34
+ return (_jsx("div", { id: id, role: "option", "aria-selected": isSelected, "aria-label": accessibleName, className: cx("gd-week-range-list__week", "s-week-range-list-week", {
35
+ "gd-week-range-list__week--active": isActive,
36
+ "gd-week-range-list__week--in-range": isInShownRange,
37
+ "gd-week-range-list__week--edge": isShownRangeEdge,
38
+ }), onMouseMove: isActive ? undefined : () => onHover(start), onClick: () => onSelect({ start, end }), children: label }));
39
+ });
40
+ const MonthHeading = memo(function MonthHeading({ label }) {
41
+ return (_jsx(ListHeading, { className: "gd-week-range-list__heading s-week-range-list-heading", children: label }));
42
+ });
43
+ /**
44
+ * A list of weeks grouped by month, with a two-click range selection and a field that jumps to a typed date.
45
+ *
46
+ * @internal
47
+ */
48
+ export function WeekRangeList({ range, onRangeChange, weekStart = "Sunday", dateFormat = DEFAULT_DATE_FORMAT, isMobile, customRangeHint, onValidityChange, withoutApply = false, submitForm, }) {
49
+ const intl = useIntl();
50
+ const listboxId = useIdPrefixed("gd-week-range-list");
51
+ const errorId = useIdPrefixed("gd-week-range-list-error");
52
+ const listRef = useRef(null);
53
+ const getOptionId = useCallback((key) => `${listboxId}-${key}`, [listboxId]);
54
+ const [weekWindow, setWeekWindow] = useState(() => createOpeningWeekWindow(range, getToday()));
55
+ const [anchor, setAnchor] = useState(undefined);
56
+ // Days rather than week starts, so that they still name a listed week after the week start changes.
57
+ const [activeDay, setActiveDay] = useState(undefined);
58
+ // A new object on every request, so that centring the same week again scrolls too.
59
+ const [centreRequest, setCentreRequest] = useState(() => ({ day: getOpeningDay(range, getToday()) }));
60
+ const [text, setText, debouncedText] = useDebouncedState("", JUMP_DELAY);
61
+ // The error waits until the field is left or Enter is pressed, so that it does not flash while typing.
62
+ const [isJumpErrorShown, setIsJumpErrorShown] = useState(false);
63
+ const jumpedTextRef = useRef("");
64
+ const closedSelection = periodRangeToWeekSelection(range, weekStart);
65
+ // The anchor holds only while the range is still the week its click reported.
66
+ const selection = closedSelection && closedSelection.from === anchor?.start && closedSelection.to === anchor.end
67
+ ? { ...closedSelection, anchor }
68
+ : closedSelection;
69
+ const rows = useMemo(() => getWeekRows(weekWindow, weekStart).map((row) => row.type === "week"
70
+ ? {
71
+ ...row,
72
+ label: formatWeekLabel(row, intl.locale),
73
+ accessibleName: formatWeekAccessibleName(row, intl.locale),
74
+ }
75
+ : { type: "heading", key: row.key, label: formatMonthHeading(row.month, intl.locale) }), [weekWindow, weekStart, intl.locale]);
76
+ const weeks = useMemo(() => rows.filter((row) => row.type === "week"), [rows]);
77
+ const weekIndexes = useMemo(() => new Map(weeks.map((week, index) => [week.key, index])), [weeks]);
78
+ const activeKey = activeDay === undefined ? undefined : getWeek(activeDay, weekStart).start;
79
+ const activeIndex = activeKey === undefined ? undefined : weekIndexes.get(activeKey);
80
+ const activeWeek = activeIndex === undefined ? undefined : weeks[activeIndex];
81
+ const centreKey = getWeek(centreRequest.day, weekStart).start;
82
+ const selectionRef = useAutoupdateRef(selection);
83
+ const onRangeChangeRef = useAutoupdateRef(onRangeChange);
84
+ const handleSelect = useCallback((week) => {
85
+ const next = selectWeek(selectionRef.current, week);
86
+ setAnchor(next.anchor);
87
+ onRangeChangeRef.current(weekSelectionToPeriodRange(next));
88
+ }, [selectionRef, onRangeChangeRef]);
89
+ const isRangeSelected = selection !== undefined;
90
+ useEffect(() => {
91
+ onValidityChange?.(isRangeSelected);
92
+ }, [isRangeSelected, onValidityChange]);
93
+ // Whatever the caller gates on validity must not stay blocked once the list is gone.
94
+ useEffect(() => {
95
+ return () => onValidityChange?.(true);
96
+ }, [onValidityChange]);
97
+ useLayoutEffect(() => {
98
+ const list = listRef.current;
99
+ const option = list?.ownerDocument.getElementById(getOptionId(centreKey));
100
+ if (list && option) {
101
+ list.scrollTop = option.offsetTop - (list.clientHeight - option.offsetHeight) / 2;
102
+ }
103
+ }, [centreRequest, centreKey, getOptionId]);
104
+ // Set while an extension is waiting for its render, so that one scroll position extends only once. It
105
+ // holds the row at the extended edge and its offset before the extension.
106
+ const pendingExtensionRef = useRef(undefined);
107
+ // Rows added at the start or dropped from it move the visible ones, so the scroll position follows the
108
+ // row at the extended edge before the browser paints.
109
+ useLayoutEffect(() => {
110
+ const extension = pendingExtensionRef.current;
111
+ const list = listRef.current;
112
+ pendingExtensionRef.current = undefined;
113
+ if (extension && list) {
114
+ list.scrollTop += extension.edgeRow.offsetTop - extension.offsetTop;
115
+ }
116
+ }, [weekWindow]);
117
+ const handleScroll = useCallback(() => {
118
+ const list = listRef.current;
119
+ if (!list || pendingExtensionRef.current) {
120
+ return;
121
+ }
122
+ const { scrollTop, scrollHeight, clientHeight } = list;
123
+ const isNearStart = scrollTop < clientHeight;
124
+ const isNearEnd = scrollHeight - scrollTop - clientHeight < clientHeight;
125
+ const side = isNearStart ? "start" : isNearEnd ? "end" : undefined;
126
+ const extended = side ? extendWeekWindow(weekWindow, side) : weekWindow;
127
+ if (!side || extended === weekWindow) {
128
+ return;
129
+ }
130
+ const edgeWeek = side === "start" ? weeks[0] : weeks[weeks.length - 1];
131
+ const edgeRow = list.ownerDocument.getElementById(getOptionId(edgeWeek.key));
132
+ if (edgeRow) {
133
+ pendingExtensionRef.current = { edgeRow, offsetTop: edgeRow.offsetTop };
134
+ // The other side gives up a year past ten years, so that scrolling does not keep adding rows.
135
+ setWeekWindow(trimWeekWindow(extended, side === "start" ? "end" : "start"));
136
+ }
137
+ }, [weekWindow, weeks, getOptionId]);
138
+ const jump = useCallback((value) => {
139
+ jumpedTextRef.current = value;
140
+ const target = readJumpText(value, dateFormat);
141
+ if (!target || !("day" in target)) {
142
+ return undefined;
143
+ }
144
+ pendingExtensionRef.current = undefined;
145
+ setWeekWindow((current) => jumpWeekWindow(current, target.day, weekStart, isRangeSelected));
146
+ setActiveDay(target.day);
147
+ setCentreRequest({ day: target.day });
148
+ return getWeek(target.day, weekStart);
149
+ }, [dateFormat, weekStart, isRangeSelected]);
150
+ useEffect(() => {
151
+ if (debouncedText !== jumpedTextRef.current) {
152
+ jump(debouncedText);
153
+ }
154
+ }, [debouncedText, jump]);
155
+ const moveActiveWeek = (delta) => {
156
+ const index = activeIndex === undefined
157
+ ? (weekIndexes.get(centreKey) ?? 0)
158
+ : Math.min(Math.max(activeIndex + delta, 0), weeks.length - 1);
159
+ const week = weeks[index];
160
+ setActiveDay(week.start);
161
+ listRef.current?.ownerDocument.getElementById(getOptionId(week.key))?.scrollIntoView({
162
+ block: "nearest",
163
+ });
164
+ };
165
+ const jumpText = readJumpText(text, dateFormat);
166
+ const handleKeyDown = (event) => {
167
+ switch (event.key) {
168
+ case "ArrowDown":
169
+ moveActiveWeek(1);
170
+ break;
171
+ case "ArrowUp":
172
+ moveActiveWeek(-1);
173
+ break;
174
+ case "PageDown":
175
+ moveActiveWeek(PAGE_WEEKS);
176
+ break;
177
+ case "PageUp":
178
+ moveActiveWeek(-PAGE_WEEKS);
179
+ break;
180
+ case "Enter": {
181
+ // A typed date that cannot be read selects nothing.
182
+ if (jumpText && "error" in jumpText) {
183
+ setIsJumpErrorShown(true);
184
+ break;
185
+ }
186
+ const canApply = !withoutApply && selection !== undefined && !selection.anchor;
187
+ // A date typed just before Enter has not jumped yet.
188
+ const week = (text === jumpedTextRef.current ? undefined : jump(text)) ?? activeWeek;
189
+ if (!week) {
190
+ // With no week to select, Enter applies a closed range if the field is empty, and is
191
+ // left to the parent otherwise.
192
+ if (jumpText !== undefined || !canApply) {
193
+ return;
194
+ }
195
+ submitForm?.();
196
+ break;
197
+ }
198
+ // Enter on a week of a closed range applies the range, unless there is nothing to apply it
199
+ // with or the week was typed, which asks for that week.
200
+ const isTypedWeek = jumpText !== undefined && getWeek(jumpText.day, weekStart).start === week.start;
201
+ if (canApply && !isTypedWeek && isInRange(week, selection)) {
202
+ submitForm?.();
203
+ }
204
+ else {
205
+ handleSelect(week);
206
+ }
207
+ break;
208
+ }
209
+ case "ArrowLeft":
210
+ case "ArrowRight":
211
+ case "Home":
212
+ case "End":
213
+ // Moves the caret, not the date filter's focus.
214
+ event.stopPropagation();
215
+ return;
216
+ default:
217
+ return;
218
+ }
219
+ event.preventDefault();
220
+ event.stopPropagation();
221
+ };
222
+ const handleTextChange = (value) => {
223
+ setText(String(value));
224
+ setIsJumpErrorShown(false);
225
+ };
226
+ const jumpError = isJumpErrorShown && jumpText && "error" in jumpText ? jumpText.error : undefined;
227
+ const errorMessage = jumpError === "format"
228
+ ? intl.formatMessage(messages.invalidDate, { format: toDisplayDateFormat(dateFormat) })
229
+ : jumpError === "range"
230
+ ? intl.formatMessage(messages.dateOutOfRange, {
231
+ firstYear: FIRST_LISTED_DAY.slice(0, 4),
232
+ lastYear: LAST_LISTED_DAY.slice(0, 4),
233
+ })
234
+ : undefined;
235
+ // Only listed weeks are previewed, so that the preview never shows a range the caller cannot submit.
236
+ const previewRange = selection
237
+ ? formatAbsoluteDateRange(selection.from, selection.to, dateFormat)
238
+ : undefined;
239
+ const [, setPreviewAnnouncement, previewAnnouncement] = useDebouncedState(null, PREVIEW_ANNOUNCEMENT_DELAY);
240
+ useEffect(() => {
241
+ setPreviewAnnouncement(previewRange === undefined
242
+ ? null
243
+ : intl.formatMessage(messages.rangePreview, { range: previewRange }));
244
+ }, [previewRange, intl, setPreviewAnnouncement]);
245
+ // While a range is open, the list shows the range a click on the active week would close; of its edges,
246
+ // only the selected one stands out.
247
+ const shownRange = selection?.anchor && activeWeek ? selectWeek(selection, activeWeek) : selection;
248
+ const isPreview = shownRange !== selection;
249
+ return (_jsxs("div", { className: cx("gd-week-range-list", "s-week-range-list", {
250
+ "gd-week-range-list--mobile": isMobile,
251
+ }), children: [_jsx(Input, { className: "gd-week-range-list__search s-week-range-list-search", isSearch: true, value: text, onChange: handleTextChange, onKeyDown: handleKeyDown, onBlur: () => setIsJumpErrorShown(true), placeholder: intl.formatMessage(messages.searchPlaceholder, {
252
+ example: format(PLACEHOLDER_EXAMPLE_DATE, dateFormat),
253
+ }), hasError: errorMessage !== undefined, accessibilityConfig: {
254
+ role: "combobox",
255
+ ariaLabel: intl.formatMessage(messages.searchLabel),
256
+ ariaExpanded: true,
257
+ ariaControls: listboxId,
258
+ ariaActiveDescendant: activeWeek ? getOptionId(activeWeek.key) : undefined,
259
+ ariaInvalid: errorMessage !== undefined,
260
+ ariaDescribedBy: errorMessage === undefined ? undefined : errorId,
261
+ } }), _jsx("div", { ref: listRef, id: listboxId, role: "listbox", "aria-multiselectable": "true", "aria-label": intl.formatMessage(messages.listLabel), className: "gd-week-range-list__rows gd-visible-scrollbar s-week-range-list-rows", onMouseDown: preventFocusChange, onScroll: handleScroll, children: rows.map((row) => row.type === "heading" ? (_jsx(MonthHeading, { label: row.label }, row.key)) : (_jsx(WeekOption, { id: getOptionId(row.key), start: row.start, end: row.end, label: row.label, accessibleName: row.accessibleName, isActive: row.key === activeKey, isSelected: isInRange(row, selection), isInShownRange: isInRange(row, shownRange), isShownRangeEdge: (row.start === shownRange?.from || row.end === shownRange?.to) &&
262
+ (!isPreview || isInRange(row, selection)), onSelect: handleSelect, onHover: setActiveDay }, row.key))) }), _jsx(InputErrorMessage, { descriptionId: errorId, errorText: errorMessage }), customRangeHint ? _jsx("div", { className: "gd-week-range-list__hint", children: customRangeHint }) : null, _jsx("div", { className: "gd-week-range-list__preview s-week-range-list-preview", children: intl.formatMessage(messages.rangePreview, { range: previewRange ?? "–" }) }), _jsx(PreviewAnnouncer, { message: previewAnnouncement })] }));
263
+ }
@@ -0,0 +1,99 @@
1
+ import { type DateString, type WeekStart } from "@gooddata/sdk-model";
2
+ import { type IPeriodRange } from "../PeriodRangePicker/types.js";
3
+ export declare const FIRST_LISTED_DAY: DateString;
4
+ export declare const LAST_LISTED_DAY: DateString;
5
+ export interface IWeek {
6
+ start: DateString;
7
+ end: DateString;
8
+ }
9
+ export interface IMonthHeadingRow {
10
+ type: "heading";
11
+ key: string;
12
+ /** `yyyy-MM` */
13
+ month: string;
14
+ }
15
+ export interface IWeekRow extends IWeek {
16
+ type: "week";
17
+ key: string;
18
+ }
19
+ export type WeekListRow = IMonthHeadingRow | IWeekRow;
20
+ /**
21
+ * The months whose weeks the list holds, both included, `yyyy-MM`.
22
+ */
23
+ export interface IWeekWindow {
24
+ firstMonth: string;
25
+ lastMonth: string;
26
+ }
27
+ /**
28
+ * Whole weeks, from the first day of the first week to the last day of the last one. `anchor` is the week
29
+ * the next click closes the range with; a closed range has none.
30
+ */
31
+ export interface IWeekSelection {
32
+ from: DateString;
33
+ to: DateString;
34
+ anchor?: IWeek;
35
+ }
36
+ export declare function getWeek(day: DateString, weekStart: WeekStart): IWeek;
37
+ /**
38
+ * The month a week is listed under: the one holding most of its days, which is the month of its fourth day.
39
+ */
40
+ export declare function getWeekMonth(week: IWeek): string;
41
+ /**
42
+ * The months from two years before the earliest day to two years after the latest one, within the listed
43
+ * years.
44
+ */
45
+ export declare function createWeekWindow(days: [DateString, ...DateString[]]): IWeekWindow;
46
+ /**
47
+ * The window the list opens with: around the stored range, around its `from` alone when that would span
48
+ * more than ten years, and around today without a stored `from`.
49
+ */
50
+ export declare function createOpeningWeekWindow({ from, to }: IPeriodRange, today: DateString): IWeekWindow;
51
+ /**
52
+ * The day whose week the list centres on open: the stored `from`, or today without one.
53
+ */
54
+ export declare function getOpeningDay({ from }: IPeriodRange, today: DateString): DateString;
55
+ /**
56
+ * Adds a year of months at the side, and returns the same window at a bound of the listed years.
57
+ */
58
+ export declare function extendWeekWindow(window: IWeekWindow, side: "start" | "end"): IWeekWindow;
59
+ /**
60
+ * Drops a year of months at the side when the window spans more than ten years, and returns the same
61
+ * window otherwise.
62
+ */
63
+ export declare function trimWeekWindow(window: IWeekWindow, side: "start" | "end"): IWeekWindow;
64
+ /**
65
+ * Widens the window by the day's own window when the day's week is not in it, and returns the same window
66
+ * otherwise.
67
+ */
68
+ export declare function widenWeekWindow(window: IWeekWindow, day: DateString, weekStart: WeekStart): IWeekWindow;
69
+ /**
70
+ * The window after a jump to the day. While nothing is selected, or when widening would span more than ten
71
+ * years, the day's own window replaces it, otherwise it is widened.
72
+ */
73
+ export declare function jumpWeekWindow(window: IWeekWindow, day: DateString, weekStart: WeekStart, hasSelection: boolean): IWeekWindow;
74
+ export declare function getWeekRows(window: IWeekWindow, weekStart: WeekStart): WeekListRow[];
75
+ /**
76
+ * The selection after a click on the week: an open selection closes into the ordered range between its
77
+ * anchor and the week; otherwise the week alone is selected and becomes the anchor.
78
+ */
79
+ export declare function selectWeek(selection: IWeekSelection | undefined, week: IWeek): IWeekSelection;
80
+ export declare function weekSelectionToPeriodRange({ from, to }: IWeekSelection): IPeriodRange;
81
+ /**
82
+ * The closed selection of a stored range, or none when the range is not whole weeks by the week start or
83
+ * its first or last week is not listed.
84
+ */
85
+ export declare function periodRangeToWeekSelection({ from, to }: IPeriodRange, weekStart: WeekStart): IWeekSelection | undefined;
86
+ export declare function formatWeekLabel(week: IWeek, locale: string): string;
87
+ export declare function formatWeekAccessibleName(week: IWeek, locale: string): string;
88
+ export declare function formatMonthHeading(month: string, locale: string): string;
89
+ export type JumpText = {
90
+ day: DateString;
91
+ } | {
92
+ error: "format" | "range";
93
+ };
94
+ /**
95
+ * Reads the jump field: the day it names, the format error while it is not a whole valid date in the date
96
+ * format, or the range error for a day outside the listed years. Blank text reads as nothing.
97
+ */
98
+ export declare function readJumpText(text: string, dateFormat: string): JumpText | undefined;
99
+ //# sourceMappingURL=weeks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"weeks.d.ts","sourceRoot":"","sources":["../../../src/DateFilter/WeekRangeList/weeks.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,229 @@
1
+ // (C) 2026 GoodData Corporation
2
+ import { addDays, addYears, differenceInCalendarMonths, format, isValid, parseISO, startOfWeek, subYears, } from "date-fns";
3
+ import { platformDateFnsFormat } from "../constants/Platform.js";
4
+ import { parseDate } from "../DateRangePicker/utils.js";
5
+ const MONTH_FORMAT = "yyyy-MM";
6
+ const WINDOW_YEARS = 2;
7
+ const MAX_WINDOW_MONTHS = 120;
8
+ // The list offers only the weeks of these years. It keeps every day and month at a four-digit year, so
9
+ // that they compare as strings.
10
+ export const FIRST_LISTED_DAY = "1900-01-01";
11
+ export const LAST_LISTED_DAY = "2100-12-31";
12
+ const FIRST_LISTED_MONTH = "1900-01";
13
+ const LAST_LISTED_MONTH = "2100-12";
14
+ const DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
15
+ // Parsing by a format pattern is several times slower, and the list parses thousands of days. Only a whole
16
+ // day parses, as it does by the pattern.
17
+ function parseDay(day) {
18
+ return DAY_PATTERN.test(day) ? parseISO(day) : new Date(Number.NaN);
19
+ }
20
+ function formatDay(date) {
21
+ return format(date, platformDateFnsFormat);
22
+ }
23
+ function parseMonth(month) {
24
+ return parseISO(month);
25
+ }
26
+ function shiftMonth(month, years) {
27
+ return format(addYears(parseMonth(month), years), MONTH_FORMAT);
28
+ }
29
+ function clampMonth(month) {
30
+ if (month < FIRST_LISTED_MONTH) {
31
+ return FIRST_LISTED_MONTH;
32
+ }
33
+ return month > LAST_LISTED_MONTH ? LAST_LISTED_MONTH : month;
34
+ }
35
+ function clampDay(day) {
36
+ if (day < FIRST_LISTED_DAY) {
37
+ return FIRST_LISTED_DAY;
38
+ }
39
+ return day > LAST_LISTED_DAY ? LAST_LISTED_DAY : day;
40
+ }
41
+ function readDay(day) {
42
+ const date = day ? parseDay(day) : undefined;
43
+ return date && isValid(date) ? formatDay(date) : undefined;
44
+ }
45
+ function spansAtMostTenYears({ firstMonth, lastMonth }) {
46
+ return differenceInCalendarMonths(parseMonth(lastMonth), parseMonth(firstMonth)) <= MAX_WINDOW_MONTHS;
47
+ }
48
+ function weekOf(date, weekStart) {
49
+ const start = startOfWeek(date, { weekStartsOn: weekStart === "Monday" ? 1 : 0 });
50
+ return { start: formatDay(start), end: formatDay(addDays(start, 6)) };
51
+ }
52
+ export function getWeek(day, weekStart) {
53
+ return weekOf(parseDay(day), weekStart);
54
+ }
55
+ /**
56
+ * The month a week is listed under: the one holding most of its days, which is the month of its fourth day.
57
+ */
58
+ export function getWeekMonth(week) {
59
+ return format(addDays(parseDay(week.start), 3), MONTH_FORMAT);
60
+ }
61
+ /**
62
+ * The months from two years before the earliest day to two years after the latest one, within the listed
63
+ * years.
64
+ */
65
+ export function createWeekWindow(days) {
66
+ const sorted = days.map(clampDay).sort();
67
+ return {
68
+ firstMonth: clampMonth(format(subYears(parseDay(sorted[0]), WINDOW_YEARS), MONTH_FORMAT)),
69
+ lastMonth: clampMonth(format(addYears(parseDay(sorted[sorted.length - 1]), WINDOW_YEARS), MONTH_FORMAT)),
70
+ };
71
+ }
72
+ /**
73
+ * The window the list opens with: around the stored range, around its `from` alone when that would span
74
+ * more than ten years, and around today without a stored `from`.
75
+ */
76
+ export function createOpeningWeekWindow({ from, to }, today) {
77
+ const fromDay = readDay(from);
78
+ if (!fromDay) {
79
+ return createWeekWindow([today]);
80
+ }
81
+ const toDay = readDay(to);
82
+ const window = createWeekWindow(toDay ? [fromDay, toDay] : [fromDay]);
83
+ return spansAtMostTenYears(window) ? window : createWeekWindow([fromDay]);
84
+ }
85
+ /**
86
+ * The day whose week the list centres on open: the stored `from`, or today without one.
87
+ */
88
+ export function getOpeningDay({ from }, today) {
89
+ return clampDay(readDay(from) ?? today);
90
+ }
91
+ /**
92
+ * Adds a year of months at the side, and returns the same window at a bound of the listed years.
93
+ */
94
+ export function extendWeekWindow(window, side) {
95
+ const extended = side === "start"
96
+ ? { ...window, firstMonth: clampMonth(shiftMonth(window.firstMonth, -1)) }
97
+ : { ...window, lastMonth: clampMonth(shiftMonth(window.lastMonth, 1)) };
98
+ return extended.firstMonth === window.firstMonth && extended.lastMonth === window.lastMonth
99
+ ? window
100
+ : extended;
101
+ }
102
+ /**
103
+ * Drops a year of months at the side when the window spans more than ten years, and returns the same
104
+ * window otherwise.
105
+ */
106
+ export function trimWeekWindow(window, side) {
107
+ if (spansAtMostTenYears(window)) {
108
+ return window;
109
+ }
110
+ return side === "start"
111
+ ? { ...window, firstMonth: shiftMonth(window.firstMonth, 1) }
112
+ : { ...window, lastMonth: shiftMonth(window.lastMonth, -1) };
113
+ }
114
+ /**
115
+ * Widens the window by the day's own window when the day's week is not in it, and returns the same window
116
+ * otherwise.
117
+ */
118
+ export function widenWeekWindow(window, day, weekStart) {
119
+ const month = getWeekMonth(getWeek(day, weekStart));
120
+ if (month >= window.firstMonth && month <= window.lastMonth) {
121
+ return window;
122
+ }
123
+ const around = createWeekWindow([day]);
124
+ return {
125
+ firstMonth: around.firstMonth < window.firstMonth ? around.firstMonth : window.firstMonth,
126
+ lastMonth: around.lastMonth > window.lastMonth ? around.lastMonth : window.lastMonth,
127
+ };
128
+ }
129
+ /**
130
+ * The window after a jump to the day. While nothing is selected, or when widening would span more than ten
131
+ * years, the day's own window replaces it, otherwise it is widened.
132
+ */
133
+ export function jumpWeekWindow(window, day, weekStart, hasSelection) {
134
+ const widened = widenWeekWindow(window, day, weekStart);
135
+ if (widened === window) {
136
+ return window;
137
+ }
138
+ return hasSelection && spansAtMostTenYears(widened) ? widened : createWeekWindow([day]);
139
+ }
140
+ export function getWeekRows(window, weekStart) {
141
+ const rows = [];
142
+ // A month's first week is the one holding its fourth day.
143
+ let week = weekOf(addDays(parseMonth(window.firstMonth), 3), weekStart);
144
+ let month = getWeekMonth(week);
145
+ let listedMonth;
146
+ while (month <= window.lastMonth) {
147
+ if (month !== listedMonth) {
148
+ rows.push({ type: "heading", key: month, month });
149
+ listedMonth = month;
150
+ }
151
+ rows.push({ type: "week", key: week.start, ...week });
152
+ week = weekOf(addDays(parseDay(week.end), 1), weekStart);
153
+ month = getWeekMonth(week);
154
+ }
155
+ return rows;
156
+ }
157
+ /**
158
+ * The selection after a click on the week: an open selection closes into the ordered range between its
159
+ * anchor and the week; otherwise the week alone is selected and becomes the anchor.
160
+ */
161
+ export function selectWeek(selection, week) {
162
+ const anchor = selection?.anchor;
163
+ if (!anchor) {
164
+ return { from: week.start, to: week.end, anchor: week };
165
+ }
166
+ return week.start < anchor.start
167
+ ? { from: week.start, to: anchor.end }
168
+ : { from: anchor.start, to: week.end };
169
+ }
170
+ export function weekSelectionToPeriodRange({ from, to }) {
171
+ return { from, to };
172
+ }
173
+ /**
174
+ * The closed selection of a stored range, or none when the range is not whole weeks by the week start or
175
+ * its first or last week is not listed.
176
+ */
177
+ export function periodRangeToWeekSelection({ from, to }, weekStart) {
178
+ if (!from || !to || !isValid(parseDay(from)) || !isValid(parseDay(to))) {
179
+ return undefined;
180
+ }
181
+ const isWholeWeeks = from <= to && getWeek(from, weekStart).start === from && getWeek(to, weekStart).end === to;
182
+ const isListed = from >= getWeek(FIRST_LISTED_DAY, weekStart).start && to <= getWeek(LAST_LISTED_DAY, weekStart).end;
183
+ return isWholeWeeks && isListed ? { from, to } : undefined;
184
+ }
185
+ const DATE_TIME_FORMAT_OPTIONS = {
186
+ weekLabel: { month: "short", day: "numeric" },
187
+ weekName: { year: "numeric", month: "long", day: "numeric" },
188
+ monthHeading: { year: "numeric", month: "long" },
189
+ };
190
+ // Creating a formatter is slow and the list labels every row, so each one is created once per locale.
191
+ const dateTimeFormats = new Map();
192
+ function getDateTimeFormat(kind, locale) {
193
+ const key = `${kind} ${locale}`;
194
+ let dateTimeFormat = dateTimeFormats.get(key);
195
+ if (!dateTimeFormat) {
196
+ dateTimeFormat = new Intl.DateTimeFormat(locale, DATE_TIME_FORMAT_OPTIONS[kind]);
197
+ dateTimeFormats.set(key, dateTimeFormat);
198
+ }
199
+ return dateTimeFormat;
200
+ }
201
+ function formatWeek(week, locale, kind) {
202
+ const dateTimeFormat = getDateTimeFormat(kind, locale);
203
+ return `${dateTimeFormat.format(parseDay(week.start))} – ${dateTimeFormat.format(parseDay(week.end))}`;
204
+ }
205
+ export function formatWeekLabel(week, locale) {
206
+ return formatWeek(week, locale, "weekLabel");
207
+ }
208
+ export function formatWeekAccessibleName(week, locale) {
209
+ return formatWeek(week, locale, "weekName");
210
+ }
211
+ export function formatMonthHeading(month, locale) {
212
+ return getDateTimeFormat("monthHeading", locale).format(parseMonth(month));
213
+ }
214
+ /**
215
+ * Reads the jump field: the day it names, the format error while it is not a whole valid date in the date
216
+ * format, or the range error for a day outside the listed years. Blank text reads as nothing.
217
+ */
218
+ export function readJumpText(text, dateFormat) {
219
+ const trimmed = text.trim();
220
+ if (!trimmed) {
221
+ return undefined;
222
+ }
223
+ const date = parseDate(trimmed, dateFormat);
224
+ if (!date) {
225
+ return { error: "format" };
226
+ }
227
+ const day = formatDay(date);
228
+ return day < FIRST_LISTED_DAY || day > LAST_LISTED_DAY ? { error: "range" } : { day };
229
+ }
package/esm/internal.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { DEFAULT_LOCALE } from "./DateFilter/utils/FormattingUtils.js";
2
+ export { WeekRangeList } from "./DateFilter/WeekRangeList/WeekRangeList.js";
2
3
  export { AbsoluteDateFilterForm } from "./DateFilter/AbsoluteDateFilterForm/AbsoluteDateFilterForm.js";
3
4
  export { RelativeDateFilterForm } from "./DateFilter/RelativeDateFilterForm/RelativeDateFilterForm.js";
4
5
  export { ExcludeCurrentPeriodToggle } from "./DateFilter/ExcludeCurrentPeriodToggle/ExcludeCurrentPeriodToggle.js";
@@ -1 +1 @@
1
- {"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAC;AACvE,OAAO,EAAE,sBAAsB,EAAE,MAAM,+DAA+D,CAAC;AACvG,OAAO,EAAE,sBAAsB,EAAE,MAAM,+DAA+D,CAAC;AACvG,OAAO,EAAE,0BAA0B,EAAE,MAAM,uEAAuE,CAAC;AACnH,OAAO,EAAE,yBAAyB,EAAE,MAAM,0DAA0D,CAAC;AACrG,OAAO,EAAE,gBAAgB,EAAE,MAAM,iDAAiD,CAAC;AACnF,OAAO,EAAE,6BAA6B,EAAE,MAAM,iDAAiD,CAAC;AAChG,OAAO,EAAE,QAAQ,EAAE,MAAM,mCAAmC,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,iDAAiD,CAAC;AAClF,OAAO,EAAE,qBAAqB,EAAE,MAAM,+CAA+C,CAAC;AACtF,OAAO,EAAE,+BAA+B,EAAE,MAAM,yDAAyD,CAAC;AAC1G,YAAY,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC"}
1
+ {"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAC;AAGvE,OAAO,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAC5E,OAAO,EAAE,sBAAsB,EAAE,MAAM,+DAA+D,CAAC;AACvG,OAAO,EAAE,sBAAsB,EAAE,MAAM,+DAA+D,CAAC;AACvG,OAAO,EAAE,0BAA0B,EAAE,MAAM,uEAAuE,CAAC;AACnH,OAAO,EAAE,yBAAyB,EAAE,MAAM,0DAA0D,CAAC;AACrG,OAAO,EAAE,gBAAgB,EAAE,MAAM,iDAAiD,CAAC;AACnF,OAAO,EAAE,6BAA6B,EAAE,MAAM,iDAAiD,CAAC;AAChG,OAAO,EAAE,QAAQ,EAAE,MAAM,mCAAmC,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,iDAAiD,CAAC;AAClF,OAAO,EAAE,qBAAqB,EAAE,MAAM,+CAA+C,CAAC;AACtF,OAAO,EAAE,+BAA+B,EAAE,MAAM,yDAAyD,CAAC;AAC1G,YAAY,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC"}
package/esm/internal.js CHANGED
@@ -1,6 +1,9 @@
1
1
  // (C) 2023-2026 GoodData Corporation
2
2
  // oxlint-disable no-barrel-files/no-barrel-files
3
3
  export { DEFAULT_LOCALE } from "./DateFilter/utils/FormattingUtils.js";
4
+ // Before every export that imports it, or Storybook's bundle does not initialize it for a story that
5
+ // imports only this list.
6
+ export { WeekRangeList } from "./DateFilter/WeekRangeList/WeekRangeList.js";
4
7
  export { AbsoluteDateFilterForm } from "./DateFilter/AbsoluteDateFilterForm/AbsoluteDateFilterForm.js";
5
8
  export { RelativeDateFilterForm } from "./DateFilter/RelativeDateFilterForm/RelativeDateFilterForm.js";
6
9
  export { ExcludeCurrentPeriodToggle } from "./DateFilter/ExcludeCurrentPeriodToggle/ExcludeCurrentPeriodToggle.js";