gbs-add-block 0.0.26 → 0.0.28
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/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
|
-
import React from "react";
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { validateEmail, validatePhoneNumber } from "./helperFunctions";
|
|
2
3
|
import { Input } from "../input";
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
5
|
+
const InputHandles = ({ item, requirementError, setRequirementError }: any) => {
|
|
6
|
+
const [inputError, setInputError] = useState<string | null>(null);
|
|
7
|
+
|
|
8
|
+
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
9
|
+
const { value } = event.target;
|
|
10
|
+
|
|
11
|
+
setRequirementError((prevErrors: string[]) =>
|
|
12
|
+
prevErrors.filter((error) => error !== item.name)
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
// Validation based on input type
|
|
16
|
+
if (item.type === "email") {
|
|
17
|
+
if (!validateEmail(value)) {
|
|
18
|
+
setInputError("Invalid email format");
|
|
19
|
+
} else {
|
|
20
|
+
setInputError(null);
|
|
21
|
+
}
|
|
22
|
+
} else if (item.type === "tel") {
|
|
23
|
+
if (!validatePhoneNumber(value)) {
|
|
24
|
+
setInputError("Invalid phone number. Must be 10 digits.");
|
|
25
|
+
} else {
|
|
26
|
+
setInputError(null);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
9
31
|
return (
|
|
10
32
|
<div className="w-full">
|
|
11
33
|
{item.label && (
|
|
@@ -17,17 +39,19 @@ export default function InputHandles({
|
|
|
17
39
|
type={item.type}
|
|
18
40
|
name={item.name}
|
|
19
41
|
placeholder={item.placeholder || ""}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
setRequirementError((prevErrors: any) =>
|
|
27
|
-
prevErrors.filter((errorName: any) => errorName !== item.name)
|
|
28
|
-
)
|
|
29
|
-
}
|
|
42
|
+
onChange={handleChange}
|
|
43
|
+
className={`border rounded p-2 w-full ${
|
|
44
|
+
inputError || requirementError.includes(item.name)
|
|
45
|
+
? "border-red-500"
|
|
46
|
+
: "border-gray-300"
|
|
47
|
+
}`}
|
|
30
48
|
/>
|
|
49
|
+
{requirementError.includes(item.name) && (
|
|
50
|
+
<p className="text-red-500 text-xs">{`${item.name} is required`}</p>
|
|
51
|
+
)}
|
|
52
|
+
{inputError && <p className="text-red-500 text-xs">{inputError}</p>}
|
|
31
53
|
</div>
|
|
32
54
|
);
|
|
33
|
-
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export default InputHandles;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const validateEmail = (email: string) => {
|
|
2
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
3
|
+
return emailRegex.test(email);
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export const validatePhoneNumber = (phone: string) => {
|
|
7
|
+
const phoneRegex = /^[0-9]{10}$/;
|
|
8
|
+
return phoneRegex.test(phone);
|
|
9
|
+
};
|
|
@@ -62,12 +62,12 @@ const FormRenderer = ({ onSubmit, sourceData }: any) => {
|
|
|
62
62
|
case "button":
|
|
63
63
|
return (
|
|
64
64
|
<div key={index} className="w-full mt-2">
|
|
65
|
-
<
|
|
65
|
+
<button
|
|
66
66
|
type={item.type}
|
|
67
67
|
className="w-full bg-black text-white py-2 rounded-xl hover:bg-gray-800"
|
|
68
68
|
>
|
|
69
69
|
{item.value}
|
|
70
|
-
</
|
|
70
|
+
</button>
|
|
71
71
|
</div>
|
|
72
72
|
);
|
|
73
73
|
|