@panneau/forms 5.0.0-alpha.17 → 5.0.0-alpha.19

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/dist/FormView.js CHANGED
@@ -2,7 +2,7 @@ import { messages } from "./messages.js";
2
2
  import { ConflictDialog } from "./ConflictDialog.js";
3
3
  import Form_module_default from "./Form.module.js";
4
4
  import { ErrorSummary } from "./ErrorSummary.js";
5
- import { FormContext, RevealContext, createRevealRegistry, useFormState } from "./context.js";
5
+ import { ErrorSectionsContext, FormContext, RevealContext, createRevealRegistry as createRevealRegistry$1, useFormState } from "./context.js";
6
6
  import { FormBinding } from "./FormBinding.js";
7
7
  import { cx } from "./cx.js";
8
8
  import { FormSkeleton } from "./FormSkeleton.js";
@@ -10,8 +10,9 @@ import { FieldFlow, LayoutNodes } from "./layout/Layout.js";
10
10
  import { useBeforeUnloadGuard, useLeaveConfirmation } from "./unsaved.js";
11
11
  import { c } from "react/compiler-runtime";
12
12
  import { getLayoutFieldNames, getProblem, resolveFields, selectVisible, useLeaveGuard, usePanneau, usePlugins } from "@panneau/core";
13
- import { Alert, Button, usePanneauIntl, useToast } from "@panneau/ui";
14
- import { jsx, jsxs } from "react/jsx-runtime";
13
+ import { Alert, Button, Icon, Tooltip, isApplePlatform, usePanneauIntl, useToast, useToastOffset } from "@panneau/ui";
14
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
15
+ import { CircleCheck } from "lucide-react";
15
16
  import { useRef, useState } from "react";
16
17
  import { FieldsProvider, builtInFields } from "@panneau/fields";
17
18
  //#region src/FormView.tsx
