gbs-add-block 0.0.49 → 0.0.50

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.49)
1
+ # GBS Building Blocks 2.0 (v0.0.50)
2
2
 
3
3
  Latest and upgraded version of GBS building blocks with headless UI and removed dependencies.
4
4
 
@@ -6,7 +6,7 @@ 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.49)
9
+ ## What's New 🎉 (Ver 0.0.50)
10
10
 
11
11
  - Update on form renderer
12
12
  - Refactored Code for Better Readability
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gbs-add-block",
3
- "version": "0.0.49",
3
+ "version": "0.0.50",
4
4
  "description": "React Component Library",
5
5
  "files": [
6
6
  "index.js",
@@ -1,149 +1,111 @@
1
- /**
2
- * Copyright (c) Grampro Business Services and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- import React, { useEffect, useRef, useState } from "react";
1
+ import React, { useRef, useState, FormEvent } from "react";
9
2
  import { useFormContext } from "@grampro/headless-helpers";
10
- import { FormElement, FormItem, FormRendererProps } from "./types";
11
3
  import InputHandles from "./componentHandles/InputHandles";
12
4
  import SelectHandles from "./componentHandles/SelectHandles";
13
5
  import MultiHandles from "./componentHandles/MultiHandles";
14
6
  import DatePickerHandles from "./componentHandles/DatePickerHandles";
15
7
  import CheckboxHandles from "./componentHandles/CheckBoxHandles";
8
+ import { FormItem, FormRendererProps } from "./types";
9
+
10
+ // Component mapping
11
+ const COMPONENT_MAP = {
12
+ input: InputHandles,
13
+ select: SelectHandles,
14
+ "multi-select": MultiHandles,
15
+ datepicker: DatePickerHandles,
16
+ checkbox: CheckboxHandles,
17
+ } as const;
18
+
19
+ const validateField = (
20
+ field: HTMLInputElement | null,
21
+ item: FormItem
22
+ ): boolean => {
23
+ if (!item.required || !item.name || field?.disabled) {
24
+ return true;
25
+ }
26
+ // Explicitly check for null and empty value
27
+ if (!field) {
28
+ return false;
29
+ }
30
+ return field.value.trim() !== "";
31
+ };
16
32
 
17
- const FormRenderer = ({
33
+ const FormRenderer: React.FC<FormRendererProps> = ({
18
34
  onSubmit,
19
- sourceData,
35
+ sourceData = [],
20
36
  formFormationClass = "grid grid-cols-1 text-left gap-4",
21
37
  formParentClass = "w-96",
22
- }: FormRendererProps) => {
23
- const formRef = useRef<FormElement>(null);
38
+ }) => {
39
+ const formRef = useRef<HTMLFormElement>(null);
24
40
  const [requirementError, setRequirementError] = useState<string[]>([]);
25
-
26
41
  const { context, updateContext } = useFormContext();
27
42
 
28
- const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
43
+ // Handle form submission
44
+ const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
29
45
  event.preventDefault();
30
- const requirementErrorItems: string[] = [];
31
46
 
32
- sourceData?.forEach((item: FormItem) => {
33
- if (item.required && item.name) {
47
+ const requirementErrorItems = sourceData
48
+ .filter((item) => item.required && item.name)
49
+ .reduce((errors: string[], item) => {
34
50
  const field = formRef.current?.querySelector(
35
51
  `[name="${item.name}"]`
36
52
  ) as HTMLInputElement | null;
37
53
 
38
- if (!field || !field.value.trim()) {
39
- requirementErrorItems.push(item.name);
54
+ if (!validateField(field, item)) {
55
+ errors.push(item.name!);
40
56
  }
41
- }
42
- });
57
+ return errors;
58
+ }, []);
43
59
 
44
60
  setRequirementError(requirementErrorItems);
45
61
 
46
- if (requirementErrorItems.length > 0) return;
62
+ if (requirementErrorItems.length === 0 && onSubmit && formRef.current) {
63
+ onSubmit(new FormData(formRef.current));
64
+ }
65
+ };
47
66
 
48
- const formData = new FormData(formRef.current as HTMLFormElement);
49
- if (onSubmit) onSubmit(formData);
67
+ // Render form component based on the type
68
+ const renderFormComponent = (item: FormItem, index: number) => {
69
+ if (item.component === "button") {
70
+ return (
71
+ <div key={index} className="w-full mt-1">
72
+ <button
73
+ type={item.button_type}
74
+ className="w-full bg-black text-white py-2 rounded-xl hover:bg-gray-800"
75
+ >
76
+ {item.value}
77
+ </button>
78
+ </div>
79
+ );
80
+ }
81
+
82
+ const Component = item.component && COMPONENT_MAP[item.component];
83
+ if (!Component) return null;
84
+
85
+ return (
86
+ <Component
87
+ key={index}
88
+ item={item}
89
+ requirementError={requirementError}
90
+ setRequirementError={setRequirementError}
91
+ formRef={formRef}
92
+ onChangeEvent={item.onChangeEvent}
93
+ context={context}
94
+ updateContext={updateContext}
95
+ />
96
+ );
50
97
  };
51
98
 
99
+ // If no source data is found, return a message
100
+ if (!sourceData.length) {
101
+ return <div>No Source Data Found</div>;
102
+ }
103
+
52
104
  return (
53
105
  <form ref={formRef} onSubmit={handleSubmit} className={formParentClass}>
54
- {sourceData && sourceData?.length > 0 ? (
55
- <div className={formFormationClass}>
56
- {sourceData.map((item: FormItem, index: number) => {
57
- switch (item.component) {
58
- case "input":
59
- return (
60
- <InputHandles
61
- key={index}
62
- item={item}
63
- requirementError={requirementError}
64
- setRequirementError={setRequirementError}
65
- onChangeEvent={item.onChangeEvent}
66
- formRef={formRef}
67
- context={context}
68
- updateContext={updateContext}
69
- />
70
- );
71
-
72
- case "select":
73
- return (
74
- <SelectHandles
75
- key={index}
76
- item={item}
77
- requirementError={requirementError}
78
- setRequirementError={setRequirementError}
79
- formRef={formRef}
80
- onChangeEvent={item.onChangeEvent}
81
- context={context}
82
- updateContext={updateContext}
83
- />
84
- );
85
-
86
- case "multi-select":
87
- return (
88
- <MultiHandles
89
- key={index}
90
- item={item}
91
- requirementError={requirementError}
92
- setRequirementError={setRequirementError}
93
- formRef={formRef}
94
- onChangeEvent={item.onChangeEvent}
95
- context={context}
96
- updateContext={updateContext}
97
- />
98
- );
99
-
100
- case "datepicker":
101
- return (
102
- <DatePickerHandles
103
- key={index}
104
- item={item}
105
- requirementError={requirementError}
106
- setRequirementError={setRequirementError}
107
- formRef={formRef}
108
- onChangeEvent={item.onChangeEvent}
109
- />
110
- );
111
-
112
- case "checkbox":
113
- return (
114
- <CheckboxHandles
115
- key={index}
116
- item={item}
117
- requirementError={requirementError}
118
- setRequirementError={setRequirementError}
119
- formRef={formRef}
120
- onChangeEvent={item.onChangeEvent}
121
- sourceData={sourceData}
122
- context={context}
123
- updateContext={updateContext}
124
- />
125
- );
126
-
127
- case "button":
128
- return (
129
- <div key={index} className="w-full mt-1">
130
- <button
131
- type={item.button_type}
132
- className="w-full bg-black text-white py-2 rounded-xl hover:bg-gray-800"
133
- >
134
- {item.value}
135
- </button>
136
- </div>
137
- );
138
-
139
- default:
140
- return null;
141
- }
142
- })}
143
- </div>
144
- ) : (
145
- <div>No Source Data Found</div>
146
- )}
106
+ <div className={formFormationClass}>
107
+ {sourceData.map(renderFormComponent)}
108
+ </div>
147
109
  </form>
148
110
  );
149
111
  };
@@ -9,7 +9,13 @@ export interface FormElement extends HTMLFormElement {
9
9
  export type FormItem = {
10
10
  id?: string;
11
11
  name?: string;
12
- component?: string;
12
+ component?:
13
+ | "input"
14
+ | "select"
15
+ | "multi-select"
16
+ | "datepicker"
17
+ | "checkbox"
18
+ | "button";
13
19
  type?: string;
14
20
  required?: boolean | string;
15
21
  placeholder?: string;