@saas-ui/forms 2.0.0-next.6 → 2.0.0-next.8

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import * as React11 from 'react';
2
- import React11__default, { createContext, useState, useContext } from 'react';
1
+ import * as React13 from 'react';
2
+ import React13__default, { useState } 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';
@@ -7,12 +7,12 @@ import { forwardRef, NumberInput as NumberInput$1, NumberInputField, NumberInput
7
7
  import { StepperProvider, ChevronUpIcon, ChevronDownIcon, useStepper, useStep, useStepperContext, StepperStep, StepperContainer, StepperSteps } 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
- import { createContext as createContext$1 } from '@chakra-ui/react-utils';
10
+ import { createContext } from '@chakra-ui/react-utils';
11
11
 
12
12
  // src/form-context.tsx
13
- var FormContext = createContext(null);
13
+ var FormContext = React13.createContext(null);
14
14
  var useFormContext = () => {
15
- const context = useContext(FormContext);
15
+ const context = React13.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 React11.Children.map(children, (child) => {
131
- if (React11.isValidElement(child) && child.props.name) {
130
+ return React13.Children.map(children, (child) => {
131
+ if (React13.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 React11.cloneElement(child, {
138
+ return React13.cloneElement(child, {
139
139
  ...child.props,
140
140
  name: `${name}.${childName}`
141
141
  });
@@ -174,7 +174,7 @@ var RadioInput = forwardRef(
174
174
  }
175
175
  );
176
176
  RadioInput.displayName = "RadioInput";
177
- var [SelectProvider, useSelectContext] = createContext$1({
177
+ var [SelectProvider, useSelectContext] = createContext({
178
178
  strict: true
179
179
  });
180
180
  var useSelect = (props) => {
@@ -195,14 +195,14 @@ var useSelect = (props) => {
195
195
  onChange
196
196
  });
197
197
  const controlProps = useFormControl({ name });
198
- const options = React11__default.useMemo(
198
+ const options = React13__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 = React11__default.useCallback(
205
+ const getDisplayValue = React13__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 = React11__default.useMemo(
219
+ const displayValue = React13__default.useMemo(
220
220
  () => currentValue ? (Array.isArray(currentValue) ? currentValue : [currentValue]).map(
221
221
  getDisplayValue
222
222
  ) : [],
@@ -245,6 +245,7 @@ var SelectButton = forwardRef(
245
245
  placeholder,
246
246
  isDisabled: isSelectDisabled
247
247
  } = useSelectContext();
248
+ const context = useFormControlContext();
248
249
  const {
249
250
  isInvalid,
250
251
  isReadOnly,
@@ -254,7 +255,7 @@ var SelectButton = forwardRef(
254
255
  id,
255
256
  onBlur,
256
257
  onFocus
257
- } = useFormControlContext();
258
+ } = context || {};
258
259
  const { rightIcon = /* @__PURE__ */ jsx(ChevronDownIcon, {}), ...rest } = props;
259
260
  const focusStyles = (_a = styles.field) == null ? void 0 : _a._focusVisible;
260
261
  const readOnlyStyles = (_b = styles.field) == null ? void 0 : _b._readOnly;
@@ -279,7 +280,7 @@ var SelectButton = forwardRef(
279
280
  MenuButton,
280
281
  {
281
282
  as: Button,
282
- id,
283
+ id: id || React13.useId(),
283
284
  ...rest,
284
285
  onFocus,
285
286
  onBlur,
@@ -552,7 +553,7 @@ var defaultFieldTypes = {
552
553
  pin: PinField,
553
554
  "native-select": NativeSelectField
554
555
  };
555
- var FieldsContext = React11__default.createContext(
556
+ var FieldsContext = React13__default.createContext(
556
557
  null
557
558
  );
558
559
  var FieldsProvider = (props) => {
@@ -560,11 +561,11 @@ var FieldsProvider = (props) => {
560
561
  return /* @__PURE__ */ jsx(FieldsContext.Provider, { value: fields, children: props.children });
561
562
  };
562
563
  var useField = (type) => {
563
- const context = React11__default.useContext(FieldsContext);
564
+ const context = React13__default.useContext(FieldsContext);
564
565
  return (context == null ? void 0 : context[type]) || InputField;
565
566
  };
566
567
  var defaultInputType = "text";
567
- var Field = React11.forwardRef(
568
+ var Field = React13.forwardRef(
568
569
  (props, ref) => {
569
570
  const { type = defaultInputType, name } = props;
570
571
  const overrides = useFieldProps(name);
@@ -591,8 +592,8 @@ var FormLayout = ({ children, ...props }) => {
591
592
  {
592
593
  ...gridProps,
593
594
  className: cx("sui-form__layout", props.className),
594
- children: React11.Children.map(children, (child) => {
595
- if (React11.isValidElement(child)) {
595
+ children: React13.Children.map(children, (child) => {
596
+ if (React13.isValidElement(child)) {
596
597
  return /* @__PURE__ */ jsx(FormLayoutItem, { children: child });
597
598
  }
598
599
  return child;
@@ -601,10 +602,10 @@ var FormLayout = ({ children, ...props }) => {
601
602
  );
602
603
  };
603
604
  FormLayout.displayName = "FormLayout";
604
- var [ArrayFieldProvider, useArrayFieldContext] = createContext$1({
605
+ var [ArrayFieldProvider, useArrayFieldContext] = createContext({
605
606
  name: "ArrayFieldContext"
606
607
  });
607
- var [ArrayFieldRowProvider, useArrayFieldRowContext] = createContext$1({
608
+ var [ArrayFieldRowProvider, useArrayFieldRowContext] = createContext({
608
609
  name: "ArrayFieldRowContext"
609
610
  });
610
611
  var useArrayField = ({
@@ -631,7 +632,7 @@ var useArrayField = ({
631
632
  var useArrayFieldRow = ({ index }) => {
632
633
  const { clearErrors } = useFormContext();
633
634
  const { name, remove, fields } = useArrayFieldContext();
634
- React11.useEffect(() => {
635
+ React13.useEffect(() => {
635
636
  clearErrors(name);
636
637
  }, []);
637
638
  return {
@@ -639,7 +640,7 @@ var useArrayFieldRow = ({ index }) => {
639
640
  isFirst: index === 0,
640
641
  isLast: index === fields.length - 1,
641
642
  name: `${name}.${index}`,
642
- remove: React11.useCallback(() => {
643
+ remove: React13.useCallback(() => {
643
644
  clearErrors(name);
644
645
  remove(index);
645
646
  }, [index])
@@ -734,7 +735,7 @@ var ArrayFieldRows = ({
734
735
  return children(fields);
735
736
  };
736
737
  ArrayFieldRows.displayName = "ArrayFieldRows";
737
- var ArrayFieldContainer = React11.forwardRef(
738
+ var ArrayFieldContainer = React13.forwardRef(
738
739
  ({
739
740
  name,
740
741
  defaultValue,
@@ -751,7 +752,7 @@ var ArrayFieldContainer = React11.forwardRef(
751
752
  min,
752
753
  max
753
754
  });
754
- React11.useImperativeHandle(ref, () => context, [ref, context]);
755
+ React13.useImperativeHandle(ref, () => context, [ref, context]);
755
756
  return /* @__PURE__ */ jsx(ArrayFieldProvider, { value: context, children: /* @__PURE__ */ jsx(BaseField, { name, ...fieldProps, children }) });
756
757
  }
757
758
  );
@@ -791,10 +792,10 @@ var AutoFields = ({
791
792
  const context = useFormContext();
792
793
  const schema = schemaProp || context.schema;
793
794
  const fieldResolver = fieldResolverProp || context.fieldResolver;
794
- const resolver = React11.useMemo(() => fieldResolver, [schema, fieldResolver]);
795
- const fields = React11.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
795
+ const resolver = React13.useMemo(() => fieldResolver, [schema, fieldResolver]);
796
+ const fields = React13.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
796
797
  const form = useFormContext();
797
- React11.useEffect(() => {
798
+ React13.useEffect(() => {
798
799
  var _a;
799
800
  if (focusFirstField && ((_a = fields == null ? void 0 : fields[0]) == null ? void 0 : _a.name)) {
800
801
  form.setFocus(fields[0].name);
@@ -910,8 +911,8 @@ var Form = forwardRef(
910
911
  };
911
912
  const methods = useForm(form);
912
913
  const { handleSubmit } = methods;
913
- React11.useImperativeHandle(formRef, () => methods, [formRef, methods]);
914
- React11.useEffect(() => {
914
+ React13.useImperativeHandle(formRef, () => methods, [formRef, methods]);
915
+ React13.useEffect(() => {
915
916
  let subscription;
916
917
  if (onChange) {
917
918
  subscription = methods.watch(onChange);
@@ -953,7 +954,7 @@ var Form = forwardRef(
953
954
  }
954
955
  );
955
956
  Form.displayName = "Form";
956
- var [StepFormProvider, useStepFormContext] = createContext$1({
957
+ var [StepFormProvider, useStepFormContext] = createContext({
957
958
  name: "StepFormContext",
958
959
  errorMessage: "useStepFormContext: `context` is undefined. Seems you forgot to wrap step form components in `<StepForm />`"
959
960
  });
@@ -961,8 +962,8 @@ function useStepForm(props) {
961
962
  const { onChange, ...rest } = props;
962
963
  const stepper = useStepper(rest);
963
964
  const { activeStep, isLastStep, nextStep } = stepper;
964
- const [steps, updateSteps] = React11.useState({});
965
- const onSubmitStep = React11.useCallback(
965
+ const [steps, updateSteps] = React13.useState({});
966
+ const onSubmitStep = React13.useCallback(
966
967
  async (data) => {
967
968
  var _a, _b;
968
969
  try {
@@ -983,7 +984,7 @@ function useStepForm(props) {
983
984
  },
984
985
  [steps, activeStep, isLastStep]
985
986
  );
986
- const getFormProps = React11.useCallback(() => {
987
+ const getFormProps = React13.useCallback(() => {
987
988
  const step = steps[activeStep];
988
989
  return {
989
990
  onSubmit: onSubmitStep,
@@ -991,7 +992,7 @@ function useStepForm(props) {
991
992
  resolver: step == null ? void 0 : step.resolver
992
993
  };
993
994
  }, [steps, onSubmitStep, activeStep]);
994
- const updateStep = React11.useCallback(
995
+ const updateStep = React13.useCallback(
995
996
  (step) => {
996
997
  updateSteps((steps2) => {
997
998
  return {
@@ -1013,7 +1014,7 @@ function useFormStep(props) {
1013
1014
  const { name, schema, resolver, onSubmit } = props;
1014
1015
  const step = useStep({ name });
1015
1016
  const { steps, updateStep } = useStepFormContext();
1016
- React11.useEffect(() => {
1017
+ React13.useEffect(() => {
1017
1018
  updateStep({ name, schema, resolver, onSubmit });
1018
1019
  }, [name, schema]);
1019
1020
  return {
@@ -1021,20 +1022,20 @@ function useFormStep(props) {
1021
1022
  ...steps[name] || { name, schema }
1022
1023
  };
1023
1024
  }
1024
- var StepForm = React11.forwardRef(
1025
+ var StepForm = React13.forwardRef(
1025
1026
  (props, ref) => {
1026
1027
  const { children, ...rest } = props;
1027
1028
  const stepper = useStepForm(props);
1028
1029
  const { getFormProps, ...ctx } = stepper;
1029
- const context = React11.useMemo(() => ctx, [ctx]);
1030
+ const context = React13.useMemo(() => ctx, [ctx]);
1030
1031
  return /* @__PURE__ */ jsx(StepperProvider, { value: context, children: /* @__PURE__ */ jsx(StepFormProvider, { value: context, children: /* @__PURE__ */ jsx(Form, { ref, ...rest, ...getFormProps(), children: runIfFn(children, stepper) }) }) });
1031
1032
  }
1032
1033
  );
1033
1034
  var FormStepper = (props) => {
1034
1035
  const { activeIndex, setIndex } = useStepperContext();
1035
1036
  const { children, orientation, variant, colorScheme, size, ...rest } = props;
1036
- const elements = React11.Children.map(children, (child) => {
1037
- if (React11.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
1037
+ const elements = React13.Children.map(children, (child) => {
1038
+ if (React13.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
1038
1039
  const { isCompleted } = useFormStep(child.props);
1039
1040
  return /* @__PURE__ */ jsx(
1040
1041
  StepperStep,
@@ -1049,7 +1050,7 @@ var FormStepper = (props) => {
1049
1050
  }
1050
1051
  return child;
1051
1052
  });
1052
- const onChange = React11.useCallback((i) => {
1053
+ const onChange = React13.useCallback((i) => {
1053
1054
  setIndex(i);
1054
1055
  }, []);
1055
1056
  return /* @__PURE__ */ jsx(