@vritti/quantum-ui 0.1.19 → 0.1.21

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, csrfEndpoint, ...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,7 +249,7 @@ export declare interface FormProps<TFieldValues extends FieldValues = FieldValue
245
249
  showRootError?: boolean;
246
250
  rootErrorPosition?: 'top' | 'bottom';
247
251
  rootErrorClassName?: string;
248
- csrfEndpoint?: string;
252
+ fieldMapping?: FieldMapping;
249
253
  }
250
254
 
251
255
  declare function Label({ className, ...props }: React_2.ComponentProps<typeof LabelPrimitive.Root>) {
package/dist/Form.js CHANGED
@@ -1,9 +1,8 @@
1
1
  import { jsx, jsxs } from 'react/jsx-runtime';
2
2
  import React__default from 'react';
3
- import { C as Checkbox } from './Checkbox.js';
4
3
  import { c as FieldError } from './field.js';
5
4
  import { c as cn } from './utils.js';
6
- import { axios, setCsrfToken, clearCsrfToken } from './utils/axios.js';
5
+ import { C as Checkbox } from './Checkbox.js';
7
6
 
8
7
  var isCheckBoxInput = (element) => element.type === 'checkbox';
9
8
 
@@ -603,6 +602,49 @@ function useController(props) {
603
602
  */
604
603
  const Controller = (props) => props.render(useController(props));
605
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
+
606
648
  function processChildren(children, control) {
607
649
  return React__default.Children.map(children, (child) => {
608
650
  if (!React__default.isValidElement(child)) {
@@ -656,29 +698,24 @@ function Form({
656
698
  showRootError = true,
657
699
  rootErrorPosition = "bottom",
658
700
  rootErrorClassName,
659
- csrfEndpoint,
701
+ fieldMapping,
660
702
  ...props
661
703
  }) {
662
- const handleSubmit = form.handleSubmit(onSubmit);
663
- React__default.useEffect(() => {
664
- if (csrfEndpoint) {
665
- axios.get(csrfEndpoint).then((response) => {
666
- const token = response.data?.csrfToken;
667
- if (token) {
668
- setCsrfToken(token);
669
- } else {
670
- console.warn("[Form] CSRF endpoint did not return a csrfToken field");
671
- }
672
- }).catch((error) => {
673
- console.error("[Form] Failed to fetch CSRF token:", error);
674
- });
675
- }
676
- return () => {
677
- if (csrfEndpoint) {
678
- clearCsrfToken();
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);
679
714
  }
680
- };
681
- }, [csrfEndpoint]);
715
+ },
716
+ [onSubmit, fieldMapping, form, showRootError]
717
+ );
718
+ const handleSubmit = form.handleSubmit(wrappedOnSubmit);
682
719
  const processedChildren = processChildren(children, form.control);
683
720
  return /* @__PURE__ */ jsx(FormProvider, { ...form, children: /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, ...props, children: [
684
721
  showRootError && rootErrorPosition === "top" && form.formState.errors.root && /* @__PURE__ */ jsx(