@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.mjs CHANGED
@@ -1,18 +1,18 @@
1
- import * as React13 from 'react';
2
- import React13__default, { useState } from 'react';
1
+ import * as React12 from 'react';
2
+ import React12__default, { useState, useMemo } from 'react';
3
3
  import { useFormContext as useFormContext$1, useForm, FormProvider as FormProvider$1, useFieldArray, useWatch, get, Controller } from 'react-hook-form';
4
4
  export { Controller, appendErrors, useController, useFieldArray, useForm, useFormState, useWatch } from 'react-hook-form';
5
5
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
6
6
  import { forwardRef, NumberInput as NumberInput$1, NumberInputField, NumberInputStepper, NumberIncrementStepper, NumberDecrementStepper, InputRightElement, Button, InputGroup, Input, RadioGroup, Stack, Radio, useMultiStyleConfig, useFormControlContext, MenuButton, omitThemingProps, Menu, chakra, MenuItemOption, Select as Select$1, Switch, Checkbox, HStack, PinInput, FormControl, FormLabel, Text, useControllableState, useFormControl, MenuList, MenuOptionGroup, Box, FormHelperText, FormErrorMessage, PinInputField, useTheme, SimpleGrid, useStyleConfig, useMergeRefs, Textarea } from '@chakra-ui/react';
7
- import { StepperProvider, ChevronUpIcon, ChevronDownIcon, useStepper, useStep, useStepperContext, StepperStep, StepperContainer, StepperSteps } from '@saas-ui/core';
7
+ import { ChevronUpIcon, ChevronDownIcon, useStepper, useStep, useStepperContext, StepperStep, StepperContainer, StepperSteps, StepperProvider } from '@saas-ui/core';
8
8
  import { ViewIcon, ViewOffIcon, MinusIcon, PlusIcon } from '@saas-ui/core/icons';
9
9
  import { dataAttr, cx, isFunction, runIfFn, callAllHandlers, get as get$1 } from '@chakra-ui/utils';
10
10
  import { createContext } from '@chakra-ui/react-utils';
11
11
 
12
12
  // src/form-context.tsx
