@algorithm-shift/design-system 1.2.37 → 1.2.39
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 +12 -1
- package/dist/index.d.ts +12 -1
- package/dist/index.js +155 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +154 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2801,6 +2801,159 @@ function StateProvider({ children }) {
|
|
|
2801
2801
|
function useAppState() {
|
|
2802
2802
|
return useContext(StateContext);
|
|
2803
2803
|
}
|
|
2804
|
+
|
|
2805
|
+
// src/components/Form/Wrapper.tsx
|
|
2806
|
+
import React18, { useMemo as useMemo2 } from "react";
|
|
2807
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
2808
|
+
import { useForm, Controller } from "react-hook-form";
|
|
2809
|
+
import { z } from "zod";
|
|
2810
|
+
import { jsx as jsx55 } from "react/jsx-runtime";
|
|
2811
|
+
function generateZodSchema(data) {
|
|
2812
|
+
const fields = data.reduce((acc, f) => {
|
|
2813
|
+
const name = f.name || "unnamed";
|
|
2814
|
+
const message = f.message || `${name} is invalid`;
|
|
2815
|
+
const passwordLen = f.passwordLength;
|
|
2816
|
+
let fieldSchema = z.string({ message });
|
|
2817
|
+
switch (f.type) {
|
|
2818
|
+
case "Text":
|
|
2819
|
+
case "Search":
|
|
2820
|
+
fieldSchema = z.string({ message });
|
|
2821
|
+
if (f?.min && f?.min !== "") fieldSchema = fieldSchema.min(f.min);
|
|
2822
|
+
else if (f.isRequired) fieldSchema = fieldSchema.min(1, { message: `${message}. Cannot be empty` });
|
|
2823
|
+
if (f?.max && f?.max !== "") fieldSchema = fieldSchema.max(f.max);
|
|
2824
|
+
if (f?.email) fieldSchema = fieldSchema.email();
|
|
2825
|
+
if (f?.url) fieldSchema = fieldSchema.url();
|
|
2826
|
+
if (f?.regex) fieldSchema = fieldSchema.regex(new RegExp(f.regex));
|
|
2827
|
+
break;
|
|
2828
|
+
case "Email":
|
|
2829
|
+
fieldSchema = z.email({ message });
|
|
2830
|
+
break;
|
|
2831
|
+
case "Password":
|
|
2832
|
+
fieldSchema = z.string({ message }).min(passwordLen, { message: `Password must be at least ${passwordLen} characters long` });
|
|
2833
|
+
break;
|
|
2834
|
+
case "Phone":
|
|
2835
|
+
fieldSchema = z.string().transform((val) => val.replace(/\D/g, "")).transform((val) => val.slice(-10)).refine((val) => {
|
|
2836
|
+
return val.length === 10;
|
|
2837
|
+
}, {
|
|
2838
|
+
message: "Phone number must be 10 digits long"
|
|
2839
|
+
});
|
|
2840
|
+
break;
|
|
2841
|
+
case "DatePicker":
|
|
2842
|
+
fieldSchema = z.iso.date({ message });
|
|
2843
|
+
break;
|
|
2844
|
+
case "FileInput":
|
|
2845
|
+
fieldSchema = z.instanceof(File, { message: "Please select a file" });
|
|
2846
|
+
if (f?.maxSize) {
|
|
2847
|
+
fieldSchema = fieldSchema.refine(
|
|
2848
|
+
(file) => file.size <= f.maxSize,
|
|
2849
|
+
{ message: `File size must be less than ${f.maxSize / 1024 / 1024}MB` }
|
|
2850
|
+
);
|
|
2851
|
+
}
|
|
2852
|
+
if (f?.acceptedTypes) {
|
|
2853
|
+
fieldSchema = fieldSchema.refine(
|
|
2854
|
+
(file) => f.acceptedTypes.includes(file.type),
|
|
2855
|
+
{ message: `File type must be one of: ${f.acceptedTypes.join(", ")}` }
|
|
2856
|
+
);
|
|
2857
|
+
}
|
|
2858
|
+
break;
|
|
2859
|
+
case "Checkbox":
|
|
2860
|
+
fieldSchema = z.boolean({ message });
|
|
2861
|
+
break;
|
|
2862
|
+
case "Dropdown":
|
|
2863
|
+
fieldSchema = z.string({ message });
|
|
2864
|
+
break;
|
|
2865
|
+
case "NumberInput":
|
|
2866
|
+
fieldSchema = z.number({ message });
|
|
2867
|
+
if (f?.min !== void 0) fieldSchema = fieldSchema.min(f.min);
|
|
2868
|
+
if (f?.max !== void 0) fieldSchema = fieldSchema.max(f.max);
|
|
2869
|
+
break;
|
|
2870
|
+
default:
|
|
2871
|
+
fieldSchema = z.any();
|
|
2872
|
+
}
|
|
2873
|
+
if (!f.isRequired) fieldSchema = fieldSchema.optional();
|
|
2874
|
+
acc[name] = fieldSchema;
|
|
2875
|
+
return acc;
|
|
2876
|
+
}, {});
|
|
2877
|
+
return z.object(fields);
|
|
2878
|
+
}
|
|
2879
|
+
var FormWrapper = ({
|
|
2880
|
+
validation,
|
|
2881
|
+
defaultValues,
|
|
2882
|
+
children,
|
|
2883
|
+
onSubmit,
|
|
2884
|
+
onReset
|
|
2885
|
+
}) => {
|
|
2886
|
+
const schema = useMemo2(() => {
|
|
2887
|
+
if (!validation || validation.length === 0) return null;
|
|
2888
|
+
return generateZodSchema(validation);
|
|
2889
|
+
}, [validation]);
|
|
2890
|
+
const {
|
|
2891
|
+
handleSubmit,
|
|
2892
|
+
control,
|
|
2893
|
+
formState: { errors },
|
|
2894
|
+
reset
|
|
2895
|
+
} = useForm({
|
|
2896
|
+
resolver: schema ? zodResolver(schema) : void 0,
|
|
2897
|
+
defaultValues
|
|
2898
|
+
});
|
|
2899
|
+
const formSubmit = (data) => {
|
|
2900
|
+
if (onSubmit) onSubmit(data);
|
|
2901
|
+
};
|
|
2902
|
+
const handleReset = () => {
|
|
2903
|
+
reset();
|
|
2904
|
+
if (onReset) onReset();
|
|
2905
|
+
};
|
|
2906
|
+
return /* @__PURE__ */ jsx55(
|
|
2907
|
+
"form",
|
|
2908
|
+
{
|
|
2909
|
+
onSubmit: handleSubmit(formSubmit),
|
|
2910
|
+
onReset: handleReset,
|
|
2911
|
+
className: cn(
|
|
2912
|
+
"space-y-4 min-h-[100px] h-auto flex justify-between flex-col"
|
|
2913
|
+
),
|
|
2914
|
+
children: /* @__PURE__ */ jsx55("div", { className: "min-h-[50px]", children: React18.Children.map(children, (child) => {
|
|
2915
|
+
const processChild = (child2) => {
|
|
2916
|
+
if (React18.isValidElement(child2)) {
|
|
2917
|
+
const node = child2.props?.node;
|
|
2918
|
+
if (node?.category === "Form Controls") {
|
|
2919
|
+
const name = node.properties?.name || "unnamed";
|
|
2920
|
+
return /* @__PURE__ */ jsx55("div", { className: "flex flex-col", children: /* @__PURE__ */ jsx55(
|
|
2921
|
+
Controller,
|
|
2922
|
+
{
|
|
2923
|
+
name,
|
|
2924
|
+
control,
|
|
2925
|
+
render: ({ field: controllerField }) => {
|
|
2926
|
+
const childElement = child2;
|
|
2927
|
+
return React18.cloneElement(childElement, {
|
|
2928
|
+
input: {
|
|
2929
|
+
...controllerField,
|
|
2930
|
+
value: controllerField.value || "",
|
|
2931
|
+
hasFormContainer: true,
|
|
2932
|
+
validateOnMount: true,
|
|
2933
|
+
errorMessage: errors[name]?.message || null
|
|
2934
|
+
},
|
|
2935
|
+
children: void 0
|
|
2936
|
+
});
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
) }, node.id);
|
|
2940
|
+
}
|
|
2941
|
+
if (child2.props?.children) {
|
|
2942
|
+
const childElement = child2;
|
|
2943
|
+
return React18.cloneElement(childElement, {
|
|
2944
|
+
children: React18.Children.map(childElement.props.children, processChild)
|
|
2945
|
+
});
|
|
2946
|
+
}
|
|
2947
|
+
return React18.cloneElement(child2);
|
|
2948
|
+
}
|
|
2949
|
+
return child2;
|
|
2950
|
+
};
|
|
2951
|
+
return processChild(child);
|
|
2952
|
+
}) })
|
|
2953
|
+
}
|
|
2954
|
+
);
|
|
2955
|
+
};
|
|
2956
|
+
var Wrapper_default = FormWrapper;
|
|
2804
2957
|
export {
|
|
2805
2958
|
BarChart_default as BarChart,
|
|
2806
2959
|
Button_default as Button,
|
|
@@ -2813,6 +2966,7 @@ export {
|
|
|
2813
2966
|
EmailComposer,
|
|
2814
2967
|
FileInput_default as FileInput,
|
|
2815
2968
|
Flex_default as FlexLayout,
|
|
2969
|
+
Wrapper_default as Form,
|
|
2816
2970
|
Grid_default as GridLayout,
|
|
2817
2971
|
Image_default as Image,
|
|
2818
2972
|
Logo_default as Logo,
|