@firecms/core 3.5.2-canary.16bf7bb → 3.5.2-canary.a0001bd

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 (51) hide show
  1. package/dist/components/EntityCollectionView/CollectionCountBadge.d.ts +8 -0
  2. package/dist/components/EntityCollectionView/CollectionPathTitle.d.ts +24 -0
  3. package/dist/components/EntityCollectionView/EntityCollectionView.d.ts +5 -0
  4. package/dist/components/EntityCollectionView/EntityIdHeaderWidget.d.ts +12 -0
  5. package/dist/components/EntityCollectionView/utils.d.ts +4 -3
  6. package/dist/{de-CUF9f7S-.js → de-CNfJQcnh.js} +1 -1
  7. package/dist/{de-CUF9f7S-.js.map → de-CNfJQcnh.js.map} +1 -1
  8. package/dist/{editor-WZytSO_1.js → editor-D0A5yaaJ.js} +1 -1
  9. package/dist/{editor-WZytSO_1.js.map → editor-D0A5yaaJ.js.map} +1 -1
  10. package/dist/{fr-DmkMX6hp.js → fr-CBtSvDVp.js} +1 -1
  11. package/dist/{fr-DmkMX6hp.js.map → fr-CBtSvDVp.js.map} +1 -1
  12. package/dist/{hi-BKuaMr7w.js → hi-Delc4l61.js} +1 -1
  13. package/dist/{hi-BKuaMr7w.js.map → hi-Delc4l61.js.map} +1 -1
  14. package/dist/index.es.js +624 -579
  15. package/dist/index.es.js.map +1 -1
  16. package/dist/index.umd.js +616 -571
  17. package/dist/index.umd.js.map +1 -1
  18. package/dist/internal/useLeavePageConfirmation.d.ts +11 -0
  19. package/dist/{it-BwQflFGz.js → it-CdsoaRVO.js} +1 -1
  20. package/dist/{it-BwQflFGz.js.map → it-CdsoaRVO.js.map} +1 -1
  21. package/dist/{pl-BPsAvZvc.js → pl-DoTvWcwp.js} +1 -1
  22. package/dist/{pl-BPsAvZvc.js.map → pl-DoTvWcwp.js.map} +1 -1
  23. package/dist/preview/components/__tests__/DatePreview.test.d.ts +1 -0
  24. package/dist/{pt-D5q7OQp1.js → pt-DCfk8Nkq.js} +1 -1
  25. package/dist/{pt-D5q7OQp1.js.map → pt-DCfk8Nkq.js.map} +1 -1
  26. package/dist/types/locales.d.ts +1 -1
  27. package/dist/types/side_dialogs_controller.d.ts +4 -2
  28. package/dist/util/__tests__/locales.test.d.ts +1 -0
  29. package/dist/util/date_fns_locales.d.ts +13 -0
  30. package/package.json +3 -3
  31. package/src/components/EntityCollectionView/CollectionCountBadge.tsx +17 -0
  32. package/src/components/EntityCollectionView/CollectionPathTitle.tsx +68 -0
  33. package/src/components/EntityCollectionView/EntityCollectionView.tsx +12 -111
  34. package/src/components/EntityCollectionView/EntityIdHeaderWidget.tsx +166 -0
  35. package/src/components/EntityCollectionView/utils.ts +13 -7
  36. package/src/components/common/default_entity_actions.tsx +2 -1
  37. package/src/core/DefaultAppBar.tsx +12 -9
  38. package/src/core/EntityEditView.tsx +26 -1
  39. package/src/core/SideDialogs.tsx +6 -13
  40. package/src/form/field_bindings/BlockFieldBinding.tsx +2 -1
  41. package/src/hooks/useReferenceDialog.tsx +2 -2
  42. package/src/internal/useBuildSideDialogsController.tsx +44 -2
  43. package/src/internal/useLeavePageConfirmation.ts +25 -0
  44. package/src/preview/components/DatePreview.tsx +14 -2
  45. package/src/preview/components/__tests__/DatePreview.test.tsx +39 -0
  46. package/src/types/locales.ts +2 -0
  47. package/src/types/side_dialogs_controller.tsx +4 -2
  48. package/src/util/__tests__/locales.test.ts +46 -0
  49. package/src/util/date_fns_locales.ts +28 -0
  50. package/dist/internal/useUnsavedChangesDialog.d.ts +0 -12
  51. package/src/internal/useUnsavedChangesDialog.tsx +0 -142