13
- var FormContext = React13.createContext(null);
13
+ var FormContext = React12.createContext(null);
14
14
  var useFormContext = () => {
15
- const context = React13.useContext(FormContext);
15
+ const context = React12.useContext(FormContext);
16
16
  const hookContext = useFormContext$1();
17
17
  if (!context) {
18
18
  throw new Error("FormProvider must be used within a Form component");
@@ -127,15 +127,15 @@ var PasswordInput = forwardRef(
127
127
  );
128
128
  PasswordInput.displayName = "PasswordInput";
129
129
  var mapNestedFields = (name, children) => {
130
- return React13.Children.map(children, (child) => {
131
- if (React13.isValidElement(child) && child.props.name) {
130
+ return React12.Children.map(children, (child) => {
131
+ if (React12.isValidElement(child) && child.props.name) {
132
132
  let childName = child.props.name;
133
133
  if (childName.includes(".")) {
134
134
  childName = childName.replace(/^.*\.(.*)/, "$1");
135
135
  } else if (childName.includes(".$")) {
136
136
  childName = childName.replace(/^.*\.\$(.*)/, "$1");
137
137
  }
138
- return React13.cloneElement(child, {
138
+ return React12.cloneElement(child, {
139
139
  ...child.props,
140
140
  name: `${name}.${childName}`
141
141
  });
@@ -195,14 +195,14 @@ var useSelect = (props) => {
195
195
  onChange
196
196
  });
197
197
  const controlProps = useFormControl({ name });
198
- const options = React13__default.useMemo(
198
+ const options = React12__default.useMemo(
199
199
  () => optionsProp && mapOptions(optionsProp),
200
200
  [optionsProp]
201
201
  );
202
202
  const handleChange = (value2) => {
203
203
  setCurrentValue(value2);
204
204
  };
205
- const getDisplayValue = React13__default.useCallback(
205
+ const getDisplayValue = React12__default.useCallback(
206
206
  (value2) => {
207
207
  if (!options) {
208
208
  return value2;
@@ -216,7 +216,7 @@ var useSelect = (props) => {
216
216
  },
217
217
  [options]
218
218
  );
219
- const displayValue = React13__default.useMemo(
219
+ const displayValue = React12__default.useMemo(
220
220
  () => currentValue ? (Array.isArray(currentValue) ? currentValue : [currentValue]).map(
221
221
  getDisplayValue
222
222
  ) : [],
@@ -280,7 +280,7 @@ var SelectButton = forwardRef(
280
280
  MenuButton,
281
281
  {
282
282
  as: Button,
283
- id: id || React13.useId(),
283
+ id: id || React12.useId(),
284
284
  ...buttonStyles,
285
285
  ...rest,
286
286
  onFocus,
@@ -553,7 +553,7 @@ var defaultFieldTypes = {
553
553
  pin: PinField,
554
554
  "native-select": NativeSelectField
555
555
  };
556
- var FieldsContext = React13__default.createContext(
556
+ var FieldsContext = React12__default.createContext(
557
557
  null
558
558
  );
559
559
  var FieldsProvider = (props) => {
@@ -561,11 +561,11 @@ var FieldsProvider = (props) => {
561
561
  return /* @__PURE__ */ jsx(FieldsContext.Provider, { value: fields, children: props.children });
562
562
  };
563
563
  var useField = (type) => {
564
- const context = React13__default.useContext(FieldsContext);
564
+ const context = React12__default.useContext(FieldsContext);
565
565
  return (context == null ? void 0 : context[type]) || InputField;
566
566
  };
567
567
  var defaultInputType = "text";
568
- var Field = React13.forwardRef(
568
+ var Field = React12.forwardRef(
569
569
  (props, ref) => {
570
570
  const { type = defaultInputType, name } = props;
571
571
  const overrides = useFieldProps(name);
@@ -592,8 +592,8 @@ var FormLayout = ({ children, ...props }) => {
592
592
  {
593
593
  ...gridProps,
594
594
  className: cx("sui-form__layout", props.className),
595
- children: React13.Children.map(children, (child) => {
596
- if (React13.isValidElement(child)) {
595
+ children: React12.Children.map(children, (child) => {
596
+ if (React12.isValidElement(child)) {
597
597
  return /* @__PURE__ */ jsx(FormLayoutItem, { children: child });
598
598
  }
599
599
  return child;
@@ -632,7 +632,7 @@ var useArrayField = ({
632
632
  var useArrayFieldRow = ({ index }) => {
633
633
  const { clearErrors } = useFormContext();
634
634
  const { name, remove, fields } = useArrayFieldContext();
635
- React13.useEffect(() => {
635
+ React12.useEffect(() => {
636
636
  clearErrors(name);
637
637
  }, []);
638
638
  return {
@@ -640,7 +640,7 @@ var useArrayFieldRow = ({ index }) => {
640
640
  isFirst: index === 0,
641
641
  isLast: index === fields.length - 1,
642
642
  name: `${name}.${index}`,
643
- remove: React13.useCallback(() => {
643
+ remove: React12.useCallback(() => {
644
644
  clearErrors(name);
645
645
  remove(index);
646
646
  }, [index])
@@ -735,7 +735,7 @@ var ArrayFieldRows = ({
735
735
  return children(fields);
736
736
  };
737
737
  ArrayFieldRows.displayName = "ArrayFieldRows";
738
- var ArrayFieldContainer = React13.forwardRef(
738
+ var ArrayFieldContainer = React12.forwardRef(
739
739
  ({
740
740
  name,
741
741
  defaultValue,
@@ -753,7 +753,7 @@ var ArrayFieldContainer = React13.forwardRef(
753
753
  min: min || (overrides == null ? void 0 : overrides.min),
754
754
  max: max || (overrides == null ? void 0 : overrides.max)
755
755
  });
756
- React13.useImperativeHandle(ref, () => context, [ref, context]);
756
+ React12.useImperativeHandle(ref, () => context, [ref, context]);
757
757
  return /* @__PURE__ */ jsx(ArrayFieldProvider, { value: context, children: /* @__PURE__ */ jsx(BaseField, { name, ...fieldProps, ...overrides, children }) });
758
758
  }
759
759
  );
@@ -811,10 +811,10 @@ var AutoFields = ({
811
811
  const context = useFormContext();
812
812
  const schema = schemaProp || context.schema;
813
813
  const fieldResolver = fieldResolverProp || context.fieldResolver;
814
- const resolver = React13.useMemo(() => fieldResolver, [schema, fieldResolver]);
815
- const fields = React13.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
814
+ const resolver = React12.useMemo(() => fieldResolver, [schema, fieldResolver]);
815
+ const fields = React12.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
816
816
  const form = useFormContext();
817
- React13.useEffect(() => {
817
+ React12.useEffect(() => {
818
818
  var _a;
819
819
  if (focusFirstField && ((_a = fields == null ? void 0 : fields[0]) == null ? void 0 : _a.name)) {
820
820
  form.setFocus(fields[0].name);
@@ -889,90 +889,6 @@ var DisplayIf = ({
889
889
  return condition(value, context) ? children : null;
890
890
  };
891
891
  DisplayIf.displayName = "DisplayIf";
892
- var Form = forwardRef(
893
- (props, ref) => {
894
- const {
895
- mode = "all",
896
- resolver,
897
- fieldResolver,
898
- fields,
899
- reValidateMode,
900
- shouldFocusError,
901
- shouldUnregister,
902
- shouldUseNativeValidation,
903
- criteriaMode,
904
- delayError,
905
- schema,
906
- defaultValues,
907
- values,
908
- context,
909
- resetOptions,
910
- onChange,
911
- onSubmit,
912
- onError,
913
- formRef,
914
- children,
915
- ...rest
916
- } = props;
917
- const form = {
918
- mode,
919
- resolver,
920
- defaultValues,
921
- values,
922
- reValidateMode,
923
- shouldFocusError,
924
- shouldUnregister,
925
- shouldUseNativeValidation,
926
- criteriaMode,
927
- delayError,
928
- context,
929
- resetOptions
930
- };
931
- const methods = useForm(form);
932
- const { handleSubmit } = methods;
933
- React13.useImperativeHandle(formRef, () => methods, [formRef, methods]);
934
- React13.useEffect(() => {
935
- let subscription;
936
- if (onChange) {
937
- subscription = methods.watch(onChange);
938
- }
939
- return () => subscription == null ? void 0 : subscription.unsubscribe();
940
- }, [methods, onChange]);
941
- let _children = children;
942
- if (!_children && fieldResolver) {
943
- _children = /* @__PURE__ */ jsxs(FormLayout, { children: [
944
- /* @__PURE__ */ jsx(AutoFields, {}),
945
- /* @__PURE__ */ jsx(SubmitButton, { ...fields == null ? void 0 : fields.submit })
946
- ] });
947
- }
948
- return /* @__PURE__ */ jsx(
949
- FormProvider,
950
- {
951
- ...methods,
952
- schema,
953
- fieldResolver,
954
- fields,
955
- children: /* @__PURE__ */ jsx(
956
- chakra.form,
957
- {
958
- ref,
959
- onSubmit: handleSubmit(onSubmit, onError),
960
- ...rest,
961
- className: cx("sui-form", props.className),
962
- children: runIfFn(_children, {
963
- Field,
964
- DisplayIf,
965
- ArrayField,
966
- ObjectField,
967
- ...methods
968
- })
969
- }
970
- )
971
- }
972
- );
973
- }
974
- );
975
- Form.displayName = "Form";
976
892
  var [StepFormProvider, useStepFormContext] = createContext({
977
893
  name: "StepFormContext",
978
894
  errorMessage: "useStepFormContext: `context` is undefined. Seems you forgot to wrap step form components in `<StepForm />`"
@@ -981,8 +897,8 @@ function useStepForm(props) {
981
897
  const { onChange, ...rest } = props;
982
898
  const stepper = useStepper(rest);
983
899
  const { activeStep, isLastStep, nextStep } = stepper;
984
- const [steps, updateSteps] = React13.useState({});
985
- const onSubmitStep = React13.useCallback(
900
+ const [steps, updateSteps] = React12.useState({});
901
+ const onSubmitStep = React12.useCallback(
986
902
  async (data) => {
987
903
  var _a, _b;
988
904
  try {
@@ -1003,7 +919,7 @@ function useStepForm(props) {
1003
919
  },
1004
920
  [steps, activeStep, isLastStep]
1005
921
  );
1006
- const getFormProps = React13.useCallback(() => {
922
+ const getFormProps = React12.useCallback(() => {
1007
923
  const step = steps[activeStep];
1008
924
  return {
1009
925
  onSubmit: onSubmitStep,
@@ -1011,7 +927,7 @@ function useStepForm(props) {
1011
927
  resolver: step == null ? void 0 : step.resolver
1012
928
  };
1013
929
  }, [steps, onSubmitStep, activeStep]);
1014
- const updateStep = React13.useCallback(
930
+ const updateStep = React12.useCallback(
1015
931
  (step) => {
1016
932
  updateSteps((steps2) => {
1017
933
  return {
@@ -1033,7 +949,7 @@ function useFormStep(props) {
1033
949
  const { name, schema, resolver, onSubmit } = props;
1034
950
  const step = useStep({ name });
1035
951
  const { steps, updateStep } = useStepFormContext();
1036
- React13.useEffect(() => {
952
+ React12.useEffect(() => {
1037
953
  updateStep({ name, schema, resolver, onSubmit });
1038
954
  }, [name, schema]);
1039
955
  return {
@@ -1041,20 +957,11 @@ function useFormStep(props) {
1041
957
  ...steps[name] || { name, schema }
1042
958
  };
1043
959
  }
1044
- var StepForm = React13.forwardRef(
1045
- (props, ref) => {
1046
- const { children, ...rest } = props;
1047
- const stepper = useStepForm(props);
1048
- const { getFormProps, ...ctx } = stepper;
1049
- const context = React13.useMemo(() => ctx, [ctx]);
1050
- return /* @__PURE__ */ jsx(StepperProvider, { value: context, children: /* @__PURE__ */ jsx(StepFormProvider, { value: context, children: /* @__PURE__ */ jsx(Form, { ref, ...rest, ...getFormProps(), children: runIfFn(children, stepper) }) }) });
1051
- }
1052
- );
1053
960
  var FormStepper = (props) => {
1054
961
  const { activeIndex, setIndex } = useStepperContext();
1055
962
  const { children, orientation, variant, colorScheme, size, ...rest } = props;
1056
- const elements = React13.Children.map(children, (child) => {
1057
- if (React13.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
963
+ const elements = React12.Children.map(children, (child) => {
964
+ if (React12.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
1058
965
  const { isCompleted } = useFormStep(child.props);
1059
966
  return /* @__PURE__ */ jsx(
1060
967
  StepperStep,
@@ -1069,7 +976,7 @@ var FormStepper = (props) => {
1069
976
  }
1070
977
  return child;
1071
978
  });
1072
- const onChange = React13.useCallback((i) => {
979
+ const onChange = React12.useCallback((i) => {
1073
980
  setIndex(i);
1074
981
  }, []);
1075
982
  return /* @__PURE__ */ jsx(
@@ -1098,7 +1005,7 @@ var PrevButton = (props) => {
1098
1005
  Button,
1099
1006
  {
1100
1007
  isDisabled: isFirstStep || isCompleted,
1101
- label: "Back",
1008
+ children: "Back",
1102
1009
  ...props,
1103
1010
  className: cx("sui-form__prev-button", props.className),
1104
1011
  onClick: callAllHandlers(props.onClick, prevStep)
@@ -1157,12 +1064,96 @@ var WatchField = (props) => {
1157
1064
  });
1158
1065
  return props.children(field, form) || null;
1159
1066
  };
1067
+ var Form = forwardRef(
1068
+ (props, ref) => {
1069
+ const {
1070
+ mode = "all",
1071
+ resolver,
1072
+ fieldResolver,
1073
+ fields,
1074
+ reValidateMode,
1075
+ shouldFocusError,
1076
+ shouldUnregister,
1077
+ shouldUseNativeValidation,
1078
+ criteriaMode,
1079
+ delayError,
1080
+ schema,
1081
+ defaultValues,
1082
+ values,
1083
+ context,
1084
+ resetOptions,
1085
+ onChange,
1086
+ onSubmit,
1087
+ onError,
1088
+ formRef,
1089
+ children,
1090
+ ...rest
1091
+ } = props;
1092
+ const form = {
1093
+ mode,
1094
+ resolver,
1095
+ defaultValues,
1096
+ values,
1097
+ reValidateMode,
1098
+ shouldFocusError,
1099
+ shouldUnregister,
1100
+ shouldUseNativeValidation,
1101
+ criteriaMode,
1102
+ delayError,
1103
+ context,
1104
+ resetOptions
1105
+ };
1106
+ const methods = useForm(form);
1107
+ const { handleSubmit } = methods;
1108
+ React12.useImperativeHandle(formRef, () => methods, [formRef, methods]);
1109
+ React12.useEffect(() => {
1110
+ let subscription;
1111
+ if (onChange) {
1112
+ subscription = methods.watch(onChange);
1113
+ }
1114
+ return () => subscription == null ? void 0 : subscription.unsubscribe();
1115
+ }, [methods, onChange]);
1116
+ let _children = children;
1117
+ if (!_children && fieldResolver) {
1118
+ _children = /* @__PURE__ */ jsxs(FormLayout, { children: [
1119
+ /* @__PURE__ */ jsx(AutoFields, {}),
1120
+ /* @__PURE__ */ jsx(SubmitButton, { ...fields == null ? void 0 : fields.submit })
1121
+ ] });
1122
+ }
1123
+ return /* @__PURE__ */ jsx(
1124
+ FormProvider,
1125
+ {
1126
+ ...methods,
1127
+ schema,
1128
+ fieldResolver,
1129
+ fields,
1130
+ children: /* @__PURE__ */ jsx(
1131
+ chakra.form,
1132
+ {
1133
+ ref,
1134
+ onSubmit: handleSubmit(onSubmit, onError),
1135
+ ...rest,
1136
+ className: cx("sui-form", props.className),
1137
+ children: runIfFn(_children, {
1138
+ Field,
1139
+ DisplayIf,
1140
+ ArrayField,
1141
+ ObjectField,
1142
+ ...methods
1143
+ })
1144
+ }
1145
+ )
1146
+ }
1147
+ );
1148
+ }
1149
+ );
1150
+ Form.displayName = "Form";
1160
1151
  function createForm({
1161
1152
  resolver,
1162
1153
  fieldResolver = objectFieldResolver,
1163
1154
  fields
1164
1155
  } = {}) {
1165
- const CreateForm = forwardRef(
1156
+ const DefaultForm = forwardRef(
1166
1157
  (props, ref) => {
1167
1158
  const { schema, ...rest } = props;
1168
1159
  return /* @__PURE__ */ jsx(FieldsProvider, { value: fields || {}, children: /* @__PURE__ */ jsx(
@@ -1176,10 +1167,30 @@ function createForm({
1176
1167
  ) });
1177
1168
  }
1178
1169
  );
1179
- return CreateForm;
1170
+ DefaultForm.displayName = "Form";
1171
+ DefaultForm.id = "Form";
1172
+ return DefaultForm;
1173
+ }
1174
+ function createStepForm(Form4) {
1175
+ const StepForm2 = forwardRef((props, ref) => {
1176
+ const { children, ...rest } = props;
1177
+ const stepper = useStepForm(props);
1178
+ const { getFormProps, ...ctx } = stepper;
1179
+ const context = useMemo(() => ctx, [ctx]);
1180
+ return /* @__PURE__ */ jsx(StepperProvider, { value: context, children: /* @__PURE__ */ jsx(StepFormProvider, { value: context, children: /* @__PURE__ */ jsx(Form4, { ref, ...rest, ...getFormProps(), children: runIfFn(children, {
1181
+ ...stepper,
1182
+ Field,
1183
+ DisplayIf,
1184
+ ArrayField,
1185
+ ObjectField
1186
+ }) }) }) });
1187
+ });
1188
+ StepForm2.displayName = `Step${Form4.displayName || Form4.name}`;
1189
+ return StepForm2;
1180
1190
  }
1181
- var Form2 = createForm();
1191
+ var Form3 = createForm();
1192
+ var StepForm = createStepForm(Form3);
1182
1193
 
1183
- export { ArrayField, ArrayFieldAddButton, ArrayFieldContainer, ArrayFieldProvider, ArrayFieldRemoveButton, ArrayFieldRow, ArrayFieldRowContainer, ArrayFieldRowFields, ArrayFieldRowProvider, ArrayFieldRows, AutoFields, BaseField, Form as BaseForm, CheckboxField, DisplayField, DisplayIf, Field, FieldsProvider, Form2 as Form, FormLayout, FormLegend, FormProvider, FormStep, FormStepper, FormValue, InputField, InputRightButton, NativeSelect, NativeSelectField, NextButton, NumberInputField2 as NumberInputField, ObjectField, PasswordInput, PasswordInputField, PinField, PrevButton, RadioField, RadioInput, Select, SelectButton, SelectField, SelectList, SelectOption, StepForm, StepFormProvider, SubmitButton, SwitchField, TextareaField, WatchField, createField, createForm, defaultFieldTypes, objectFieldResolver, useArrayField, useArrayFieldAddButton, useArrayFieldContext, useArrayFieldRemoveButton, useArrayFieldRow, useArrayFieldRowContext, useField, useFormContext, useFormStep, useStepForm, useStepFormContext };
1194
+ export { ArrayField, ArrayFieldAddButton, ArrayFieldContainer, ArrayFieldProvider, ArrayFieldRemoveButton, ArrayFieldRow, ArrayFieldRowContainer, ArrayFieldRowFields, ArrayFieldRowProvider, ArrayFieldRows, AutoFields, BaseField, Form as BaseForm, CheckboxField, DisplayField, DisplayIf, Field, FieldsProvider, Form3 as Form, FormLayout, FormLegend, FormProvider, FormStep, FormStepper, FormValue, InputField, InputRightButton, NativeSelect, NativeSelectField, NextButton, NumberInputField2 as NumberInputField, ObjectField, PasswordInput, PasswordInputField, PinField, PrevButton, RadioField, RadioInput, Select, SelectButton, SelectField, SelectList, SelectOption, StepForm, StepFormProvider, SubmitButton, SwitchField, TextareaField, WatchField, createField, createForm, defaultFieldTypes, objectFieldResolver, useArrayField, useArrayFieldAddButton, useArrayFieldContext, useArrayFieldRemoveButton, useArrayFieldRow, useArrayFieldRowContext, useField, useFormContext, useFormStep, useStepForm, useStepFormContext };
1184
1195
  //# sourceMappingURL=out.js.map
1185
1196
  //# sourceMappingURL=index.mjs.map