@@ -47,27 +48,41 @@ function orderErrors(names, errors, locales = []) {
47
48
  function isUnplacedField(node, placed) {
48
49
  return node?.type === "field" && node.name !== void 0 && !placed.has(node.name);
49
50
  }
51
+ /** Whether a key press saves: ⌘ Enter on Apple platforms, Ctrl Enter elsewhere. */
52
+ function isSaveShortcut(event, apple) {
53
+ return event.key === "Enter" && (apple ? event.metaKey : event.ctrlKey) && !event.altKey && !event.shiftKey && !event.repeat && !event.nativeEvent.isComposing;
54
+ }
50
55
  /**
51
56
  * Renders a form: its fields in their layout, bound to the form state of `@panneau/core`, with
52
57
  * an error summary, a scroll to the first error, error counts on tabs and sections, a guard for
53
58
  * unsaved changes and the dialog of version conflicts. Saving is the job of the form's
54
59
  * `onSubmit`.
60
+ *
61
+ * ⌘ Enter (Ctrl Enter elsewhere) saves from any of its fields. After a failed save, every
62
+ * collapsed section that holds an error opens, and the first error is focused.
55
63
  */
56
64
  function FormView(t0) {
57
- const $ = c(43);
58
- const { form, fields, layout: t1, locales, relationSource, uploadSource, checkSource, components, submitLabel, actions, onSubmitted, onReload, guardUnsavedChanges: t2, showSavedToast: t3, isLoading: t4, id, "aria-label": ariaLabel, className, layoutClassName, sidebarClassName, footerClassName, summaryClassName } = t0;
65
+ const $ = c(49);
66
+ const { form, fields, layout: t1, locales, relationSource, uploadSource, checkSource, components, submitLabel, actions, onSubmitted, onReload, guardUnsavedChanges: t2, showSavedToast: t3, saved: t4, isLoading: t5, id, "aria-label": ariaLabel, className, layoutClassName, sidebarClassName, footerClassName, summaryClassName } = t0;
59
67
  const layout = t1 === void 0 ? [] : t1;
60
68
  const guardUnsavedChanges = t2 === void 0 ? true : t2;
61
69
  const showSavedToast = t3 === void 0 ? true : t3;
62
- const isLoading = t4 === void 0 ? false : t4;
70
+ const saved = t4 === void 0 ? false : t4;
71
+ const isLoading = t5 === void 0 ? false : t5;
63
72
  const intl = usePanneauIntl();
64
73
  const toasts = useToast();
65
74
  const { definition } = usePanneau();
66
75
  const plugins = usePlugins();
67
76
  const state = useFormState(form);
68
- const [registry] = useState(createRevealRegistry);
77
+ const [registry] = useState(createRevealRegistry$1);
78
+ const [errorSections] = useState(createRevealRegistry$1);
69
79
  const [resolving, setResolving] = useState(false);
70
80
  const root = useRef(null);
81
+ const footer = useRef(null);
82
+ const hasFooter = submitLabel !== null || actions !== void 0;
83
+ useToastOffset(footer, hasFooter && !isLoading);
84
+ const [savedShown, setSavedShown] = useState(saved);
85
+ if (savedShown && state.dirty) setSavedShown(false);
71
86
  const guarded = guardUnsavedChanges && state.dirty;
72
87
  useBeforeUnloadGuard(guarded);
73
88
  const leave = useLeaveConfirmation(guarded);
@@ -80,33 +95,33 @@ function FormView(t0) {
80
95
  while (split > 0 && isUnplacedField(main[split - 1], placed)) split = split - 1;
81
96
  const flow = main.slice(split);
82
97
  const laidOut = main.slice(0, split);
83
- let t5;
98
+ let t6;
84
99
  if ($[0] !== state) {
85
- t5 = (node_0) => node_0.type === "sidebar" && selectVisible(state, node_0);
100
+ t6 = (node_0) => node_0.type === "sidebar" && selectVisible(state, node_0);
86
101
  $[0] = state;
87
- $[1] = t5;
88
- } else t5 = $[1];
89
- const sidebar = resolved.layout.filter(t5);
102
+ $[1] = t6;
103
+ } else t6 = $[1];
104
+ const sidebar = resolved.layout.filter(t6);
90
105
  const order = [...getLayoutFieldNames(main), ...getLayoutFieldNames(sidebar.flatMap(_temp2))];
91
- let t6;
106
+ let t7;
92
107
  if ($[2] !== plugins.fields) {
93
- t6 = Object.fromEntries(plugins.fields);
108
+ t7 = Object.fromEntries(plugins.fields);
94
109
  $[2] = plugins.fields;
95
- $[3] = t6;
96
- } else t6 = $[3];
97
- const t7 = t6;
98
- let t8;
99
- if ($[4] !== components || $[5] !== t7) {
100
- t8 = {
110
+ $[3] = t7;
111
+ } else t7 = $[3];
112
+ const t8 = t7;
113
+ let t9;
114
+ if ($[4] !== components || $[5] !== t8) {
115
+ t9 = {
101
116
  ...builtInFields,
102
- ...t7,
117
+ ...t8,
103
118
  ...components
104
119
  };
105
120
  $[4] = components;
106
- $[5] = t7;
107
- $[6] = t8;
108
- } else t8 = $[6];
109
- const entries = t8;
121
+ $[5] = t8;
122
+ $[6] = t9;
123
+ } else t9 = $[6];
124
+ const entries = t9;
110
125
  const goTo = (path) => {
111
126
  registry.reveal(path);
112
127
  const show = (frames) => {
@@ -128,22 +143,17 @@ function FormView(t0) {
128
143
  const handle = (result) => {
129
144
  if (result === null) return;
130
145
  if (result.status === "invalid") {
146
+ Object.keys(result.errors).forEach((path_0) => errorSections.reveal(path_0));
131
147
  const first = Object.keys(orderErrors(order, result.errors, formLocales))[0];
132
148
  if (first !== void 0) goTo(first);
133
149
  } else if (result.status === "submitted") {
134
- if (showSavedToast) toasts.show({
150
+ setSavedShown(true);
151
+ if (showSavedToast && !hasFooter) toasts.show({
135
152
  title: intl.formatMessage(messages.saved),
136
153
  tone: "success"
137
- });
154
+ }, { key: "panneau-form-saved" });
138
155
  onSubmitted?.(result.result);
139
- } else if (result.status === "error") toasts.show({
140
- title: intl.formatMessage(messages.saveFailed),
141
- tone: "danger",
142
- action: {
143
- label: intl.formatMessage(messages.retry),
144
- onAction: () => void form.submit().then(handle)
145
- }
146
- }, { timeout: 8e3 });
156
+ }
147
157
  };
148
158
  const resolve = async (choice) => {
149
159
  if (choice === "reload" && state.conflict?.current === null && onReload !== void 0) {
@@ -153,14 +163,31 @@ function FormView(t0) {
153
163
  setResolving(true);
154
164
  await form.resolveConflict(choice).then(handle).finally(() => setResolving(false));
155
165
  };
156
- let t9;
157
- if ($[7] !== state.error || $[8] !== state.status) {
158
- t9 = state.status === "error" ? getProblem(state.error) : null;
159
- $[7] = state.error;
160
- $[8] = state.status;
161
- $[9] = t9;
162
- } else t9 = $[9];
163
- const problem = t9;
166
+ let t10;
167
+ if ($[7] === Symbol.for("react.memo_cache_sentinel")) {
168
+ t10 = isApplePlatform();
169
+ $[7] = t10;
170
+ } else t10 = $[7];
171
+ const apple = t10;
172
+ let t11;
173
+ if ($[8] !== apple) {
174
+ t11 = (event) => {
175
+ if (!isSaveShortcut(event, apple) || !(event.target instanceof Node) || !event.currentTarget.contains(event.target)) return;
176
+ event.preventDefault();
177
+ event.currentTarget.requestSubmit();
178
+ };
179
+ $[8] = apple;
180
+ $[9] = t11;
181
+ } else t11 = $[9];
182
+ const onKeyDown = t11;
183
+ let t12;
184
+ if ($[10] !== state.error || $[11] !== state.status) {
185
+ t12 = state.status === "error" ? getProblem(state.error) : null;
186
+ $[10] = state.error;
187
+ $[11] = state.status;
188
+ $[12] = t12;
189
+ } else t12 = $[12];
190
+ const problem = t12;
164
191
  const errorDetail = problem?.detail ?? (problem === null && state.error instanceof Error ? state.error.message : void 0);
165
192
  const showSummary = state.status === "invalid" && state.submitCount > 0;
166
193
  if (isLoading) return /* @__PURE__ */ jsx(FormSkeleton, {
@@ -168,39 +195,39 @@ function FormView(t0) {
168
195
  flow,
169
196
  sidebar: resolved.layout.filter(_temp3),
170
197
  fields: resolved.fieldsByName,
171
- footer: submitLabel !== null || actions !== void 0,
198
+ footer: hasFooter,
172
199
  className,
173
200
  layoutClassName,
174
201
  sidebarClassName,
175
202
  footerClassName
176
203
  });
177
- let t10;
178
- if ($[10] !== relationSource) {
179
- t10 = relationSource !== void 0 ? { relationSource } : {};
180
- $[10] = relationSource;
181
- $[11] = t10;
182
- } else t10 = $[11];
183
- let t11;
184
- if ($[12] !== uploadSource) {
185
- t11 = uploadSource !== void 0 ? { uploadSource } : {};
186
- $[12] = uploadSource;
187
- $[13] = t11;
188
- } else t11 = $[13];
189
- let t12;
190
- if ($[14] !== checkSource) {
191
- t12 = checkSource !== void 0 ? { checkSource } : {};
192
- $[14] = checkSource;
193
- $[15] = t12;
194
- } else t12 = $[15];
195
204
  let t13;
196
- if ($[16] !== className) {
197
- t13 = cx(Form_module_default.form, className);
198
- $[16] = className;
199
- $[17] = t13;
200
- } else t13 = $[17];
205
+ if ($[13] !== relationSource) {
206
+ t13 = relationSource !== void 0 ? { relationSource } : {};
207
+ $[13] = relationSource;
208
+ $[14] = t13;
209
+ } else t13 = $[14];
201
210
  let t14;
202
- if ($[18] !== errorDetail || $[19] !== intl || $[20] !== state.status) {
203
- t14 = state.status === "error" ? /* @__PURE__ */ jsx(Alert, {
211
+ if ($[15] !== uploadSource) {
212
+ t14 = uploadSource !== void 0 ? { uploadSource } : {};
213
+ $[15] = uploadSource;
214
+ $[16] = t14;
215
+ } else t14 = $[16];
216
+ let t15;
217
+ if ($[17] !== checkSource) {
218
+ t15 = checkSource !== void 0 ? { checkSource } : {};
219
+ $[17] = checkSource;
220
+ $[18] = t15;
221
+ } else t15 = $[18];
222
+ let t16;
223
+ if ($[19] !== className) {
224
+ t16 = cx(Form_module_default.form, className);
225
+ $[19] = className;
226
+ $[20] = t16;
227
+ } else t16 = $[20];
228
+ let t17;
229
+ if ($[21] !== errorDetail || $[22] !== intl || $[23] !== state.status) {
230
+ t17 = state.status === "error" ? /* @__PURE__ */ jsx(Alert, {
204
231
  tone: "danger",
205
232
  role: "alert",
206
233
  title: intl.formatMessage(messages.saveFailed),
@@ -211,138 +238,150 @@ function FormView(t0) {
211
238
  }),
212
239
  children: errorDetail
213
240
  }) : null;
214
- $[18] = errorDetail;
215
- $[19] = intl;
216
- $[20] = state.status;
217
- $[21] = t14;
218
- } else t14 = $[21];
219
- const t15 = sidebar.length > 0 && Form_module_default.withSidebar;
220
- let t16;
221
- if ($[22] !== layoutClassName || $[23] !== t15) {
222
- t16 = cx(Form_module_default.layout, t15, layoutClassName);
223
- $[22] = layoutClassName;
224
- $[23] = t15;
225
- $[24] = t16;
226
- } else t16 = $[24];
227
- let t17;
228
- if ($[25] !== actions || $[26] !== footerClassName || $[27] !== intl || $[28] !== state.dirty || $[29] !== state.status || $[30] !== submitLabel) {
229
- t17 = submitLabel !== null || actions !== void 0 ? /* @__PURE__ */ jsxs("div", {
241
+ $[21] = errorDetail;
242
+ $[22] = intl;
243
+ $[23] = state.status;
244
+ $[24] = t17;
245
+ } else t17 = $[24];
246
+ const t18 = sidebar.length > 0 && Form_module_default.withSidebar;
247
+ let t19;
248
+ if ($[25] !== layoutClassName || $[26] !== t18) {
249
+ t19 = cx(Form_module_default.layout, t18, layoutClassName);
250
+ $[25] = layoutClassName;
251
+ $[26] = t18;
252
+ $[27] = t19;
253
+ } else t19 = $[27];
254
+ let t20;
255
+ if ($[28] !== actions || $[29] !== apple || $[30] !== footerClassName || $[31] !== hasFooter || $[32] !== intl || $[33] !== savedShown || $[34] !== state.dirty || $[35] !== state.status || $[36] !== submitLabel) {
256
+ t20 = hasFooter ? /* @__PURE__ */ jsxs("div", {
257
+ ref: footer,
230
258
  className: cx(Form_module_default.footer, footerClassName),
231
259
  children: [
232
- state.dirty ? /* @__PURE__ */ jsx("span", {
233
- className: Form_module_default.dirty,
260
+ /* @__PURE__ */ jsx("span", {
261
+ className: cx(Form_module_default.status, state.status === "error" && Form_module_default.statusError),
234
262
  role: "status",
235
- children: intl.formatMessage(messages.unsaved)
236
- }) : null,
263
+ children: state.status === "error" ? intl.formatMessage(messages.saveFailed) : state.dirty ? intl.formatMessage(messages.unsaved) : savedShown && state.status !== "submitting" ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Icon, { icon: CircleCheck }), intl.formatMessage(messages.saved)] }) : null
264
+ }),
237
265
  actions,
238
- submitLabel !== null ? /* @__PURE__ */ jsx(Button, {
239
- type: "submit",
240
- variant: "primary",
241
- isPending: state.status === "submitting",
242
- children: submitLabel ?? intl.formatMessage(messages.save)
266
+ submitLabel !== null ? /* @__PURE__ */ jsx(Tooltip, {
267
+ content: intl.formatMessage(messages.saveShortcut, { modifier: apple ? "⌘" : "Ctrl" }),
268
+ children: /* @__PURE__ */ jsx(Button, {
269
+ type: "submit",
270
+ variant: "primary",
271
+ isPending: state.status === "submitting",
272
+ "aria-keyshortcuts": apple ? "Meta+Enter" : "Control+Enter",
273
+ children: submitLabel ?? intl.formatMessage(messages.save)
274
+ })
243
275
  }) : null
244
276
  ]
245
277
  }) : null;
246
- $[25] = actions;
247
- $[26] = footerClassName;
248
- $[27] = intl;
249
- $[28] = state.dirty;
250
- $[29] = state.status;
251
- $[30] = submitLabel;
252
- $[31] = t17;
253
- } else t17 = $[31];
254
- const t18 = state.status === "conflict" || resolving;
255
- const t19 = state.conflict?.current ?? null;
256
- let t20;
257
- let t21;
258
- if ($[32] !== resolve) {
259
- t20 = () => void resolve("reload");
260
- t21 = () => void resolve("overwrite");
261
- $[32] = resolve;
262
- $[33] = t20;
263
- $[34] = t21;
278
+ $[28] = actions;
279
+ $[29] = apple;
280
+ $[30] = footerClassName;
281
+ $[31] = hasFooter;
282
+ $[32] = intl;
283
+ $[33] = savedShown;
284
+ $[34] = state.dirty;
285
+ $[35] = state.status;
286
+ $[36] = submitLabel;
287
+ $[37] = t20;
288
+ } else t20 = $[37];
289
+ const t21 = state.status === "conflict" || resolving;
290
+ const t22 = state.conflict?.current ?? null;
291
+ let t23;
292
+ let t24;
293
+ if ($[38] !== resolve) {
294
+ t23 = () => void resolve("reload");
295
+ t24 = () => void resolve("overwrite");
296
+ $[38] = resolve;
297
+ $[39] = t23;
298
+ $[40] = t24;
264
299
  } else {
265
- t20 = $[33];
266
- t21 = $[34];
300
+ t23 = $[39];
301
+ t24 = $[40];
267
302
  }
268
- let t22;
269
- if ($[35] !== resolved.fields || $[36] !== resolving || $[37] !== state.values || $[38] !== t18 || $[39] !== t19 || $[40] !== t20 || $[41] !== t21) {
270
- t22 = /* @__PURE__ */ jsx(ConflictDialog, {
271
- isOpen: t18,
303
+ let t25;
304
+ if ($[41] !== resolved.fields || $[42] !== resolving || $[43] !== state.values || $[44] !== t21 || $[45] !== t22 || $[46] !== t23 || $[47] !== t24) {
305
+ t25 = /* @__PURE__ */ jsx(ConflictDialog, {
306
+ isOpen: t21,
272
307
  fields: resolved.fields,
273
308
  values: state.values,
274
- current: t19,
309
+ current: t22,
275
310
  isPending: resolving,
276
- onReload: t20,
277
- onOverwrite: t21
311
+ onReload: t23,
312
+ onOverwrite: t24
278
313
  });
279
- $[35] = resolved.fields;
280
- $[36] = resolving;
281
- $[37] = state.values;
282
- $[38] = t18;
283
- $[39] = t19;
284
- $[40] = t20;
285
- $[41] = t21;
286
- $[42] = t22;
287
- } else t22 = $[42];
314
+ $[41] = resolved.fields;
315
+ $[42] = resolving;
316
+ $[43] = state.values;
317
+ $[44] = t21;
318
+ $[45] = t22;
319
+ $[46] = t23;
320
+ $[47] = t24;
321
+ $[48] = t25;
322
+ } else t25 = $[48];
288
323
  return /* @__PURE__ */ jsx(FormContext, {
289
324
  value: form,
290
325
  children: /* @__PURE__ */ jsx(RevealContext, {
291
326
  value: registry,
292
- children: /* @__PURE__ */ jsxs(FieldsProvider, {
293
- components: entries,
294
- locales: formLocales,
295
- ...t10,
296
- ...t11,
297
- ...t12,
298
- binding: FormBinding,
299
- children: [
300
- /* @__PURE__ */ jsxs("form", {
301
- ref: root,
302
- id,
303
- "aria-label": ariaLabel,
304
- noValidate: true,
305
- className: t13,
306
- onSubmit: (event) => {
307
- event.preventDefault();
308
- form.submit().then(handle);
309
- },
310
- children: [
311
- showSummary ? /* @__PURE__ */ jsx(ErrorSummary, {
312
- errors: orderErrors(order, state.errors, formLocales),
313
- fields: resolved.fields,
314
- onSelect: goTo,
315
- ...summaryClassName !== void 0 ? { className: summaryClassName } : {}
316
- }) : null,
317
- t14,
318
- /* @__PURE__ */ jsxs("div", {
319
- className: t16,
320
- children: [/* @__PURE__ */ jsxs("div", {
321
- className: Form_module_default.stack,
322
- children: [/* @__PURE__ */ jsx(LayoutNodes, {
323
- nodes: laidOut,
324
- fields: resolved.fieldsByName
325
- }), flow.length > 0 ? /* @__PURE__ */ jsx(FieldFlow, {
326
- nodes: flow,
327
- fields: resolved.fieldsByName
328
- }) : null]
329
- }), sidebar.length > 0 ? /* @__PURE__ */ jsx("aside", {
330
- className: cx(Form_module_default.sidebar, sidebarClassName),
331
- children: sidebar.map((node_3, index) => /* @__PURE__ */ jsx("div", {
327
+ children: /* @__PURE__ */ jsx(ErrorSectionsContext, {
328
+ value: errorSections,
329
+ children: /* @__PURE__ */ jsxs(FieldsProvider, {
330
+ components: entries,
331
+ locales: formLocales,
332
+ ...t13,
333
+ ...t14,
334
+ ...t15,
335
+ binding: FormBinding,
336
+ children: [
337
+ /* @__PURE__ */ jsxs("form", {
338
+ ref: root,
339
+ id,
340
+ "aria-label": ariaLabel,
341
+ noValidate: true,
342
+ className: t16,
343
+ onSubmit: (event_0) => {
344
+ event_0.preventDefault();
345
+ form.submit().then(handle);
346
+ },
347
+ onKeyDown,
348
+ children: [
349
+ showSummary ? /* @__PURE__ */ jsx(ErrorSummary, {
350
+ errors: orderErrors(order, state.errors, formLocales),
351
+ fields: resolved.fields,
352
+ onSelect: goTo,
353
+ ...summaryClassName !== void 0 ? { className: summaryClassName } : {}
354
+ }) : null,
355
+ t17,
356
+ /* @__PURE__ */ jsxs("div", {
357
+ className: t19,
358
+ children: [/* @__PURE__ */ jsxs("div", {
332
359
  className: Form_module_default.stack,
333
- children: /* @__PURE__ */ jsx(LayoutNodes, {
334
- nodes: node_3.children ?? [],
360
+ children: [/* @__PURE__ */ jsx(LayoutNodes, {
361
+ nodes: laidOut,
362
+ fields: resolved.fieldsByName
363
+ }), flow.length > 0 ? /* @__PURE__ */ jsx(FieldFlow, {
364
+ nodes: flow,
335
365
  fields: resolved.fieldsByName
336
- })
337
- }, index))
338
- }) : null]
339
- }),
340
- t17
341
- ]
342
- }),
343
- t22,
344
- leave.dialog
345
- ]
366
+ }) : null]
367
+ }), sidebar.length > 0 ? /* @__PURE__ */ jsx("aside", {
368
+ className: cx(Form_module_default.sidebar, sidebarClassName),
369
+ children: sidebar.map((node_3, index) => /* @__PURE__ */ jsx("div", {
370
+ className: Form_module_default.stack,
371
+ children: /* @__PURE__ */ jsx(LayoutNodes, {
372
+ nodes: node_3.children ?? [],
373
+ fields: resolved.fieldsByName
374
+ })
375
+ }, index))
376
+ }) : null]
377
+ }),
378
+ t20
379
+ ]
380
+ }),
381
+ t25,
382
+ leave.dialog
383
+ ]
384
+ })
346
385
  })
347
386
  })
348
387
  });