@@ -14,6 +14,9 @@ export function useBuildSideDialogsController(): SideDialogsController {
14
14
  const routesStore = useRef<Record<string, SideDialogPanelProps>>({});
15
15
  const routesCount = useRef<number>(0);
16
16
 
17
+ // The history entry each panel without a URL was opened on, by panel key.
18
+ const entryKeysOfPanelsWithoutUrl = useRef<Record<string, string>>({});
19
+
17
20
  const updateSidePanels = (newPanels: SideDialogPanelProps[]) => {
18
21
  sidePanelsRef.current = newPanels;
19
22
  setSidePanels(newPanels);
@@ -22,20 +25,55 @@ export function useBuildSideDialogsController(): SideDialogsController {
22
25
  useEffect(() => {
23
26
  const state = location.state as any;
24
27
  const panelKeys: string[] = state?.panels ?? [];
25
- const newPanels = panelKeys
28
+ const panelsInHistory = panelKeys
26
29
  .map(key => routesStore.current[key])
27
30
  .filter(p => Boolean(p)) as SideDialogPanelProps[];
31
+
32
+ // A panel without a URL, like a reference dialog, pushes no history entry, so the
33
+ // entry under it does not list it, and going back to that entry, as closing a
34
+ // panel opened on top of it does, would drop it. It stays open for as long as
35
+ // what it was opened over does: the panel below it or, with none, the entry itself.
36
+ const keptPanels: SideDialogPanelProps[] = [];
37
+ let matched = 0;
38
+ for (const panel of sidePanelsRef.current) {
39
+ if (panelsInHistory[matched]?.key === panel.key) {
40
+ keptPanels.push(panel);
41
+ matched++;
42
+ } else if (!panel.urlPath &&
43
+ (keptPanels.length > 0 || entryKeysOfPanelsWithoutUrl.current[panel.key] === location.key)) {
44
+ keptPanels.push(panel);
45
+ } else {
46
+ break;
47
+ }
48
+ }
49
+ const newPanels = [...keptPanels, ...panelsInHistory.slice(matched)];
50
+
28
51
  if (!equal(sidePanelsRef.current.map(p => p.key), newPanels.map(p => p.key))) {
29
52
  updateSidePanels(newPanels);
30
53
  }
31
54
  }, [location]);
32
55
 
33
- const close = useCallback(() => {
56
+ const close = useCallback((key?: string) => {
34
57
 
35
58
  const currentPanels = sidePanelsRef.current;
36
59
  if (currentPanels.length === 0)
37
60
  return;
38
61
 
62
+ // A panel can ask to close itself while another one is open on top of it: the
63
+ // reference dialog does when an entity created from it is saved. Closing the top
64
+ // one instead would close that entity, and then the entity's own close would take
65
+ // the panel under the dialog with it. Only a panel without a URL can be taken out
66
+ // of the middle, since it has no history entry of its own. Should two panels share
67
+ // a key (a reference dialog opened from an entity created in another one for the
68
+ // same collection), it is the upper one.
69
+ const index = key !== undefined ? currentPanels.findLastIndex(p => p.key === key) : -1;
70
+ if (index !== -1 && index < currentPanels.length - 1 && !currentPanels[index].urlPath) {
71
+ updateSidePanels(currentPanels.filter((_, i) => i !== index));
72
+ if (routesCount.current > 0)
73
+ routesCount.current--;
74
+ return;
75
+ }
76
+
39
77
  const lastSidePanel = currentPanels[currentPanels.length - 1];
40
78
  const updatedPanels = [...currentPanels.slice(0, -1)];
41
79
  updateSidePanels(updatedPanels);
@@ -65,6 +103,8 @@ export function useBuildSideDialogsController(): SideDialogsController {
65
103
 
66
104
  newPanels.forEach((panel) => {
67
105
  routesStore.current[panel.key] = panel;
106
+ if (!panel.urlPath)
107
+ entryKeysOfPanelsWithoutUrl.current[panel.key] = location.key;
68
108
  });
69
109
  routesCount.current = routesCount.current + newPanels.length;
70
110
 
@@ -95,6 +135,8 @@ export function useBuildSideDialogsController(): SideDialogsController {
95
135
  const newPanels: SideDialogPanelProps[] = Array.isArray(panelProps) ? panelProps : [panelProps];
96
136
  newPanels.forEach((panel) => {
97
137
  routesStore.current[panel.key] = panel;
138
+ if (!panel.urlPath)
139
+ entryKeysOfPanelsWithoutUrl.current[panel.key] = location.key;
98
140
  });
99
141
 
100
142
  const baseLocation = (location.state as any)?.base_location ?? location;
@@ -0,0 +1,25 @@
1
+ import { useEffect } from "react";
2
+
3
+ /**
4
+ * Asks the browser to confirm leaving the page (reload, tab close, a link that
5
+ * loads another document) while `when` is true.
6
+ *
7
+ * Navigation inside the app is not intercepted: unsaved entity values are kept
8
+ * in the dirty entity cache, so they survive it. Cancelling link clicks here
9
+ * used to leave plain and new-tab links doing nothing, with no dialog shown.
10
+ *
11
+ * @param when - Whether leaving the page should be confirmed.
12
+ */
13
+ export function useLeavePageConfirmation(when: boolean): void {
14
+ useEffect(() => {
15
+ if (!when) return;
16
+
17
+ const handleBeforeUnload = (e: BeforeUnloadEvent) => {
18
+ e.preventDefault();
19
+ e.returnValue = "";
20
+ };
21
+
22
+ window.addEventListener("beforeunload", handleBeforeUnload);
23
+ return () => window.removeEventListener("beforeunload", handleBeforeUnload);
24
+ }, [when]);
25
+ }
@@ -2,7 +2,7 @@ import React from "react";
2
2
 
3
3
  import { format } from "date-fns";
4
4
  import { useCustomizationController } from "../../hooks";
5
- import { defaultDateFormat, useDateFnsLocale } from "../../util";
5
+ import { defaultDateFormat, localeToLanguageTag, useDateFnsLocale } from "../../util";
6
6
 
7
7
  export interface DatePreviewProps {
8
8
  date: Date;
@@ -17,6 +17,18 @@ export interface DatePreviewProps {
17
17
  timezone?: string;
18
18
  }
19
19
 
20
+ /**
21
+ * The locale is a date-fns key ("enUS", "ptBR"), which `Intl` refuses: it threw here,
22
+ * and took the field down with it, for every region locale.
23
+ */
24
+ function dateTimeFormat(locale: string | undefined, options: Intl.DateTimeFormatOptions): Intl.DateTimeFormat {
25
+ try {
26
+ return new Intl.DateTimeFormat(locale ? localeToLanguageTag(locale) : "en-US", options);
27
+ } catch {
28
+ return new Intl.DateTimeFormat("en-US", options);
29
+ }
30
+ }
31
+
20
32
  /**
21
33
  * @group Preview components
22
34
  */
@@ -47,7 +59,7 @@ export function DatePreview({
47
59
  options.minute = "2-digit";
48
60
  }
49
61
 
50
- const formatter = new Intl.DateTimeFormat(customizationController?.locale ?? "en-US", options);
62
+ const formatter = dateTimeFormat(customizationController?.locale, options);
51
63
  const formattedDate = formatter.format(date);
52
64
 
53
65
  // Get timezone abbreviation
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ */
4
+ import { describe, expect, it } from "@jest/globals";
5
+ import React from "react";
6
+ import { render } from "@testing-library/react";
7
+
8
+ import { DatePreview } from "../DatePreview";
9
+ import { CustomizationControllerContext } from "../../../contexts/CustomizationControllerContext";
10
+ import { CustomizationController } from "../../../types";
11
+
12
+ // FireCMS locales are date-fns keys. Intl.DateTimeFormat threw on a region key such as
13
+ // "enUS", so a date with a timezone took its whole field down for those projects.
14
+
15
+ function renderWithLocale(locale: string | undefined) {
16
+ return render(
17
+ <CustomizationControllerContext.Provider value={{ locale } as CustomizationController}>
18
+ <DatePreview date={new Date("2026-11-10T09:30:00Z")} mode="date" timezone="Europe/Madrid"/>
19
+ </CustomizationControllerContext.Provider>
20
+ );
21
+ }
22
+
23
+ describe("DatePreview with a timezone", () => {
24
+
25
+ it("formats in a region locale's language tag", () => {
26
+ const { container } = renderWithLocale("ptBR");
27
+ expect(container.textContent).toContain("2026");
28
+ expect(container.textContent).toContain("nov");
29
+ });
30
+
31
+ it("formats with a plain language key", () => {
32
+ expect(renderWithLocale("es").container.textContent).toContain("2026");
33
+ expect(renderWithLocale(undefined).container.textContent).toContain("Nov 10, 2026");
34
+ });
35
+
36
+ it("falls back to en-US for a stored value Intl can't read", () => {
37
+ expect(renderWithLocale("en_US").container.textContent).toContain("Nov 10, 2026");
38
+ });
39
+ });
@@ -17,6 +17,8 @@ export type Locale =
17
17
  "da" |
18
18
  "de" |
19
19
  "el" |
20
+ // date-fns's built-in English (en-US), what FireCMS Cloud stores for English
21
+ "en" |
20
22
  "enAU" |
21
23
  "enCA" |
22
24
  "enGB" |
@@ -8,9 +8,11 @@ import { EntityFormProps } from "../form";
8
8
  export interface SideDialogsController {
9
9
 
10
10
  /**
11
- * Close the last panel
11
+ * Close the last panel.
12
+ * When the `key` of a panel without a URL is given, that panel is closed
13
+ * instead, even if other panels are open on top of it.
12
14
  */
13
- close: () => void;
15
+ close: (key?: string) => void;
14
16
 
15
17
  /**
16
18
  * List of side panels currently open
@@ -0,0 +1,46 @@
1
+ import { describe, expect, it } from "@jest/globals";
2
+ import { localeToLanguageTag, resolveLocale } from "../date_fns_locales";
3
+
4
+ // FireCMS locales are date-fns keys ("enUS", "ptBR"). On 2026-11-10 FireCMS Cloud stored
5
+ // "en_US" through the MCP server, and a key such as "enUS" itself made
6
+ // Intl.DateTimeFormat throw in DatePreview.
7
+
8
+ describe("resolveLocale", () => {
9
+
10
+ it("keeps the keys as they are", () => {
11
+ expect(resolveLocale("es")).toBe("es");
12
+ expect(resolveLocale("ptBR")).toBe("ptBR");
13
+ expect(resolveLocale("srLatn")).toBe("srLatn");
14
+ expect(resolveLocale("en")).toBe("en");
15
+ });
16
+
17
+ it("reads a language tag in any spelling as its key", () => {
18
+ expect(resolveLocale("en_US")).toBe("enUS");
19
+ expect(resolveLocale("en-US")).toBe("enUS");
20
+ expect(resolveLocale("EN-us")).toBe("enUS");
21
+ expect(resolveLocale("pt-BR")).toBe("ptBR");
22
+ expect(resolveLocale("sr-Latn")).toBe("srLatn");
23
+ expect(resolveLocale(" de ")).toBe("de");
24
+ });
25
+
26
+ it("falls back to the language for a region date-fns doesn't have", () => {
27
+ expect(resolveLocale("es-MX")).toBe("es");
28
+ expect(resolveLocale("en-ZA")).toBe("en");
29
+ });
30
+
31
+ it("gives nothing for what isn't a locale", () => {
32
+ for (const value of ["", " ", "xx", "klingon", "not a locale", 42, null, undefined, {}]) {
33
+ expect(resolveLocale(value)).toBeUndefined();
34
+ }
35
+ });
36
+ });
37
+
38
+ describe("localeToLanguageTag", () => {
39
+
40
+ it("turns region and script keys into tags Intl accepts", () => {
41
+ for (const [key, tag] of [["enUS", "en-US"], ["ptBR", "pt-BR"], ["srLatn", "sr-Latn"], ["faIR", "fa-IR"], ["es", "es"], ["en", "en"]]) {
42
+ expect(localeToLanguageTag(key)).toBe(tag);
43
+ expect(() => new Intl.DateTimeFormat(localeToLanguageTag(key))).not.toThrow();
44
+ }
45
+ });
46
+ });
@@ -166,3 +166,31 @@ export function useDateFnsLocale(locale?: Locale): DateFnsLocale | undefined {
166
166
 
167
167
  return locale ? cache.get(locale) : undefined;
168
168
  }
169
+
170
+ /** Every value of {@link Locale}, folded to lower case without separators. */
171
+ const supportedLocales = new Map<string, Locale>(
172
+ ([...Object.keys(loaders), "en", "fil"] as Locale[]).map((locale) => [locale.toLowerCase(), locale])
173
+ );
174
+
175
+ /**
176
+ * The {@link Locale} a stored or typed value stands for, or undefined.
177
+ *
178
+ * Locales are date-fns keys ("enUS", "ptBR"), but the same setting is often written as
179
+ * a language tag ("en-US", "pt_BR"). Both spellings resolve to the key; a region
180
+ * date-fns doesn't have falls back to its language ("es-MX" to "es").
181
+ */
182
+ export function resolveLocale(value: unknown): Locale | undefined {
183
+ if (typeof value !== "string") return undefined;
184
+ const parts = value.trim().split(/[-_\s]+/).filter(Boolean);
185
+ if (parts.length === 0) return undefined;
186
+ return supportedLocales.get(parts.join("").toLowerCase())
187
+ ?? supportedLocales.get(parts[0].toLowerCase());
188
+ }
189
+
190
+ /**
191
+ * The BCP 47 language tag of a locale key, for `Intl`: "ptBR" is "pt-BR" and "srLatn"
192
+ * is "sr-Latn". `Intl.DateTimeFormat` throws on the key itself.
193
+ */
194
+ export function localeToLanguageTag(locale: string): string {
195
+ return locale.replace(/^([a-z]{2,3})([A-Z][A-Za-z]+)$/, "$1-$2");
196
+ }
@@ -1,12 +0,0 @@
1
- /**
2
- * Custom hook to handle navigation blocking when there are unsaved changes.
3
- *
4
- * @param when - Indicates whether to block navigation.
5
- * @param onSuccess - Callback invoked when navigation is confirmed.
6
- * @returns An object containing the state of navigation blocking and handlers.
7
- */
8
- export declare function useNavigationUnsavedChangesDialog(when: boolean, onSuccess: () => void): {
9
- navigationWasBlocked: boolean;
10
- handleCancel: () => void;
11
- handleOk: () => void;
12
- };
@@ -1,142 +0,0 @@
1
- import React, { useCallback, useEffect, useState } from "react";
2
- import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Typography } from "@firecms/ui";
3
-
4
- /**
5
- * Type representing a pending navigation action.
6
- */
7
- type PendingNavigation =
8
- | {
9
- type: "popstate";
10
- delta: number;
11
- }
12
- | {
13
- type: "link";
14
- href: string;
15
- }
16
- | null;
17
-
18
- /**
19
- * Custom hook to handle navigation blocking when there are unsaved changes.
20
- *
21
- * @param when - Indicates whether to block navigation.
22
- * @param onSuccess - Callback invoked when navigation is confirmed.
23
- * @returns An object containing the state of navigation blocking and handlers.
24
- */
25
- export function useNavigationUnsavedChangesDialog(
26
- when: boolean,
27
- onSuccess: () => void
28
- ): {
29
- navigationWasBlocked: boolean;
30
- handleCancel: () => void;
31
- handleOk: () => void;
32
- } {
33
- const [isDialogOpen, setIsDialogOpen] = useState<boolean>(false);
34
- const [pendingNavigation, setPendingNavigation] = useState<PendingNavigation>(null);
35
-
36
- /**
37
- * Handler to cancel the navigation attempt.
38
- */
39
- const handleCancel = useCallback(() => {
40
- setIsDialogOpen(false);
41
- setPendingNavigation(null);
42
- }, []);
43
-
44
- /**
45
- * Handler to confirm and proceed with the navigation.
46
- */
47
- const handleOk = useCallback(() => {
48
- setIsDialogOpen(false);
49
- if (pendingNavigation) {
50
- onSuccess();
51
- if (pendingNavigation.type === "popstate") {
52
- window.history.go(pendingNavigation.delta);
53
- } else if (pendingNavigation.type === "link") {
54
- window.location.href = pendingNavigation.href;
55
- }
56
- setPendingNavigation(null);
57
- }
58
- }, [onSuccess, pendingNavigation]);
59
-
60
- /**
61
- * Event handler for beforeunload to handle page refresh or tab close.
62
- */
63
- const handleBeforeUnload = useCallback(
64
- (e: BeforeUnloadEvent) => {
65
- if (when) {
66
- e.preventDefault();
67
- e.returnValue = "";
68
- }
69
- },
70
- [when]
71
- );
72
-
73
- /**
74
- * Event handler for popstate to handle back and forward browser buttons.
75
- */
76
- const handlePopState = useCallback(
77
- (e: PopStateEvent) => {
78
- if (when) {
79
- e.preventDefault();
80
- // Assuming backward navigation; adjust delta as needed
81
- setPendingNavigation({
82
- type: "popstate",
83
- delta: -1
84
- });
85
- setIsDialogOpen(true);
86
- }
87
- },
88
- [when]
89
- );
90
-
91
- /**
92
- * Event handler to intercept link clicks within the application.
93
- */
94
- const handleLinkClick = useCallback(
95
- (e: MouseEvent) => {
96
- if (!when) return;
97
-
98
- const target = e.target as HTMLElement;
99
- const anchor = target.closest("a[href]") as HTMLAnchorElement | null;
100
- if (anchor && anchor.host === window.location.host) {
101
- e.preventDefault();
102
- const href = anchor.getAttribute("href");
103
- if (href) {
104
- setPendingNavigation({
105
- type: "link",
106
- href
107
- });
108
- setIsDialogOpen(true);
109
- }
110
- }
111
- },
112
- [when]
113
- );
114
-
115
- /**
116
- * Effect hook to add and clean up event listeners based on the `when` condition.
117
- */
118
- useEffect(() => {
119
- if (when) {
120
- window.addEventListener("beforeunload", handleBeforeUnload);
121
- window.addEventListener("popstate", handlePopState);
122
- document.addEventListener("click", handleLinkClick);
123
- } else {
124
- window.removeEventListener("beforeunload", handleBeforeUnload);
125
- window.removeEventListener("popstate", handlePopState);
126
- document.removeEventListener("click", handleLinkClick);
127
- }
128
-
129
- // Cleanup on unmount or when `when` changes
130
- return () => {
131
- window.removeEventListener("beforeunload", handleBeforeUnload);
132
- window.removeEventListener("popstate", handlePopState);
133
- document.removeEventListener("click", handleLinkClick);
134
- };
135
- }, [when, handleBeforeUnload, handlePopState, handleLinkClick]);
136
-
137
- return {
138
- navigationWasBlocked: isDialogOpen,
139
- handleCancel,
140
- handleOk,
141
- };
142
- }