gbs-add-block 0.0.33 → 0.0.35

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 CHANGED
@@ -1,4 +1,4 @@
1
- # GBS Building Blocks 2.0 (v0.0.33)
1
+ # GBS Building Blocks 2.0 (v0.0.35)
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.23)
9
+ ## What's New 🎉 (Ver 0.0.35)
10
10
 
11
- - Updated Form Renderer (Added Multi Select)
11
+ - Updated Form Renderer (Added Multi Select) with form builder
12
+ - Updated Date Picker
12
13
 
13
14
  ## Authors
14
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gbs-add-block",
3
- "version": "0.0.33",
3
+ "version": "0.0.35",
4
4
  "description": "React Component Library",
5
5
  "files": [
6
6
  "index.js",
@@ -17,7 +17,7 @@ export const DatePicker = ({
17
17
  minDate = null,
18
18
  maxDate = null,
19
19
  yearLimitStart = 50,
20
- yearLimitEnd = 30,
20
+ yearLimitEnd = 0,
21
21
  onDateChange,
22
22
  }: DatePickerProps) => {
23
23
  const [showDatepicker, setShowDatepicker] = useState(false);
@@ -36,6 +36,12 @@ export const DatePicker = ({
36
36
  const dateRef = useRef<HTMLDivElement>(null);
37
37
  const today = new Date();
38
38
 
39
+ const actualCurrentYear = today.getFullYear();
40
+ const years = Array.from(
41
+ { length: yearLimitStart + yearLimitEnd + 1 },
42
+ (_, i) => actualCurrentYear - yearLimitStart + i
43
+ ).sort((a, b) => b - a);
44
+
39
45
  useEffect(() => {
40
46
  if (selectedDateValue) {
41
47
  setSelectedDate(selectedDateValue);
@@ -44,13 +50,10 @@ export const DatePicker = ({
44
50
  }
45
51
  }, [selectedDateValue]);
46
52
 
47
- // This useEffect will ensure the server and client
48
- // rendererd date will not mismatch
49
53
  useEffect(() => {
50
54
  setHasMounted(true);
51
55
  }, []);
52
56
 
53
- // This will helps to close the popup when clicked outside of datepicker component
54
57
  useEffect(() => {
55
58
  function handleClickOutside(event: MouseEvent) {
56
59
  if (dateRef.current && !dateRef.current.contains(event.target as Node)) {
@@ -68,13 +71,11 @@ export const DatePicker = ({
68
71
  setDays(generateCalendarHelper(currentYear, currentMonth));
69
72
  }, [currentYear, currentMonth]);
70
73
 
71
- // DatePicker Toggler
72
74
  const toggleDatepicker = () => {
73
75
  setShowDatepicker(!showDatepicker);
74
76
  setShowYearMonthPicker(false);
75
77
  };
76
78
 
77
- // YearMonthPicker Toggler
78
79
  const toggleYearMonthPicker = () => {
79
80
  setShowYearMonthPicker(!showYearMonthPicker);
80
81
  };
@@ -93,7 +94,6 @@ export const DatePicker = ({
93
94
  setDays(generateCalendarHelper(year, month));
94
95
  };
95
96
 
96
- // *** DatePicker navigation starts here
97
97
  const prevMonth = () => {
98
98
  if (currentMonth === 0) {
99
99
  setCurrentMonth(11);
@@ -118,12 +118,6 @@ export const DatePicker = ({
118
118
  setSelectedDate(new Date());
119
119
  if (onDateChange) onDateChange(new Date());
120
120
  };
121
- // DatePicker navigation ends here ***
122
-
123
- const years = Array.from(
124
- { length: yearLimitEnd },
125
- (_, i) => currentYear - yearLimitStart + i
126
- );
127
121
 
128
122
  if (!hasMounted) {
129
123
  return null;
@@ -137,9 +131,9 @@ export const DatePicker = ({
137
131
  className="border p-2 rounded w-full flex items-center gap-2 dark:text-white"
138
132
  >
139
133
  <Icon
140
- dimensions={{ width: "30", height: "30" }}
134
+ dimensions={{ width: "20", height: "20" }}
141
135
  elements={calender}
142
- svgClass={"stroke-black fill-none dark:stroke-white"}
136
+ svgClass={"stroke-gray-500 fill-none dark:stroke-white"}
143
137
  />
144
138
 
145
139
  {showPlaceHolder ? showPlaceHolder : selectedDate.toLocaleDateString()}
@@ -1,36 +1,114 @@
1
- import React from "react";
1
+ import React, { useEffect, useState } from "react";
2
2
  import { Select } from "../select";
3
- import { FormItem } from "./types";
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 handleSelect = (value: string, key: string) => {
19
- if (formRef && formRef.current) {
20
- const hiddenInput = formRef.current.querySelector(
21
- `input[name="${key}"]`
22
- ) as HTMLInputElement;
23
-
24
- if (hiddenInput) {
25
- hiddenInput.value = value;
26
- } else {
27
- const input = document.createElement("input");
28
- input.type = "hidden";
29
- input.name = key;
30
- input.value = value;
31
- formRef.current.appendChild(input);
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 (
@@ -42,13 +120,13 @@ export default function SelectHandles({
42
120
  )}
43
121
  <Select
44
122
  name={item?.name}
45
- items={item?.options}
123
+ items={options}
46
124
  selectedItem={item?.value ?? ""}
47
125
  onSelect={(value: string) => {
48
126
  item?.name && handleSelect(value, item.name);
49
127
  setRequirementError &&
50
- setRequirementError((prevErrors: any) =>
51
- prevErrors.filter((errorName: any) => errorName !== item?.name)
128
+ setRequirementError((prevErrors) =>
129
+ prevErrors.filter((errorName) => errorName !== item?.name)
52
130
  );
53
131
  }}
54
132
  error={
@@ -56,7 +134,7 @@ export default function SelectHandles({
56
134
  ? `${item.name} is required`
57
135
  : undefined
58
136
  }
59
- ></Select>
137
+ />
60
138
  </div>
61
139
  );
62
140
  }
@@ -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
 
@@ -9,11 +9,40 @@ export type FormItem = {
9
9
  label?: string;
10
10
  button_type?: "button" | "submit" | "reset";
11
11
  key?: string;
12
+ dependency?: string;
13
+ hasDependents?: boolean;
12
14
  };
13
15
 
16
+ interface DependencyConfig {
17
+ source: string;
18
+ parent?: string;
19
+ dataStructure: any;
20
+ }
21
+
14
22
  export type FormRendererProps = {
15
23
  onSubmit?: (formData: FormData) => void;
16
24
  sourceData?: FormItem[] | undefined;
17
25
  formFormationClass?: string;
18
26
  formParentClass?: string;
27
+ dependencyConfig?: Record<string, DependencyConfig>;
28
+ };
29
+
30
+ export type SelectHandlesProps = {
31
+ item?: FormItem;
32
+ requirementError: string[];
33
+ setRequirementError?: React.Dispatch<React.SetStateAction<string[]>>;
34
+ formRef?: React.RefObject<HTMLFormElement | null>;
35
+ dependencyMap?: Record<
36
+ string,
37
+ {
38
+ source: string;
39
+ parent?: string;
40
+ dataStructure: any;
41
+ }
42
+ >;
43
+ };
44
+
45
+ export type Option = {
46
+ value: string;
47
+ label: string;
19
48
  };