@saas-ui/forms 2.4.1 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -477,6 +477,7 @@ NativeSelect.displayName = "NativeSelect";
477
477
  import { Controller } from "react-hook-form";
478
478
  import { forwardRef as forwardRef7, useMergeRefs } from "@chakra-ui/react";
479
479
  import { callAllHandlers } from "@chakra-ui/utils";
480
+ import { splitProps as splitProps2 } from "@saas-ui/core/utils";
480
481
 
481
482
  // src/base-field.tsx
482
483
  import { get } from "react-hook-form";
@@ -487,18 +488,39 @@ import {
487
488
  FormHelperText,
488
489
  FormErrorMessage
489
490
  } from "@chakra-ui/react";
491
+ import { splitProps } from "@saas-ui/core";
490
492
  import { jsx as jsx9, jsxs as jsxs5 } from "react/jsx-runtime";
491
493
  var getError = (name, formState) => {
492
494
  return get(formState.errors, name);
493
495
  };
494
- var BaseField = (props) => {
495
- const { name, label, help, hideLabel, children, ...controlProps } = props;
496
+ var isTouched = (name, formState) => {
497
+ return get(formState.touchedFields, name);
498
+ };
499
+ var useBaseField = (props) => {
500
+ const [fieldProps] = splitProps(props, ["name", "label", "help", "hideLabel"]);
501
+ const [controlProps] = splitProps(props, [
502
+ "id",
503
+ "isDisabled",
504
+ "isInvalid",
505
+ "isReadOnly",
506
+ "isRequired"
507
+ ]);
496
508
  const { formState } = useFormContext();
497
- const error = getError(name, formState);
509
+ const error = getError(fieldProps.name, formState);
510
+ const touched = isTouched(fieldProps.name, formState);
511
+ return {
512
+ ...fieldProps,
513
+ controlProps,
514
+ error,
515
+ touched
516
+ };
517
+ };
518
+ var BaseField = (props) => {
519
+ const { controlProps, label, help, hideLabel, error } = useBaseField(props);
498
520
  return /* @__PURE__ */ jsxs5(FormControl2, { ...controlProps, isInvalid: !!error, children: [
499
521
  label && !hideLabel ? /* @__PURE__ */ jsx9(FormLabel2, { children: label }) : null,
500
522
  /* @__PURE__ */ jsxs5(Box, { children: [
501
- children,
523
+ props.children,
502
524
  help && !(error == null ? void 0 : error.message) ? /* @__PURE__ */ jsx9(FormHelperText, { children: help }) : null,
503
525
  (error == null ? void 0 : error.message) && /* @__PURE__ */ jsx9(FormErrorMessage, { children: error == null ? void 0 : error.message })
504
526
  ] })
@@ -508,49 +530,41 @@ BaseField.displayName = "BaseField";
508
530
 
509
531
  // src/create-field.tsx
510
532
  import { jsx as jsx10 } from "react/jsx-runtime";
511
- var _createField = (InputComponent, { displayName, hideLabel, BaseField: BaseField2 }) => {
533
+ var _createField = (InputComponent, { displayName, hideLabel, getBaseField: getBaseFieldProp }) => {
512
534
  const Field2 = forwardRef7((props, ref) => {
513
- const {
514
- id,
515
- name,
516
- label,
517
- help,
518
- isDisabled,
519
- isInvalid,
520
- isReadOnly,
521
- isRequired,
522
- rules,
523
- ...inputProps
524
- } = props;
535
+ var _a;
536
+ const { id, name, label, isRequired, rules } = props;
525
537
  const inputRules = {
526
538
  required: isRequired,
527
539
  ...rules
528
540
  };
529
- return /* @__PURE__ */ jsx10(
530
- BaseField2,
541
+ const fieldContext = useFieldsContext();
542
+ const getBaseField = (_a = fieldContext == null ? void 0 : fieldContext.getBaseField) != null ? _a : getBaseFieldProp;
543
+ const { extraProps, BaseField: BaseField2 } = getBaseField();
544
+ const [, inputProps] = splitProps2(
545
+ props,
546
+ [
547
+ "children",
548
+ "name",
549
+ "label",
550
+ "isRequired",
551
+ "isDisabled",
552
+ "isInvalid",
553
+ "isReadOnly",
554
+ "help"
555
+ ].concat(extraProps)
556
+ );
557
+ return /* @__PURE__ */ jsx10(BaseField2, { ...props, children: /* @__PURE__ */ jsx10(
558
+ InputComponent,
531
559
  {
560
+ ref,
532
561
  id,
533
562
  name,
534
- label,
535
- help,
536
- hideLabel,
537
- isDisabled,
538
- isInvalid,
539
- isReadOnly,
540
- isRequired,
541
- children: /* @__PURE__ */ jsx10(
542
- InputComponent,
543
- {
544
- ref,
545
- id,
546
- name,
547
- label: hideLabel ? label : void 0,
548
- rules: inputRules,
549
- ...inputProps
550
- }
551
- )
563
+ label: hideLabel ? label : void 0,
564
+ ...inputProps,
565
+ rules: inputRules
552
566
  }
553
- );
567
+ ) });
554
568
  });
555
569
  Field2.displayName = displayName;
556
570
  return Field2;
@@ -611,7 +625,10 @@ var createField = (component, options) => {
611
625
  const Field2 = _createField(InputComponent, {
612
626
  displayName: `${(_a = component.displayName) != null ? _a : "Custom"}Field`,
613
627
  hideLabel: options == null ? void 0 : options.hideLabel,
614
- BaseField: (options == null ? void 0 : options.BaseField) || BaseField
628
+ getBaseField: () => ({
629
+ extraProps: [],
630
+ BaseField
631
+ })
615
632
  });
616
633
  return Field2;
617
634
  };
@@ -709,16 +726,24 @@ var defaultFieldTypes = {
709
726
 
710
727
  // src/fields-context.tsx
711
728
  import { jsx as jsx12 } from "react/jsx-runtime";
712
- var FieldsContext = React5.createContext(
713
- null
714
- );
729
+ var FieldsContext = React5.createContext(null);
715
730
  var FieldsProvider = (props) => {
716
- const fields = { ...defaultFieldTypes, ...props.value };
717
- return /* @__PURE__ */ jsx12(FieldsContext.Provider, { value: fields, children: props.children });
731
+ const fields = { ...defaultFieldTypes, ...props.value.fields };
732
+ return /* @__PURE__ */ jsx12(
733
+ FieldsContext.Provider,
734
+ {
735
+ value: { fields, getBaseField: props.value.getBaseField },
736
+ children: props.children
737
+ }
738
+ );
739
+ };
740
+ var useFieldsContext = () => {
741
+ return React5.useContext(FieldsContext);
718
742
  };
719
743
  var useField = (type) => {
744
+ var _a;
720
745
  const context = React5.useContext(FieldsContext);
721
- return (context == null ? void 0 : context[type]) || InputField;
746
+ return ((_a = context == null ? void 0 : context.fields) == null ? void 0 : _a[type]) || InputField;
722
747
  };
723
748
 
724
749
  // src/field.tsx
@@ -804,6 +829,7 @@ var useArrayField = ({
804
829
  name,
805
830
  keyName
806
831
  });
832
+ console.log(context);
807
833
  return {
808
834
  ...context,
809
835
  name,
@@ -1365,6 +1391,9 @@ var WatchField = (props) => {
1365
1391
  return props.children(field, form) || null;
1366
1392
  };
1367
1393
 
1394
+ // src/create-form.tsx
1395
+ import { forwardRef as forwardRef14 } from "@chakra-ui/react";
1396
+
1368
1397
  // src/form.tsx
1369
1398
  import * as React13 from "react";
1370
1399
  import { chakra as chakra5, forwardRef as forwardRef13 } from "@chakra-ui/react";
@@ -1459,12 +1488,12 @@ var Form = forwardRef13(
1459
1488
  Form.displayName = "Form";
1460
1489
 
1461
1490
  // src/create-form.tsx
1462
- import { forwardRef as forwardRef14 } from "@chakra-ui/react";
1463
1491
  import { jsx as jsx21 } from "react/jsx-runtime";
1464
1492
  function createForm({
1465
1493
  resolver,
1466
1494
  fieldResolver = objectFieldResolver,
1467
- fields
1495
+ fields,
1496
+ getBaseField
1468
1497
  } = {}) {
1469
1498
  const DefaultForm = forwardRef14(
1470
1499
  (props, ref) => {
@@ -1474,7 +1503,7 @@ function createForm({
1474
1503
  fieldResolver: fieldResolverProp,
1475
1504
  ...rest
1476
1505
  } = props;
1477
- return /* @__PURE__ */ jsx21(FieldsProvider, { value: fields || {}, children: /* @__PURE__ */ jsx21(
1506
+ return /* @__PURE__ */ jsx21(FieldsProvider, { value: { fields, getBaseField }, children: /* @__PURE__ */ jsx21(
1478
1507
  Form,
1479
1508
  {
1480
1509
  ref,
@@ -1496,7 +1525,12 @@ import { forwardRef as forwardRef15 } from "@chakra-ui/react";
1496
1525
  import { StepperProvider } from "@saas-ui/core";
1497
1526
  import { runIfFn as runIfFn2 } from "@chakra-ui/utils";
1498
1527
  import { jsx as jsx22 } from "react/jsx-runtime";
1499
- function createStepForm({ fields, resolver, fieldResolver } = {}) {
1528
+ function createStepForm({
1529
+ fields,
1530
+ resolver,
1531
+ fieldResolver,
1532
+ getBaseField
1533
+ } = {}) {
1500
1534
  const StepForm2 = forwardRef15((props, ref) => {
1501
1535
  const { children, steps, ...rest } = props;
1502
1536
  const stepper = useStepForm({
@@ -1506,7 +1540,7 @@ function createStepForm({ fields, resolver, fieldResolver } = {}) {
1506
1540
  });
1507
1541
  const { getFormProps, ...ctx } = stepper;
1508
1542
  const context = useMemo2(() => ctx, [ctx]);
1509
- return /* @__PURE__ */ jsx22(StepperProvider, { value: context, children: /* @__PURE__ */ jsx22(StepFormProvider, { value: context, children: /* @__PURE__ */ jsx22(FieldsProvider, { value: fields || {}, children: /* @__PURE__ */ jsx22(Form, { ref, ...rest, ...getFormProps(), children: runIfFn2(children, {
1543
+ return /* @__PURE__ */ jsx22(StepperProvider, { value: context, children: /* @__PURE__ */ jsx22(StepFormProvider, { value: context, children: /* @__PURE__ */ jsx22(FieldsProvider, { value: { fields, getBaseField }, children: /* @__PURE__ */ jsx22(Form, { ref, ...rest, ...getFormProps(), children: runIfFn2(children, {
1510
1544
  ...stepper,
1511
1545
  Field,
1512
1546
  FormStep,
@@ -1594,6 +1628,7 @@ export {
1594
1628
  useArrayFieldRemoveButton,
1595
1629
  useArrayFieldRow,
1596
1630
  useArrayFieldRowContext,
1631
+ useBaseField,
1597
1632
  useController,
1598
1633
  useField,
1599
1634
  useFieldArray2 as useFieldArray,