@saas-ui/forms 2.0.0-rc.30 → 2.0.0-rc.32
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +15 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +59 -43
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +58 -42
- package/dist/index.mjs.map +1 -1
- package/dist/yup/index.d.ts +2 -2
- package/dist/zod/index.d.ts +2 -2
- package/package.json +2 -2
- package/src/select/select-context.tsx +4 -0
- package/src/select/select.stories.tsx +79 -0
- package/src/select/select.tsx +46 -25
package/dist/index.mjs
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
import * as
|
2
|
-
import
|
1
|
+
import * as React11 from 'react';
|
2
|
+
import React11__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
|
-
import { forwardRef, NumberInput as NumberInput$1, NumberInputField, NumberInputStepper, NumberIncrementStepper, NumberDecrementStepper, InputRightElement, Button, InputGroup, Input, RadioGroup, Stack, Radio,
|
6
|
+
import { forwardRef, NumberInput as NumberInput$1, NumberInputField, NumberInputStepper, NumberIncrementStepper, NumberDecrementStepper, InputRightElement, Button, InputGroup, Input, RadioGroup, Stack, Radio, createStylesContext, useFormControlContext, useMenuButton, chakra, useMultiStyleConfig, omitThemingProps, Menu, 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
7
|
import { ChevronUpIcon, ChevronDownIcon, useStepper, useStep, useStepperContext, StepsItem, Steps, 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 =
|
13
|
+
var FormContext = React11.createContext(null);
|
14
14
|
var useFormContext = () => {
|
15
|
-
const context =
|
15
|
+
const context = React11.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 React11.Children.map(children, (child) => {
|
131
|
+
if (React11.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 React11.cloneElement(child, {
|
139
139
|
...child.props,
|
140
140
|
name: `${name}.${childName}`
|
141
141
|
});
|
@@ -174,6 +174,7 @@ var RadioInput = forwardRef(
|
|
174
174
|
}
|
175
175
|
);
|
176
176
|
RadioInput.displayName = "RadioInput";
|
177
|
+
var [SelectStylesProvider, useSelectStyles] = createStylesContext("SuiSelect");
|
177
178
|
var [SelectProvider, useSelectContext] = createContext({
|
178
179
|
strict: true
|
179
180
|
});
|
@@ -195,14 +196,14 @@ var useSelect = (props) => {
|
|
195
196
|
onChange
|
196
197
|
});
|
197
198
|
const controlProps = useFormControl({ name });
|
198
|
-
const options =
|
199
|
+
const options = React11__default.useMemo(
|
199
200
|
() => optionsProp && mapOptions(optionsProp),
|
200
201
|
[optionsProp]
|
201
202
|
);
|
202
203
|
const handleChange = (value2) => {
|
203
204
|
setCurrentValue(value2);
|
204
205
|
};
|
205
|
-
const getDisplayValue =
|
206
|
+
const getDisplayValue = React11__default.useCallback(
|
206
207
|
(value2) => {
|
207
208
|
if (!options) {
|
208
209
|
return value2;
|
@@ -216,7 +217,7 @@ var useSelect = (props) => {
|
|
216
217
|
},
|
217
218
|
[options]
|
218
219
|
);
|
219
|
-
const displayValue =
|
220
|
+
const displayValue = React11__default.useMemo(
|
220
221
|
() => currentValue ? (Array.isArray(currentValue) ? currentValue : [currentValue]).map(
|
221
222
|
getDisplayValue
|
222
223
|
) : [],
|
@@ -238,7 +239,6 @@ var useSelect = (props) => {
|
|
238
239
|
var SelectButton = forwardRef(
|
239
240
|
(props, ref) => {
|
240
241
|
var _a, _b, _c, _d, _e;
|
241
|
-
const styles = useMultiStyleConfig("SuiSelect", props);
|
242
242
|
const {
|
243
243
|
displayValue,
|
244
244
|
renderValue,
|
@@ -246,6 +246,7 @@ var SelectButton = forwardRef(
|
|
246
246
|
isDisabled: isSelectDisabled
|
247
247
|
} = useSelectContext();
|
248
248
|
const context = useFormControlContext();
|
249
|
+
const styles = useSelectStyles();
|
249
250
|
const {
|
250
251
|
isInvalid,
|
251
252
|
isReadOnly,
|
@@ -273,15 +274,17 @@ var SelectButton = forwardRef(
|
|
273
274
|
_expanded: focusStyles,
|
274
275
|
_readOnly: readOnlyStyles,
|
275
276
|
_invalid: invalid,
|
277
|
+
minW: 0,
|
276
278
|
...styles.field,
|
277
279
|
h: "auto"
|
278
280
|
};
|
281
|
+
const buttonProps = useMenuButton(rest, ref);
|
279
282
|
return /* @__PURE__ */ jsx(
|
280
|
-
|
283
|
+
Button,
|
281
284
|
{
|
282
|
-
|
283
|
-
id: id || React12.useId(),
|
285
|
+
...buttonProps,
|
284
286
|
...buttonStyles,
|
287
|
+
id: id || buttonProps.id,
|
285
288
|
...rest,
|
286
289
|
onFocus,
|
287
290
|
onBlur,
|
@@ -291,8 +294,20 @@ var SelectButton = forwardRef(
|
|
291
294
|
"data-focus": dataAttr(isFocused),
|
292
295
|
"data-required": dataAttr(isRequired),
|
293
296
|
rightIcon,
|
294
|
-
|
295
|
-
|
297
|
+
children: /* @__PURE__ */ jsx(
|
298
|
+
chakra.span,
|
299
|
+
{
|
300
|
+
__css: {
|
301
|
+
display: "block",
|
302
|
+
pointerEvents: "none",
|
303
|
+
flex: "1 1 auto",
|
304
|
+
minW: 0,
|
305
|
+
overflow: "hidden",
|
306
|
+
textOverflow: "ellipsis"
|
307
|
+
},
|
308
|
+
children: renderValue(displayValue) || placeholder
|
309
|
+
}
|
310
|
+
)
|
296
311
|
}
|
297
312
|
);
|
298
313
|
}
|
@@ -300,10 +315,11 @@ var SelectButton = forwardRef(
|
|
300
315
|
SelectButton.displayName = "SelectButton";
|
301
316
|
var Select = forwardRef((props, ref) => {
|
302
317
|
const { name, children, isDisabled, multiple, ...rest } = props;
|
318
|
+
const styles = useMultiStyleConfig("SuiSelect", props);
|
303
319
|
const menuProps = omitThemingProps(rest);
|
304
320
|
const context = useSelect(props);
|
305
321
|
const { value, controlProps } = context;
|
306
|
-
return /* @__PURE__ */ jsx(SelectProvider, { value: context, children: /* @__PURE__ */ jsx(Menu, { ...menuProps, closeOnSelect: !multiple, children: /* @__PURE__ */ jsxs(chakra.div, { className: cx("sui-select"), children: [
|
322
|
+
return /* @__PURE__ */ jsx(SelectProvider, { value: context, children: /* @__PURE__ */ jsx(SelectStylesProvider, { value: styles, children: /* @__PURE__ */ jsx(Menu, { ...menuProps, closeOnSelect: !multiple, children: /* @__PURE__ */ jsxs(chakra.div, { className: cx("sui-select"), children: [
|
307
323
|
children,
|
308
324
|
/* @__PURE__ */ jsx(
|
309
325
|
chakra.input,
|
@@ -316,7 +332,7 @@ var Select = forwardRef((props, ref) => {
|
|
316
332
|
className: "saas-select__input"
|
317
333
|
}
|
318
334
|
)
|
319
|
-
] }) }) });
|
335
|
+
] }) }) }) });
|
320
336
|
});
|
321
337
|
var SelectList = (props) => {
|
322
338
|
const { defaultValue, value, options, multiple, onChange } = useSelectContext();
|
@@ -553,7 +569,7 @@ var defaultFieldTypes = {
|
|
553
569
|
pin: PinField,
|
554
570
|
"native-select": NativeSelectField
|
555
571
|
};
|
556
|
-
var FieldsContext =
|
572
|
+
var FieldsContext = React11__default.createContext(
|
557
573
|
null
|
558
574
|
);
|
559
575
|
var FieldsProvider = (props) => {
|
@@ -561,11 +577,11 @@ var FieldsProvider = (props) => {
|
|
561
577
|
return /* @__PURE__ */ jsx(FieldsContext.Provider, { value: fields, children: props.children });
|
562
578
|
};
|
563
579
|
var useField = (type) => {
|
564
|
-
const context =
|
580
|
+
const context = React11__default.useContext(FieldsContext);
|
565
581
|
return (context == null ? void 0 : context[type]) || InputField;
|
566
582
|
};
|
567
583
|
var defaultInputType = "text";
|
568
|
-
var Field =
|
584
|
+
var Field = React11.forwardRef(
|
569
585
|
(props, ref) => {
|
570
586
|
const { type = defaultInputType, name } = props;
|
571
587
|
const overrides = useFieldProps(name);
|
@@ -592,8 +608,8 @@ var FormLayout = ({ children, ...props }) => {
|
|
592
608
|
{
|
593
609
|
...gridProps,
|
594
610
|
className: cx("sui-form__layout", props.className),
|
595
|
-
children:
|
596
|
-
if (
|
611
|
+
children: React11.Children.map(children, (child) => {
|
612
|
+
if (React11.isValidElement(child)) {
|
597
613
|
return /* @__PURE__ */ jsx(FormLayoutItem, { children: child });
|
598
614
|
}
|
599
615
|
return child;
|
@@ -632,7 +648,7 @@ var useArrayField = ({
|
|
632
648
|
var useArrayFieldRow = ({ index }) => {
|
633
649
|
const { clearErrors } = useFormContext();
|
634
650
|
const { name, remove, fields } = useArrayFieldContext();
|
635
|
-
|
651
|
+
React11.useEffect(() => {
|
636
652
|
clearErrors(name);
|
637
653
|
}, []);
|
638
654
|
return {
|
@@ -640,7 +656,7 @@ var useArrayFieldRow = ({ index }) => {
|
|
640
656
|
isFirst: index === 0,
|
641
657
|
isLast: index === fields.length - 1,
|
642
658
|
name: `${name}.${index}`,
|
643
|
-
remove:
|
659
|
+
remove: React11.useCallback(() => {
|
644
660
|
clearErrors(name);
|
645
661
|
remove(index);
|
646
662
|
}, [index])
|
@@ -735,7 +751,7 @@ var ArrayFieldRows = ({
|
|
735
751
|
return children(fields);
|
736
752
|
};
|
737
753
|
ArrayFieldRows.displayName = "ArrayFieldRows";
|
738
|
-
var ArrayFieldContainer =
|
754
|
+
var ArrayFieldContainer = React11.forwardRef(
|
739
755
|
({
|
740
756
|
name,
|
741
757
|
defaultValue,
|
@@ -753,7 +769,7 @@ var ArrayFieldContainer = React12.forwardRef(
|
|
753
769
|
min: min || (overrides == null ? void 0 : overrides.min),
|
754
770
|
max: max || (overrides == null ? void 0 : overrides.max)
|
755
771
|
});
|
756
|
-
|
772
|
+
React11.useImperativeHandle(ref, () => context, [ref, context]);
|
757
773
|
return /* @__PURE__ */ jsx(ArrayFieldProvider, { value: context, children: /* @__PURE__ */ jsx(BaseField, { name, ...fieldProps, ...overrides, children }) });
|
758
774
|
}
|
759
775
|
);
|
@@ -811,10 +827,10 @@ var AutoFields = ({
|
|
811
827
|
const context = useFormContext();
|
812
828
|
const schema = schemaProp || context.schema;
|
813
829
|
const fieldResolver = fieldResolverProp || context.fieldResolver;
|
814
|
-
const resolver =
|
815
|
-
const fields =
|
830
|
+
const resolver = React11.useMemo(() => fieldResolver, [schema, fieldResolver]);
|
831
|
+
const fields = React11.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
|
816
832
|
const form = useFormContext();
|
817
|
-
|
833
|
+
React11.useEffect(() => {
|
818
834
|
var _a;
|
819
835
|
if (focusFirstField && ((_a = fields == null ? void 0 : fields[0]) == null ? void 0 : _a.name)) {
|
820
836
|
form.setFocus(fields[0].name);
|
@@ -896,10 +912,10 @@ var [StepFormProvider, useStepFormContext] = createContext({
|
|
896
912
|
function useStepForm(props) {
|
897
913
|
const { onChange, steps: stepsOptions, resolver, ...rest } = props;
|
898
914
|
const stepper = useStepper(rest);
|
899
|
-
const [options, setOptions] =
|
915
|
+
const [options, setOptions] = React11.useState(stepsOptions);
|
900
916
|
const { activeStep, isLastStep, nextStep } = stepper;
|
901
|
-
const [steps, updateSteps] =
|
902
|
-
const onSubmitStep =
|
917
|
+
const [steps, updateSteps] = React11.useState({});
|
918
|
+
const onSubmitStep = React11.useCallback(
|
903
919
|
async (data) => {
|
904
920
|
var _a, _b;
|
905
921
|
try {
|
@@ -920,7 +936,7 @@ function useStepForm(props) {
|
|
920
936
|
},
|
921
937
|
[steps, activeStep, isLastStep]
|
922
938
|
);
|
923
|
-
const getFormProps =
|
939
|
+
const getFormProps = React11.useCallback(() => {
|
924
940
|
const step = steps[activeStep];
|
925
941
|
return {
|
926
942
|
onSubmit: onSubmitStep,
|
@@ -931,7 +947,7 @@ function useStepForm(props) {
|
|
931
947
|
) : void 0
|
932
948
|
};
|
933
949
|
}, [steps, onSubmitStep, activeStep]);
|
934
|
-
const updateStep =
|
950
|
+
const updateStep = React11.useCallback(
|
935
951
|
(step) => {
|
936
952
|
const stepOptions = options == null ? void 0 : options.find((s) => s.name === step.name);
|
937
953
|
updateSteps((steps2) => {
|
@@ -957,7 +973,7 @@ function useFormStep(props) {
|
|
957
973
|
const { name, schema, resolver, onSubmit } = props;
|
958
974
|
const step = useStep({ name });
|
959
975
|
const { steps, updateStep } = useStepFormContext();
|
960
|
-
|
976
|
+
React11.useEffect(() => {
|
961
977
|
updateStep({ name, schema, resolver, onSubmit });
|
962
978
|
}, [name, schema]);
|
963
979
|
return {
|
@@ -977,8 +993,8 @@ var FormStepper = (props) => {
|
|
977
993
|
render,
|
978
994
|
...rest
|
979
995
|
} = props;
|
980
|
-
const elements =
|
981
|
-
if (
|
996
|
+
const elements = React11.Children.map(children, (child) => {
|
997
|
+
if (React11.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
|
982
998
|
const { isCompleted } = useFormStep(child.props);
|
983
999
|
return /* @__PURE__ */ jsx(
|
984
1000
|
StepsItem,
|
@@ -994,7 +1010,7 @@ var FormStepper = (props) => {
|
|
994
1010
|
}
|
995
1011
|
return child;
|
996
1012
|
});
|
997
|
-
const onChange =
|
1013
|
+
const onChange = React11.useCallback((i) => {
|
998
1014
|
setIndex(i);
|
999
1015
|
onChangeProp == null ? void 0 : onChangeProp(i);
|
1000
1016
|
}, []);
|
@@ -1124,8 +1140,8 @@ var Form = forwardRef(
|
|
1124
1140
|
};
|
1125
1141
|
const methods = useForm(form);
|
1126
1142
|
const { handleSubmit } = methods;
|
1127
|
-
|
1128
|
-
|
1143
|
+
React11.useImperativeHandle(formRef, () => methods, [formRef, methods]);
|
1144
|
+
React11.useEffect(() => {
|
1129
1145
|
let subscription;
|
1130
1146
|
if (onChange) {
|
1131
1147
|
subscription = methods.watch(onChange);
|