@saas-ui/forms 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -93,6 +93,7 @@ __export(src_exports, {
93
93
  useArrayFieldRemoveButton: () => useArrayFieldRemoveButton,
94
94
  useArrayFieldRow: () => useArrayFieldRow,
95
95
  useArrayFieldRowContext: () => useArrayFieldRowContext,
96
+ useBaseField: () => useBaseField,
96
97
  useController: () => import_react_hook_form9.useController,
97
98
  useField: () => useField,
98
99
  useFieldArray: () => import_react_hook_form9.useFieldArray,
@@ -530,22 +531,44 @@ NativeSelect.displayName = "NativeSelect";
530
531
  var import_react_hook_form3 = require("react-hook-form");
531
532
  var import_react12 = require("@chakra-ui/react");
532
533
  var import_utils4 = require("@chakra-ui/utils");
534
+ var import_utils5 = require("@saas-ui/core/utils");
533
535
 
534
536
  // src/base-field.tsx
535
537
  var import_react_hook_form2 = require("react-hook-form");
536
538
  var import_react11 = require("@chakra-ui/react");
539
+ var import_core3 = require("@saas-ui/core");
537
540
  var import_jsx_runtime9 = require("react/jsx-runtime");
538
541
  var getError = (name, formState) => {
539
542
  return (0, import_react_hook_form2.get)(formState.errors, name);
540
543
  };
541
- var BaseField = (props) => {
542
- const { name, label, help, hideLabel, children, ...controlProps } = props;
544
+ var isTouched = (name, formState) => {
545
+ return (0, import_react_hook_form2.get)(formState.touchedFields, name);
546
+ };
547
+ var useBaseField = (props) => {
548
+ const [fieldProps] = (0, import_core3.splitProps)(props, ["name", "label", "help", "hideLabel"]);
549
+ const [controlProps] = (0, import_core3.splitProps)(props, [
550
+ "id",
551
+ "isDisabled",
552
+ "isInvalid",
553
+ "isReadOnly",
554
+ "isRequired"
555
+ ]);
543
556
  const { formState } = useFormContext();
544
- const error = getError(name, formState);
557
+ const error = getError(fieldProps.name, formState);
558
+ const touched = isTouched(fieldProps.name, formState);
559
+ return {
560
+ ...fieldProps,
561
+ controlProps,
562
+ error,
563
+ touched
564
+ };
565
+ };
566
+ var BaseField = (props) => {
567
+ const { controlProps, label, help, hideLabel, error } = useBaseField(props);
545
568
  return /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(import_react11.FormControl, { ...controlProps, isInvalid: !!error, children: [
546
569
  label && !hideLabel ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(import_react11.FormLabel, { children: label }) : null,
547
570
  /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(import_react11.Box, { children: [
548
- children,
571
+ props.children,
549
572
  help && !(error == null ? void 0 : error.message) ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(import_react11.FormHelperText, { children: help }) : null,
550
573
  (error == null ? void 0 : error.message) && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(import_react11.FormErrorMessage, { children: error == null ? void 0 : error.message })
551
574
  ] })
@@ -555,49 +578,41 @@ BaseField.displayName = "BaseField";
555
578
 
556
579
  // src/create-field.tsx
557
580
  var import_jsx_runtime10 = require("react/jsx-runtime");
558
- var _createField = (InputComponent, { displayName, hideLabel, BaseField: BaseField2 }) => {
581
+ var _createField = (InputComponent, { displayName, hideLabel, getBaseField: getBaseFieldProp }) => {
559
582
  const Field2 = (0, import_react12.forwardRef)((props, ref) => {
560
- const {
561
- id,
562
- name,
563
- label,
564
- help,
565
- isDisabled,
566
- isInvalid,
567
- isReadOnly,
568
- isRequired,
569
- rules,
570
- ...inputProps
571
- } = props;
583
+ var _a;
584
+ const { id, name, label, isRequired, rules } = props;
572
585
  const inputRules = {
573
586
  required: isRequired,
574
587
  ...rules
575
588
  };
576
- return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
577
- BaseField2,
589
+ const fieldContext = useFieldsContext();
590
+ const getBaseField = (_a = fieldContext == null ? void 0 : fieldContext.getBaseField) != null ? _a : getBaseFieldProp;
591
+ const { extraProps, BaseField: BaseField2 } = getBaseField();
592
+ const [, inputProps] = (0, import_utils5.splitProps)(
593
+ props,
594
+ [
595
+ "children",
596
+ "name",
597
+ "label",
598
+ "isRequired",
599
+ "isDisabled",
600
+ "isInvalid",
601
+ "isReadOnly",
602
+ "help"
603
+ ].concat(extraProps)
604
+ );
605
+ return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(BaseField2, { ...props, children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
606
+ InputComponent,
578
607
  {
608
+ ref,
579
609
  id,
580
610
  name,
581
- label,
582
- help,
583
- hideLabel,
584
- isDisabled,
585
- isInvalid,
586
- isReadOnly,
587
- isRequired,
588
- children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
589
- InputComponent,
590
- {
591
- ref,
592
- id,
593
- name,
594
- label: hideLabel ? label : void 0,
595
- rules: inputRules,
596
- ...inputProps
597
- }
598
- )
611
+ label: hideLabel ? label : void 0,
612
+ ...inputProps,
613
+ rules: inputRules
599
614
  }
600
- );
615
+ ) });
601
616
  });
602
617
  Field2.displayName = displayName;
603
618
  return Field2;
@@ -658,7 +673,10 @@ var createField = (component, options) => {
658
673
  const Field2 = _createField(InputComponent, {
659
674
  displayName: `${(_a = component.displayName) != null ? _a : "Custom"}Field`,
660
675
  hideLabel: options == null ? void 0 : options.hideLabel,
661
- BaseField: (options == null ? void 0 : options.BaseField) || BaseField
676
+ getBaseField: () => ({
677
+ extraProps: [],
678
+ BaseField
679
+ })
662
680
  });
663
681
  return Field2;
664
682
  };
@@ -756,16 +774,24 @@ var defaultFieldTypes = {
756
774
 
757
775
  // src/fields-context.tsx
758
776
  var import_jsx_runtime12 = require("react/jsx-runtime");
759
- var FieldsContext = import_react14.default.createContext(
760
- null
761
- );
777
+ var FieldsContext = import_react14.default.createContext(null);
762
778
  var FieldsProvider = (props) => {
763
- const fields = { ...defaultFieldTypes, ...props.value };
764
- return /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(FieldsContext.Provider, { value: fields, children: props.children });
779
+ const fields = { ...defaultFieldTypes, ...props.value.fields };
780
+ return /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(
781
+ FieldsContext.Provider,
782
+ {
783
+ value: { fields, getBaseField: props.value.getBaseField },
784
+ children: props.children
785
+ }
786
+ );
787
+ };
788
+ var useFieldsContext = () => {
789
+ return import_react14.default.useContext(FieldsContext);
765
790
  };
766
791
  var useField = (type) => {
792
+ var _a;
767
793
  const context = import_react14.default.useContext(FieldsContext);
768
- return (context == null ? void 0 : context[type]) || InputField;
794
+ return ((_a = context == null ? void 0 : context.fields) == null ? void 0 : _a[type]) || InputField;
769
795
  };
770
796
 
771
797
  // src/field.tsx
@@ -785,7 +811,7 @@ var React9 = __toESM(require("react"));
785
811
 
786
812
  // src/layout.tsx
787
813
  var import_react15 = require("@chakra-ui/react");
788
- var import_utils5 = require("@chakra-ui/utils");
814
+ var import_utils6 = require("@chakra-ui/utils");
789
815
  var import_jsx_runtime14 = require("react/jsx-runtime");
790
816
  var FormLayoutItem = ({ children }) => {
791
817
  return /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(import_react15.chakra.div, { children });
@@ -805,7 +831,7 @@ var FormLayout = ({ children, ...props }) => {
805
831
  import_react15.SimpleGrid,
806
832
  {
807
833
  ...gridProps,
808
- className: (0, import_utils5.cx)("sui-form__layout", props.className),
834
+ className: (0, import_utils6.cx)("sui-form__layout", props.className),
809
835
  children
810
836
  }
811
837
  );
@@ -840,6 +866,7 @@ var useArrayField = ({
840
866
  name,
841
867
  keyName
842
868
  });
869
+ console.log(context);
843
870
  return {
844
871
  ...context,
845
872
  name,
@@ -886,7 +913,7 @@ var useArrayFieldAddButton = () => {
886
913
  };
887
914
 
888
915
  // src/array-field.tsx
889
- var import_utils7 = require("@chakra-ui/utils");
916
+ var import_utils8 = require("@chakra-ui/utils");
890
917
  var import_jsx_runtime15 = require("react/jsx-runtime");
891
918
  var ArrayFieldRow = ({
892
919
  children,
@@ -943,7 +970,7 @@ ArrayFieldAddButton.displayName = "ArrayFieldAddButton";
943
970
  var ArrayField = (0, import_react16.forwardRef)(
944
971
  (props, ref) => {
945
972
  const { children, ...containerProps } = props;
946
- const rowFn = (0, import_utils7.isFunction)(children) ? children : (fields) => /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_jsx_runtime15.Fragment, { children: fields.map(({ id }, index) => /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(ArrayFieldRow, { index, children }, id)) || null });
973
+ const rowFn = (0, import_utils8.isFunction)(children) ? children : (fields) => /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_jsx_runtime15.Fragment, { children: fields.map(({ id }, index) => /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(ArrayFieldRow, { index, children }, id)) || null });
947
974
  return /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(ArrayFieldContainer, { ref, ...containerProps, children: [
948
975
  /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(ArrayFieldRows, { children: rowFn }),
949
976
  /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(ArrayFieldAddButton, {})
@@ -1161,13 +1188,13 @@ DisplayIf.displayName = "DisplayIf";
1161
1188
  // src/step-form.tsx
1162
1189
  var React12 = __toESM(require("react"));
1163
1190
  var import_react19 = require("@chakra-ui/react");
1164
- var import_utils9 = require("@chakra-ui/utils");
1165
- var import_core4 = require("@saas-ui/core");
1191
+ var import_utils10 = require("@chakra-ui/utils");
1192
+ var import_core5 = require("@saas-ui/core");
1166
1193
 
1167
1194
  // src/use-step-form.tsx
1168
1195
  var React11 = __toESM(require("react"));
1169
1196
  var import_react_utils3 = require("@chakra-ui/react-utils");
1170
- var import_core3 = require("@saas-ui/core");
1197
+ var import_core4 = require("@saas-ui/core");
1171
1198
  var [StepFormProvider, useStepFormContext] = (0, import_react_utils3.createContext)({
1172
1199
  name: "StepFormContext",
1173
1200
  errorMessage: "useStepFormContext: `context` is undefined. Seems you forgot to wrap step form components in `<StepForm />`"
@@ -1180,7 +1207,7 @@ function useStepForm(props) {
1180
1207
  fieldResolver,
1181
1208
  ...rest
1182
1209
  } = props;
1183
- const stepper = (0, import_core3.useStepper)(rest);
1210
+ const stepper = (0, import_core4.useStepper)(rest);
1184
1211
  const [options, setOptions] = React11.useState(stepsOptions);
1185
1212
  const { activeStep, isLastStep, nextStep } = stepper;
1186
1213
  const [steps, updateSteps] = React11.useState({});
@@ -1246,7 +1273,7 @@ function useStepForm(props) {
1246
1273
  }
1247
1274
  function useFormStep(props) {
1248
1275
  const { name, schema, resolver, onSubmit } = props;
1249
- const step = (0, import_core3.useStep)({ name });
1276
+ const step = (0, import_core4.useStep)({ name });
1250
1277
  const { steps, updateStep } = useStepFormContext();
1251
1278
  React11.useEffect(() => {
1252
1279
  updateStep({ name, schema, resolver, onSubmit });
@@ -1260,7 +1287,7 @@ function useFormStep(props) {
1260
1287
  // src/step-form.tsx
1261
1288
  var import_jsx_runtime19 = require("react/jsx-runtime");
1262
1289
  var FormStepper = (props) => {
1263
- const { activeIndex, setIndex } = (0, import_core4.useStepperContext)();
1290
+ const { activeIndex, setIndex } = (0, import_core5.useStepperContext)();
1264
1291
  const {
1265
1292
  children,
1266
1293
  orientation,
@@ -1275,7 +1302,7 @@ var FormStepper = (props) => {
1275
1302
  if (React12.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
1276
1303
  const { isCompleted } = useFormStep(child.props);
1277
1304
  return /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
1278
- import_core4.StepsItem,
1305
+ import_core5.StepsItem,
1279
1306
  {
1280
1307
  render,
1281
1308
  name: child.props.name,
@@ -1293,7 +1320,7 @@ var FormStepper = (props) => {
1293
1320
  onChangeProp == null ? void 0 : onChangeProp(i);
1294
1321
  }, []);
1295
1322
  return /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
1296
- import_core4.Steps,
1323
+ import_core5.Steps,
1297
1324
  {
1298
1325
  orientation,
1299
1326
  step: activeIndex,
@@ -1309,32 +1336,32 @@ var FormStep = (props) => {
1309
1336
  const { name, children, className, onSubmit, ...rest } = props;
1310
1337
  const step = useFormStep({ name, onSubmit });
1311
1338
  const { isActive } = step;
1312
- return isActive ? /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(import_react19.chakra.div, { ...rest, className: (0, import_utils9.cx)("sui-form__step", className), children }) : null;
1339
+ return isActive ? /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(import_react19.chakra.div, { ...rest, className: (0, import_utils10.cx)("sui-form__step", className), children }) : null;
1313
1340
  };
1314
1341
  FormStep.displayName = "FormStep";
1315
1342
  var PrevButton = (props) => {
1316
- const { isFirstStep, isCompleted, prevStep } = (0, import_core4.useStepperContext)();
1343
+ const { isFirstStep, isCompleted, prevStep } = (0, import_core5.useStepperContext)();
1317
1344
  return /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
1318
1345
  import_react19.Button,
1319
1346
  {
1320
1347
  isDisabled: isFirstStep || isCompleted,
1321
1348
  children: "Back",
1322
1349
  ...props,
1323
- className: (0, import_utils9.cx)("sui-form__prev-button", props.className),
1324
- onClick: (0, import_utils9.callAllHandlers)(props.onClick, prevStep)
1350
+ className: (0, import_utils10.cx)("sui-form__prev-button", props.className),
1351
+ onClick: (0, import_utils10.callAllHandlers)(props.onClick, prevStep)
1325
1352
  }
1326
1353
  );
1327
1354
  };
1328
1355
  PrevButton.displayName = "PrevButton";
1329
1356
  var NextButton = (props) => {
1330
1357
  const { label = "Next", submitLabel = "Complete", ...rest } = props;
1331
- const { isLastStep, isCompleted } = (0, import_core4.useStepperContext)();
1358
+ const { isLastStep, isCompleted } = (0, import_core5.useStepperContext)();
1332
1359
  return /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
1333
1360
  SubmitButton,
1334
1361
  {
1335
1362
  ...rest,
1336
1363
  isDisabled: isCompleted,
1337
- className: (0, import_utils9.cx)("sui-form__next-button", props.className),
1364
+ className: (0, import_utils10.cx)("sui-form__next-button", props.className),
1338
1365
  children: isLastStep || isCompleted ? submitLabel : label
1339
1366
  }
1340
1367
  );
@@ -1342,7 +1369,7 @@ var NextButton = (props) => {
1342
1369
  NextButton.displayName = "NextButton";
1343
1370
 
1344
1371
  // src/field-resolver.ts
1345
- var import_utils10 = require("@chakra-ui/utils");
1372
+ var import_utils11 = require("@chakra-ui/utils");
1346
1373
  var mapFields = (schema) => schema && Object.entries(schema).map(([name, props]) => {
1347
1374
  const { items, label, title, ...field } = props;
1348
1375
  return {
@@ -1358,7 +1385,7 @@ var objectFieldResolver = (schema) => {
1358
1385
  };
1359
1386
  const getNestedFields = (name) => {
1360
1387
  var _a;
1361
- const field = (0, import_utils10.get)(schema, name);
1388
+ const field = (0, import_utils11.get)(schema, name);
1362
1389
  if (!field)
1363
1390
  return [];
1364
1391
  if (((_a = field.items) == null ? void 0 : _a.type) === "object") {
@@ -1385,10 +1412,13 @@ var WatchField = (props) => {
1385
1412
  return props.children(field, form) || null;
1386
1413
  };
1387
1414
 
1415
+ // src/create-form.tsx
1416
+ var import_react21 = require("@chakra-ui/react");
1417
+
1388
1418
  // src/form.tsx
1389
1419
  var React13 = __toESM(require("react"));
1390
1420
  var import_react20 = require("@chakra-ui/react");
1391
- var import_utils11 = require("@chakra-ui/utils");
1421
+ var import_utils12 = require("@chakra-ui/utils");
1392
1422
  var import_react_hook_form8 = require("react-hook-form");
1393
1423
  var import_jsx_runtime20 = require("react/jsx-runtime");
1394
1424
  var Form = (0, import_react20.forwardRef)(
@@ -1460,8 +1490,8 @@ var Form = (0, import_react20.forwardRef)(
1460
1490
  ref,
1461
1491
  onSubmit: handleSubmit(onSubmit, onError),
1462
1492
  ...rest,
1463
- className: (0, import_utils11.cx)("sui-form", props.className),
1464
- children: (0, import_utils11.runIfFn)(_children, {
1493
+ className: (0, import_utils12.cx)("sui-form", props.className),
1494
+ children: (0, import_utils12.runIfFn)(_children, {
1465
1495
  Field,
1466
1496
  DisplayIf,
1467
1497
  ArrayField,
@@ -1477,12 +1507,12 @@ var Form = (0, import_react20.forwardRef)(
1477
1507
  Form.displayName = "Form";
1478
1508
 
1479
1509
  // src/create-form.tsx
1480
- var import_react21 = require("@chakra-ui/react");
1481
1510
  var import_jsx_runtime21 = require("react/jsx-runtime");
1482
1511
  function createForm({
1483
1512
  resolver,
1484
1513
  fieldResolver = objectFieldResolver,
1485
- fields
1514
+ fields,
1515
+ getBaseField
1486
1516
  } = {}) {
1487
1517
  const DefaultForm = (0, import_react21.forwardRef)(
1488
1518
  (props, ref) => {
@@ -1492,7 +1522,7 @@ function createForm({
1492
1522
  fieldResolver: fieldResolverProp,
1493
1523
  ...rest
1494
1524
  } = props;
1495
- return /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(FieldsProvider, { value: fields || {}, children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(
1525
+ return /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(FieldsProvider, { value: { fields, getBaseField }, children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(
1496
1526
  Form,
1497
1527
  {
1498
1528
  ref,
@@ -1511,10 +1541,15 @@ function createForm({
1511
1541
  // src/create-step-form.tsx
1512
1542
  var import_react22 = require("react");
1513
1543
  var import_react23 = require("@chakra-ui/react");
1514
- var import_core5 = require("@saas-ui/core");
1515
- var import_utils12 = require("@chakra-ui/utils");
1544
+ var import_core6 = require("@saas-ui/core");
1545
+ var import_utils13 = require("@chakra-ui/utils");
1516
1546
  var import_jsx_runtime22 = require("react/jsx-runtime");
1517
- function createStepForm({ fields, resolver, fieldResolver } = {}) {
1547
+ function createStepForm({
1548
+ fields,
1549
+ resolver,
1550
+ fieldResolver,
1551
+ getBaseField
1552
+ } = {}) {
1518
1553
  const StepForm2 = (0, import_react23.forwardRef)((props, ref) => {
1519
1554
  const { children, steps, ...rest } = props;
1520
1555
  const stepper = useStepForm({
@@ -1524,7 +1559,7 @@ function createStepForm({ fields, resolver, fieldResolver } = {}) {
1524
1559
  });
1525
1560
  const { getFormProps, ...ctx } = stepper;
1526
1561
  const context = (0, import_react22.useMemo)(() => ctx, [ctx]);
1527
- return /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(import_core5.StepperProvider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(StepFormProvider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(FieldsProvider, { value: fields || {}, children: /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(Form, { ref, ...rest, ...getFormProps(), children: (0, import_utils12.runIfFn)(children, {
1562
+ return /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(import_core6.StepperProvider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(StepFormProvider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(FieldsProvider, { value: { fields, getBaseField }, children: /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(Form, { ref, ...rest, ...getFormProps(), children: (0, import_utils13.runIfFn)(children, {
1528
1563
  ...stepper,
1529
1564
  Field,
1530
1565
  FormStep,
@@ -1605,6 +1640,7 @@ var StepForm = createStepForm();
1605
1640
  useArrayFieldRemoveButton,
1606
1641
  useArrayFieldRow,
1607
1642
  useArrayFieldRowContext,
1643
+ useBaseField,
1608
1644
  useController,
1609
1645
  useField,
1610
1646
  useFieldArray,