@algorithm-shift/design-system 1.2.41 → 1.2.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +13 -2
- package/dist/index.d.ts +13 -2
- package/dist/index.js +115 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +114 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2909,7 +2909,7 @@ function useAppState() {
|
|
|
2909
2909
|
return useContext(StateContext);
|
|
2910
2910
|
}
|
|
2911
2911
|
|
|
2912
|
-
// src/components/Form/
|
|
2912
|
+
// src/components/Form/Form.tsx
|
|
2913
2913
|
import React18, { useMemo as useMemo2 } from "react";
|
|
2914
2914
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
2915
2915
|
import { useForm, Controller } from "react-hook-form";
|
|
@@ -2983,7 +2983,7 @@ function generateZodSchema(data) {
|
|
|
2983
2983
|
}, {});
|
|
2984
2984
|
return z.object(fields);
|
|
2985
2985
|
}
|
|
2986
|
-
var
|
|
2986
|
+
var Form = ({
|
|
2987
2987
|
validation,
|
|
2988
2988
|
defaultValues,
|
|
2989
2989
|
children,
|
|
@@ -3060,6 +3060,116 @@ var FormWrapper = ({
|
|
|
3060
3060
|
}
|
|
3061
3061
|
);
|
|
3062
3062
|
};
|
|
3063
|
+
var Form_default = Form;
|
|
3064
|
+
|
|
3065
|
+
// src/components/Form/Wrapper.tsx
|
|
3066
|
+
import { useMemo as useMemo3 } from "react";
|
|
3067
|
+
import { zodResolver as zodResolver2 } from "@hookform/resolvers/zod";
|
|
3068
|
+
import { useForm as useForm2 } from "react-hook-form";
|
|
3069
|
+
import { z as z2 } from "zod";
|
|
3070
|
+
import { jsx as jsx58 } from "react/jsx-runtime";
|
|
3071
|
+
function generateZodSchema2(data) {
|
|
3072
|
+
const fields = data.reduce((acc, f) => {
|
|
3073
|
+
const name = f.name || "unnamed";
|
|
3074
|
+
const message = f.message || `${name} is invalid`;
|
|
3075
|
+
const passwordLen = f.passwordLength;
|
|
3076
|
+
let fieldSchema = z2.string({ message });
|
|
3077
|
+
switch (f.type) {
|
|
3078
|
+
case "Text":
|
|
3079
|
+
case "Search":
|
|
3080
|
+
fieldSchema = z2.string({ message });
|
|
3081
|
+
if (f?.min && f?.min !== "") fieldSchema = fieldSchema.min(f.min);
|
|
3082
|
+
else if (f.isRequired) fieldSchema = fieldSchema.min(1, { message: `${message}. Cannot be empty` });
|
|
3083
|
+
if (f?.max && f?.max !== "") fieldSchema = fieldSchema.max(f.max);
|
|
3084
|
+
if (f?.email) fieldSchema = fieldSchema.email();
|
|
3085
|
+
if (f?.url) fieldSchema = fieldSchema.url();
|
|
3086
|
+
if (f?.regex) fieldSchema = fieldSchema.regex(new RegExp(f.regex));
|
|
3087
|
+
break;
|
|
3088
|
+
case "Email":
|
|
3089
|
+
fieldSchema = z2.email({ message });
|
|
3090
|
+
break;
|
|
3091
|
+
case "Password":
|
|
3092
|
+
fieldSchema = z2.string({ message }).min(passwordLen, { message: `Password must be at least ${passwordLen} characters long` });
|
|
3093
|
+
break;
|
|
3094
|
+
case "Phone":
|
|
3095
|
+
fieldSchema = z2.string().transform((val) => val.replace(/\D/g, "")).transform((val) => val.slice(-10)).refine((val) => {
|
|
3096
|
+
return val.length === 10;
|
|
3097
|
+
}, {
|
|
3098
|
+
message: "Phone number must be 10 digits long"
|
|
3099
|
+
});
|
|
3100
|
+
break;
|
|
3101
|
+
case "DatePicker":
|
|
3102
|
+
fieldSchema = z2.iso.date({ message });
|
|
3103
|
+
break;
|
|
3104
|
+
case "FileInput":
|
|
3105
|
+
fieldSchema = z2.instanceof(File, { message: "Please select a file" });
|
|
3106
|
+
if (f?.maxSize) {
|
|
3107
|
+
fieldSchema = fieldSchema.refine(
|
|
3108
|
+
(file) => file.size <= f.maxSize,
|
|
3109
|
+
{ message: `File size must be less than ${f.maxSize / 1024 / 1024}MB` }
|
|
3110
|
+
);
|
|
3111
|
+
}
|
|
3112
|
+
if (f?.acceptedTypes) {
|
|
3113
|
+
fieldSchema = fieldSchema.refine(
|
|
3114
|
+
(file) => f.acceptedTypes.includes(file.type),
|
|
3115
|
+
{ message: `File type must be one of: ${f.acceptedTypes.join(", ")}` }
|
|
3116
|
+
);
|
|
3117
|
+
}
|
|
3118
|
+
break;
|
|
3119
|
+
case "Checkbox":
|
|
3120
|
+
fieldSchema = z2.boolean({ message });
|
|
3121
|
+
break;
|
|
3122
|
+
case "Dropdown":
|
|
3123
|
+
fieldSchema = z2.string({ message });
|
|
3124
|
+
break;
|
|
3125
|
+
case "NumberInput":
|
|
3126
|
+
fieldSchema = z2.number({ message });
|
|
3127
|
+
if (f?.min !== void 0) fieldSchema = fieldSchema.min(f.min);
|
|
3128
|
+
if (f?.max !== void 0) fieldSchema = fieldSchema.max(f.max);
|
|
3129
|
+
break;
|
|
3130
|
+
default:
|
|
3131
|
+
fieldSchema = z2.any();
|
|
3132
|
+
}
|
|
3133
|
+
if (!f.isRequired) fieldSchema = fieldSchema.optional();
|
|
3134
|
+
acc[name] = fieldSchema;
|
|
3135
|
+
return acc;
|
|
3136
|
+
}, {});
|
|
3137
|
+
return z2.object(fields);
|
|
3138
|
+
}
|
|
3139
|
+
var FormWrapper = ({
|
|
3140
|
+
validation,
|
|
3141
|
+
defaultValues,
|
|
3142
|
+
children,
|
|
3143
|
+
onSubmit,
|
|
3144
|
+
onReset
|
|
3145
|
+
}) => {
|
|
3146
|
+
const schema = useMemo3(() => {
|
|
3147
|
+
if (!validation || validation.length === 0) return null;
|
|
3148
|
+
return generateZodSchema2(validation);
|
|
3149
|
+
}, [validation]);
|
|
3150
|
+
const form = useForm2({
|
|
3151
|
+
resolver: schema ? zodResolver2(schema) : void 0,
|
|
3152
|
+
defaultValues
|
|
3153
|
+
});
|
|
3154
|
+
const formSubmit = (data) => {
|
|
3155
|
+
if (onSubmit) onSubmit(data);
|
|
3156
|
+
};
|
|
3157
|
+
const handleReset = () => {
|
|
3158
|
+
form.reset();
|
|
3159
|
+
if (onReset) onReset();
|
|
3160
|
+
};
|
|
3161
|
+
return /* @__PURE__ */ jsx58(
|
|
3162
|
+
"form",
|
|
3163
|
+
{
|
|
3164
|
+
onSubmit: form.handleSubmit(formSubmit),
|
|
3165
|
+
onReset: handleReset,
|
|
3166
|
+
className: cn(
|
|
3167
|
+
"space-y-4 min-h-[100px] h-auto flex justify-between flex-col"
|
|
3168
|
+
),
|
|
3169
|
+
children: /* @__PURE__ */ jsx58("div", { className: "min-h-[50px]", children: typeof children === "function" ? children(form) : children })
|
|
3170
|
+
}
|
|
3171
|
+
);
|
|
3172
|
+
};
|
|
3063
3173
|
var Wrapper_default = FormWrapper;
|
|
3064
3174
|
export {
|
|
3065
3175
|
BarChart_default as BarChart,
|
|
@@ -3075,7 +3185,8 @@ export {
|
|
|
3075
3185
|
EmailComposer,
|
|
3076
3186
|
FileInput_default as FileInput,
|
|
3077
3187
|
Flex_default as FlexLayout,
|
|
3078
|
-
|
|
3188
|
+
Form_default as Form,
|
|
3189
|
+
Wrapper_default as FormWrapper,
|
|
3079
3190
|
Grid_default as GridLayout,
|
|
3080
3191
|
Image_default as Image,
|
|
3081
3192
|
Modal_default as Modal,
|