@saas-ui/forms 2.0.0-next.7 → 2.0.0-next.8
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +6 -0
- package/dist/index.js +36 -36
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +40 -40
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/form-context.tsx +3 -3
package/dist/index.mjs
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import * as
|
2
|
-
import
|
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
|
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
|
131
|
-
if (
|
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
|
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
|
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 =
|
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 =
|
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 =
|
219
|
+
const displayValue = React13__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 ||
|
283
|
+
id: id || React13.useId(),
|
284
284
|
...rest,
|
285
285
|
onFocus,
|
286
286
|
onBlur,
|
@@ -553,7 +553,7 @@ var defaultFieldTypes = {
|
|
553
553
|
pin: PinField,
|
554
554
|
"native-select": NativeSelectField
|
555
555
|
};
|
556
|
-
var FieldsContext =
|
556
|
+
var FieldsContext = React13__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 =
|
564
|
+
const context = React13__default.useContext(FieldsContext);
|
565
565
|
return (context == null ? void 0 : context[type]) || InputField;
|
566
566
|
};
|
567
567
|
var defaultInputType = "text";
|
568
|
-
var Field =
|
568
|
+
var Field = React13.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:
|
596
|
-
if (
|
595
|
+
children: React13.Children.map(children, (child) => {
|
596
|
+
if (React13.isValidElement(child)) {
|
597
597
|
return /* @__PURE__ */ jsx(FormLayoutItem, { children: child });
|
598
598
|
}
|
599
599
|
return child;
|
@@ -602,10 +602,10 @@ var FormLayout = ({ children, ...props }) => {
|
|
602
602
|
);
|
603
603
|
};
|
604
604
|
FormLayout.displayName = "FormLayout";
|
605
|
-
var [ArrayFieldProvider, useArrayFieldContext] = createContext
|
605
|
+
var [ArrayFieldProvider, useArrayFieldContext] = createContext({
|
606
606
|
name: "ArrayFieldContext"
|
607
607
|
});
|
608
|
-
var [ArrayFieldRowProvider, useArrayFieldRowContext] = createContext
|
608
|
+
var [ArrayFieldRowProvider, useArrayFieldRowContext] = createContext({
|
609
609
|
name: "ArrayFieldRowContext"
|
610
610
|
});
|
611
611
|
var useArrayField = ({
|
@@ -632,7 +632,7 @@ var useArrayField = ({
|
|
632
632
|
var useArrayFieldRow = ({ index }) => {
|
633
633
|
const { clearErrors } = useFormContext();
|
634
634
|
const { name, remove, fields } = useArrayFieldContext();
|
635
|
-
|
635
|
+
React13.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:
|
643
|
+
remove: React13.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 =
|
738
|
+
var ArrayFieldContainer = React13.forwardRef(
|
739
739
|
({
|
740
740
|
name,
|
741
741
|
defaultValue,
|
@@ -752,7 +752,7 @@ var ArrayFieldContainer = React12.forwardRef(
|
|
752
752
|
min,
|
753
753
|
max
|
754
754
|
});
|
755
|
-
|
755
|
+
React13.useImperativeHandle(ref, () => context, [ref, context]);
|
756
756
|
return /* @__PURE__ */ jsx(ArrayFieldProvider, { value: context, children: /* @__PURE__ */ jsx(BaseField, { name, ...fieldProps, children }) });
|
757
757
|
}
|
758
758
|
);
|
@@ -792,10 +792,10 @@ var AutoFields = ({
|
|
792
792
|
const context = useFormContext();
|
793
793
|
const schema = schemaProp || context.schema;
|
794
794
|
const fieldResolver = fieldResolverProp || context.fieldResolver;
|
795
|
-
const resolver =
|
796
|
-
const fields =
|
795
|
+
const resolver = React13.useMemo(() => fieldResolver, [schema, fieldResolver]);
|
796
|
+
const fields = React13.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
|
797
797
|
const form = useFormContext();
|
798
|
-
|
798
|
+
React13.useEffect(() => {
|
799
799
|
var _a;
|
800
800
|
if (focusFirstField && ((_a = fields == null ? void 0 : fields[0]) == null ? void 0 : _a.name)) {
|
801
801
|
form.setFocus(fields[0].name);
|
@@ -911,8 +911,8 @@ var Form = forwardRef(
|
|
911
911
|
};
|
912
912
|
const methods = useForm(form);
|
913
913
|
const { handleSubmit } = methods;
|
914
|
-
|
915
|
-
|
914
|
+
React13.useImperativeHandle(formRef, () => methods, [formRef, methods]);
|
915
|
+
React13.useEffect(() => {
|
916
916
|
let subscription;
|
917
917
|
if (onChange) {
|
918
918
|
subscription = methods.watch(onChange);
|
@@ -954,7 +954,7 @@ var Form = forwardRef(
|
|
954
954
|
}
|
955
955
|
);
|
956
956
|
Form.displayName = "Form";
|
957
|
-
var [StepFormProvider, useStepFormContext] = createContext
|
957
|
+
var [StepFormProvider, useStepFormContext] = createContext({
|
958
958
|
name: "StepFormContext",
|
959
959
|
errorMessage: "useStepFormContext: `context` is undefined. Seems you forgot to wrap step form components in `<StepForm />`"
|
960
960
|
});
|
@@ -962,8 +962,8 @@ function useStepForm(props) {
|
|
962
962
|
const { onChange, ...rest } = props;
|
963
963
|
const stepper = useStepper(rest);
|
964
964
|
const { activeStep, isLastStep, nextStep } = stepper;
|
965
|
-
const [steps, updateSteps] =
|
966
|
-
const onSubmitStep =
|
965
|
+
const [steps, updateSteps] = React13.useState({});
|
966
|
+
const onSubmitStep = React13.useCallback(
|
967
967
|
async (data) => {
|
968
968
|
var _a, _b;
|
969
969
|
try {
|
@@ -984,7 +984,7 @@ function useStepForm(props) {
|
|
984
984
|
},
|
985
985
|
[steps, activeStep, isLastStep]
|
986
986
|
);
|
987
|
-
const getFormProps =
|
987
|
+
const getFormProps = React13.useCallback(() => {
|
988
988
|
const step = steps[activeStep];
|
989
989
|
return {
|
990
990
|
onSubmit: onSubmitStep,
|
@@ -992,7 +992,7 @@ function useStepForm(props) {
|
|
992
992
|
resolver: step == null ? void 0 : step.resolver
|
993
993
|
};
|
994
994
|
}, [steps, onSubmitStep, activeStep]);
|
995
|
-
const updateStep =
|
995
|
+
const updateStep = React13.useCallback(
|
996
996
|
(step) => {
|
997
997
|
updateSteps((steps2) => {
|
998
998
|
return {
|
@@ -1014,7 +1014,7 @@ function useFormStep(props) {
|
|
1014
1014
|
const { name, schema, resolver, onSubmit } = props;
|
1015
1015
|
const step = useStep({ name });
|
1016
1016
|
const { steps, updateStep } = useStepFormContext();
|
1017
|
-
|
1017
|
+
React13.useEffect(() => {
|
1018
1018
|
updateStep({ name, schema, resolver, onSubmit });
|
1019
1019
|
}, [name, schema]);
|
1020
1020
|
return {
|
@@ -1022,20 +1022,20 @@ function useFormStep(props) {
|
|
1022
1022
|
...steps[name] || { name, schema }
|
1023
1023
|
};
|
1024
1024
|
}
|
1025
|
-
var StepForm =
|
1025
|
+
var StepForm = React13.forwardRef(
|
1026
1026
|
(props, ref) => {
|
1027
1027
|
const { children, ...rest } = props;
|
1028
1028
|
const stepper = useStepForm(props);
|
1029
1029
|
const { getFormProps, ...ctx } = stepper;
|
1030
|
-
const context =
|
1030
|
+
const context = React13.useMemo(() => ctx, [ctx]);
|
1031
1031
|
return /* @__PURE__ */ jsx(StepperProvider, { value: context, children: /* @__PURE__ */ jsx(StepFormProvider, { value: context, children: /* @__PURE__ */ jsx(Form, { ref, ...rest, ...getFormProps(), children: runIfFn(children, stepper) }) }) });
|
1032
1032
|
}
|
1033
1033
|
);
|
1034
1034
|
var FormStepper = (props) => {
|
1035
1035
|
const { activeIndex, setIndex } = useStepperContext();
|
1036
1036
|
const { children, orientation, variant, colorScheme, size, ...rest } = props;
|
1037
|
-
const elements =
|
1038
|
-
if (
|
1037
|
+
const elements = React13.Children.map(children, (child) => {
|
1038
|
+
if (React13.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
|
1039
1039
|
const { isCompleted } = useFormStep(child.props);
|
1040
1040
|
return /* @__PURE__ */ jsx(
|
1041
1041
|
StepperStep,
|
@@ -1050,7 +1050,7 @@ var FormStepper = (props) => {
|
|
1050
1050
|
}
|
1051
1051
|
return child;
|
1052
1052
|
});
|
1053
|
-
const onChange =
|
1053
|
+
const onChange = React13.useCallback((i) => {
|
1054
1054
|
setIndex(i);
|
1055
1055
|
}, []);
|
1056
1056
|
return /* @__PURE__ */ jsx(
|