@saas-ui/forms 2.0.0-rc.31 → 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 +8 -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/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
@@ -5,7 +5,7 @@ import { FieldValues, FieldPath, UseFieldArrayReturn, RegisterOptions, UseFormRe
|
|
5
5
|
export { BatchFieldArrayUpdate, ChangeHandler, Control, Controller, ControllerFieldState, ControllerProps, ControllerRenderProps, CriteriaMode, CustomElement, DeepMap, DeepPartial, DeepPartialSkipArrayKey, DefaultValues, DelayCallback, EmptyObject, ErrorOption, EventType, FieldArray, FieldArrayMethodProps, FieldArrayWithId, Field as FieldDef, FieldElement, FieldError, FieldErrors, FieldName, FieldNamesMarkedBoolean, FieldRefs, FieldValue, FieldValues, FormProviderProps, FormState, FormStateProxy, FormStateSubjectRef, GetIsDirty, InternalFieldErrors, InternalFieldName, InternalNameSet, IsAny, IsFlatObject, KeepStateOptions, LiteralUnion, Message, Mode, MultipleFieldErrors, Names, NativeFieldValue, NestedValue, NonUndefined, Noop, Primitive, ReadFormState, Ref, RefCallBack, RegisterOptions, Resolver, ResolverError, ResolverOptions, ResolverResult, ResolverSuccess, SetFieldValue, SetValueConfig, Subjects, SubmitErrorHandler, SubmitHandler, TriggerConfig, UnpackNestedValue, UseControllerProps, UseControllerReturn, UseFieldArrayProps, UseFieldArrayReturn, UseFormClearErrors, UseFormGetValues, UseFormHandleSubmit, UseFormProps, UseFormRegister, UseFormRegisterReturn, UseFormReset, UseFormResetField, UseFormReturn, UseFormSetError, UseFormSetFocus, UseFormSetValue, UseFormStateProps, UseFormStateReturn, UseFormTrigger, UseFormUnregister, UseFormWatch, UseWatchProps, Validate, ValidateResult, ValidationMode, ValidationRule, ValidationValue, ValidationValueMessage, WatchInternal, WatchObserver, appendErrors, useController, useFieldArray, useForm, useFormState, useWatch } from 'react-hook-form';
|
6
6
|
import { MaybeRenderProp } from '@chakra-ui/react-utils';
|
7
7
|
import { StepsProps, StepsItemProps, UseStepperReturn, UseStepperProps } from '@saas-ui/core';
|
8
|
-
import { NumberInputProps as NumberInputProps$1, InputProps, RadioProps, RadioGroupProps, SystemProps, StackDirection, MenuProps, ButtonProps, MenuListProps, MenuItemOptionProps, SelectProps as SelectProps$1, TextareaProps, SwitchProps, CheckboxProps, UsePinInputProps, ResponsiveValue, FormLabelProps, SimpleGridProps, FormControlProps, HTMLChakraProps
|
8
|
+
import { NumberInputProps as NumberInputProps$1, InputProps, RadioProps, RadioGroupProps, SystemProps, StackDirection, MenuProps, ThemingProps, ButtonProps, MenuListProps, MenuItemOptionProps, SelectProps as SelectProps$1, TextareaProps, SwitchProps, CheckboxProps, UsePinInputProps, ResponsiveValue, FormLabelProps, SimpleGridProps, FormControlProps, HTMLChakraProps } from '@chakra-ui/react';
|
9
9
|
import { FocusableElement } from '@chakra-ui/utils';
|
10
10
|
import * as _chakra_ui_system_dist_system_types from '@chakra-ui/system/dist/system.types';
|
11
11
|
|
@@ -90,9 +90,9 @@ interface SelectOptions {
|
|
90
90
|
renderValue?: (value: string | string[]) => React__default.ReactNode;
|
91
91
|
}
|
92
92
|
|
93
|
-
interface SelectProps extends Omit<MenuProps, 'children'>, SelectOptions {
|
93
|
+
interface SelectProps extends Omit<MenuProps, 'children' | 'variant' | 'size'>, ThemingProps<'SuiSelect'>, SelectOptions {
|
94
94
|
}
|
95
|
-
interface SelectButtonProps extends ButtonProps {
|
95
|
+
interface SelectButtonProps extends Omit<ButtonProps, 'size' | 'variant'> {
|
96
96
|
}
|
97
97
|
/**
|
98
98
|
* Button that opens the select menu and displays the selected value.
|
package/dist/index.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
var
|
3
|
+
var React11 = require('react');
|
4
4
|
var reactHookForm = require('react-hook-form');
|
5
5
|
var jsxRuntime = require('react/jsx-runtime');
|
6
6
|
var react = require('@chakra-ui/react');
|
@@ -27,12 +27,12 @@ function _interopNamespace(e) {
|
|
27
27
|
return Object.freeze(n);
|
28
28
|
}
|
29
29
|
|
30
|
-
var
|
30
|
+
var React11__namespace = /*#__PURE__*/_interopNamespace(React11);
|
31
31
|
|
32
32
|
// src/form-context.tsx
|
33
|
-
var FormContext =
|
33
|
+
var FormContext = React11__namespace.createContext(null);
|
34
34
|
var useFormContext = () => {
|
35
|
-
const context =
|
35
|
+
const context = React11__namespace.useContext(FormContext);
|
36
36
|
const hookContext = reactHookForm.useFormContext();
|
37
37
|
if (!context) {
|
38
38
|
throw new Error("FormProvider must be used within a Form component");
|
@@ -108,7 +108,7 @@ var PasswordInput = react.forwardRef(
|
|
108
108
|
leftAddon,
|
109
109
|
...inputProps
|
110
110
|
} = props;
|
111
|
-
const [show, setShow] =
|
111
|
+
const [show, setShow] = React11.useState(false);
|
112
112
|
const handleClick = () => setShow(!show);
|
113
113
|
const label = show ? "Hide password" : "Show password";
|
114
114
|
let icon;
|
@@ -147,15 +147,15 @@ var PasswordInput = react.forwardRef(
|
|
147
147
|
);
|
148
148
|
PasswordInput.displayName = "PasswordInput";
|
149
149
|
var mapNestedFields = (name, children) => {
|
150
|
-
return
|
151
|
-
if (
|
150
|
+
return React11__namespace.Children.map(children, (child) => {
|
151
|
+
if (React11__namespace.isValidElement(child) && child.props.name) {
|
152
152
|
let childName = child.props.name;
|
153
153
|
if (childName.includes(".")) {
|
154
154
|
childName = childName.replace(/^.*\.(.*)/, "$1");
|
155
155
|
} else if (childName.includes(".$")) {
|
156
156
|
childName = childName.replace(/^.*\.\$(.*)/, "$1");
|
157
157
|
}
|
158
|
-
return
|
158
|
+
return React11__namespace.cloneElement(child, {
|
159
159
|
...child.props,
|
160
160
|
name: `${name}.${childName}`
|
161
161
|
});
|
@@ -194,6 +194,7 @@ var RadioInput = react.forwardRef(
|
|
194
194
|
}
|
195
195
|
);
|
196
196
|
RadioInput.displayName = "RadioInput";
|
197
|
+
var [SelectStylesProvider, useSelectStyles] = react.createStylesContext("SuiSelect");
|
197
198
|
var [SelectProvider, useSelectContext] = reactUtils.createContext({
|
198
199
|
strict: true
|
199
200
|
});
|
@@ -215,14 +216,14 @@ var useSelect = (props) => {
|
|
215
216
|
onChange
|
216
217
|
});
|
217
218
|
const controlProps = react.useFormControl({ name });
|
218
|
-
const options =
|
219
|
+
const options = React11__namespace.default.useMemo(
|
219
220
|
() => optionsProp && mapOptions(optionsProp),
|
220
221
|
[optionsProp]
|
221
222
|
);
|
222
223
|
const handleChange = (value2) => {
|
223
224
|
setCurrentValue(value2);
|
224
225
|
};
|
225
|
-
const getDisplayValue =
|
226
|
+
const getDisplayValue = React11__namespace.default.useCallback(
|
226
227
|
(value2) => {
|
227
228
|
if (!options) {
|
228
229
|
return value2;
|
@@ -236,7 +237,7 @@ var useSelect = (props) => {
|
|
236
237
|
},
|
237
238
|
[options]
|
238
239
|
);
|
239
|
-
const displayValue =
|
240
|
+
const displayValue = React11__namespace.default.useMemo(
|
240
241
|
() => currentValue ? (Array.isArray(currentValue) ? currentValue : [currentValue]).map(
|
241
242
|
getDisplayValue
|
242
243
|
) : [],
|
@@ -258,7 +259,6 @@ var useSelect = (props) => {
|
|
258
259
|
var SelectButton = react.forwardRef(
|
259
260
|
(props, ref) => {
|
260
261
|
var _a, _b, _c, _d, _e;
|
261
|
-
const styles = react.useMultiStyleConfig("SuiSelect", props);
|
262
262
|
const {
|
263
263
|
displayValue,
|
264
264
|
renderValue,
|
@@ -266,6 +266,7 @@ var SelectButton = react.forwardRef(
|
|
266
266
|
isDisabled: isSelectDisabled
|
267
267
|
} = useSelectContext();
|
268
268
|
const context = react.useFormControlContext();
|
269
|
+
const styles = useSelectStyles();
|
269
270
|
const {
|
270
271
|
isInvalid,
|
271
272
|
isReadOnly,
|
@@ -293,15 +294,17 @@ var SelectButton = react.forwardRef(
|
|
293
294
|
_expanded: focusStyles,
|
294
295
|
_readOnly: readOnlyStyles,
|
295
296
|
_invalid: invalid,
|
297
|
+
minW: 0,
|
296
298
|
...styles.field,
|
297
299
|
h: "auto"
|
298
300
|
};
|
301
|
+
const buttonProps = react.useMenuButton(rest, ref);
|
299
302
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
300
|
-
react.
|
303
|
+
react.Button,
|
301
304
|
{
|
302
|
-
|
303
|
-
id: id || React12__namespace.useId(),
|
305
|
+
...buttonProps,
|
304
306
|
...buttonStyles,
|
307
|
+
id: id || buttonProps.id,
|
305
308
|
...rest,
|
306
309
|
onFocus,
|
307
310
|
onBlur,
|
@@ -311,8 +314,20 @@ var SelectButton = react.forwardRef(
|
|
311
314
|
"data-focus": utils.dataAttr(isFocused),
|
312
315
|
"data-required": utils.dataAttr(isRequired),
|
313
316
|
rightIcon,
|
314
|
-
|
315
|
-
|
317
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(
|
318
|
+
react.chakra.span,
|
319
|
+
{
|
320
|
+
__css: {
|
321
|
+
display: "block",
|
322
|
+
pointerEvents: "none",
|
323
|
+
flex: "1 1 auto",
|
324
|
+
minW: 0,
|
325
|
+
overflow: "hidden",
|
326
|
+
textOverflow: "ellipsis"
|
327
|
+
},
|
328
|
+
children: renderValue(displayValue) || placeholder
|
329
|
+
}
|
330
|
+
)
|
316
331
|
}
|
317
332
|
);
|
318
333
|
}
|
@@ -320,10 +335,11 @@ var SelectButton = react.forwardRef(
|
|
320
335
|
SelectButton.displayName = "SelectButton";
|
321
336
|
var Select = react.forwardRef((props, ref) => {
|
322
337
|
const { name, children, isDisabled, multiple, ...rest } = props;
|
338
|
+
const styles = react.useMultiStyleConfig("SuiSelect", props);
|
323
339
|
const menuProps = react.omitThemingProps(rest);
|
324
340
|
const context = useSelect(props);
|
325
341
|
const { value, controlProps } = context;
|
326
|
-
return /* @__PURE__ */ jsxRuntime.jsx(SelectProvider, { value: context, children: /* @__PURE__ */ jsxRuntime.jsx(react.Menu, { ...menuProps, closeOnSelect: !multiple, children: /* @__PURE__ */ jsxRuntime.jsxs(react.chakra.div, { className: utils.cx("sui-select"), children: [
|
342
|
+
return /* @__PURE__ */ jsxRuntime.jsx(SelectProvider, { value: context, children: /* @__PURE__ */ jsxRuntime.jsx(SelectStylesProvider, { value: styles, children: /* @__PURE__ */ jsxRuntime.jsx(react.Menu, { ...menuProps, closeOnSelect: !multiple, children: /* @__PURE__ */ jsxRuntime.jsxs(react.chakra.div, { className: utils.cx("sui-select"), children: [
|
327
343
|
children,
|
328
344
|
/* @__PURE__ */ jsxRuntime.jsx(
|
329
345
|
react.chakra.input,
|
@@ -336,7 +352,7 @@ var Select = react.forwardRef((props, ref) => {
|
|
336
352
|
className: "saas-select__input"
|
337
353
|
}
|
338
354
|
)
|
339
|
-
] }) }) });
|
355
|
+
] }) }) }) });
|
340
356
|
});
|
341
357
|
var SelectList = (props) => {
|
342
358
|
const { defaultValue, value, options, multiple, onChange } = useSelectContext();
|
@@ -573,7 +589,7 @@ var defaultFieldTypes = {
|
|
573
589
|
pin: PinField,
|
574
590
|
"native-select": NativeSelectField
|
575
591
|
};
|
576
|
-
var FieldsContext =
|
592
|
+
var FieldsContext = React11__namespace.default.createContext(
|
577
593
|
null
|
578
594
|
);
|
579
595
|
var FieldsProvider = (props) => {
|
@@ -581,11 +597,11 @@ var FieldsProvider = (props) => {
|
|
581
597
|
return /* @__PURE__ */ jsxRuntime.jsx(FieldsContext.Provider, { value: fields, children: props.children });
|
582
598
|
};
|
583
599
|
var useField = (type) => {
|
584
|
-
const context =
|
600
|
+
const context = React11__namespace.default.useContext(FieldsContext);
|
585
601
|
return (context == null ? void 0 : context[type]) || InputField;
|
586
602
|
};
|
587
603
|
var defaultInputType = "text";
|
588
|
-
var Field =
|
604
|
+
var Field = React11__namespace.forwardRef(
|
589
605
|
(props, ref) => {
|
590
606
|
const { type = defaultInputType, name } = props;
|
591
607
|
const overrides = useFieldProps(name);
|
@@ -612,8 +628,8 @@ var FormLayout = ({ children, ...props }) => {
|
|
612
628
|
{
|
613
629
|
...gridProps,
|
614
630
|
className: utils.cx("sui-form__layout", props.className),
|
615
|
-
children:
|
616
|
-
if (
|
631
|
+
children: React11__namespace.Children.map(children, (child) => {
|
632
|
+
if (React11__namespace.isValidElement(child)) {
|
617
633
|
return /* @__PURE__ */ jsxRuntime.jsx(FormLayoutItem, { children: child });
|
618
634
|
}
|
619
635
|
return child;
|
@@ -652,7 +668,7 @@ var useArrayField = ({
|
|
652
668
|
var useArrayFieldRow = ({ index }) => {
|
653
669
|
const { clearErrors } = useFormContext();
|
654
670
|
const { name, remove, fields } = useArrayFieldContext();
|
655
|
-
|
671
|
+
React11__namespace.useEffect(() => {
|
656
672
|
clearErrors(name);
|
657
673
|
}, []);
|
658
674
|
return {
|
@@ -660,7 +676,7 @@ var useArrayFieldRow = ({ index }) => {
|
|
660
676
|
isFirst: index === 0,
|
661
677
|
isLast: index === fields.length - 1,
|
662
678
|
name: `${name}.${index}`,
|
663
|
-
remove:
|
679
|
+
remove: React11__namespace.useCallback(() => {
|
664
680
|
clearErrors(name);
|
665
681
|
remove(index);
|
666
682
|
}, [index])
|
@@ -755,7 +771,7 @@ var ArrayFieldRows = ({
|
|
755
771
|
return children(fields);
|
756
772
|
};
|
757
773
|
ArrayFieldRows.displayName = "ArrayFieldRows";
|
758
|
-
var ArrayFieldContainer =
|
774
|
+
var ArrayFieldContainer = React11__namespace.forwardRef(
|
759
775
|
({
|
760
776
|
name,
|
761
777
|
defaultValue,
|
@@ -773,7 +789,7 @@ var ArrayFieldContainer = React12__namespace.forwardRef(
|
|
773
789
|
min: min || (overrides == null ? void 0 : overrides.min),
|
774
790
|
max: max || (overrides == null ? void 0 : overrides.max)
|
775
791
|
});
|
776
|
-
|
792
|
+
React11__namespace.useImperativeHandle(ref, () => context, [ref, context]);
|
777
793
|
return /* @__PURE__ */ jsxRuntime.jsx(ArrayFieldProvider, { value: context, children: /* @__PURE__ */ jsxRuntime.jsx(BaseField, { name, ...fieldProps, ...overrides, children }) });
|
778
794
|
}
|
779
795
|
);
|
@@ -831,10 +847,10 @@ var AutoFields = ({
|
|
831
847
|
const context = useFormContext();
|
832
848
|
const schema = schemaProp || context.schema;
|
833
849
|
const fieldResolver = fieldResolverProp || context.fieldResolver;
|
834
|
-
const resolver =
|
835
|
-
const fields =
|
850
|
+
const resolver = React11__namespace.useMemo(() => fieldResolver, [schema, fieldResolver]);
|
851
|
+
const fields = React11__namespace.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
|
836
852
|
const form = useFormContext();
|
837
|
-
|
853
|
+
React11__namespace.useEffect(() => {
|
838
854
|
var _a;
|
839
855
|
if (focusFirstField && ((_a = fields == null ? void 0 : fields[0]) == null ? void 0 : _a.name)) {
|
840
856
|
form.setFocus(fields[0].name);
|
@@ -916,10 +932,10 @@ var [StepFormProvider, useStepFormContext] = reactUtils.createContext({
|
|
916
932
|
function useStepForm(props) {
|
917
933
|
const { onChange, steps: stepsOptions, resolver, ...rest } = props;
|
918
934
|
const stepper = core.useStepper(rest);
|
919
|
-
const [options, setOptions] =
|
935
|
+
const [options, setOptions] = React11__namespace.useState(stepsOptions);
|
920
936
|
const { activeStep, isLastStep, nextStep } = stepper;
|
921
|
-
const [steps, updateSteps] =
|
922
|
-
const onSubmitStep =
|
937
|
+
const [steps, updateSteps] = React11__namespace.useState({});
|
938
|
+
const onSubmitStep = React11__namespace.useCallback(
|
923
939
|
async (data) => {
|
924
940
|
var _a, _b;
|
925
941
|
try {
|
@@ -940,7 +956,7 @@ function useStepForm(props) {
|
|
940
956
|
},
|
941
957
|
[steps, activeStep, isLastStep]
|
942
958
|
);
|
943
|
-
const getFormProps =
|
959
|
+
const getFormProps = React11__namespace.useCallback(() => {
|
944
960
|
const step = steps[activeStep];
|
945
961
|
return {
|
946
962
|
onSubmit: onSubmitStep,
|
@@ -951,7 +967,7 @@ function useStepForm(props) {
|
|
951
967
|
) : void 0
|
952
968
|
};
|
953
969
|
}, [steps, onSubmitStep, activeStep]);
|
954
|
-
const updateStep =
|
970
|
+
const updateStep = React11__namespace.useCallback(
|
955
971
|
(step) => {
|
956
972
|
const stepOptions = options == null ? void 0 : options.find((s) => s.name === step.name);
|
957
973
|
updateSteps((steps2) => {
|
@@ -977,7 +993,7 @@ function useFormStep(props) {
|
|
977
993
|
const { name, schema, resolver, onSubmit } = props;
|
978
994
|
const step = core.useStep({ name });
|
979
995
|
const { steps, updateStep } = useStepFormContext();
|
980
|
-
|
996
|
+
React11__namespace.useEffect(() => {
|
981
997
|
updateStep({ name, schema, resolver, onSubmit });
|
982
998
|
}, [name, schema]);
|
983
999
|
return {
|
@@ -997,8 +1013,8 @@ var FormStepper = (props) => {
|
|
997
1013
|
render,
|
998
1014
|
...rest
|
999
1015
|
} = props;
|
1000
|
-
const elements =
|
1001
|
-
if (
|
1016
|
+
const elements = React11__namespace.Children.map(children, (child) => {
|
1017
|
+
if (React11__namespace.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
|
1002
1018
|
const { isCompleted } = useFormStep(child.props);
|
1003
1019
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
1004
1020
|
core.StepsItem,
|
@@ -1014,7 +1030,7 @@ var FormStepper = (props) => {
|
|
1014
1030
|
}
|
1015
1031
|
return child;
|
1016
1032
|
});
|
1017
|
-
const onChange =
|
1033
|
+
const onChange = React11__namespace.useCallback((i) => {
|
1018
1034
|
setIndex(i);
|
1019
1035
|
onChangeProp == null ? void 0 : onChangeProp(i);
|
1020
1036
|
}, []);
|
@@ -1144,8 +1160,8 @@ var Form = react.forwardRef(
|
|
1144
1160
|
};
|
1145
1161
|
const methods = reactHookForm.useForm(form);
|
1146
1162
|
const { handleSubmit } = methods;
|
1147
|
-
|
1148
|
-
|
1163
|
+
React11__namespace.useImperativeHandle(formRef, () => methods, [formRef, methods]);
|
1164
|
+
React11__namespace.useEffect(() => {
|
1149
1165
|
let subscription;
|
1150
1166
|
if (onChange) {
|
1151
1167
|
subscription = methods.watch(onChange);
|
@@ -1219,7 +1235,7 @@ function createStepForm({ fields, resolver, fieldResolver } = {}) {
|
|
1219
1235
|
...props
|
1220
1236
|
});
|
1221
1237
|
const { getFormProps, ...ctx } = stepper;
|
1222
|
-
const context =
|
1238
|
+
const context = React11.useMemo(() => ctx, [ctx]);
|
1223
1239
|
return /* @__PURE__ */ jsxRuntime.jsx(core.StepperProvider, { value: context, children: /* @__PURE__ */ jsxRuntime.jsx(StepFormProvider, { value: context, children: /* @__PURE__ */ jsxRuntime.jsx(FieldsProvider, { value: fields || {}, children: /* @__PURE__ */ jsxRuntime.jsx(Form2, { ref, ...rest, ...getFormProps(), children: utils.runIfFn(children, {
|
1224
1240
|
...stepper,
|
1225
1241
|
Field,
|