@vritti/quantum-ui 0.1.20 → 0.1.22

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/Checkbox.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
2
  import * as React from 'react';
3
3
  import React__default from 'react';
4
- import { F as Field, h as FieldContent, a as FieldLabel, b as FieldDescription, c as FieldError } from './field.js';
4
+ import { F as Field, a as FieldContent, e as FieldLabel, b as FieldDescription, c as FieldError } from './field.js';
5
5
  import { u as useComposedRefs } from './index2.js';
6
6
  import { c as createContextScope } from './index4.js';
7
7
  import { P as Primitive } from './index5.js';
package/dist/Form.d.ts CHANGED
@@ -151,6 +151,10 @@ export declare function FieldLegend({
151
151
  )
152
152
  }
153
153
 
154
+ declare interface FieldMapping {
155
+ [apiField: string]: string;
156
+ }
157
+
154
158
  export declare function FieldSeparator({
155
159
  children,
156
160
  className,
@@ -232,7 +236,7 @@ declare const fieldVariants = cva(
232
236
  }
233
237
  );
234
238
 
235
- export declare function Form<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = TFieldValues>({ form, onSubmit, children, showRootError, rootErrorPosition, rootErrorClassName, ...props }: FormProps<TFieldValues, TContext, TTransformedValues>): JSX.Element;
239
+ export declare function Form<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = TFieldValues>({ form, onSubmit, children, showRootError, rootErrorPosition, rootErrorClassName, fieldMapping, ...props }: FormProps<TFieldValues, TContext, TTransformedValues>): JSX.Element;
236
240
 
237
241
  export declare namespace Form {
238
242
  var displayName: string;
@@ -245,6 +249,7 @@ export declare interface FormProps<TFieldValues extends FieldValues = FieldValue
245
249
  showRootError?: boolean;
246
250
  rootErrorPosition?: 'top' | 'bottom';
247
251
  rootErrorClassName?: string;
252
+ fieldMapping?: FieldMapping;
248
253
  }
249
254
 
250
255
  declare function Label({ className, ...props }: React_2.ComponentProps<typeof LabelPrimitive.Root>) {
package/dist/Form.js CHANGED
@@ -602,6 +602,49 @@ function useController(props) {
602
602
  */
603
603
  const Controller = (props) => props.render(useController(props));
604
604
 
605
+ function mapApiErrorsToForm(error, form, options = {}) {
606
+ const {
607
+ fieldMapping = {},
608
+ setRootError = true
609
+ } = options;
610
+ if (!error || typeof error !== "object") {
611
+ if (setRootError) {
612
+ form.setError("root", {
613
+ type: "manual",
614
+ message: "An error occurred"
615
+ });
616
+ }
617
+ return;
618
+ }
619
+ const errorData = error?.response?.data || error;
620
+ const apiError = errorData;
621
+ const generalMessage = apiError.message || apiError.error;
622
+ let hasFieldErrors = false;
623
+ if (apiError.errors && Array.isArray(apiError.errors)) {
624
+ for (const errorItem of apiError.errors) {
625
+ if (errorItem.field) {
626
+ const formField = fieldMapping[errorItem.field] || errorItem.field;
627
+ form.setError(formField, {
628
+ type: "manual",
629
+ message: errorItem.message
630
+ });
631
+ hasFieldErrors = true;
632
+ } else if (errorItem.message && setRootError) {
633
+ form.setError("root", {
634
+ type: "manual",
635
+ message: errorItem.message
636
+ });
637
+ }
638
+ }
639
+ }
640
+ if (!hasFieldErrors && generalMessage && setRootError) {
641
+ form.setError("root", {
642
+ type: "manual",
643
+ message: generalMessage
644
+ });
645
+ }
646
+ }
647
+
605
648
  function processChildren(children, control) {
606
649
  return React__default.Children.map(children, (child) => {
607
650
  if (!React__default.isValidElement(child)) {
@@ -655,9 +698,24 @@ function Form({
655
698
  showRootError = true,
656
699
  rootErrorPosition = "bottom",
657
700
  rootErrorClassName,
701
+ fieldMapping,
658
702
  ...props
659
703
  }) {
660
- const handleSubmit = form.handleSubmit(onSubmit);
704
+ const wrappedOnSubmit = React__default.useCallback(
705
+ async (data) => {
706
+ try {
707
+ await onSubmit(data);
708
+ } catch (error) {
709
+ mapApiErrorsToForm(error, form, {
710
+ fieldMapping,
711
+ setRootError: showRootError
712
+ });
713
+ console.error("[Form Submission Error]", error);
714
+ }
715
+ },
716
+ [onSubmit, fieldMapping, form, showRootError]
717
+ );
718
+ const handleSubmit = form.handleSubmit(wrappedOnSubmit);
661
719
  const processedChildren = processChildren(children, form.control);
662
720
  return /* @__PURE__ */ jsx(FormProvider, { ...form, children: /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, ...props, children: [
663
721
  showRootError && rootErrorPosition === "top" && form.formState.errors.root && /* @__PURE__ */ jsx(