@saas-ui/forms 2.1.3 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -90,10 +90,13 @@ var NumberInput = forwardRef((props, ref) => {
90
90
  hideStepper,
91
91
  incrementIcon = /* @__PURE__ */ jsx3(ChevronUpIcon, {}),
92
92
  decrementIcon = /* @__PURE__ */ jsx3(ChevronDownIcon, {}),
93
+ placeholder,
94
+ fieldProps: _fieldProps,
93
95
  ...rest
94
96
  } = props;
97
+ const fieldProps = { placeholder, ..._fieldProps };
95
98
  return /* @__PURE__ */ jsxs2(ChakraNumberInput, { ...rest, ref, children: [
96
- /* @__PURE__ */ jsx3(NumberInputField, {}),
99
+ /* @__PURE__ */ jsx3(NumberInputField, { ...fieldProps }),
97
100
  !hideStepper && /* @__PURE__ */ jsxs2(NumberInputStepper, { children: [
98
101
  /* @__PURE__ */ jsx3(NumberIncrementStepper, { children: incrementIcon }),
99
102
  /* @__PURE__ */ jsx3(NumberDecrementStepper, { children: decrementIcon })
@@ -553,6 +556,7 @@ var withControlledInput = (InputComponent) => {
553
556
  return forwardRef7(
554
557
  ({ name, rules, ...inputProps }, ref) => {
555
558
  const { control } = useFormContext();
559
+ const onChange = inputProps.onChange;
556
560
  return /* @__PURE__ */ jsx10(
557
561
  Controller,
558
562
  {
@@ -564,7 +568,7 @@ var withControlledInput = (InputComponent) => {
564
568
  {
565
569
  ...field,
566
570
  ...inputProps,
567
- onChange: callAllHandlers(inputProps.onChange, field.onChange),
571
+ onChange: callAllHandlers(onChange, field.onChange),
568
572
  onBlur: callAllHandlers(inputProps.onBlur, field.onBlur),
569
573
  ref: useMergeRefs(ref, _ref)
570
574
  }
@@ -579,12 +583,13 @@ var withUncontrolledInput = (InputComponent) => {
579
583
  ({ name, rules, ...inputProps }, ref) => {
580
584
  const { register } = useFormContext();
581
585
  const { ref: _ref, ...field } = register(name, rules);
586
+ const onChange = inputProps.onChange;
582
587
  return /* @__PURE__ */ jsx10(
583
588
  InputComponent,
584
589
  {
585
590
  ...field,
586
591
  ...inputProps,
587
- onChange: callAllHandlers(inputProps.onChange, field.onChange),
592
+ onChange: callAllHandlers(onChange, field.onChange),
588
593
  onBlur: callAllHandlers(inputProps.onBlur, field.onBlur),
589
594
  ref: useMergeRefs(ref, _ref)
590
595
  }
@@ -643,9 +648,10 @@ var SwitchField = createField(
643
648
  );
644
649
  var SelectField = createField(
645
650
  forwardRef8((props, ref) => {
646
- return /* @__PURE__ */ jsxs6(Select, { ref, ...props, children: [
647
- /* @__PURE__ */ jsx11(SelectButton, {}),
648
- /* @__PURE__ */ jsx11(SelectList, {})
651
+ const { buttonProps, listProps, ...rest } = props;
652
+ return /* @__PURE__ */ jsxs6(Select, { ref, ...rest, children: [
653
+ /* @__PURE__ */ jsx11(SelectButton, { ...buttonProps }),
654
+ /* @__PURE__ */ jsx11(SelectList, { ...listProps })
649
655
  ] });
650
656
  }),
651
657
  {
@@ -722,15 +728,10 @@ var Field = React6.forwardRef(
722
728
  );
723
729
 
724
730
  // src/fields.tsx
725
- import * as React10 from "react";
731
+ import * as React9 from "react";
726
732
 
727
733
  // src/layout.tsx
728
- import * as React7 from "react";
729
- import {
730
- chakra as chakra2,
731
- SimpleGrid,
732
- useTheme
733
- } from "@chakra-ui/react";
734
+ import { chakra as chakra2, SimpleGrid, useTheme } from "@chakra-ui/react";
734
735
  import { cx as cx2 } from "@chakra-ui/utils";
735
736
  import { jsx as jsx14 } from "react/jsx-runtime";
736
737
  var FormLayoutItem = ({ children }) => {
@@ -752,19 +753,14 @@ var FormLayout = ({ children, ...props }) => {
752
753
  {
753
754
  ...gridProps,
754
755
  className: cx2("sui-form__layout", props.className),
755
- children: React7.Children.map(children, (child) => {
756
- if (React7.isValidElement(child)) {
757
- return /* @__PURE__ */ jsx14(FormLayoutItem, { children: child });
758
- }
759
- return child;
760
- })
756
+ children
761
757
  }
762
758
  );
763
759
  };
764
760
  FormLayout.displayName = "FormLayout";
765
761
 
766
762
  // src/array-field.tsx
767
- import * as React9 from "react";
763
+ import * as React8 from "react";
768
764
  import {
769
765
  chakra as chakra3,
770
766
  forwardRef as forwardRef11,
@@ -773,7 +769,7 @@ import {
773
769
  import { PlusIcon, MinusIcon } from "@saas-ui/core/icons";
774
770
 
775
771
  // src/use-array-field.tsx
776
- import * as React8 from "react";
772
+ import * as React7 from "react";
777
773
  import {
778
774
  useFieldArray
779
775
  } from "react-hook-form";
@@ -808,7 +804,7 @@ var useArrayField = ({
808
804
  var useArrayFieldRow = ({ index }) => {
809
805
  const { clearErrors } = useFormContext();
810
806
  const { name, remove, fields } = useArrayFieldContext();
811
- React8.useEffect(() => {
807
+ React7.useEffect(() => {
812
808
  clearErrors(name);
813
809
  }, []);
814
810
  return {
@@ -816,7 +812,7 @@ var useArrayFieldRow = ({ index }) => {
816
812
  isFirst: index === 0,
817
813
  isLast: index === fields.length - 1,
818
814
  name: `${name}.${index}`,
819
- remove: React8.useCallback(() => {
815
+ remove: React7.useCallback(() => {
820
816
  clearErrors(name);
821
817
  remove(index);
822
818
  }, [index])
@@ -915,7 +911,7 @@ var ArrayFieldRows = ({
915
911
  return children(fields);
916
912
  };
917
913
  ArrayFieldRows.displayName = "ArrayFieldRows";
918
- var ArrayFieldContainer = React9.forwardRef(
914
+ var ArrayFieldContainer = React8.forwardRef(
919
915
  ({
920
916
  name,
921
917
  defaultValue,
@@ -933,7 +929,7 @@ var ArrayFieldContainer = React9.forwardRef(
933
929
  min: min || (overrides == null ? void 0 : overrides.min),
934
930
  max: max || (overrides == null ? void 0 : overrides.max)
935
931
  });
936
- React9.useImperativeHandle(ref, () => context, [ref, context]);
932
+ React8.useImperativeHandle(ref, () => context, [ref, context]);
937
933
  return /* @__PURE__ */ jsx15(ArrayFieldProvider, { value: context, children: /* @__PURE__ */ jsx15(BaseField, { name, ...fieldProps, ...overrides, children }) });
938
934
  }
939
935
  );
@@ -1002,10 +998,10 @@ var AutoFields = ({
1002
998
  const context = useFormContext();
1003
999
  const schema = schemaProp || context.schema;
1004
1000
  const fieldResolver = fieldResolverProp || context.fieldResolver;
1005
- const resolver = React10.useMemo(() => fieldResolver, [schema, fieldResolver]);
1006
- const fields = React10.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
1001
+ const resolver = React9.useMemo(() => fieldResolver, [schema, fieldResolver]);
1002
+ const fields = React9.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
1007
1003
  const form = useFormContext();
1008
- React10.useEffect(() => {
1004
+ React9.useEffect(() => {
1009
1005
  var _a;
1010
1006
  if (focusFirstField && ((_a = fields == null ? void 0 : fields[0]) == null ? void 0 : _a.name)) {
1011
1007
  form.setFocus(fields[0].name);
@@ -1026,7 +1022,16 @@ var AutoFields = ({
1026
1022
  } else if (type === "object") {
1027
1023
  return /* @__PURE__ */ jsx17(ObjectField, { name, ...fieldProps, children: mapNestedFields2(resolver, name) }, name);
1028
1024
  }
1029
- return /* @__PURE__ */ jsx17(Field, { name, type, ...fieldProps }, name);
1025
+ return /* @__PURE__ */ jsx17(
1026
+ Field,
1027
+ {
1028
+ name,
1029
+ type,
1030
+ defaultValue,
1031
+ ...fieldProps
1032
+ },
1033
+ name
1034
+ );
1030
1035
  }
1031
1036
  ) });
1032
1037
  };
@@ -1092,7 +1097,7 @@ var DisplayIf = ({
1092
1097
  DisplayIf.displayName = "DisplayIf";
1093
1098
 
1094
1099
  // src/step-form.tsx
1095
- import * as React12 from "react";
1100
+ import * as React11 from "react";
1096
1101
  import {
1097
1102
  chakra as chakra4,
1098
1103
  Button as Button5
@@ -1105,7 +1110,7 @@ import {
1105
1110
  } from "@saas-ui/core";
1106
1111
 
1107
1112
  // src/use-step-form.tsx
1108
- import * as React11 from "react";
1113
+ import * as React10 from "react";
1109
1114
  import { createContext as createContext4 } from "@chakra-ui/react-utils";
1110
1115
  import {
1111
1116
  useStepper,
@@ -1116,18 +1121,29 @@ var [StepFormProvider, useStepFormContext] = createContext4({
1116
1121
  errorMessage: "useStepFormContext: `context` is undefined. Seems you forgot to wrap step form components in `<StepForm />`"
1117
1122
  });
1118
1123
  function useStepForm(props) {
1119
- const { onChange, steps: stepsOptions, resolver, ...rest } = props;
1124
+ const {
1125
+ onChange,
1126
+ steps: stepsOptions,
1127
+ resolver,
1128
+ fieldResolver,
1129
+ ...rest
1130
+ } = props;
1120
1131
  const stepper = useStepper(rest);
1121
- const [options, setOptions] = React11.useState(stepsOptions);
1132
+ const [options, setOptions] = React10.useState(stepsOptions);
1122
1133
  const { activeStep, isLastStep, nextStep } = stepper;
1123
- const [steps, updateSteps] = React11.useState({});
1124
- const onSubmitStep = React11.useCallback(
1134
+ const [steps, updateSteps] = React10.useState({});
1135
+ const mergedData = React10.useRef({});
1136
+ const onSubmitStep = React10.useCallback(
1125
1137
  async (data) => {
1126
1138
  var _a, _b;
1127
1139
  try {
1128
1140
  const step = steps[activeStep];
1141
+ mergedData.current = {
1142
+ ...mergedData.current,
1143
+ ...data
1144
+ };
1129
1145
  if (isLastStep) {
1130
- await ((_a = props.onSubmit) == null ? void 0 : _a.call(props, data));
1146
+ await ((_a = props.onSubmit) == null ? void 0 : _a.call(props, mergedData.current));
1131
1147
  updateStep({
1132
1148
  ...step,
1133
1149
  isCompleted: true
@@ -1140,9 +1156,9 @@ function useStepForm(props) {
1140
1156
  } catch (e) {
1141
1157
  }
1142
1158
  },
1143
- [steps, activeStep, isLastStep]
1159
+ [steps, activeStep, isLastStep, mergedData]
1144
1160
  );
1145
- const getFormProps = React11.useCallback(() => {
1161
+ const getFormProps = React10.useCallback(() => {
1146
1162
  const step = steps[activeStep];
1147
1163
  return {
1148
1164
  onSubmit: onSubmitStep,
@@ -1150,10 +1166,11 @@ function useStepForm(props) {
1150
1166
  resolver: (step == null ? void 0 : step.schema) ? (
1151
1167
  /* @todo fix resolver type */
1152
1168
  resolver == null ? void 0 : resolver(step.schema)
1153
- ) : void 0
1169
+ ) : void 0,
1170
+ fieldResolver: (step == null ? void 0 : step.schema) ? fieldResolver == null ? void 0 : fieldResolver(step.schema) : void 0
1154
1171
  };
1155
- }, [steps, onSubmitStep, activeStep]);
1156
- const updateStep = React11.useCallback(
1172
+ }, [steps, onSubmitStep, activeStep, resolver, fieldResolver]);
1173
+ const updateStep = React10.useCallback(
1157
1174
  (step) => {
1158
1175
  const stepOptions = options == null ? void 0 : options.find((s) => s.name === step.name);
1159
1176
  updateSteps((steps2) => {
@@ -1179,7 +1196,7 @@ function useFormStep(props) {
1179
1196
  const { name, schema, resolver, onSubmit } = props;
1180
1197
  const step = useStep({ name });
1181
1198
  const { steps, updateStep } = useStepFormContext();
1182
- React11.useEffect(() => {
1199
+ React10.useEffect(() => {
1183
1200
  updateStep({ name, schema, resolver, onSubmit });
1184
1201
  }, [name, schema]);
1185
1202
  return {
@@ -1202,8 +1219,8 @@ var FormStepper = (props) => {
1202
1219
  render,
1203
1220
  ...rest
1204
1221
  } = props;
1205
- const elements = React12.Children.map(children, (child) => {
1206
- if (React12.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
1222
+ const elements = React11.Children.map(children, (child) => {
1223
+ if (React11.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
1207
1224
  const { isCompleted } = useFormStep(child.props);
1208
1225
  return /* @__PURE__ */ jsx19(
1209
1226
  StepsItem,
@@ -1219,7 +1236,7 @@ var FormStepper = (props) => {
1219
1236
  }
1220
1237
  return child;
1221
1238
  });
1222
- const onChange = React12.useCallback((i) => {
1239
+ const onChange = React11.useCallback((i) => {
1223
1240
  setIndex(i);
1224
1241
  onChangeProp == null ? void 0 : onChangeProp(i);
1225
1242
  }, []);
@@ -1316,7 +1333,7 @@ var WatchField = (props) => {
1316
1333
  };
1317
1334
 
1318
1335
  // src/form.tsx
1319
- import * as React13 from "react";
1336
+ import * as React12 from "react";
1320
1337
  import { chakra as chakra5, forwardRef as forwardRef13 } from "@chakra-ui/react";
1321
1338
  import { cx as cx4, runIfFn } from "@chakra-ui/utils";
1322
1339
  import {
@@ -1364,8 +1381,8 @@ var Form = forwardRef13(
1364
1381
  };
1365
1382
  const methods = useForm(form);
1366
1383
  const { handleSubmit } = methods;
1367
- React13.useImperativeHandle(formRef, () => methods, [formRef, methods]);
1368
- React13.useEffect(() => {
1384
+ React12.useImperativeHandle(formRef, () => methods, [formRef, methods]);
1385
+ React12.useEffect(() => {
1369
1386
  let subscription;
1370
1387
  if (onChange) {
1371
1388
  subscription = methods.watch(onChange);