@saas-ui/forms 2.0.0-next.14 → 2.0.0-next.16

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var React13 = require('react');
3
+ var React12 = require('react');
4
4
  var reactHookForm = require('react-hook-form');
5
5
  var jsxRuntime = require('react/jsx-runtime');
6
6
  var react = require('@chakra-ui/react');
@@ -27,12 +27,12 @@ function _interopNamespace(e) {
27
27
  return Object.freeze(n);
28
28
  }
29
29
 
30
- var React13__namespace = /*#__PURE__*/_interopNamespace(React13);
30
+ var React12__namespace = /*#__PURE__*/_interopNamespace(React12);
31
31
 
32
32
  // src/form-context.tsx
33
- var FormContext = React13__namespace.createContext(null);
33
+ var FormContext = React12__namespace.createContext(null);
34
34
  var useFormContext = () => {
35
- const context = React13__namespace.useContext(FormContext);
35
+ const context = React12__namespace.useContext(FormContext);
36
36
  const hookContext = reactHookForm.useFormContext();
37
37
  if (!context) {
38
38
  throw new Error("FormProvider must be used within a Form component");
@@ -108,7 +108,7 @@ var PasswordInput = react.forwardRef(
108
108
  leftAddon,
109
109
  ...inputProps
110
110
  } = props;
111
- const [show, setShow] = React13.useState(false);
111
+ const [show, setShow] = React12.useState(false);
112
112
  const handleClick = () => setShow(!show);
113
113
  const label = show ? "Hide password" : "Show password";
114
114
  let icon;
@@ -147,15 +147,15 @@ var PasswordInput = react.forwardRef(
147
147
  );
148
148
  PasswordInput.displayName = "PasswordInput";
149
149
  var mapNestedFields = (name, children) => {
150
- return React13__namespace.Children.map(children, (child) => {
151
- if (React13__namespace.isValidElement(child) && child.props.name) {
150
+ return React12__namespace.Children.map(children, (child) => {
151
+ if (React12__namespace.isValidElement(child) && child.props.name) {
152
152
  let childName = child.props.name;
153
153
  if (childName.includes(".")) {
154
154
  childName = childName.replace(/^.*\.(.*)/, "$1");
155
155
  } else if (childName.includes(".$")) {
156
156
  childName = childName.replace(/^.*\.\$(.*)/, "$1");
157
157
  }
158
- return React13__namespace.cloneElement(child, {
158
+ return React12__namespace.cloneElement(child, {
159
159
  ...child.props,
160
160
  name: `${name}.${childName}`
161
161
  });
@@ -215,14 +215,14 @@ var useSelect = (props) => {
215
215
  onChange
216
216
  });
217
217
  const controlProps = react.useFormControl({ name });
218
- const options = React13__namespace.default.useMemo(
218
+ const options = React12__namespace.default.useMemo(
219
219
  () => optionsProp && mapOptions(optionsProp),
220
220
  [optionsProp]
221
221
  );
222
222
  const handleChange = (value2) => {
223
223
  setCurrentValue(value2);
224
224
  };
225
- const getDisplayValue = React13__namespace.default.useCallback(
225
+ const getDisplayValue = React12__namespace.default.useCallback(
226
226
  (value2) => {
227
227
  if (!options) {
228
228
  return value2;
@@ -236,7 +236,7 @@ var useSelect = (props) => {
236
236
  },
237
237
  [options]
238
238
  );
239
- const displayValue = React13__namespace.default.useMemo(
239
+ const displayValue = React12__namespace.default.useMemo(
240
240
  () => currentValue ? (Array.isArray(currentValue) ? currentValue : [currentValue]).map(
241
241
  getDisplayValue
242
242
  ) : [],
@@ -300,7 +300,7 @@ var SelectButton = react.forwardRef(
300
300
  react.MenuButton,
301
301
  {
302
302
  as: react.Button,
303
- id: id || React13__namespace.useId(),
303
+ id: id || React12__namespace.useId(),
304
304
  ...buttonStyles,
305
305
  ...rest,
306
306
  onFocus,
@@ -573,7 +573,7 @@ var defaultFieldTypes = {
573
573
  pin: PinField,
574
574
  "native-select": NativeSelectField
575
575
  };
576
- var FieldsContext = React13__namespace.default.createContext(
576
+ var FieldsContext = React12__namespace.default.createContext(
577
577
  null
578
578
  );
579
579
  var FieldsProvider = (props) => {
@@ -581,11 +581,11 @@ var FieldsProvider = (props) => {
581
581
  return /* @__PURE__ */ jsxRuntime.jsx(FieldsContext.Provider, { value: fields, children: props.children });
582
582
  };
583
583
  var useField = (type) => {
584
- const context = React13__namespace.default.useContext(FieldsContext);
584
+ const context = React12__namespace.default.useContext(FieldsContext);
585
585
  return (context == null ? void 0 : context[type]) || InputField;
586
586
  };
587
587
  var defaultInputType = "text";
588
- var Field = React13__namespace.forwardRef(
588
+ var Field = React12__namespace.forwardRef(
589
589
  (props, ref) => {
590
590
  const { type = defaultInputType, name } = props;
591
591
  const overrides = useFieldProps(name);
@@ -612,8 +612,8 @@ var FormLayout = ({ children, ...props }) => {
612
612
  {
613
613
  ...gridProps,
614
614
  className: utils.cx("sui-form__layout", props.className),
615
- children: React13__namespace.Children.map(children, (child) => {
616
- if (React13__namespace.isValidElement(child)) {
615
+ children: React12__namespace.Children.map(children, (child) => {
616
+ if (React12__namespace.isValidElement(child)) {
617
617
  return /* @__PURE__ */ jsxRuntime.jsx(FormLayoutItem, { children: child });
618
618
  }
619
619
  return child;
@@ -652,7 +652,7 @@ var useArrayField = ({
652
652
  var useArrayFieldRow = ({ index }) => {
653
653
  const { clearErrors } = useFormContext();
654
654
  const { name, remove, fields } = useArrayFieldContext();
655
- React13__namespace.useEffect(() => {
655
+ React12__namespace.useEffect(() => {
656
656
  clearErrors(name);
657
657
  }, []);
658
658
  return {
@@ -660,7 +660,7 @@ var useArrayFieldRow = ({ index }) => {
660
660
  isFirst: index === 0,
661
661
  isLast: index === fields.length - 1,
662
662
  name: `${name}.${index}`,
663
- remove: React13__namespace.useCallback(() => {
663
+ remove: React12__namespace.useCallback(() => {
664
664
  clearErrors(name);
665
665
  remove(index);
666
666
  }, [index])
@@ -755,7 +755,7 @@ var ArrayFieldRows = ({
755
755
  return children(fields);
756
756
  };
757
757
  ArrayFieldRows.displayName = "ArrayFieldRows";
758
- var ArrayFieldContainer = React13__namespace.forwardRef(
758
+ var ArrayFieldContainer = React12__namespace.forwardRef(
759
759
  ({
760
760
  name,
761
761
  defaultValue,
@@ -773,7 +773,7 @@ var ArrayFieldContainer = React13__namespace.forwardRef(
773
773
  min: min || (overrides == null ? void 0 : overrides.min),
774
774
  max: max || (overrides == null ? void 0 : overrides.max)
775
775
  });
776
- React13__namespace.useImperativeHandle(ref, () => context, [ref, context]);
776
+ React12__namespace.useImperativeHandle(ref, () => context, [ref, context]);
777
777
  return /* @__PURE__ */ jsxRuntime.jsx(ArrayFieldProvider, { value: context, children: /* @__PURE__ */ jsxRuntime.jsx(BaseField, { name, ...fieldProps, ...overrides, children }) });
778
778
  }
779
779
  );
@@ -831,10 +831,10 @@ var AutoFields = ({
831
831
  const context = useFormContext();
832
832
  const schema = schemaProp || context.schema;
833
833
  const fieldResolver = fieldResolverProp || context.fieldResolver;
834
- const resolver = React13__namespace.useMemo(() => fieldResolver, [schema, fieldResolver]);
835
- const fields = React13__namespace.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
834
+ const resolver = React12__namespace.useMemo(() => fieldResolver, [schema, fieldResolver]);
835
+ const fields = React12__namespace.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
836
836
  const form = useFormContext();
837
- React13__namespace.useEffect(() => {
837
+ React12__namespace.useEffect(() => {
838
838
  var _a;
839
839
  if (focusFirstField && ((_a = fields == null ? void 0 : fields[0]) == null ? void 0 : _a.name)) {
840
840
  form.setFocus(fields[0].name);
@@ -909,90 +909,6 @@ var DisplayIf = ({
909
909
  return condition(value, context) ? children : null;
910
910
  };
911
911
  DisplayIf.displayName = "DisplayIf";
912
- var Form = react.forwardRef(
913
- (props, ref) => {
914
- const {
915
- mode = "all",
916
- resolver,
917
- fieldResolver,
918
- fields,
919
- reValidateMode,
920
- shouldFocusError,
921
- shouldUnregister,
922
- shouldUseNativeValidation,
923
- criteriaMode,
924
- delayError,
925
- schema,
926
- defaultValues,
927
- values,
928
- context,
929
- resetOptions,
930
- onChange,
931
- onSubmit,
932
- onError,
933
- formRef,
934
- children,
935
- ...rest
936
- } = props;
937
- const form = {
938
- mode,
939
- resolver,
940
- defaultValues,
941
- values,
942
- reValidateMode,
943
- shouldFocusError,
944
- shouldUnregister,
945
- shouldUseNativeValidation,
946
- criteriaMode,
947
- delayError,
948
- context,
949
- resetOptions
950
- };
951
- const methods = reactHookForm.useForm(form);
952
- const { handleSubmit } = methods;
953
- React13__namespace.useImperativeHandle(formRef, () => methods, [formRef, methods]);
954
- React13__namespace.useEffect(() => {
955
- let subscription;
956
- if (onChange) {
957
- subscription = methods.watch(onChange);
958
- }
959
- return () => subscription == null ? void 0 : subscription.unsubscribe();
960
- }, [methods, onChange]);
961
- let _children = children;
962
- if (!_children && fieldResolver) {
963
- _children = /* @__PURE__ */ jsxRuntime.jsxs(FormLayout, { children: [
964
- /* @__PURE__ */ jsxRuntime.jsx(AutoFields, {}),
965
- /* @__PURE__ */ jsxRuntime.jsx(SubmitButton, { ...fields == null ? void 0 : fields.submit })
966
- ] });
967
- }
968
- return /* @__PURE__ */ jsxRuntime.jsx(
969
- FormProvider,
970
- {
971
- ...methods,
972
- schema,
973
- fieldResolver,
974
- fields,
975
- children: /* @__PURE__ */ jsxRuntime.jsx(
976
- react.chakra.form,
977
- {
978
- ref,
979
- onSubmit: handleSubmit(onSubmit, onError),
980
- ...rest,
981
- className: utils.cx("sui-form", props.className),
982
- children: utils.runIfFn(_children, {
983
- Field,
984
- DisplayIf,
985
- ArrayField,
986
- ObjectField,
987
- ...methods
988
- })
989
- }
990
- )
991
- }
992
- );
993
- }
994
- );
995
- Form.displayName = "Form";
996
912
  var [StepFormProvider, useStepFormContext] = reactUtils.createContext({
997
913
  name: "StepFormContext",
998
914
  errorMessage: "useStepFormContext: `context` is undefined. Seems you forgot to wrap step form components in `<StepForm />`"
@@ -1001,8 +917,8 @@ function useStepForm(props) {
1001
917
  const { onChange, ...rest } = props;
1002
918
  const stepper = core.useStepper(rest);
1003
919
  const { activeStep, isLastStep, nextStep } = stepper;
1004
- const [steps, updateSteps] = React13__namespace.useState({});
1005
- const onSubmitStep = React13__namespace.useCallback(
920
+ const [steps, updateSteps] = React12__namespace.useState({});
921
+ const onSubmitStep = React12__namespace.useCallback(
1006
922
  async (data) => {
1007
923
  var _a, _b;
1008
924
  try {
@@ -1023,7 +939,7 @@ function useStepForm(props) {
1023
939
  },
1024
940
  [steps, activeStep, isLastStep]
1025
941
  );
1026
- const getFormProps = React13__namespace.useCallback(() => {
942
+ const getFormProps = React12__namespace.useCallback(() => {
1027
943
  const step = steps[activeStep];
1028
944
  return {
1029
945
  onSubmit: onSubmitStep,
@@ -1031,7 +947,7 @@ function useStepForm(props) {
1031
947
  resolver: step == null ? void 0 : step.resolver
1032
948
  };
1033
949
  }, [steps, onSubmitStep, activeStep]);
1034
- const updateStep = React13__namespace.useCallback(
950
+ const updateStep = React12__namespace.useCallback(
1035
951
  (step) => {
1036
952
  updateSteps((steps2) => {
1037
953
  return {
@@ -1053,7 +969,7 @@ function useFormStep(props) {
1053
969
  const { name, schema, resolver, onSubmit } = props;
1054
970
  const step = core.useStep({ name });
1055
971
  const { steps, updateStep } = useStepFormContext();
1056
- React13__namespace.useEffect(() => {
972
+ React12__namespace.useEffect(() => {
1057
973
  updateStep({ name, schema, resolver, onSubmit });
1058
974
  }, [name, schema]);
1059
975
  return {
@@ -1061,20 +977,11 @@ function useFormStep(props) {
1061
977
  ...steps[name] || { name, schema }
1062
978
  };
1063
979
  }
1064
- var StepForm = React13__namespace.forwardRef(
1065
- (props, ref) => {
1066
- const { children, ...rest } = props;
1067
- const stepper = useStepForm(props);
1068
- const { getFormProps, ...ctx } = stepper;
1069
- const context = React13__namespace.useMemo(() => ctx, [ctx]);
1070
- return /* @__PURE__ */ jsxRuntime.jsx(core.StepperProvider, { value: context, children: /* @__PURE__ */ jsxRuntime.jsx(StepFormProvider, { value: context, children: /* @__PURE__ */ jsxRuntime.jsx(Form, { ref, ...rest, ...getFormProps(), children: utils.runIfFn(children, stepper) }) }) });
1071
- }
1072
- );
1073
980
  var FormStepper = (props) => {
1074
981
  const { activeIndex, setIndex } = core.useStepperContext();
1075
982
  const { children, orientation, variant, colorScheme, size, ...rest } = props;
1076
- const elements = React13__namespace.Children.map(children, (child) => {
1077
- if (React13__namespace.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
983
+ const elements = React12__namespace.Children.map(children, (child) => {
984
+ if (React12__namespace.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
1078
985
  const { isCompleted } = useFormStep(child.props);
1079
986
  return /* @__PURE__ */ jsxRuntime.jsx(
1080
987
  core.StepperStep,
@@ -1089,7 +996,7 @@ var FormStepper = (props) => {
1089
996
  }
1090
997
  return child;
1091
998
  });
1092
- const onChange = React13__namespace.useCallback((i) => {
999
+ const onChange = React12__namespace.useCallback((i) => {
1093
1000
  setIndex(i);
1094
1001
  }, []);
1095
1002
  return /* @__PURE__ */ jsxRuntime.jsx(
@@ -1118,7 +1025,7 @@ var PrevButton = (props) => {
1118
1025
  react.Button,
1119
1026
  {
1120
1027
  isDisabled: isFirstStep || isCompleted,
1121
- label: "Back",
1028
+ children: "Back",
1122
1029
  ...props,
1123
1030
  className: utils.cx("sui-form__prev-button", props.className),
1124
1031
  onClick: utils.callAllHandlers(props.onClick, prevStep)
@@ -1177,12 +1084,96 @@ var WatchField = (props) => {
1177
1084
  });
1178
1085
  return props.children(field, form) || null;
1179
1086
  };
1087
+ var Form = react.forwardRef(
1088
+ (props, ref) => {
1089
+ const {
1090
+ mode = "all",
1091
+ resolver,
1092
+ fieldResolver,
1093
+ fields,
1094
+ reValidateMode,
1095
+ shouldFocusError,
1096
+ shouldUnregister,
1097
+ shouldUseNativeValidation,
1098
+ criteriaMode,
1099
+ delayError,
1100
+ schema,
1101
+ defaultValues,
1102
+ values,
1103
+ context,
1104
+ resetOptions,
1105
+ onChange,
1106
+ onSubmit,
1107
+ onError,
1108
+ formRef,
1109
+ children,
1110
+ ...rest
1111
+ } = props;
1112
+ const form = {
1113
+ mode,
1114
+ resolver,
1115
+ defaultValues,
1116
+ values,
1117
+ reValidateMode,
1118
+ shouldFocusError,
1119
+ shouldUnregister,
1120
+ shouldUseNativeValidation,
1121
+ criteriaMode,
1122
+ delayError,
1123
+ context,
1124
+ resetOptions
1125
+ };
1126
+ const methods = reactHookForm.useForm(form);
1127
+ const { handleSubmit } = methods;
1128
+ React12__namespace.useImperativeHandle(formRef, () => methods, [formRef, methods]);
1129
+ React12__namespace.useEffect(() => {
1130
+ let subscription;
1131
+ if (onChange) {
1132
+ subscription = methods.watch(onChange);
1133
+ }
1134
+ return () => subscription == null ? void 0 : subscription.unsubscribe();
1135
+ }, [methods, onChange]);
1136
+ let _children = children;
1137
+ if (!_children && fieldResolver) {
1138
+ _children = /* @__PURE__ */ jsxRuntime.jsxs(FormLayout, { children: [
1139
+ /* @__PURE__ */ jsxRuntime.jsx(AutoFields, {}),
1140
+ /* @__PURE__ */ jsxRuntime.jsx(SubmitButton, { ...fields == null ? void 0 : fields.submit })
1141
+ ] });
1142
+ }
1143
+ return /* @__PURE__ */ jsxRuntime.jsx(
1144
+ FormProvider,
1145
+ {
1146
+ ...methods,
1147
+ schema,
1148
+ fieldResolver,
1149
+ fields,
1150
+ children: /* @__PURE__ */ jsxRuntime.jsx(
1151
+ react.chakra.form,
1152
+ {
1153
+ ref,
1154
+ onSubmit: handleSubmit(onSubmit, onError),
1155
+ ...rest,
1156
+ className: utils.cx("sui-form", props.className),
1157
+ children: utils.runIfFn(_children, {
1158
+ Field,
1159
+ DisplayIf,
1160
+ ArrayField,
1161
+ ObjectField,
1162
+ ...methods
1163
+ })
1164
+ }
1165
+ )
1166
+ }
1167
+ );
1168
+ }
1169
+ );
1170
+ Form.displayName = "Form";
1180
1171
  function createForm({
1181
1172
  resolver,
1182
1173
  fieldResolver = objectFieldResolver,
1183
1174
  fields
1184
1175
  } = {}) {
1185
- const CreateForm = react.forwardRef(
1176
+ const DefaultForm = react.forwardRef(
1186
1177
  (props, ref) => {
1187
1178
  const { schema, ...rest } = props;
1188
1179
  return /* @__PURE__ */ jsxRuntime.jsx(FieldsProvider, { value: fields || {}, children: /* @__PURE__ */ jsxRuntime.jsx(
@@ -1196,9 +1187,29 @@ function createForm({
1196
1187
  ) });
1197
1188
  }
1198
1189
  );
1199
- return CreateForm;
1190
+ DefaultForm.displayName = "Form";
1191
+ DefaultForm.id = "Form";
1192
+ return DefaultForm;
1193
+ }
1194
+ function createStepForm(Form4) {
1195
+ const StepForm2 = react.forwardRef((props, ref) => {
1196
+ const { children, ...rest } = props;
1197
+ const stepper = useStepForm(props);
1198
+ const { getFormProps, ...ctx } = stepper;
1199
+ const context = React12.useMemo(() => ctx, [ctx]);
1200
+ return /* @__PURE__ */ jsxRuntime.jsx(core.StepperProvider, { value: context, children: /* @__PURE__ */ jsxRuntime.jsx(StepFormProvider, { value: context, children: /* @__PURE__ */ jsxRuntime.jsx(Form4, { ref, ...rest, ...getFormProps(), children: utils.runIfFn(children, {
1201
+ ...stepper,
1202
+ Field,
1203
+ DisplayIf,
1204
+ ArrayField,
1205
+ ObjectField
1206
+ }) }) }) });
1207
+ });
1208
+ StepForm2.displayName = `Step${Form4.displayName || Form4.name}`;
1209
+ return StepForm2;
1200
1210
  }
1201
- var Form2 = createForm();
1211
+ var Form3 = createForm();
1212
+ var StepForm = createStepForm(Form3);
1202
1213
 
1203
1214
  Object.defineProperty(exports, 'Controller', {
1204
1215
  enumerable: true,
@@ -1246,7 +1257,7 @@ exports.DisplayField = DisplayField;
1246
1257
  exports.DisplayIf = DisplayIf;
1247
1258
  exports.Field = Field;
1248
1259
  exports.FieldsProvider = FieldsProvider;
1249
- exports.Form = Form2;
1260
+ exports.Form = Form3;
1250
1261
  exports.FormLayout = FormLayout;
1251
1262
  exports.FormLegend = FormLegend;
1252
1263
  exports.FormProvider = FormProvider;