@olenbetong/appframe-ds 1.1.0 → 1.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/CHANGELOG.md +18 -0
- package/package.json +8 -7
- package/scripts/copyAssets.mjs +23 -51
- package/src/AfLookup/Lookup.css +4 -0
- package/src/AfLookup/Lookup.tsx +24 -2
- package/src/cutoff/CutOffPicker.css +12 -0
- package/src/cutoff/CutOffPicker.tsx +165 -0
- package/src/cutoff/context.tsx +194 -0
- package/src/cutoff/dsCutOffDates.ts +21 -0
- package/src/cutoff/index.ts +4 -0
- package/src/cutoff/utils.test.ts +59 -0
- package/src/cutoff/utils.ts +276 -0
- package/src/grid/GridEditLookup.css +12 -0
- package/src/grid/GridEditLookup.tsx +121 -0
- package/src/grid/index.tsx +19 -0
- package/src/grid/useAfServerAggregation.test.ts +68 -0
- package/src/grid/useAfServerAggregation.ts +13 -0
- package/src/index.ts +2 -0
- package/src/layout/PageContainer.css +21 -2
- package/src/layout/PageContainer.tsx +18 -2
- package/src/layout/ResponsiveDialog.css +86 -0
- package/src/layout/ResponsiveDialog.tsx +55 -0
- package/src/layout/index.tsx +1 -0
- package/src/link/Link.tsx +23 -0
- package/src/link/index.ts +1 -0
- package/src/page/PageTitle.css +5 -0
- package/src/page/PageTitle.tsx +21 -0
- package/src/page/index.ts +1 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
cutOffToPlainDate,
|
|
5
|
+
getCutOffFromSearchParams,
|
|
6
|
+
getHistoryRangeStart,
|
|
7
|
+
getWeekRange,
|
|
8
|
+
isPeriodValidForCoType,
|
|
9
|
+
plainDateToCutOff,
|
|
10
|
+
subWeekString,
|
|
11
|
+
} from "./utils.js";
|
|
12
|
+
|
|
13
|
+
describe("cut-off periods", () => {
|
|
14
|
+
test("a date maps to every period type", () => {
|
|
15
|
+
expect(plainDateToCutOff("2026-09-21", "AllDates")).toBe("2026-09-21");
|
|
16
|
+
expect(plainDateToCutOff("2026-09-21", "WS")).toBe("2026-W39");
|
|
17
|
+
expect(plainDateToCutOff("2026-09-21", "MLD")).toBe("2026-09");
|
|
18
|
+
expect(plainDateToCutOff("2026-09-21", "QLS")).toBe("2026-Q3");
|
|
19
|
+
expect(plainDateToCutOff("2026-09-21", "YLD")).toBe("2026");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("ISO weeks follow the ISO year, not the calendar year", () => {
|
|
23
|
+
expect(plainDateToCutOff("2026-01-01", "WS")).toBe("2026-W01");
|
|
24
|
+
expect(plainDateToCutOff("2027-01-01", "WS")).toBe("2026-W53");
|
|
25
|
+
expect(plainDateToCutOff("2024-12-30", "WS")).toBe("2025-W01");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("a period maps back to its first day", () => {
|
|
29
|
+
expect(cutOffToPlainDate("WS", "2026-W39").toString()).toBe("2026-09-21");
|
|
30
|
+
expect(cutOffToPlainDate("WS", "2026-39").toString()).toBe("2026-09-21");
|
|
31
|
+
expect(cutOffToPlainDate("QLD", "2026-Q3").toString()).toBe("2026-07-01");
|
|
32
|
+
expect(cutOffToPlainDate("MLS", "2026-09").toString()).toBe("2026-09-01");
|
|
33
|
+
expect(cutOffToPlainDate("YLD", "2026").toString()).toBe("2026-01-01");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("week arithmetic", () => {
|
|
37
|
+
expect(subWeekString("2026-W02", 3)).toBe("2025-W51");
|
|
38
|
+
expect(getWeekRange("2025-W52", "2026-W02")).toEqual(["2025-W52", "2026-W01", "2026-W02"]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("history ranges", () => {
|
|
42
|
+
expect(getHistoryRangeStart("YLD", "2026")).toBe("2021");
|
|
43
|
+
expect(getHistoryRangeStart("QLD", "2026-Q3")).toBe("2024-Q3");
|
|
44
|
+
expect(getHistoryRangeStart("MLD", "2026-09")).toBe("2025-09");
|
|
45
|
+
expect(getHistoryRangeStart("WS", "2026-W39")).toBe("2026-W13");
|
|
46
|
+
expect(getHistoryRangeStart("AllDates", "2026-09-21")).toBe("2026-08-21");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("search parameters: defaults for missing or invalid values, weeks without the W", () => {
|
|
50
|
+
expect(getCutOffFromSearchParams(new URLSearchParams("co=WS&period=2026-W39"))).toEqual({
|
|
51
|
+
coType: "WS",
|
|
52
|
+
period: "2026-39",
|
|
53
|
+
});
|
|
54
|
+
expect(getCutOffFromSearchParams(new URLSearchParams("co=QLD&period=2026-Q7")).period).toMatch(/^\d{4}-Q[1-4]$/);
|
|
55
|
+
expect(getCutOffFromSearchParams(new URLSearchParams("co=nope")).coType).toBe("MLD");
|
|
56
|
+
expect(isPeriodValidForCoType("MLS", "2026-09")).toBe(true);
|
|
57
|
+
expect(isPeriodValidForCoType("QLS", "2026-Q2")).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Cut-off periods: the period types Synergi reports on (day, ISO week, month, quarter, year,
|
|
3
|
+
* with "last sunday" variants for month and quarter) and the string form each is stored in
|
|
4
|
+
* (`2026-09-21`, `2026-W38`, `2026-09`, `2026-Q3`, `2026`). Ported from
|
|
5
|
+
* @olenbetong/appframe-mui, with Temporal in place of date-fns for the ISO week arithmetic.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Temporal } from "temporal-polyfill";
|
|
9
|
+
|
|
10
|
+
import { getLocalizedString } from "../i18n.js";
|
|
11
|
+
|
|
12
|
+
export type PeriodType = "AllDates" | "WS" | "MLD" | "MLS" | "QLD" | "QLS" | "YLD";
|
|
13
|
+
export type PeriodTypeValueMap = { [K in PeriodType]: string };
|
|
14
|
+
|
|
15
|
+
/** ISO week string, `2026-W38`, of a date. */
|
|
16
|
+
function toISOWeek(date: Temporal.PlainDate) {
|
|
17
|
+
return `${date.yearOfWeek}-W${String(date.weekOfYear).padStart(2, "0")}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The Monday of an ISO week given as `2026-W38` or `2026-38`. */
|
|
21
|
+
function isoWeekToDate(week: string) {
|
|
22
|
+
let [year, weekNo] = week.replace("W", "").split("-");
|
|
23
|
+
let firstOfYear = Temporal.PlainDate.from(`${year}-01-01`);
|
|
24
|
+
let daysToAdd = (Number(weekNo) - 1) * 7;
|
|
25
|
+
let firstOfWeekOne: Temporal.PlainDate;
|
|
26
|
+
if (firstOfYear.dayOfWeek <= 4) {
|
|
27
|
+
firstOfWeekOne = firstOfYear.add({ days: -firstOfYear.dayOfWeek + 1 });
|
|
28
|
+
} else {
|
|
29
|
+
firstOfWeekOne = firstOfYear.add({ days: 8 - firstOfYear.dayOfWeek });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return firstOfWeekOne.add({ days: daysToAdd });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A date from the app's own Temporal copy or a string, as this module's `PlainDate`. */
|
|
36
|
+
function toPlainDate(date: Temporal.PlainDate | string) {
|
|
37
|
+
return Temporal.PlainDate.from(typeof date === "string" ? date : date.toString());
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const now = Temporal.Now.plainDateISO();
|
|
41
|
+
const nowDate = now.toString();
|
|
42
|
+
const nowWeek = toISOWeek(now);
|
|
43
|
+
const nowMonth = Temporal.PlainYearMonth.from(now).toString();
|
|
44
|
+
const nowQuarter = `${now.year}-Q${Math.ceil(now.month / 3)}`;
|
|
45
|
+
const nowYear = String(now.year);
|
|
46
|
+
|
|
47
|
+
export const defaultCutOffType: PeriodType = "MLD";
|
|
48
|
+
export const defaultPeriod = nowMonth;
|
|
49
|
+
|
|
50
|
+
export const defaultPeriods: PeriodTypeValueMap = {
|
|
51
|
+
AllDates: nowDate,
|
|
52
|
+
WS: nowWeek,
|
|
53
|
+
MLD: nowMonth,
|
|
54
|
+
MLS: nowMonth,
|
|
55
|
+
QLD: nowQuarter,
|
|
56
|
+
QLS: nowQuarter,
|
|
57
|
+
YLD: nowYear,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export function getYearRange(start: number, end: number) {
|
|
61
|
+
let years: string[] = [];
|
|
62
|
+
for (let i = Math.min(start, end); i < Math.max(start, end); i++) {
|
|
63
|
+
years.push(String(i));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return years;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function subMonthString(month: string, amount: number) {
|
|
70
|
+
return Temporal.PlainYearMonth.from(month).subtract({ months: amount }).toString();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function getMonthRange(start: string, end: string) {
|
|
74
|
+
let startDate = Temporal.PlainYearMonth.from(start);
|
|
75
|
+
let endDate = Temporal.PlainYearMonth.from(end);
|
|
76
|
+
|
|
77
|
+
if (Temporal.PlainYearMonth.compare(startDate, endDate) > 0) {
|
|
78
|
+
throw new Error("Start month must be lower than end month");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let months: string[] = [];
|
|
82
|
+
|
|
83
|
+
while (Temporal.PlainYearMonth.compare(startDate, endDate) <= 0) {
|
|
84
|
+
months.push(startDate.toString());
|
|
85
|
+
startDate = startDate.add({ months: 1 });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return months;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function subWeekString(week: string, amount: number) {
|
|
92
|
+
return toISOWeek(isoWeekToDate(week).subtract({ weeks: amount }));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function getWeekRange(start: string, end: string) {
|
|
96
|
+
let startDate = isoWeekToDate(start);
|
|
97
|
+
let endDate = isoWeekToDate(end);
|
|
98
|
+
|
|
99
|
+
if (Temporal.PlainDate.compare(startDate, endDate) > 0) {
|
|
100
|
+
throw new Error("Start week must be lower than end week");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let weeks: string[] = [];
|
|
104
|
+
|
|
105
|
+
while (Temporal.PlainDate.compare(startDate, endDate) <= 0) {
|
|
106
|
+
weeks.push(toISOWeek(startDate));
|
|
107
|
+
startDate = startDate.add({ weeks: 1 });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return weeks;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function subDateString(date: string, amount: number) {
|
|
114
|
+
return Temporal.PlainDate.from(date).subtract({ days: amount }).toString();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function getDateRange(start: string, end: string) {
|
|
118
|
+
let startDate = Temporal.PlainDate.from(start);
|
|
119
|
+
let endDate = Temporal.PlainDate.from(end);
|
|
120
|
+
|
|
121
|
+
if (Temporal.PlainDate.compare(startDate, endDate) > 0) {
|
|
122
|
+
throw new Error("Start date must be lower than end date");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let dates: string[] = [];
|
|
126
|
+
|
|
127
|
+
while (Temporal.PlainDate.compare(startDate, endDate) <= 0) {
|
|
128
|
+
dates.push(startDate.toString());
|
|
129
|
+
startDate = startDate.add({ days: 1 });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return dates;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The period of `periodType` a date falls in, in its stored string form. */
|
|
136
|
+
export function plainDateToCutOff(date: Temporal.PlainDate | string, periodType: PeriodType) {
|
|
137
|
+
let plain = toPlainDate(date);
|
|
138
|
+
|
|
139
|
+
switch (periodType) {
|
|
140
|
+
case "AllDates":
|
|
141
|
+
return plain.toString();
|
|
142
|
+
case "YLD":
|
|
143
|
+
return String(plain.year);
|
|
144
|
+
case "QLD":
|
|
145
|
+
case "QLS":
|
|
146
|
+
return `${plain.year}-Q${String(Math.ceil(plain.month / 3))}`;
|
|
147
|
+
case "MLD":
|
|
148
|
+
case "MLS":
|
|
149
|
+
return Temporal.PlainYearMonth.from(plain).toString();
|
|
150
|
+
case "WS":
|
|
151
|
+
return toISOWeek(plain);
|
|
152
|
+
default:
|
|
153
|
+
return "";
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The first day of a period. */
|
|
158
|
+
export function cutOffToPlainDate(cutOffType: PeriodType, period: string) {
|
|
159
|
+
switch (cutOffType) {
|
|
160
|
+
case "YLD":
|
|
161
|
+
return Temporal.PlainDate.from({ year: Number(period), month: 1, day: 1 });
|
|
162
|
+
case "QLD":
|
|
163
|
+
case "QLS": {
|
|
164
|
+
let [year, quarter] = period.split("-");
|
|
165
|
+
quarter = quarter.replace("Q", "");
|
|
166
|
+
return Temporal.PlainDate.from({ year: Number(year), month: (Number(quarter) - 1) * 3 + 1, day: 1 });
|
|
167
|
+
}
|
|
168
|
+
case "MLD":
|
|
169
|
+
case "MLS":
|
|
170
|
+
return Temporal.PlainDate.from(`${period}-01`);
|
|
171
|
+
case "WS":
|
|
172
|
+
return isoWeekToDate(period);
|
|
173
|
+
case "AllDates":
|
|
174
|
+
return Temporal.PlainDate.from(period);
|
|
175
|
+
default:
|
|
176
|
+
return Temporal.Now.plainDateISO();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const culture = (globalThis as { af?: { userSession?: { culture?: string } } }).af?.userSession?.culture ?? "en-GB";
|
|
181
|
+
|
|
182
|
+
/** A label for a period in the user's culture: "september 2026", "Week 38", "Q3 2026". */
|
|
183
|
+
export function getPeriodLabel(periodType: PeriodType, period: string | null | undefined) {
|
|
184
|
+
if (!period) return "";
|
|
185
|
+
|
|
186
|
+
switch (periodType) {
|
|
187
|
+
case "YLD":
|
|
188
|
+
return String(period);
|
|
189
|
+
case "QLD":
|
|
190
|
+
case "QLS": {
|
|
191
|
+
let [year, quarter] = period.split("-");
|
|
192
|
+
return `${quarter} ${year}`;
|
|
193
|
+
}
|
|
194
|
+
case "MLD":
|
|
195
|
+
case "MLS": {
|
|
196
|
+
let month = Temporal.PlainDate.from(`${period}-01`);
|
|
197
|
+
return month.toLocaleString(culture, { month: "long", year: "numeric" });
|
|
198
|
+
}
|
|
199
|
+
case "WS": {
|
|
200
|
+
const [, week] = period.replace("-W", "-").split("-");
|
|
201
|
+
return `${getLocalizedString("Week")} ${Number(week)}`;
|
|
202
|
+
}
|
|
203
|
+
case "AllDates": {
|
|
204
|
+
let date = Temporal.PlainDate.from(period);
|
|
205
|
+
return date.toLocaleString(culture, { weekday: "long", day: "numeric", month: "short" });
|
|
206
|
+
}
|
|
207
|
+
default:
|
|
208
|
+
return "";
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function isPeriodValidForCoType(coType: PeriodType, period: string) {
|
|
213
|
+
switch (coType) {
|
|
214
|
+
case "AllDates":
|
|
215
|
+
return /^\d{4}-\d{2}-\d{2}$/.test(period);
|
|
216
|
+
case "WS":
|
|
217
|
+
return /^\d{4}-W\d{2}$/.test(period);
|
|
218
|
+
case "MLD":
|
|
219
|
+
case "MLS":
|
|
220
|
+
return /^\d{4}-\d{2}$/.test(period);
|
|
221
|
+
case "QLD":
|
|
222
|
+
case "QLS":
|
|
223
|
+
return /^\d{4}-Q[1234]$/.test(period);
|
|
224
|
+
case "YLD":
|
|
225
|
+
return /^\d{4}$/.test(period);
|
|
226
|
+
default:
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Where a history range for a period starts: five years, two years, twelve months, 26 weeks or 31 days back. */
|
|
232
|
+
export function getHistoryRangeStart(cutOffType: PeriodType, period: string) {
|
|
233
|
+
switch (cutOffType) {
|
|
234
|
+
case "YLD":
|
|
235
|
+
return String(Number(period) - 5);
|
|
236
|
+
case "QLD":
|
|
237
|
+
case "QLS": {
|
|
238
|
+
// Two years back, same quarter: "2026-Q3" -> "2024-Q3". (The appframe-mui original
|
|
239
|
+
// produced "2024-QQ3" here.)
|
|
240
|
+
let year = Number(period.substring(0, 4)) - 2;
|
|
241
|
+
return `${year}-${period.substring(5)}`;
|
|
242
|
+
}
|
|
243
|
+
case "MLD":
|
|
244
|
+
case "MLS":
|
|
245
|
+
return subMonthString(period, 12);
|
|
246
|
+
case "WS":
|
|
247
|
+
return subWeekString(period, 26);
|
|
248
|
+
case "AllDates":
|
|
249
|
+
return subDateString(period, 31);
|
|
250
|
+
default:
|
|
251
|
+
return "";
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function stringToCutOffType(coType: string | null): PeriodType {
|
|
256
|
+
if (!coType || !["AllDates", "WS", "MLS", "MLD", "QLS", "QLD", "YLD"].includes(coType)) {
|
|
257
|
+
return defaultCutOffType;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return coType as PeriodType;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** The cut-off in the `co` and `period` search parameters, defaults for what is missing or invalid. */
|
|
264
|
+
export function getCutOffFromSearchParams(searchParams: URLSearchParams) {
|
|
265
|
+
let coType = stringToCutOffType(searchParams.get("co"));
|
|
266
|
+
let period = searchParams.get("period") ?? defaultPeriods[coType];
|
|
267
|
+
|
|
268
|
+
if (!isPeriodValidForCoType(coType, period)) {
|
|
269
|
+
period = defaultPeriods[coType];
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
coType,
|
|
274
|
+
period: period.replace("W", ""), // Appframe stores weeks without the W
|
|
275
|
+
};
|
|
276
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import "./GridEditLookup.css";
|
|
2
|
+
|
|
3
|
+
import { Button } from "@digdir/designsystemet-react";
|
|
4
|
+
import { GridEditInputCell, type GridRenderEditCellParams } from "@mui/x-data-grid-premium";
|
|
5
|
+
import { MagnifyingGlassIcon, XMarkIcon } from "@navikt/aksel-icons";
|
|
6
|
+
import type { DataObject } from "@olenbetong/appframe-data";
|
|
7
|
+
import type { DistributiveOmit } from "@olenbetong/appframe-react";
|
|
8
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
9
|
+
|
|
10
|
+
import { type AfLookupProps, useLookup } from "../AfLookup/Lookup.js";
|
|
11
|
+
import { getLocalizedString } from "../i18n.js";
|
|
12
|
+
|
|
13
|
+
export type GridEditLookupProps<
|
|
14
|
+
TBoundData extends Record<string, unknown>,
|
|
15
|
+
TLookupData extends Record<string, unknown>,
|
|
16
|
+
> = DistributiveOmit<AfLookupProps<TLookupData>, "value" | "onChange"> & {
|
|
17
|
+
dataObject: DataObject<TLookupData>;
|
|
18
|
+
/** The fields of the edited row that a picked item sets; non-editable columns are updated directly. */
|
|
19
|
+
getChanges: (selectedItem: TLookupData | null) => Partial<TBoundData>;
|
|
20
|
+
params: GridRenderEditCellParams;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A lookup as a grid cell editor: a read-only input with a search button that opens the
|
|
25
|
+
* lookup dialog anchored to the cell, and a clear button when the field is nullable. Give it
|
|
26
|
+
* to a column's `renderEditCell`. Equivalent of `GridEditLookup` in `@olenbetong/appframe-mui`.
|
|
27
|
+
*/
|
|
28
|
+
export function GridEditLookup<
|
|
29
|
+
TBoundData extends Record<string, unknown>,
|
|
30
|
+
TLookupData extends Record<string, unknown>,
|
|
31
|
+
>({ dataObject, getChanges, params, ...props }: GridEditLookupProps<TBoundData, TLookupData>) {
|
|
32
|
+
let getChangesRef = useRef(getChanges);
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
getChangesRef.current = getChanges;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
let handleChange = useCallback(
|
|
38
|
+
(item: TLookupData | null) => {
|
|
39
|
+
let changes = getChangesRef.current(item);
|
|
40
|
+
let { id, api } = params;
|
|
41
|
+
let nonEditableFields: Record<string, unknown> = {};
|
|
42
|
+
|
|
43
|
+
for (let key of Object.keys(changes)) {
|
|
44
|
+
let column = api.getColumn(key);
|
|
45
|
+
if (column) {
|
|
46
|
+
if (column.editable) {
|
|
47
|
+
api.setEditCellValue({ id, field: key, value: changes[key] });
|
|
48
|
+
} else {
|
|
49
|
+
nonEditableFields[key] = changes[key];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (Object.keys(nonEditableFields).length > 0) {
|
|
55
|
+
api.updateRows([{ [af.data.uid]: id, id, ...nonEditableFields }]);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
[params],
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
let getAnchorElement = useCallback(
|
|
62
|
+
() => params.api.getCellElement(params.id, params.field) ?? null,
|
|
63
|
+
[params.api, params.field, params.id],
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
let { inputId, dialog, anchorName, openDialog, onKeyDown } = useLookup<TLookupData>({
|
|
67
|
+
dataObject,
|
|
68
|
+
onChange: handleChange,
|
|
69
|
+
getAnchorElement,
|
|
70
|
+
...props,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<div className="ObGridEditLookup-root" style={{ anchorName } as React.CSSProperties}>
|
|
75
|
+
<GridEditInputCell
|
|
76
|
+
fullWidth
|
|
77
|
+
slotProps={{
|
|
78
|
+
root: {
|
|
79
|
+
// @ts-expect-error: typed as HTMLElement, but it is the input
|
|
80
|
+
readOnly: true,
|
|
81
|
+
},
|
|
82
|
+
}}
|
|
83
|
+
onKeyDown={onKeyDown}
|
|
84
|
+
className="MuiDataGrid-editInputCell"
|
|
85
|
+
endAdornment={
|
|
86
|
+
<span className="ObGridEditLookup-actions">
|
|
87
|
+
{props.nullable && params.value && (
|
|
88
|
+
<Button
|
|
89
|
+
type="button"
|
|
90
|
+
variant="tertiary"
|
|
91
|
+
data-size="sm"
|
|
92
|
+
icon
|
|
93
|
+
title={getLocalizedString("Clear")}
|
|
94
|
+
onKeyDown={(evt) => evt.stopPropagation()}
|
|
95
|
+
onClick={(evt) => {
|
|
96
|
+
evt.stopPropagation();
|
|
97
|
+
handleChange(null);
|
|
98
|
+
document.getElementById(inputId)?.focus();
|
|
99
|
+
}}
|
|
100
|
+
>
|
|
101
|
+
<XMarkIcon aria-hidden />
|
|
102
|
+
</Button>
|
|
103
|
+
)}
|
|
104
|
+
<Button
|
|
105
|
+
type="button"
|
|
106
|
+
variant="tertiary"
|
|
107
|
+
data-size="sm"
|
|
108
|
+
icon
|
|
109
|
+
title={getLocalizedString("Search")}
|
|
110
|
+
onClick={() => openDialog()}
|
|
111
|
+
>
|
|
112
|
+
<MagnifyingGlassIcon aria-hidden />
|
|
113
|
+
</Button>
|
|
114
|
+
</span>
|
|
115
|
+
}
|
|
116
|
+
{...params}
|
|
117
|
+
/>
|
|
118
|
+
{dialog}
|
|
119
|
+
</div>
|
|
120
|
+
);
|
|
121
|
+
}
|
package/src/grid/index.tsx
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
type GridAggregationFunction,
|
|
10
10
|
type GridApi,
|
|
11
11
|
type GridFilterModel,
|
|
12
|
+
type GridGroupNode,
|
|
12
13
|
useGridApiRef,
|
|
13
14
|
} from "@mui/x-data-grid-premium";
|
|
14
15
|
import { type DataObject, uid } from "@olenbetong/appframe-data";
|
|
@@ -62,6 +63,11 @@ import { useAfSortModel } from "./useAfSortModel.js";
|
|
|
62
63
|
import { getLocalizedString } from "../i18n.js";
|
|
63
64
|
|
|
64
65
|
export * from "./formatters.js";
|
|
66
|
+
export * from "./GridEditLookup.js";
|
|
67
|
+
// MUI X as a namespace (`DataGrid.DataGridPremium`, `DataGrid.GridColDef`, `DataGrid.useGridApiRef`,
|
|
68
|
+
// ...), for grids that are not bound to a data object - from here rather than the app, so a
|
|
69
|
+
// platform app does not bundle a second MUI X. Same shape as appframe-mui exported it.
|
|
70
|
+
export * as DataGrid from "@mui/x-data-grid-premium";
|
|
65
71
|
export * as MuiDataGrid from "@mui/x-data-grid-premium";
|
|
66
72
|
|
|
67
73
|
registerLicense();
|
|
@@ -131,6 +137,11 @@ export {
|
|
|
131
137
|
useAfSortModel,
|
|
132
138
|
};
|
|
133
139
|
|
|
140
|
+
/** MUI's own default: the grand total in the footer, group subtotals inline. */
|
|
141
|
+
function getDefaultAggregationPosition(groupNode: GridGroupNode) {
|
|
142
|
+
return groupNode.depth === -1 ? "footer" : "inline";
|
|
143
|
+
}
|
|
144
|
+
|
|
134
145
|
export type AfGridProps = {
|
|
135
146
|
apiRef?: React.RefObject<GridApi>;
|
|
136
147
|
/**
|
|
@@ -430,6 +441,14 @@ function AfGrid_(props: AfGridProps) {
|
|
|
430
441
|
* the first render.
|
|
431
442
|
*/
|
|
432
443
|
{...(aggregationFunctions ? { aggregationFunctions } : {})}
|
|
444
|
+
/*
|
|
445
|
+
* The server totals arrive after the rows, as new aggregation functions, and the
|
|
446
|
+
* grid only re-aggregates on row, column and model changes: without a nudge the
|
|
447
|
+
* footer kept the totals of the previous load. Its one effect that re-aggregates
|
|
448
|
+
* when the functions change is keyed on this prop and skipped while it is unset,
|
|
449
|
+
* so the default position is passed explicitly.
|
|
450
|
+
*/
|
|
451
|
+
getAggregationPosition={other.getAggregationPosition ?? getDefaultAggregationPosition}
|
|
433
452
|
localeText={{ ...other.localeText, ...AF_GRID_LOCALE_TEXT }}
|
|
434
453
|
slots={{
|
|
435
454
|
...dsBaseSlots,
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, test, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { useAfServerAggregation } from "./useAfServerAggregation.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Just enough of a data object for the hook: fields to aggregate, parameters to copy, a data
|
|
8
|
+
* handler to count retrieves, and events fired the way the real one fires them.
|
|
9
|
+
*/
|
|
10
|
+
function createDataObject() {
|
|
11
|
+
let target = new EventTarget();
|
|
12
|
+
let retrieve = vi.fn(async () => [{ Amount: 100 }]);
|
|
13
|
+
|
|
14
|
+
let dataObject = {
|
|
15
|
+
getFields: (name: string) => (name === "Amount" ? { name } : undefined),
|
|
16
|
+
getParameter: () => undefined,
|
|
17
|
+
dataHandler: { retrieve },
|
|
18
|
+
attachEvent: (name: string, handler: (event: Event) => void, options?: AddEventListenerOptions) =>
|
|
19
|
+
target.addEventListener(name, handler, options),
|
|
20
|
+
fire: (name: string, detail?: unknown) => target.dispatchEvent(new CustomEvent(name, { detail })),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
return { dataObject, retrieve };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const model = { Amount: "sum" };
|
|
27
|
+
|
|
28
|
+
describe("useAfServerAggregation", () => {
|
|
29
|
+
test("reloads the totals when a paged load starts over", async () => {
|
|
30
|
+
let { dataObject, retrieve } = createDataObject();
|
|
31
|
+
renderHook(() => useAfServerAggregation(dataObject as any, model, true));
|
|
32
|
+
|
|
33
|
+
await waitFor(() => expect(retrieve).toHaveBeenCalledTimes(1));
|
|
34
|
+
|
|
35
|
+
act(() => {
|
|
36
|
+
dataObject.fire("onPartialDataLoaded", { skip: 0, count: 50, total: 120 });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
await waitFor(() => expect(retrieve).toHaveBeenCalledTimes(2));
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("leaves the totals alone while later pages of the same load arrive", async () => {
|
|
43
|
+
let { dataObject, retrieve } = createDataObject();
|
|
44
|
+
renderHook(() => useAfServerAggregation(dataObject as any, model, true));
|
|
45
|
+
|
|
46
|
+
await waitFor(() => expect(retrieve).toHaveBeenCalledTimes(1));
|
|
47
|
+
|
|
48
|
+
act(() => {
|
|
49
|
+
dataObject.fire("onPartialDataLoaded", { skip: 50, count: 50, total: 120 });
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
53
|
+
expect(retrieve).toHaveBeenCalledTimes(1);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("reloads the totals after a full load", async () => {
|
|
57
|
+
let { dataObject, retrieve } = createDataObject();
|
|
58
|
+
renderHook(() => useAfServerAggregation(dataObject as any, model, true));
|
|
59
|
+
|
|
60
|
+
await waitFor(() => expect(retrieve).toHaveBeenCalledTimes(1));
|
|
61
|
+
|
|
62
|
+
act(() => {
|
|
63
|
+
dataObject.fire("onDataLoaded", 10);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
await waitFor(() => expect(retrieve).toHaveBeenCalledTimes(2));
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -85,6 +85,19 @@ export function useAfServerAggregation(
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
dataObject.attachEvent("onDataLoaded", reload, { signal: controller.signal });
|
|
88
|
+
/*
|
|
89
|
+
* A dynamically loading data object reports every load page by page and never fires
|
|
90
|
+
* onDataLoaded, so listening for that alone left the totals on whatever the first load
|
|
91
|
+
* saw. The first page of a load is the one that follows a new filter; later pages only
|
|
92
|
+
* scroll further into the same result, which the totals already cover.
|
|
93
|
+
*/
|
|
94
|
+
dataObject.attachEvent(
|
|
95
|
+
"onPartialDataLoaded",
|
|
96
|
+
(event) => {
|
|
97
|
+
if (event.detail.skip === 0) reload();
|
|
98
|
+
},
|
|
99
|
+
{ signal: controller.signal },
|
|
100
|
+
);
|
|
88
101
|
dataObject.attachEvent("onAfterSave", reload, { signal: controller.signal });
|
|
89
102
|
dataObject.attachEvent("onRecordDeleted", reload, { signal: controller.signal });
|
|
90
103
|
|
package/src/index.ts
CHANGED
|
@@ -2,9 +2,11 @@ export * from "./AfLookup/index.js";
|
|
|
2
2
|
export * from "./autocomplete/index.js";
|
|
3
3
|
export * from "./binding/index.js";
|
|
4
4
|
export * from "./container/index.js";
|
|
5
|
+
export * from "./cutoff/index.js";
|
|
5
6
|
export * from "./filter/index.js";
|
|
6
7
|
export * from "./input/index.js";
|
|
7
8
|
export * from "./layout/index.js";
|
|
9
|
+
export * from "./link/index.js";
|
|
8
10
|
export * from "./list/index.js";
|
|
9
11
|
export * from "./page/index.js";
|
|
10
12
|
export * from "./paper/index.js";
|
|
@@ -10,9 +10,11 @@
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
.ObPageContainer-inner {
|
|
13
|
+
--ob-page-container-gutter: 1rem;
|
|
14
|
+
|
|
13
15
|
margin-left: 0;
|
|
14
16
|
height: auto;
|
|
15
|
-
padding: 1rem;
|
|
17
|
+
padding: 1rem var(--ob-page-container-gutter);
|
|
16
18
|
box-sizing: border-box;
|
|
17
19
|
max-width: 100%;
|
|
18
20
|
}
|
|
@@ -20,10 +22,27 @@
|
|
|
20
22
|
.ObPageContainer-fullHeight .ObPageContainer-inner {
|
|
21
23
|
height: 100%;
|
|
22
24
|
}
|
|
25
|
+
|
|
26
|
+
/* The content width on the MUI Container scale; the gutters come on top. */
|
|
27
|
+
.ObPageContainer-inner.ObPageContainer-text {
|
|
28
|
+
max-width: calc(36rem + 2 * var(--ob-page-container-gutter));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.ObPageContainer-inner.ObPageContainer-md {
|
|
32
|
+
max-width: calc(48rem + 2 * var(--ob-page-container-gutter));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.ObPageContainer-inner.ObPageContainer-lg {
|
|
36
|
+
max-width: calc(64rem + 2 * var(--ob-page-container-gutter));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.ObPageContainer-inner.ObPageContainer-xl {
|
|
40
|
+
max-width: calc(80rem + 2 * var(--ob-page-container-gutter));
|
|
41
|
+
}
|
|
23
42
|
}
|
|
24
43
|
|
|
25
44
|
@media screen and (min-width: 600px) {
|
|
26
45
|
.ObPageContainer-inner {
|
|
27
|
-
|
|
46
|
+
--ob-page-container-gutter: 1.5rem;
|
|
28
47
|
}
|
|
29
48
|
}
|