@douglasneuroinformatics/libui 3.7.6 → 3.8.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.
@@ -1247,9 +1247,10 @@ type FormProps<TSchema extends z.ZodType<FormDataType>, TData extends z.TypeOf<T
1247
1247
  resetBtn?: boolean;
1248
1248
  revalidateOnBlur?: boolean;
1249
1249
  submitBtnLabel?: string;
1250
+ suspendWhileSubmitting?: boolean;
1250
1251
  validationSchema: z.ZodType<TData>;
1251
1252
  };
1252
- declare const Form: <TSchema extends z.ZodType<FormDataType>, TData extends z.TypeOf<TSchema> = z.TypeOf<TSchema>>({ additionalButtons, className, content, fieldsFooter, id, initialValues, onError, onSubmit, preventResetValuesOnReset, readOnly, resetBtn, revalidateOnBlur, submitBtnLabel, validationSchema, ...props }: FormProps<TSchema, TData>) => react_jsx_runtime.JSX.Element;
1253
+ declare const Form: <TSchema extends z.ZodType<FormDataType>, TData extends z.TypeOf<TSchema> = z.TypeOf<TSchema>>({ additionalButtons, className, content, fieldsFooter, id, initialValues, onError, onSubmit, preventResetValuesOnReset, readOnly, resetBtn, revalidateOnBlur, submitBtnLabel, suspendWhileSubmitting, validationSchema, ...props }: FormProps<TSchema, TData>) => react_jsx_runtime.JSX.Element;
1253
1254
 
1254
1255
  type HeadingProps = {
1255
1256
  children: string;
@@ -3445,6 +3445,7 @@ var Form = ({
3445
3445
  resetBtn,
3446
3446
  revalidateOnBlur,
3447
3447
  submitBtnLabel,
3448
+ suspendWhileSubmitting,
3448
3449
  validationSchema,
3449
3450
  ...props
3450
3451
  }) => {
@@ -3454,6 +3455,7 @@ var Form = ({
3454
3455
  const [values, setValues] = useState8(
3455
3456
  initialValues ? getInitialValues(initialValues) : {}
3456
3457
  );
3458
+ const [isSubmitting, setIsSubmitting] = useState8(false);
3457
3459
  const handleError = (error) => {
3458
3460
  const fieldErrors = {};
3459
3461
  for (const issue of error.issues) {
@@ -3481,14 +3483,21 @@ var Form = ({
3481
3483
  }
3482
3484
  };
3483
3485
  const handleSubmit = async (event) => {
3484
- event.preventDefault();
3485
- const result = await validationSchema.safeParseAsync(values);
3486
- if (result.success) {
3487
- reset();
3488
- await onSubmit(result.data);
3489
- } else {
3490
- console.error(result.error.issues);
3491
- handleError(result.error);
3486
+ const minSubmitTime = new Promise((resolve) => setTimeout(resolve, 500));
3487
+ try {
3488
+ setIsSubmitting(true);
3489
+ event.preventDefault();
3490
+ const result = await validationSchema.safeParseAsync(values);
3491
+ if (result.success) {
3492
+ reset();
3493
+ await onSubmit(result.data);
3494
+ } else {
3495
+ console.error(result.error.issues);
3496
+ handleError(result.error);
3497
+ }
3498
+ } finally {
3499
+ await minSubmitTime;
3500
+ setIsSubmitting(false);
3492
3501
  }
3493
3502
  };
3494
3503
  const isGrouped = Array.isArray(content);
@@ -3505,6 +3514,7 @@ var Form = ({
3505
3514
  useEffect10(() => {
3506
3515
  revalidate();
3507
3516
  }, [resolvedLanguage]);
3517
+ const isSuspended = Boolean(suspendWhileSubmitting && isSubmitting);
3508
3518
  return /* @__PURE__ */ jsxs47(
3509
3519
  "form",
3510
3520
  {
@@ -3547,7 +3557,35 @@ var Form = ({
3547
3557
  fieldsFooter,
3548
3558
  /* @__PURE__ */ jsxs47("div", { className: "flex w-full gap-3", children: [
3549
3559
  additionalButtons?.left,
3550
- /* @__PURE__ */ jsx136(Button, { "aria-label": "Submit", className: "block w-full", disabled: readOnly, type: "submit", variant: "primary", children: submitBtnLabel ?? t("form.submit") }),
3560
+ /* @__PURE__ */ jsxs47(
3561
+ Button,
3562
+ {
3563
+ "aria-label": "Submit",
3564
+ className: "flex w-32 items-center justify-center gap-2",
3565
+ disabled: readOnly || isSuspended,
3566
+ type: "submit",
3567
+ variant: "primary",
3568
+ children: [
3569
+ submitBtnLabel ?? t("form.submit"),
3570
+ /* @__PURE__ */ jsx136(
3571
+ "svg",
3572
+ {
3573
+ className: cn("hidden h-4 w-4 animate-spin", isSuspended && "block"),
3574
+ fill: "none",
3575
+ height: "24",
3576
+ stroke: "currentColor",
3577
+ strokeLinecap: "round",
3578
+ strokeLinejoin: "round",
3579
+ strokeWidth: "2",
3580
+ viewBox: "0 0 24 24",
3581
+ width: "24",
3582
+ xmlns: "http://www.w3.org/2000/svg",
3583
+ children: /* @__PURE__ */ jsx136("path", { d: "M21 12a9 9 0 1 1-6.219-8.56" })
3584
+ }
3585
+ )
3586
+ ]
3587
+ }
3588
+ ),
3551
3589
  resetBtn && /* @__PURE__ */ jsx136(
3552
3590
  Button,
3553
3591
  {