@saas-ui/forms 2.0.0-next.6 → 2.0.0-next.7
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +7 -0
- package/dist/index.js +38 -37
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +35 -34
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/select/select.tsx +4 -2
package/dist/index.mjs
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import * as
|
2
|
-
import
|
1
|
+
import * as React12 from 'react';
|
2
|
+
import React12__default, { createContext, useState, useContext } 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';
|
@@ -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 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
|
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 =
|
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 =
|
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 =
|
219
|
+
const displayValue = React12__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
|
-
} =
|
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 || React12.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 =
|
556
|
+
var FieldsContext = React12__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 =
|
564
|
+
const context = React12__default.useContext(FieldsContext);
|
564
565
|
return (context == null ? void 0 : context[type]) || InputField;
|
565
566
|
};
|
566
567
|
var defaultInputType = "text";
|
567
|
-
var Field =
|
568
|
+
var Field = React12.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:
|
595
|
-
if (
|
595
|
+
children: React12.Children.map(children, (child) => {
|
596
|
+
if (React12.isValidElement(child)) {
|
596
597
|
return /* @__PURE__ */ jsx(FormLayoutItem, { children: child });
|
597
598
|
}
|
598
599
|
return child;
|
@@ -631,7 +632,7 @@ var useArrayField = ({
|
|
631
632
|
var useArrayFieldRow = ({ index }) => {
|
632
633
|
const { clearErrors } = useFormContext();
|
633
634
|
const { name, remove, fields } = useArrayFieldContext();
|
634
|
-
|
635
|
+
React12.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:
|
643
|
+
remove: React12.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 =
|
738
|
+
var ArrayFieldContainer = React12.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
|
-
|
755
|
+
React12.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 =
|
795
|
-
const fields =
|
795
|
+
const resolver = React12.useMemo(() => fieldResolver, [schema, fieldResolver]);
|
796
|
+
const fields = React12.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
|
796
797
|
const form = useFormContext();
|
797
|
-
|
798
|
+
React12.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
|
-
|
914
|
-
|
914
|
+
React12.useImperativeHandle(formRef, () => methods, [formRef, methods]);
|
915
|
+
React12.useEffect(() => {
|
915
916
|
let subscription;
|
916
917
|
if (onChange) {
|
917
918
|
subscription = methods.watch(onChange);
|
@@ -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] =
|
965
|
-
const onSubmitStep =
|
965
|
+
const [steps, updateSteps] = React12.useState({});
|
966
|
+
const onSubmitStep = React12.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 =
|
987
|
+
const getFormProps = React12.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 =
|
995
|
+
const updateStep = React12.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
|
-
|
1017
|
+
React12.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 =
|
1025
|
+
var StepForm = React12.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 =
|
1030
|
+
const context = React12.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 =
|
1037
|
-
if (
|
1037
|
+
const elements = React12.Children.map(children, (child) => {
|
1038
|
+
if (React12.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 =
|
1053
|
+
const onChange = React12.useCallback((i) => {
|
1053
1054
|
setIndex(i);
|
1054
1055
|
}, []);
|
1055
1056
|
return /* @__PURE__ */ jsx(
|