gbs-add-block 0.0.44 → 0.0.45
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/package.json +1 -1
- package/source/components/formrenderer/CheckBoxHandles.tsx +6 -1
- package/source/components/formrenderer/DatePickerHandles.tsx +3 -0
- package/source/components/formrenderer/InputHandles.tsx +65 -23
- package/source/components/formrenderer/MultiHandles.tsx +3 -0
- package/source/components/formrenderer/SelectHandles.tsx +28 -91
- package/source/components/formrenderer/index.tsx +21 -3
- package/source/components/formrenderer/types.ts +26 -1
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import React, { useEffect, useState } from "react";
|
|
2
|
-
import { CheckboxHandlesProps
|
|
2
|
+
import { CheckboxHandlesProps } from "./types";
|
|
3
3
|
|
|
4
4
|
const CheckboxHandles = ({
|
|
5
5
|
item,
|
|
6
6
|
formRef,
|
|
7
7
|
requirementError,
|
|
8
8
|
setRequirementError,
|
|
9
|
+
onChangeEvent,
|
|
10
|
+
updateContext,
|
|
9
11
|
}: CheckboxHandlesProps) => {
|
|
10
12
|
const [checked, setChecked] = useState<boolean>(!!item?.value);
|
|
11
13
|
|
|
@@ -47,6 +49,9 @@ const CheckboxHandles = ({
|
|
|
47
49
|
prevErrors.filter((error) => error !== item.name)
|
|
48
50
|
);
|
|
49
51
|
}
|
|
52
|
+
onChangeEvent?.(e);
|
|
53
|
+
|
|
54
|
+
item?.name && updateContext("checkbox", item.name, newValue);
|
|
50
55
|
}}
|
|
51
56
|
className={`w-5 h-5 cursor-pointer rounded-md border-2 transition-colors duration-200 ${
|
|
52
57
|
item?.name && requirementError.includes(item?.name)
|
|
@@ -7,6 +7,7 @@ interface DatePickerHandlesProps {
|
|
|
7
7
|
requirementError: string[];
|
|
8
8
|
setRequirementError?: React.Dispatch<React.SetStateAction<string[]>>;
|
|
9
9
|
formRef?: React.RefObject<HTMLFormElement | null>;
|
|
10
|
+
onChangeEvent?: (event: any) => void;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export default function DatePickerHandles({
|
|
@@ -14,6 +15,7 @@ export default function DatePickerHandles({
|
|
|
14
15
|
requirementError,
|
|
15
16
|
setRequirementError,
|
|
16
17
|
formRef,
|
|
18
|
+
onChangeEvent,
|
|
17
19
|
}: DatePickerHandlesProps) {
|
|
18
20
|
const handleSelectDate = (value: string[], key: string) => {
|
|
19
21
|
if (formRef && formRef.current) {
|
|
@@ -52,6 +54,7 @@ export default function DatePickerHandles({
|
|
|
52
54
|
prevErrors.filter((errorName) => errorName !== item.name)
|
|
53
55
|
);
|
|
54
56
|
}
|
|
57
|
+
onChangeEvent?.(value);
|
|
55
58
|
}}
|
|
56
59
|
error={
|
|
57
60
|
item?.name && requirementError.includes(item.name)
|
|
@@ -1,43 +1,80 @@
|
|
|
1
|
-
import React, { useState } from "react";
|
|
1
|
+
import React, { useEffect, useState } from "react";
|
|
2
2
|
import { validateEmail, validatePhoneNumber } from "./helperFunctions";
|
|
3
3
|
import { Input } from "../input";
|
|
4
|
-
import { FormItem } from "./types";
|
|
4
|
+
import { FieldValue, FormContext, FormItem } from "./types";
|
|
5
|
+
import { evaluateExpression } from "@grampro/expression-evaluator";
|
|
6
|
+
import { twMerge } from "tailwind-merge";
|
|
5
7
|
|
|
6
8
|
interface InputHandlesProps {
|
|
7
9
|
item?: FormItem;
|
|
8
10
|
requirementError: string[];
|
|
9
11
|
setRequirementError?: React.Dispatch<React.SetStateAction<string[]>>;
|
|
12
|
+
onChangeEvent?: (event: any) => void;
|
|
13
|
+
formRef: React.RefObject<HTMLFormElement | null>;
|
|
14
|
+
context: FormContext;
|
|
15
|
+
updateContext: (
|
|
16
|
+
componentName: string,
|
|
17
|
+
fieldName: string,
|
|
18
|
+
value: FieldValue
|
|
19
|
+
) => void;
|
|
10
20
|
}
|
|
11
21
|
|
|
12
22
|
const InputHandles = ({
|
|
13
23
|
item,
|
|
14
24
|
requirementError,
|
|
15
25
|
setRequirementError,
|
|
26
|
+
onChangeEvent,
|
|
27
|
+
context,
|
|
28
|
+
updateContext,
|
|
16
29
|
}: InputHandlesProps) => {
|
|
17
30
|
const [inputError, setInputError] = useState<string | null>(null);
|
|
31
|
+
const [isDisabled, setIsDisabled] = useState<boolean>(false);
|
|
32
|
+
const [isRequired, setIsRequired] = useState<boolean>(false);
|
|
33
|
+
|
|
34
|
+
// Initialize context with default values
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (item?.name && item?.value !== undefined) {
|
|
37
|
+
updateContext("input", item.name, item.value);
|
|
38
|
+
}
|
|
39
|
+
}, [item?.name, item?.value, updateContext]);
|
|
40
|
+
|
|
41
|
+
// Handle dynamic disabled/required states expressions
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (typeof item?.disabled === "string") {
|
|
44
|
+
setIsDisabled(evaluateExpression(item.disabled, context));
|
|
45
|
+
} else if (typeof item?.disabled === "boolean") {
|
|
46
|
+
setIsDisabled(item.disabled);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (typeof item?.required === "string") {
|
|
50
|
+
setIsRequired(evaluateExpression(item.required, context));
|
|
51
|
+
} else if (typeof item?.required === "boolean") {
|
|
52
|
+
setIsRequired(item.required);
|
|
53
|
+
}
|
|
54
|
+
}, [item, context]);
|
|
18
55
|
|
|
19
56
|
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
20
57
|
const { value } = event.target;
|
|
21
58
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
59
|
+
if (item?.name) {
|
|
60
|
+
updateContext("input", item.name, value);
|
|
61
|
+
|
|
62
|
+
setRequirementError?.((prevErrors) =>
|
|
63
|
+
prevErrors.filter((error) => error !== item.name)
|
|
25
64
|
);
|
|
65
|
+
}
|
|
26
66
|
|
|
27
67
|
// Validation based on input type
|
|
28
|
-
if (item?.type === "email") {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
} else if (item?.type === "tel") {
|
|
35
|
-
if (!validatePhoneNumber(value)) {
|
|
36
|
-
setInputError("Invalid phone number. Must be 10 digits.");
|
|
37
|
-
} else {
|
|
38
|
-
setInputError(null);
|
|
39
|
-
}
|
|
68
|
+
if (item?.type === "email" && !validateEmail(value)) {
|
|
69
|
+
setInputError("Invalid email format");
|
|
70
|
+
} else if (item?.type === "tel" && !validatePhoneNumber(value)) {
|
|
71
|
+
setInputError("Invalid phone number. Must be 10 digits.");
|
|
72
|
+
} else {
|
|
73
|
+
setInputError(null);
|
|
40
74
|
}
|
|
75
|
+
|
|
76
|
+
// Trigger onChange event
|
|
77
|
+
onChangeEvent?.(event);
|
|
41
78
|
};
|
|
42
79
|
|
|
43
80
|
return (
|
|
@@ -45,7 +82,7 @@ const InputHandles = ({
|
|
|
45
82
|
{item?.label && (
|
|
46
83
|
<label htmlFor={item.name} className="font-medium text-sm">
|
|
47
84
|
{item.label}
|
|
48
|
-
{
|
|
85
|
+
{isRequired && <span className="text-red-500">*</span>}
|
|
49
86
|
</label>
|
|
50
87
|
)}
|
|
51
88
|
<Input
|
|
@@ -53,12 +90,17 @@ const InputHandles = ({
|
|
|
53
90
|
name={item?.name}
|
|
54
91
|
placeholder={item?.placeholder || ""}
|
|
55
92
|
onChange={handleChange}
|
|
56
|
-
className={
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
93
|
+
className={twMerge(
|
|
94
|
+
`border rounded-md px-4 py-[6px] w-full text-black ${
|
|
95
|
+
inputError || (item?.name && requirementError.includes(item?.name))
|
|
96
|
+
? "border-red-500"
|
|
97
|
+
: "border-gray-300"
|
|
98
|
+
}`,
|
|
99
|
+
item?.stepProperty?.customClass?.inputClass
|
|
100
|
+
)}
|
|
61
101
|
defaultValue={item?.value ?? ""}
|
|
102
|
+
disabled={isDisabled}
|
|
103
|
+
required={isRequired}
|
|
62
104
|
/>
|
|
63
105
|
{item?.name && requirementError.includes(item.name) && (
|
|
64
106
|
<p className="text-red-500 text-xs">{`${item?.name} is required`}</p>
|
|
@@ -7,6 +7,7 @@ interface MultiSelectHandlesProps {
|
|
|
7
7
|
requirementError: string[];
|
|
8
8
|
setRequirementError?: React.Dispatch<React.SetStateAction<string[]>>;
|
|
9
9
|
formRef?: React.RefObject<HTMLFormElement | null>;
|
|
10
|
+
onChangeEvent?: (event: any) => void;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export default function MultiHandles({
|
|
@@ -14,6 +15,7 @@ export default function MultiHandles({
|
|
|
14
15
|
requirementError,
|
|
15
16
|
setRequirementError,
|
|
16
17
|
formRef,
|
|
18
|
+
onChangeEvent,
|
|
17
19
|
}: MultiSelectHandlesProps) {
|
|
18
20
|
const handleSelect = (value: string[], key: string) => {
|
|
19
21
|
if (formRef && formRef.current) {
|
|
@@ -53,6 +55,7 @@ export default function MultiHandles({
|
|
|
53
55
|
prevErrors.filter((errorName) => errorName !== item.name)
|
|
54
56
|
);
|
|
55
57
|
}
|
|
58
|
+
onChangeEvent?.(value);
|
|
56
59
|
}}
|
|
57
60
|
error={
|
|
58
61
|
item?.name && requirementError.includes(item.name)
|
|
@@ -1,103 +1,40 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
import { Select } from "../
|
|
3
|
-
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { Select } from "../refactoredSelect/Select";
|
|
3
|
+
|
|
4
|
+
interface Option {
|
|
5
|
+
value: string;
|
|
6
|
+
label: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface SelectHandlesProps {
|
|
10
|
+
item?: {
|
|
11
|
+
name?: string;
|
|
12
|
+
label?: string;
|
|
13
|
+
options?: Option[];
|
|
14
|
+
value?: string;
|
|
15
|
+
required?: boolean;
|
|
16
|
+
};
|
|
17
|
+
requirementError: string[];
|
|
18
|
+
setRequirementError: React.Dispatch<React.SetStateAction<string[]>>;
|
|
19
|
+
formRef: React.RefObject<HTMLFormElement | null>;
|
|
20
|
+
onChangeEvent?: (event: any) => void;
|
|
21
|
+
}
|
|
4
22
|
|
|
5
23
|
export default function SelectHandles({
|
|
6
24
|
item,
|
|
7
25
|
requirementError,
|
|
8
26
|
setRequirementError,
|
|
9
27
|
formRef,
|
|
10
|
-
|
|
28
|
+
onChangeEvent,
|
|
11
29
|
}: SelectHandlesProps) {
|
|
12
|
-
const [options
|
|
13
|
-
|
|
14
|
-
const getFieldValue = (fieldName: string): string => {
|
|
15
|
-
if (!formRef?.current) return "";
|
|
16
|
-
const field = formRef.current.querySelector(
|
|
17
|
-
`input[name="${fieldName}"]`
|
|
18
|
-
) as HTMLInputElement;
|
|
19
|
-
return field?.value || "";
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const getDependentOptions = (fieldName: string): Option[] => {
|
|
23
|
-
const fieldConfig = dependencyMap[fieldName];
|
|
24
|
-
if (!fieldConfig) return [];
|
|
25
|
-
|
|
26
|
-
let currentData = fieldConfig.dataStructure;
|
|
27
|
-
|
|
28
|
-
if (fieldConfig.parent) {
|
|
29
|
-
const parentValue = getFieldValue(fieldConfig.parent);
|
|
30
|
-
if (!parentValue) return [];
|
|
31
|
-
|
|
32
|
-
const getNestedValue = (data: any, path: string[]): any => {
|
|
33
|
-
return path.reduce((acc, key) => acc?.[key], data);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const parentChain: string[] = [];
|
|
37
|
-
let currentField = fieldName;
|
|
38
|
-
while (dependencyMap[currentField]?.parent) {
|
|
39
|
-
const parent = dependencyMap[currentField].parent!;
|
|
40
|
-
parentChain.unshift(getFieldValue(parent));
|
|
41
|
-
currentField = parent;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
currentData = getNestedValue(currentData, parentChain);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (Array.isArray(currentData)) {
|
|
48
|
-
return currentData.map((value) => ({ value, label: value }));
|
|
49
|
-
} else if (typeof currentData === "object" && currentData !== null) {
|
|
50
|
-
return Object.keys(currentData).map((key) => ({
|
|
51
|
-
value: key,
|
|
52
|
-
label: key,
|
|
53
|
-
}));
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return [];
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
useEffect(() => {
|
|
60
|
-
if (!item?.name || !dependencyMap[item.name]) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const updateOptions = () => {
|
|
65
|
-
const newOptions = getDependentOptions(item.name!);
|
|
66
|
-
setOptions(newOptions);
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
updateOptions();
|
|
70
|
-
|
|
71
|
-
const parentField = dependencyMap[item.name]?.parent;
|
|
72
|
-
if (parentField && formRef?.current) {
|
|
73
|
-
const parentInput = formRef.current.querySelector(
|
|
74
|
-
`input[name="${parentField}"]`
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
const handleParentChange = () => {
|
|
78
|
-
if (formRef.current) {
|
|
79
|
-
const currentField = formRef.current.querySelector(
|
|
80
|
-
`input[name="${item.name}"]`
|
|
81
|
-
) as HTMLInputElement;
|
|
82
|
-
if (currentField) {
|
|
83
|
-
currentField.value = "";
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
updateOptions();
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
parentInput?.addEventListener("change", handleParentChange);
|
|
90
|
-
return () =>
|
|
91
|
-
parentInput?.removeEventListener("change", handleParentChange);
|
|
92
|
-
}
|
|
93
|
-
}, [item?.name, dependencyMap, formRef]);
|
|
30
|
+
const [options] = useState<Option[]>(item?.options || []);
|
|
94
31
|
|
|
95
32
|
const handleSelect = (value: string | undefined, key: string) => {
|
|
96
33
|
if (!formRef?.current) return;
|
|
97
34
|
|
|
98
35
|
let input = formRef.current.querySelector(
|
|
99
36
|
`input[name="${key}"]`
|
|
100
|
-
) as HTMLInputElement;
|
|
37
|
+
) as HTMLInputElement | null;
|
|
101
38
|
|
|
102
39
|
if (!input) {
|
|
103
40
|
input = document.createElement("input");
|
|
@@ -127,10 +64,10 @@ export default function SelectHandles({
|
|
|
127
64
|
selectedItem={item?.value ?? ""}
|
|
128
65
|
onSelect={(value: string | undefined) => {
|
|
129
66
|
item?.name && handleSelect(value, item.name);
|
|
130
|
-
setRequirementError
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
67
|
+
setRequirementError((prevErrors) =>
|
|
68
|
+
prevErrors.filter((errorName) => errorName !== item?.name)
|
|
69
|
+
);
|
|
70
|
+
onChangeEvent?.(value);
|
|
134
71
|
}}
|
|
135
72
|
error={
|
|
136
73
|
item?.name && requirementError.includes(item.name)
|
|
@@ -12,17 +12,19 @@ import { FormElement, FormItem, FormRendererProps } from "./types";
|
|
|
12
12
|
import MultiHandles from "./MultiHandles";
|
|
13
13
|
import DatePickerHandles from "./DatePickerHandles";
|
|
14
14
|
import CheckboxHandles from "./CheckBoxHandles";
|
|
15
|
+
import { useFormContext } from "@grampro/expression-evaluator";
|
|
15
16
|
|
|
16
17
|
const FormRenderer = ({
|
|
17
18
|
onSubmit,
|
|
18
19
|
sourceData,
|
|
19
20
|
formFormationClass = "grid grid-cols-1 text-left gap-4",
|
|
20
21
|
formParentClass = "w-96",
|
|
21
|
-
dependencyConfig,
|
|
22
22
|
}: FormRendererProps) => {
|
|
23
23
|
const formRef = useRef<FormElement>(null);
|
|
24
24
|
const [requirementError, setRequirementError] = useState<string[]>([]);
|
|
25
25
|
|
|
26
|
+
const { context, updateContext } = useFormContext();
|
|
27
|
+
|
|
26
28
|
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
|
27
29
|
event.preventDefault();
|
|
28
30
|
const requirementErrorItems: string[] = [];
|
|
@@ -60,6 +62,10 @@ const FormRenderer = ({
|
|
|
60
62
|
item={item}
|
|
61
63
|
requirementError={requirementError}
|
|
62
64
|
setRequirementError={setRequirementError}
|
|
65
|
+
onChangeEvent={item.onChangeEvent}
|
|
66
|
+
formRef={formRef}
|
|
67
|
+
context={context}
|
|
68
|
+
updateContext={updateContext}
|
|
63
69
|
/>
|
|
64
70
|
);
|
|
65
71
|
|
|
@@ -67,11 +73,17 @@ const FormRenderer = ({
|
|
|
67
73
|
return (
|
|
68
74
|
<SelectHandles
|
|
69
75
|
key={index}
|
|
70
|
-
item={
|
|
76
|
+
item={{
|
|
77
|
+
name: item.name,
|
|
78
|
+
label: item.label,
|
|
79
|
+
options: item.options,
|
|
80
|
+
value: item.value,
|
|
81
|
+
required: Boolean(item.required),
|
|
82
|
+
}}
|
|
71
83
|
requirementError={requirementError}
|
|
72
84
|
setRequirementError={setRequirementError}
|
|
73
85
|
formRef={formRef}
|
|
74
|
-
|
|
86
|
+
onChangeEvent={item.onChangeEvent}
|
|
75
87
|
/>
|
|
76
88
|
);
|
|
77
89
|
|
|
@@ -83,6 +95,7 @@ const FormRenderer = ({
|
|
|
83
95
|
requirementError={requirementError}
|
|
84
96
|
setRequirementError={setRequirementError}
|
|
85
97
|
formRef={formRef}
|
|
98
|
+
onChangeEvent={item.onChangeEvent}
|
|
86
99
|
/>
|
|
87
100
|
);
|
|
88
101
|
|
|
@@ -94,6 +107,7 @@ const FormRenderer = ({
|
|
|
94
107
|
requirementError={requirementError}
|
|
95
108
|
setRequirementError={setRequirementError}
|
|
96
109
|
formRef={formRef}
|
|
110
|
+
onChangeEvent={item.onChangeEvent}
|
|
97
111
|
/>
|
|
98
112
|
);
|
|
99
113
|
|
|
@@ -105,6 +119,10 @@ const FormRenderer = ({
|
|
|
105
119
|
requirementError={requirementError}
|
|
106
120
|
setRequirementError={setRequirementError}
|
|
107
121
|
formRef={formRef}
|
|
122
|
+
onChangeEvent={item.onChangeEvent}
|
|
123
|
+
sourceData={sourceData}
|
|
124
|
+
context={context}
|
|
125
|
+
updateContext={updateContext}
|
|
108
126
|
/>
|
|
109
127
|
);
|
|
110
128
|
|
|
@@ -7,7 +7,7 @@ export type FormItem = {
|
|
|
7
7
|
name?: string;
|
|
8
8
|
component?: string;
|
|
9
9
|
type?: string;
|
|
10
|
-
required?: boolean;
|
|
10
|
+
required?: boolean | string;
|
|
11
11
|
placeholder?: string;
|
|
12
12
|
options?: { value: string; label: string }[];
|
|
13
13
|
value?: string;
|
|
@@ -17,6 +17,13 @@ export type FormItem = {
|
|
|
17
17
|
dependency?: string;
|
|
18
18
|
hasDependents?: boolean;
|
|
19
19
|
isReady?: boolean;
|
|
20
|
+
disabled?: string | boolean;
|
|
21
|
+
onChangeEvent?: (event: any) => void;
|
|
22
|
+
stepProperty?: {
|
|
23
|
+
customClass?: {
|
|
24
|
+
inputClass?: string;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
20
27
|
};
|
|
21
28
|
|
|
22
29
|
interface DependencyConfig {
|
|
@@ -58,4 +65,22 @@ export type CheckboxHandlesProps = {
|
|
|
58
65
|
requirementError: string[];
|
|
59
66
|
setRequirementError?: React.Dispatch<React.SetStateAction<string[]>>;
|
|
60
67
|
formRef?: React.RefObject<HTMLFormElement | null>;
|
|
68
|
+
onChangeEvent?: (event: any) => void;
|
|
69
|
+
changeTrigger?: number;
|
|
70
|
+
setChangeTrigger?: React.Dispatch<React.SetStateAction<number>>;
|
|
71
|
+
sourceData?: FormItem[];
|
|
72
|
+
context: FormContext;
|
|
73
|
+
updateContext: (
|
|
74
|
+
componentName: string,
|
|
75
|
+
fieldName: string,
|
|
76
|
+
value: FieldValue
|
|
77
|
+
) => void;
|
|
61
78
|
};
|
|
79
|
+
|
|
80
|
+
export type FieldValue = string | boolean | number | null;
|
|
81
|
+
|
|
82
|
+
export interface FormContext {
|
|
83
|
+
[componentName: string]: {
|
|
84
|
+
[fieldName: string]: FieldValue;
|
|
85
|
+
};
|
|
86
|
+
}
|