gbs-add-block 0.0.34 → 0.0.36
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/README.md +4 -3
- package/package.json +1 -1
- package/source/components/formrenderer/InputHandles.tsx +1 -0
- package/source/components/formrenderer/MultiHandles.tsx +1 -0
- package/source/components/formrenderer/SelectHandles.tsx +106 -27
- package/source/components/formrenderer/helperFunctions.ts +13 -0
- package/source/components/formrenderer/index.tsx +2 -2
- package/source/components/formrenderer/types.ts +31 -0
- package/source/components/grid/Grid.tsx +14 -1
- package/source/components/grid/RenderCell.tsx +32 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# GBS Building Blocks 2.0 (v0.0.
|
|
1
|
+
# GBS Building Blocks 2.0 (v0.0.36)
|
|
2
2
|
|
|
3
3
|
Latest and upgraded version of GBS building blocks with headless UI and removed dependencies.
|
|
4
4
|
|
|
@@ -6,9 +6,10 @@ Latest and upgraded version of GBS building blocks with headless UI and removed
|
|
|
6
6
|
|
|
7
7
|
For detailed documentation on usage and props, Please visit: [Building Block Documentation v2.0](https://blackmax-designs.gitbook.io/building-block-v2.0)
|
|
8
8
|
|
|
9
|
-
## What's New 🎉 (Ver 0.0.
|
|
9
|
+
## What's New 🎉 (Ver 0.0.36)
|
|
10
10
|
|
|
11
|
-
- Updated Form Renderer
|
|
11
|
+
- Updated Form Renderer with form builder and AI support
|
|
12
|
+
- Updated Grid
|
|
12
13
|
|
|
13
14
|
## Authors
|
|
14
15
|
|
package/package.json
CHANGED
|
@@ -1,36 +1,114 @@
|
|
|
1
|
-
import React from "react";
|
|
1
|
+
import React, { useEffect, useState } from "react";
|
|
2
2
|
import { Select } from "../select";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
interface SelectHandlesProps {
|
|
6
|
-
item?: FormItem;
|
|
7
|
-
requirementError: string[];
|
|
8
|
-
setRequirementError?: React.Dispatch<React.SetStateAction<string[]>>;
|
|
9
|
-
formRef?: React.RefObject<HTMLFormElement | null>;
|
|
10
|
-
}
|
|
3
|
+
import { Option, SelectHandlesProps } from "./types";
|
|
11
4
|
|
|
12
5
|
export default function SelectHandles({
|
|
13
6
|
item,
|
|
14
7
|
requirementError,
|
|
15
8
|
setRequirementError,
|
|
16
9
|
formRef,
|
|
10
|
+
dependencyMap = {},
|
|
17
11
|
}: SelectHandlesProps) {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
12
|
+
const [options, setOptions] = useState<Option[]>(item?.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;
|
|
32
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;
|
|
33
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]);
|
|
94
|
+
|
|
95
|
+
const handleSelect = (value: string, key: string) => {
|
|
96
|
+
if (!formRef?.current) return;
|
|
97
|
+
|
|
98
|
+
let input = formRef.current.querySelector(
|
|
99
|
+
`input[name="${key}"]`
|
|
100
|
+
) as HTMLInputElement;
|
|
101
|
+
|
|
102
|
+
if (!input) {
|
|
103
|
+
input = document.createElement("input");
|
|
104
|
+
input.type = "hidden";
|
|
105
|
+
input.name = key;
|
|
106
|
+
formRef.current.appendChild(input);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
input.value = value;
|
|
110
|
+
const event = new Event("change", { bubbles: true });
|
|
111
|
+
input.dispatchEvent(event);
|
|
34
112
|
};
|
|
35
113
|
|
|
36
114
|
return (
|
|
@@ -38,17 +116,18 @@ export default function SelectHandles({
|
|
|
38
116
|
{item?.label && (
|
|
39
117
|
<label htmlFor={item.name} className="font-medium text-sm">
|
|
40
118
|
{item.label}
|
|
119
|
+
{item.required && <span className="text-red-500">*</span>}
|
|
41
120
|
</label>
|
|
42
121
|
)}
|
|
43
122
|
<Select
|
|
44
123
|
name={item?.name}
|
|
45
|
-
items={
|
|
124
|
+
items={options}
|
|
46
125
|
selectedItem={item?.value ?? ""}
|
|
47
126
|
onSelect={(value: string) => {
|
|
48
127
|
item?.name && handleSelect(value, item.name);
|
|
49
128
|
setRequirementError &&
|
|
50
|
-
setRequirementError((prevErrors
|
|
51
|
-
prevErrors.filter((errorName
|
|
129
|
+
setRequirementError((prevErrors) =>
|
|
130
|
+
prevErrors.filter((errorName) => errorName !== item?.name)
|
|
52
131
|
);
|
|
53
132
|
}}
|
|
54
133
|
error={
|
|
@@ -56,7 +135,7 @@ export default function SelectHandles({
|
|
|
56
135
|
? `${item.name} is required`
|
|
57
136
|
: undefined
|
|
58
137
|
}
|
|
59
|
-
|
|
138
|
+
/>
|
|
60
139
|
</div>
|
|
61
140
|
);
|
|
62
141
|
}
|
|
@@ -7,3 +7,16 @@ export const validatePhoneNumber = (phone: string) => {
|
|
|
7
7
|
const phoneRegex = /^[0-9]{10}$/;
|
|
8
8
|
return phoneRegex.test(phone);
|
|
9
9
|
};
|
|
10
|
+
|
|
11
|
+
// In case of return as objects
|
|
12
|
+
export const formDataToObject = (formData: FormData) => {
|
|
13
|
+
const obj: Record<string, any> = {};
|
|
14
|
+
formData.forEach((value, key) => {
|
|
15
|
+
try {
|
|
16
|
+
obj[key] = JSON.parse(value as string);
|
|
17
|
+
} catch {
|
|
18
|
+
obj[key] = value;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return obj;
|
|
22
|
+
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use client";
|
|
2
1
|
/**
|
|
3
2
|
* Copyright (c) Grampro Business Services and affiliates.
|
|
4
3
|
*
|
|
@@ -17,13 +16,13 @@ const FormRenderer = ({
|
|
|
17
16
|
sourceData,
|
|
18
17
|
formFormationClass = "grid grid-cols-1 text-left gap-4",
|
|
19
18
|
formParentClass = "w-96",
|
|
19
|
+
dependencyConfig,
|
|
20
20
|
}: FormRendererProps) => {
|
|
21
21
|
const formRef = useRef<HTMLFormElement>(null);
|
|
22
22
|
const [requirementError, setRequirementError] = useState<string[]>([]);
|
|
23
23
|
|
|
24
24
|
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
|
25
25
|
event.preventDefault();
|
|
26
|
-
|
|
27
26
|
const requirementErrorItems: string[] = [];
|
|
28
27
|
|
|
29
28
|
sourceData?.forEach((item: FormItem) => {
|
|
@@ -70,6 +69,7 @@ const FormRenderer = ({
|
|
|
70
69
|
requirementError={requirementError}
|
|
71
70
|
setRequirementError={setRequirementError}
|
|
72
71
|
formRef={formRef}
|
|
72
|
+
dependencyMap={dependencyConfig}
|
|
73
73
|
/>
|
|
74
74
|
);
|
|
75
75
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export type FormItem = {
|
|
2
|
+
id?: string;
|
|
2
3
|
name?: string;
|
|
3
4
|
component?: string;
|
|
4
5
|
type?: string;
|
|
@@ -9,11 +10,41 @@ export type FormItem = {
|
|
|
9
10
|
label?: string;
|
|
10
11
|
button_type?: "button" | "submit" | "reset";
|
|
11
12
|
key?: string;
|
|
13
|
+
dependency?: string;
|
|
14
|
+
hasDependents?: boolean;
|
|
15
|
+
isReady?: boolean;
|
|
12
16
|
};
|
|
13
17
|
|
|
18
|
+
interface DependencyConfig {
|
|
19
|
+
source: string;
|
|
20
|
+
parent?: string;
|
|
21
|
+
dataStructure: any;
|
|
22
|
+
}
|
|
23
|
+
|
|
14
24
|
export type FormRendererProps = {
|
|
15
25
|
onSubmit?: (formData: FormData) => void;
|
|
16
26
|
sourceData?: FormItem[] | undefined;
|
|
17
27
|
formFormationClass?: string;
|
|
18
28
|
formParentClass?: string;
|
|
29
|
+
dependencyConfig?: Record<string, DependencyConfig>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type SelectHandlesProps = {
|
|
33
|
+
item?: FormItem;
|
|
34
|
+
requirementError: string[];
|
|
35
|
+
setRequirementError?: React.Dispatch<React.SetStateAction<string[]>>;
|
|
36
|
+
formRef?: React.RefObject<HTMLFormElement | null>;
|
|
37
|
+
dependencyMap?: Record<
|
|
38
|
+
string,
|
|
39
|
+
{
|
|
40
|
+
source: string;
|
|
41
|
+
parent?: string;
|
|
42
|
+
dataStructure: any;
|
|
43
|
+
}
|
|
44
|
+
>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type Option = {
|
|
48
|
+
value: string;
|
|
49
|
+
label: string;
|
|
19
50
|
};
|
|
@@ -232,6 +232,8 @@ export const GridMemoised = forwardRef((props: GridProps, ref) => {
|
|
|
232
232
|
|
|
233
233
|
// This will render template passed to Grid
|
|
234
234
|
const renderCell = (rowData: any, column: any, rowIndex: number) => {
|
|
235
|
+
const cellValue = rowData[column.field];
|
|
236
|
+
|
|
235
237
|
if (column.template) {
|
|
236
238
|
return (
|
|
237
239
|
<column.template
|
|
@@ -243,7 +245,18 @@ export const GridMemoised = forwardRef((props: GridProps, ref) => {
|
|
|
243
245
|
/>
|
|
244
246
|
);
|
|
245
247
|
}
|
|
246
|
-
|
|
248
|
+
|
|
249
|
+
if (column.tooltip) {
|
|
250
|
+
return (
|
|
251
|
+
<span title={cellValue?.toString()}>
|
|
252
|
+
{cellValue && cellValue.length > 10
|
|
253
|
+
? `${cellValue.slice(0, 10)}...`
|
|
254
|
+
: cellValue}
|
|
255
|
+
</span>
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return cellValue;
|
|
247
260
|
};
|
|
248
261
|
|
|
249
262
|
// *** Filter Handling Functions Starts Here
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const renderCell = (
|
|
2
|
+
rowData: any,
|
|
3
|
+
column: any,
|
|
4
|
+
rowIndex: number,
|
|
5
|
+
currentPage: number,
|
|
6
|
+
pageSettings: any,
|
|
7
|
+
rowChange?: any
|
|
8
|
+
) => {
|
|
9
|
+
const cellValue = rowData[column.field];
|
|
10
|
+
|
|
11
|
+
if (column.template) {
|
|
12
|
+
return (
|
|
13
|
+
<column.template
|
|
14
|
+
rowData={rowData}
|
|
15
|
+
rowIndex={rowIndex + currentPage * pageSettings.pageNumber}
|
|
16
|
+
rowChange={(changes: any) => {
|
|
17
|
+
if (rowChange) rowChange(changes);
|
|
18
|
+
}}
|
|
19
|
+
/>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (column.tooltip) {
|
|
24
|
+
return (
|
|
25
|
+
<span title={cellValue?.toString()}>
|
|
26
|
+
{column.tooltip ? cellValue.slice(0, 10) : cellValue}
|
|
27
|
+
</span>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return cellValue;
|
|
32
|
+
};
|