@panneau/forms 5.0.0-alpha.27 → 5.0.0-alpha.29

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
@@ -6,13 +6,14 @@ import { ErrorSectionsContext, FormContext, RevealContext, createRevealRegistry
6
6
  import { FormBinding } from "./FormBinding.js";
7
7
  import { cx } from "./cx.js";
8
8
  import { FormSkeleton } from "./FormSkeleton.js";
9
- import { FieldFlow, LayoutNodes } from "./layout/Layout.js";
9
+ import { SaveContext, SaveControls, SummaryContext } from "./panels/SaveControls.js";
10
+ import { PanelsContext, builtInPanels } from "./panels/panels.js";
11
+ import { FieldFlow, LayoutNodes, SidebarNode } from "./layout/Layout.js";
10
12
  import { useBeforeUnloadGuard, useLeaveConfirmation } from "./unsaved.js";
11
13
  import { c } from "react/compiler-runtime";
12
14
  import { getLayoutFieldNames, getProblem, resolveFields, selectVisible, useLeaveGuard, usePanneau, usePlugins } from "@panneau/core";
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
+ import { Alert, Aside, AsideLayout, Button, isApplePlatform, usePanneauIntl, useToast, useToastOffset } from "@panneau/ui";
16
+ import { jsx, jsxs } from "react/jsx-runtime";
16
17
  import { useRef, useState } from "react";
17
18
  import { FieldsProvider, builtInFields } from "@panneau/fields";
18
19
  //#region src/FormView.tsx
@@ -62,8 +63,8 @@ function isSaveShortcut(event, apple) {
62
63
  * collapsed section that holds an error opens, and the first error is focused.
63
64
  */
64
65
  function FormView(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;
66
+ const $ = c(48);
67
+ const { form, fields, layout: t1, locales, relationSource, uploadSource, checkSource, components, panels, submitLabel, actions, onSubmitted, onReload, guardUnsavedChanges: t2, showSavedToast: t3, saved: t4, isLoading: t5, id, "aria-label": ariaLabel, className, layoutClassName, sidebarClassName, sidebarStorageKey, footerClassName, summaryClassName } = t0;
67
68
  const layout = t1 === void 0 ? [] : t1;
68
69
  const guardUnsavedChanges = t2 === void 0 ? true : t2;
69
70
  const showSavedToast = t3 === void 0 ? true : t3;
@@ -104,24 +105,49 @@ function FormView(t0) {
104
105
  const sidebar = resolved.layout.filter(t6);
105
106
  const order = [...getLayoutFieldNames(main), ...getLayoutFieldNames(sidebar.flatMap(_temp2))];
106
107
  let t7;
107
- if ($[2] !== plugins.fields) {
108
- t7 = Object.fromEntries(plugins.fields);
109
- $[2] = plugins.fields;
110
- $[3] = t7;
111
- } else t7 = $[3];
112
- const t8 = t7;
108
+ if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
109
+ t7 = Object.entries(builtInPanels);
110
+ $[2] = t7;
111
+ } else t7 = $[2];
112
+ const t8 = plugins.panels;
113
113
  let t9;
114
- if ($[4] !== components || $[5] !== t8) {
115
- t9 = {
116
- ...builtInFields,
114
+ if ($[3] !== panels) {
115
+ t9 = Object.entries(panels ?? {});
116
+ $[3] = panels;
117
+ $[4] = t9;
118
+ } else t9 = $[4];
119
+ let t10;
120
+ if ($[5] !== t8 || $[6] !== t9) {
121
+ t10 = new Map([
122
+ ...t7,
117
123
  ...t8,
118
- ...components
119
- };
120
- $[4] = components;
124
+ ...t9
125
+ ]);
121
126
  $[5] = t8;
122
127
  $[6] = t9;
123
- } else t9 = $[6];
124
- const entries = t9;
128
+ $[7] = t10;
129
+ } else t10 = $[7];
130
+ const panelEntries = t10;
131
+ const hasPanel = (name) => sidebar.some((node_2) => node_2.panel === name);
132
+ let t11;
133
+ if ($[8] !== plugins.fields) {
134
+ t11 = Object.fromEntries(plugins.fields);
135
+ $[8] = plugins.fields;
136
+ $[9] = t11;
137
+ } else t11 = $[9];
138
+ const t12 = t11;
139
+ let t13;
140
+ if ($[10] !== components || $[11] !== t12) {
141
+ t13 = {
142
+ ...builtInFields,
143
+ ...t12,
144
+ ...components
145
+ };
146
+ $[10] = components;
147
+ $[11] = t12;
148
+ $[12] = t13;
149
+ } else t13 = $[12];
150
+ const entries = t13;
125
151
  const goTo = (path) => {
126
152
  registry.reveal(path);
127
153
  const show = (frames) => {
@@ -163,33 +189,52 @@ function FormView(t0) {
163
189
  setResolving(true);
164
190
  await form.resolveConflict(choice).then(handle).finally(() => setResolving(false));
165
191
  };
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) => {
192
+ let t14;
193
+ if ($[13] === Symbol.for("react.memo_cache_sentinel")) {
194
+ const apple = isApplePlatform();
195
+ t14 = (event) => {
175
196
  if (!isSaveShortcut(event, apple) || !(event.target instanceof Node) || !event.currentTarget.contains(event.target)) return;
176
197
  event.preventDefault();
177
198
  event.currentTarget.requestSubmit();
178
199
  };
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;
200
+ $[13] = t14;
201
+ } else t14 = $[13];
202
+ const onKeyDown = t14;
203
+ let t15;
204
+ if ($[14] !== state.error || $[15] !== state.status) {
205
+ t15 = state.status === "error" ? getProblem(state.error) : null;
206
+ $[14] = state.error;
207
+ $[15] = state.status;
208
+ $[16] = t15;
209
+ } else t15 = $[16];
210
+ const problem = t15;
191
211
  const errorDetail = problem?.detail ?? (problem === null && state.error instanceof Error ? state.error.message : void 0);
192
212
  const showSummary = state.status === "invalid" && state.submitCount > 0;
213
+ const orderedErrors = showSummary ? orderErrors(order, state.errors, formLocales) : {};
214
+ const t16 = state.status === "error" ? "error" : state.dirty ? "unsaved" : savedShown && state.status !== "submitting" ? "saved" : null;
215
+ const t17 = state.status === "submitting";
216
+ let t18;
217
+ if ($[17] !== intl || $[18] !== submitLabel) {
218
+ t18 = submitLabel === void 0 ? intl.formatMessage(messages.save) : submitLabel;
219
+ $[17] = intl;
220
+ $[18] = submitLabel;
221
+ $[19] = t18;
222
+ } else t18 = $[19];
223
+ let t19;
224
+ if ($[20] !== actions || $[21] !== t16 || $[22] !== t17 || $[23] !== t18) {
225
+ t19 = {
226
+ status: t16,
227
+ isSubmitting: t17,
228
+ actions,
229
+ submitLabel: t18
230
+ };
231
+ $[20] = actions;
232
+ $[21] = t16;
233
+ $[22] = t17;
234
+ $[23] = t18;
235
+ $[24] = t19;
236
+ } else t19 = $[24];
237
+ const save = t19;
193
238
  if (isLoading) return /* @__PURE__ */ jsx(FormSkeleton, {
194
239
  main: laidOut,
195
240
  flow,
@@ -201,33 +246,33 @@ function FormView(t0) {
201
246
  sidebarClassName,
202
247
  footerClassName
203
248
  });
204
- let t13;
205
- if ($[13] !== relationSource) {
206
- t13 = relationSource !== void 0 ? { relationSource } : {};
207
- $[13] = relationSource;
208
- $[14] = t13;
209
- } else t13 = $[14];
210
- let t14;
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, {
249
+ let t20;
250
+ if ($[25] !== relationSource) {
251
+ t20 = relationSource !== void 0 ? { relationSource } : {};
252
+ $[25] = relationSource;
253
+ $[26] = t20;
254
+ } else t20 = $[26];
255
+ let t21;
256
+ if ($[27] !== uploadSource) {
257
+ t21 = uploadSource !== void 0 ? { uploadSource } : {};
258
+ $[27] = uploadSource;
259
+ $[28] = t21;
260
+ } else t21 = $[28];
261
+ let t22;
262
+ if ($[29] !== checkSource) {
263
+ t22 = checkSource !== void 0 ? { checkSource } : {};
264
+ $[29] = checkSource;
265
+ $[30] = t22;
266
+ } else t22 = $[30];
267
+ let t23;
268
+ if ($[31] !== className) {
269
+ t23 = cx(Form_module_default.form, className);
270
+ $[31] = className;
271
+ $[32] = t23;
272
+ } else t23 = $[32];
273
+ let t24;
274
+ if ($[33] !== errorDetail || $[34] !== intl || $[35] !== state.status) {
275
+ t24 = state.status === "error" ? /* @__PURE__ */ jsx(Alert, {
231
276
  tone: "danger",
232
277
  role: "alert",
233
278
  title: intl.formatMessage(messages.saveFailed),
@@ -238,156 +283,128 @@ function FormView(t0) {
238
283
  }),
239
284
  children: errorDetail
240
285
  }) : null;
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,
258
- className: cx(Form_module_default.footer, footerClassName),
259
- children: [
260
- /* @__PURE__ */ jsx("span", {
261
- className: cx(Form_module_default.status, state.status === "error" && Form_module_default.statusError),
262
- role: "status",
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
- }),
265
- actions,
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
- })
275
- }) : null
276
- ]
277
- }) : null;
278
- $[28] = actions;
279
- $[29] = apple;
280
- $[30] = footerClassName;
281
- $[31] = hasFooter;
282
- $[32] = intl;
283
- $[33] = savedShown;
284
- $[34] = state.dirty;
286
+ $[33] = errorDetail;
287
+ $[34] = intl;
285
288
  $[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;
289
+ $[36] = t24;
290
+ } else t24 = $[36];
291
+ const t25 = state.status === "conflict" || resolving;
292
+ const t26 = state.conflict?.current ?? null;
293
+ let t27;
294
+ let t28;
295
+ if ($[37] !== resolve) {
296
+ t27 = () => void resolve("reload");
297
+ t28 = () => void resolve("overwrite");
298
+ $[37] = resolve;
299
+ $[38] = t27;
300
+ $[39] = t28;
299
301
  } else {
300
- t23 = $[39];
301
- t24 = $[40];
302
+ t27 = $[38];
303
+ t28 = $[39];
302
304
  }
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,
305
+ let t29;
306
+ if ($[40] !== resolved.fields || $[41] !== resolving || $[42] !== state.values || $[43] !== t25 || $[44] !== t26 || $[45] !== t27 || $[46] !== t28) {
307
+ t29 = /* @__PURE__ */ jsx(ConflictDialog, {
308
+ isOpen: t25,
307
309
  fields: resolved.fields,
308
310
  values: state.values,
309
- current: t22,
311
+ current: t26,
310
312
  isPending: resolving,
311
- onReload: t23,
312
- onOverwrite: t24
313
+ onReload: t27,
314
+ onOverwrite: t28
313
315
  });
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];
316
+ $[40] = resolved.fields;
317
+ $[41] = resolving;
318
+ $[42] = state.values;
319
+ $[43] = t25;
320
+ $[44] = t26;
321
+ $[45] = t27;
322
+ $[46] = t28;
323
+ $[47] = t29;
324
+ } else t29 = $[47];
323
325
  return /* @__PURE__ */ jsx(FormContext, {
324
326
  value: form,
325
- children: /* @__PURE__ */ jsx(RevealContext, {
326
- value: registry,
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", {
359
- className: Form_module_default.stack,
360
- children: [/* @__PURE__ */ jsx(LayoutNodes, {
361
- nodes: laidOut,
362
- fields: resolved.fieldsByName
363
- }), flow.length > 0 ? /* @__PURE__ */ jsx(FieldFlow, {
364
- nodes: flow,
365
- fields: resolved.fieldsByName
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
- ]
327
+ children: /* @__PURE__ */ jsx(PanelsContext, {
328
+ value: panelEntries,
329
+ children: /* @__PURE__ */ jsx(SaveContext, {
330
+ value: save,
331
+ children: /* @__PURE__ */ jsx(SummaryContext, {
332
+ value: {
333
+ errors: orderedErrors,
334
+ fields: resolved.fields,
335
+ onSelect: goTo
336
+ },
337
+ children: /* @__PURE__ */ jsx(RevealContext, {
338
+ value: registry,
339
+ children: /* @__PURE__ */ jsx(ErrorSectionsContext, {
340
+ value: errorSections,
341
+ children: /* @__PURE__ */ jsxs(FieldsProvider, {
342
+ components: entries,
343
+ locales: formLocales,
344
+ ...t20,
345
+ ...t21,
346
+ ...t22,
347
+ binding: FormBinding,
348
+ children: [
349
+ /* @__PURE__ */ jsxs("form", {
350
+ ref: root,
351
+ id,
352
+ "aria-label": ariaLabel,
353
+ noValidate: true,
354
+ className: t23,
355
+ onSubmit: (event_0) => {
356
+ event_0.preventDefault();
357
+ form.submit().then(handle);
358
+ },
359
+ onKeyDown,
360
+ children: [
361
+ showSummary ? /* @__PURE__ */ jsx(ErrorSummary, {
362
+ errors: orderedErrors,
363
+ fields: resolved.fields,
364
+ onSelect: goTo,
365
+ className: cx(summaryClassName ?? Form_module_default.summary, hasPanel("errors") && Form_module_default.besideAsideHidden)
366
+ }) : null,
367
+ t24,
368
+ /* @__PURE__ */ jsxs(AsideLayout, {
369
+ className: layoutClassName,
370
+ mainClassName: Form_module_default.stack,
371
+ aside: sidebar.length > 0 ? /* @__PURE__ */ jsx(Aside, {
372
+ label: intl.formatMessage(messages.sidebar),
373
+ className: sidebarClassName,
374
+ children: sidebar.map((node_4, index) => /* @__PURE__ */ jsx(SidebarNode, {
375
+ node: node_4,
376
+ fields: resolved.fieldsByName,
377
+ storageKey: sidebarStorageKey
378
+ }, index))
379
+ }) : null,
380
+ children: [/* @__PURE__ */ jsx(LayoutNodes, {
381
+ nodes: laidOut,
382
+ fields: resolved.fieldsByName
383
+ }), flow.length > 0 ? /* @__PURE__ */ jsx(FieldFlow, {
384
+ nodes: flow,
385
+ fields: resolved.fieldsByName
386
+ }) : null]
387
+ }),
388
+ hasFooter ? /* @__PURE__ */ jsx("div", {
389
+ ref: footer,
390
+ className: cx(Form_module_default.footer, hasPanel("save") && Form_module_default.besideAsideHidden, footerClassName),
391
+ children: /* @__PURE__ */ jsx(SaveControls, { ...save })
392
+ }) : null
393
+ ]
394
+ }),
395
+ t29,
396
+ leave.dialog
397
+ ]
398
+ })
399
+ })
400
+ })
384
401
  })
385
402
  })
386
403
  })
387
404
  });
388
405
  }
389
- function _temp3(node_2) {
390
- return node_2.type === "sidebar";
406
+ function _temp3(node_3) {
407
+ return node_3.type === "sidebar";
391
408
  }
392
409
  function _temp2(node_1) {
393
410
  return node_1.children ?? [];