react-groundwork 0.0.0-stage → 0.1.0
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/.env.example +6 -0
- package/.gitattributes +2 -0
- package/.prettierignore +7 -0
- package/.prettierrc.json +12 -0
- package/README.md +24 -2
- package/cli/index.js +119 -0
- package/components.json +25 -0
- package/eslint.config.js +17 -0
- package/index.html +13 -0
- package/jsconfig.json +8 -0
- package/package.json +111 -4
- package/public/.htaccess +11 -0
- package/public/logo.png +0 -0
- package/src/App.jsx +14 -0
- package/src/api/api.routes.js +12 -0
- package/src/api/api.services.js +8 -0
- package/src/api/apiCall.js +128 -0
- package/src/api/apiHelpers.js +20 -0
- package/src/assets/css/index.css +173 -0
- package/src/assets/images/placeholders/placeholder-170x166.png +0 -0
- package/src/assets/images/placeholders/placeholder-360x260.png +0 -0
- package/src/assets/images/placeholders/placeholder-372x225.png +0 -0
- package/src/assets/images/placeholders/placeholder-40x40.png +0 -0
- package/src/assets/images/placeholders/placeholder-451x392.png +0 -0
- package/src/components/common/AppChart.jsx +56 -0
- package/src/components/common/Breadcrumbs.jsx +90 -0
- package/src/components/common/DataTable.jsx +203 -0
- package/src/components/common/DatePicker.jsx +275 -0
- package/src/components/common/DialogComponent.jsx +308 -0
- package/src/components/common/ErrorStateComponent.jsx +46 -0
- package/src/components/common/FullscreenToggle.jsx +54 -0
- package/src/components/common/HeadingComponent.jsx +29 -0
- package/src/components/common/LoadingComponent.jsx +33 -0
- package/src/components/common/NetworkStatus.jsx +103 -0
- package/src/components/common/NoDataComponent.jsx +45 -0
- package/src/components/common/Pagination.jsx +128 -0
- package/src/components/common/ParagraphComponent.jsx +14 -0
- package/src/components/common/PhoneInputComponent.jsx +242 -0
- package/src/components/common/ReactSelect.jsx +145 -0
- package/src/components/common/SearchSelect.jsx +176 -0
- package/src/components/common/SeeMore.jsx +26 -0
- package/src/components/common/SortableHeader.jsx +23 -0
- package/src/components/common/theme-toggle.jsx +39 -0
- package/src/components/layout/AuthLayout.jsx +90 -0
- package/src/components/layout/PageLayout.jsx +32 -0
- package/src/components/layout/SidebarLayout.jsx +233 -0
- package/src/components/ui/avatar.jsx +84 -0
- package/src/components/ui/badge.jsx +43 -0
- package/src/components/ui/button.jsx +44 -0
- package/src/components/ui/card.jsx +69 -0
- package/src/components/ui/checkbox.jsx +25 -0
- package/src/components/ui/dialog.jsx +149 -0
- package/src/components/ui/empty.jsx +82 -0
- package/src/components/ui/input.jsx +19 -0
- package/src/components/ui/label.jsx +19 -0
- package/src/components/ui/select.jsx +156 -0
- package/src/components/ui/skeleton.jsx +7 -0
- package/src/components/ui/sonner.jsx +37 -0
- package/src/components/ui/switch.jsx +23 -0
- package/src/components/ui/table.jsx +74 -0
- package/src/components/ui/textarea.jsx +16 -0
- package/src/constants/constant.js +16 -0
- package/src/context/theme-provider.jsx +48 -0
- package/src/features/components/ComponentFormDialog.jsx +162 -0
- package/src/features/components/ComponentsTable.jsx +219 -0
- package/src/features/components/componentStyles.js +16 -0
- package/src/features/components/mockData.js +407 -0
- package/src/features/playground/BasicsSection.jsx +122 -0
- package/src/features/playground/DataSection.jsx +210 -0
- package/src/features/playground/DemoCard.jsx +21 -0
- package/src/features/playground/FeedbackSection.jsx +309 -0
- package/src/features/playground/FormsSection.jsx +188 -0
- package/src/features/playground/UtilitiesSection.jsx +200 -0
- package/src/hooks/useApiHandler.jsx +82 -0
- package/src/hooks/useDebounce.js +12 -0
- package/src/lib/utils.js +1 -0
- package/src/main.jsx +16 -0
- package/src/pages/auth/ForgotPasswordPage.jsx +101 -0
- package/src/pages/auth/LoginPage.jsx +120 -0
- package/src/pages/auth/SignupPage.jsx +135 -0
- package/src/pages/views/ComponentsPage.jsx +316 -0
- package/src/pages/views/DashboardPage.jsx +260 -0
- package/src/pages/views/LandingPage.jsx +772 -0
- package/src/pages/views/NotFoundPage.jsx +30 -0
- package/src/pages/views/PlaygroundPage.jsx +50 -0
- package/src/routes/AppRoutes.jsx +29 -0
- package/src/routes/ProtectedRoute.jsx +18 -0
- package/src/utils/exportCsv.js +17 -0
- package/src/utils/formatCurrency.js +19 -0
- package/src/utils/formatDateTime.js +34 -0
- package/src/utils/handleSessionExpire.js +22 -0
- package/src/utils/logger.js +34 -0
- package/vite.config.js +23 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { cn } from "@/lib/utils";
|
|
2
|
+
|
|
3
|
+
const SIZE_CLASSES = {
|
|
4
|
+
page: "text-xl font-semibold",
|
|
5
|
+
section: "text-base font-semibold",
|
|
6
|
+
sm: "text-sm font-medium",
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const SUBTITLE_SIZE_CLASSES = {
|
|
10
|
+
page: "text-sm text-muted-foreground",
|
|
11
|
+
section: "text-xs text-muted-foreground",
|
|
12
|
+
sm: "text-xs text-muted-foreground",
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const HeadingComponent = ({ title, subtitle, size = "page", actions, className }) => {
|
|
16
|
+
return (
|
|
17
|
+
<div className={cn("flex flex-col justify-between gap-2 sm:flex-row sm:items-center", className)}>
|
|
18
|
+
<div className="min-w-0">
|
|
19
|
+
<h2 className={cn("truncate font-heading tracking-tight", SIZE_CLASSES[size] ?? SIZE_CLASSES.page)}>{title}</h2>
|
|
20
|
+
{subtitle && (
|
|
21
|
+
<p className={cn("truncate", SUBTITLE_SIZE_CLASSES[size] ?? SUBTITLE_SIZE_CLASSES.page)}>{subtitle}</p>
|
|
22
|
+
)}
|
|
23
|
+
</div>
|
|
24
|
+
{actions && <div className="flex shrink-0 items-center gap-2">{actions}</div>}
|
|
25
|
+
</div>
|
|
26
|
+
);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export default HeadingComponent;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Loader2 } from "lucide-react";
|
|
2
|
+
import { cn } from "@/lib/utils";
|
|
3
|
+
|
|
4
|
+
const VARIANT_CLASSES = {
|
|
5
|
+
inline: "w-full py-10",
|
|
6
|
+
page: "w-full min-h-[70vh]",
|
|
7
|
+
overlay: "fixed inset-0 z-50 bg-background/80 backdrop-blur-sm",
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const SIZE_CLASSES = {
|
|
11
|
+
sm: "h-4 w-4",
|
|
12
|
+
default: "h-6 w-6",
|
|
13
|
+
lg: "h-8 w-8",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const LoadingComponent = ({ variant = "inline", size = "default", label = "Loading...", className }) => {
|
|
17
|
+
return (
|
|
18
|
+
<div
|
|
19
|
+
role="status"
|
|
20
|
+
aria-live="polite"
|
|
21
|
+
className={cn(
|
|
22
|
+
"flex flex-col items-center justify-center gap-2 text-center text-muted-foreground",
|
|
23
|
+
VARIANT_CLASSES[variant] ?? VARIANT_CLASSES.inline,
|
|
24
|
+
className,
|
|
25
|
+
)}
|
|
26
|
+
>
|
|
27
|
+
<Loader2 className={cn("animate-spin text-primary", SIZE_CLASSES[size] ?? SIZE_CLASSES.default)} />
|
|
28
|
+
{label && <p className="text-sm">{label}</p>}
|
|
29
|
+
</div>
|
|
30
|
+
);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export default LoadingComponent;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
function NetworkStatus() {
|
|
4
|
+
const [isOnline, setIsOnline] = useState(navigator.onLine);
|
|
5
|
+
const [showChip, setShowChip] = useState(false);
|
|
6
|
+
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
const goOnline = () => {
|
|
9
|
+
setIsOnline(true);
|
|
10
|
+
setShowChip(true);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const goOffline = () => {
|
|
14
|
+
setIsOnline(false);
|
|
15
|
+
setShowChip(true);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
window.addEventListener("online", goOnline);
|
|
19
|
+
window.addEventListener("offline", goOffline);
|
|
20
|
+
|
|
21
|
+
return () => {
|
|
22
|
+
window.removeEventListener("online", goOnline);
|
|
23
|
+
window.removeEventListener("offline", goOffline);
|
|
24
|
+
};
|
|
25
|
+
}, []);
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (showChip) {
|
|
29
|
+
const timer = setTimeout(() => setShowChip(false), 3000);
|
|
30
|
+
return () => clearTimeout(timer);
|
|
31
|
+
}
|
|
32
|
+
}, [showChip, isOnline]);
|
|
33
|
+
|
|
34
|
+
if (!showChip) return null;
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div
|
|
38
|
+
role="alert"
|
|
39
|
+
aria-live="polite"
|
|
40
|
+
className={`
|
|
41
|
+
fixed bottom-5 left-5 z-[9999]
|
|
42
|
+
flex items-center gap-2
|
|
43
|
+
rounded-full
|
|
44
|
+
px-3.5 py-2
|
|
45
|
+
text-xs font-medium
|
|
46
|
+
shadow-lg
|
|
47
|
+
animate-[slideInLeft_0.3s_cubic-bezier(0.16,1,0.3,1)]
|
|
48
|
+
sm:text-sm
|
|
49
|
+
${isOnline ? "bg-green-600 text-white" : "bg-destructive text-white"}
|
|
50
|
+
`}
|
|
51
|
+
>
|
|
52
|
+
<span className="flex items-center justify-center">
|
|
53
|
+
{isOnline ? (
|
|
54
|
+
<svg
|
|
55
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
56
|
+
width="24"
|
|
57
|
+
height="24"
|
|
58
|
+
viewBox="0 0 24 24"
|
|
59
|
+
fill="none"
|
|
60
|
+
stroke="currentColor"
|
|
61
|
+
stroke-width="2"
|
|
62
|
+
stroke-linecap="round"
|
|
63
|
+
stroke-linejoin="round"
|
|
64
|
+
class="lucide lucide-wifi-sync preview-icon"
|
|
65
|
+
>
|
|
66
|
+
<path d="M11.965 10.105v4L13.5 12.5a5 5 0 0 1 8 1.5" />
|
|
67
|
+
<path d="M11.965 14.105h4" />
|
|
68
|
+
<path d="M17.965 18.105h4L20.43 19.71a5 5 0 0 1-8-1.5" />
|
|
69
|
+
<path d="M2 8.82a15 15 0 0 1 20 0" />
|
|
70
|
+
<path d="M21.965 22.105v-4" />
|
|
71
|
+
<path d="M5 12.86a10 10 0 0 1 3-2.032" />
|
|
72
|
+
<path d="M8.5 16.429h.01" />
|
|
73
|
+
</svg>
|
|
74
|
+
) : (
|
|
75
|
+
<svg
|
|
76
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
77
|
+
width="24"
|
|
78
|
+
height="24"
|
|
79
|
+
viewBox="0 0 24 24"
|
|
80
|
+
fill="none"
|
|
81
|
+
stroke="currentColor"
|
|
82
|
+
stroke-width="2"
|
|
83
|
+
stroke-linecap="round"
|
|
84
|
+
stroke-linejoin="round"
|
|
85
|
+
class="lucide lucide-wifi-off preview-icon"
|
|
86
|
+
>
|
|
87
|
+
<path d="M12 20h.01" />
|
|
88
|
+
<path d="M8.5 16.429a5 5 0 0 1 7 0" />
|
|
89
|
+
<path d="M5 12.859a10 10 0 0 1 5.17-2.69" />
|
|
90
|
+
<path d="M19 12.859a10 10 0 0 0-2.007-1.523" />
|
|
91
|
+
<path d="M2 8.82a15 15 0 0 1 4.177-2.643" />
|
|
92
|
+
<path d="M22 8.82a15 15 0 0 0-11.288-3.764" />
|
|
93
|
+
<path d="m2 2 20 20" />
|
|
94
|
+
</svg>
|
|
95
|
+
)}
|
|
96
|
+
</span>
|
|
97
|
+
|
|
98
|
+
<span>{isOnline ? "Internet connection restored" : "No internet connection"}</span>
|
|
99
|
+
</div>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export default NetworkStatus;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Inbox } from "lucide-react";
|
|
2
|
+
import { cn } from "@/lib/utils";
|
|
3
|
+
import { Button } from "@/components/ui/button";
|
|
4
|
+
import { Empty, EmptyHeader, EmptyMedia, EmptyTitle, EmptyDescription, EmptyContent } from "@/components/ui/empty";
|
|
5
|
+
|
|
6
|
+
const VARIANT_CLASSES = {
|
|
7
|
+
inline: "w-full py-10",
|
|
8
|
+
page: "w-full min-h-[70vh]",
|
|
9
|
+
overlay: "fixed inset-0 z-50 bg-background",
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const NoDataComponent = ({
|
|
13
|
+
variant = "inline",
|
|
14
|
+
icon: Icon = Inbox,
|
|
15
|
+
title = "No data found",
|
|
16
|
+
description = "There's nothing to show here yet.",
|
|
17
|
+
actionLabel,
|
|
18
|
+
onAction,
|
|
19
|
+
className,
|
|
20
|
+
}) => {
|
|
21
|
+
return (
|
|
22
|
+
<div
|
|
23
|
+
className={cn("flex items-center justify-center", VARIANT_CLASSES[variant] ?? VARIANT_CLASSES.inline, className)}
|
|
24
|
+
>
|
|
25
|
+
<Empty className="border-none p-0">
|
|
26
|
+
<EmptyHeader>
|
|
27
|
+
<EmptyMedia variant="icon">
|
|
28
|
+
<Icon />
|
|
29
|
+
</EmptyMedia>
|
|
30
|
+
<EmptyTitle>{title}</EmptyTitle>
|
|
31
|
+
{description && <EmptyDescription>{description}</EmptyDescription>}
|
|
32
|
+
</EmptyHeader>
|
|
33
|
+
{actionLabel && onAction && (
|
|
34
|
+
<EmptyContent>
|
|
35
|
+
<Button variant="outline" size="sm" onClick={onAction}>
|
|
36
|
+
{actionLabel}
|
|
37
|
+
</Button>
|
|
38
|
+
</EmptyContent>
|
|
39
|
+
)}
|
|
40
|
+
</Empty>
|
|
41
|
+
</div>
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export default NoDataComponent;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { ChevronLeft, ChevronRight } from "lucide-react";
|
|
3
|
+
import { cn } from "@/lib/utils";
|
|
4
|
+
import { Button } from "@/components/ui/button";
|
|
5
|
+
import SearchSelect from "@/components/common/SearchSelect";
|
|
6
|
+
|
|
7
|
+
const PAGE_SIZE_OPTIONS = [10, 20, 50, 100];
|
|
8
|
+
|
|
9
|
+
const getPageNumbers = (page, totalPages, siblings = 1) => {
|
|
10
|
+
const pages = [];
|
|
11
|
+
const start = Math.max(2, page - siblings);
|
|
12
|
+
const end = Math.min(totalPages - 1, page + siblings);
|
|
13
|
+
|
|
14
|
+
pages.push(1);
|
|
15
|
+
if (start > 2) pages.push("start-ellipsis");
|
|
16
|
+
for (let i = start; i <= end; i++) pages.push(i);
|
|
17
|
+
if (end < totalPages - 1) pages.push("end-ellipsis");
|
|
18
|
+
if (totalPages > 1) pages.push(totalPages);
|
|
19
|
+
|
|
20
|
+
return pages;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const PageButtons = ({ page, totalPages, onPageChange }) => (
|
|
24
|
+
<div className="flex items-center gap-1">
|
|
25
|
+
<Button variant="outline" size="icon-sm" disabled={page <= 1} onClick={() => onPageChange(page - 1)}>
|
|
26
|
+
<ChevronLeft />
|
|
27
|
+
</Button>
|
|
28
|
+
|
|
29
|
+
{getPageNumbers(page, totalPages).map((p) =>
|
|
30
|
+
typeof p === "number" ? (
|
|
31
|
+
<Button key={p} variant={p === page ? "default" : "outline"} size="icon-sm" onClick={() => onPageChange(p)}>
|
|
32
|
+
{p}
|
|
33
|
+
</Button>
|
|
34
|
+
) : (
|
|
35
|
+
<span key={p} className="px-1 text-xs text-muted-foreground">
|
|
36
|
+
…
|
|
37
|
+
</span>
|
|
38
|
+
),
|
|
39
|
+
)}
|
|
40
|
+
|
|
41
|
+
<Button variant="outline" size="icon-sm" disabled={page >= totalPages} onClick={() => onPageChange(page + 1)}>
|
|
42
|
+
<ChevronRight />
|
|
43
|
+
</Button>
|
|
44
|
+
</div>
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const Pagination = ({
|
|
48
|
+
items,
|
|
49
|
+
label = "items",
|
|
50
|
+
children,
|
|
51
|
+
showPageSize = true,
|
|
52
|
+
pageSizeOptions = PAGE_SIZE_OPTIONS,
|
|
53
|
+
page: controlledPage = 1,
|
|
54
|
+
totalPages: controlledTotalPages = 1,
|
|
55
|
+
onPageChange,
|
|
56
|
+
className,
|
|
57
|
+
}) => {
|
|
58
|
+
const [page, setPage] = useState(1);
|
|
59
|
+
const [pageSize, setPageSize] = useState(pageSizeOptions[0]);
|
|
60
|
+
const [itemCount, setItemCount] = useState(items?.length ?? 0);
|
|
61
|
+
|
|
62
|
+
if (!items) {
|
|
63
|
+
if (controlledTotalPages <= 1) return null;
|
|
64
|
+
return (
|
|
65
|
+
<div className={cn("flex items-center justify-between gap-3", className)}>
|
|
66
|
+
<p className="text-xs text-muted-foreground">
|
|
67
|
+
Page {controlledPage} of {controlledTotalPages}
|
|
68
|
+
</p>
|
|
69
|
+
<PageButtons page={controlledPage} totalPages={controlledTotalPages} onPageChange={(p) => onPageChange?.(p)} />
|
|
70
|
+
</div>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (items.length !== itemCount) {
|
|
75
|
+
setItemCount(items.length);
|
|
76
|
+
setPage(1);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const totalPages = Math.max(1, Math.ceil(items.length / pageSize));
|
|
80
|
+
const currentPage = Math.min(page, totalPages);
|
|
81
|
+
const startIndex = (currentPage - 1) * pageSize;
|
|
82
|
+
const pageRows = items.slice(startIndex, startIndex + pageSize);
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<>
|
|
86
|
+
{children?.(pageRows)}
|
|
87
|
+
|
|
88
|
+
{items.length > 0 && (
|
|
89
|
+
<div
|
|
90
|
+
className={cn(
|
|
91
|
+
"flex shrink-0 flex-col items-center justify-between gap-3 border-t border-border px-4 py-3 sm:flex-row",
|
|
92
|
+
className,
|
|
93
|
+
)}
|
|
94
|
+
>
|
|
95
|
+
<p className="text-xs text-muted-foreground">
|
|
96
|
+
Showing{" "}
|
|
97
|
+
<span className="font-medium text-foreground">
|
|
98
|
+
{startIndex + 1}–{startIndex + pageRows.length}
|
|
99
|
+
</span>{" "}
|
|
100
|
+
of <span className="font-medium text-foreground">{items.length}</span> {label}
|
|
101
|
+
</p>
|
|
102
|
+
|
|
103
|
+
<div className="flex flex-wrap items-center justify-center gap-4">
|
|
104
|
+
{showPageSize && (
|
|
105
|
+
<div className="flex items-center gap-2">
|
|
106
|
+
<span className="text-xs whitespace-nowrap text-muted-foreground">Rows per page</span>
|
|
107
|
+
<SearchSelect
|
|
108
|
+
options={pageSizeOptions.map((size) => ({ value: size, label: String(size) }))}
|
|
109
|
+
value={pageSize}
|
|
110
|
+
searchable={false}
|
|
111
|
+
onValueChange={(value) => {
|
|
112
|
+
setPageSize(Number(value) || pageSizeOptions[0]);
|
|
113
|
+
setPage(1);
|
|
114
|
+
}}
|
|
115
|
+
className="h-8 w-20 text-xs"
|
|
116
|
+
/>
|
|
117
|
+
</div>
|
|
118
|
+
)}
|
|
119
|
+
|
|
120
|
+
<PageButtons page={currentPage} totalPages={totalPages} onPageChange={setPage} />
|
|
121
|
+
</div>
|
|
122
|
+
</div>
|
|
123
|
+
)}
|
|
124
|
+
</>
|
|
125
|
+
);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export default Pagination;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { cn } from "@/lib/utils";
|
|
2
|
+
|
|
3
|
+
const VARIANT_CLASSES = {
|
|
4
|
+
default: "text-sm text-foreground",
|
|
5
|
+
muted: "text-sm text-muted-foreground",
|
|
6
|
+
small: "text-xs text-muted-foreground",
|
|
7
|
+
lead: "text-base text-foreground",
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const ParagraphComponent = ({ children, variant = "default", as: Tag = "p", className }) => {
|
|
11
|
+
return <Tag className={cn(VARIANT_CLASSES[variant] ?? VARIANT_CLASSES.default, className)}>{children}</Tag>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default ParagraphComponent;
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import "react-phone-number-input/style.css";
|
|
2
|
+
import PhoneInput from "react-phone-number-input";
|
|
3
|
+
import flags from "react-phone-number-input/flags";
|
|
4
|
+
import { forwardRef, useState, useEffect, useRef, useMemo, useCallback } from "react";
|
|
5
|
+
import { isValidPhoneNumber } from "libphonenumber-js";
|
|
6
|
+
import { getCountryCallingCode } from "libphonenumber-js";
|
|
7
|
+
import { cn } from "@/lib/utils";
|
|
8
|
+
import { ChevronDown, Check, Search, Globe } from "lucide-react";
|
|
9
|
+
|
|
10
|
+
const FlagIcon = ({ country }) => {
|
|
11
|
+
if (!country) return <Globe className="h-4 w-4 text-muted-foreground" />;
|
|
12
|
+
const Flag = flags[country];
|
|
13
|
+
if (!Flag) return <Globe className="h-4 w-4 text-muted-foreground" />;
|
|
14
|
+
return <Flag title={country} className="w-5 h-4 object-cover rounded-sm" />;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const CountrySelect = ({ value, onChange, options = [], disabled, tabIndex }) => {
|
|
18
|
+
const [open, setOpen] = useState(false);
|
|
19
|
+
const [search, setSearch] = useState("");
|
|
20
|
+
const dropdownRef = useRef(null);
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
const handleClickOutside = (e) => {
|
|
24
|
+
if (dropdownRef.current && !dropdownRef.current.contains(e.target)) {
|
|
25
|
+
setOpen(false);
|
|
26
|
+
setSearch("");
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
document.addEventListener("mousedown", handleClickOutside);
|
|
30
|
+
return () => document.removeEventListener("mousedown", handleClickOutside);
|
|
31
|
+
}, []);
|
|
32
|
+
|
|
33
|
+
const filtered = useMemo(() => {
|
|
34
|
+
const q = search.toLowerCase().trim();
|
|
35
|
+
const list = options.filter((o) => !o.divider);
|
|
36
|
+
if (!q) return list;
|
|
37
|
+
return list.filter((o) => {
|
|
38
|
+
if (o.label?.toLowerCase().includes(q)) return true;
|
|
39
|
+
if (o.value) {
|
|
40
|
+
try {
|
|
41
|
+
const code = getCountryCallingCode(o.value).toString();
|
|
42
|
+
if (code.includes(q.replace("+", ""))) return true;
|
|
43
|
+
} catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
});
|
|
49
|
+
}, [options, search]);
|
|
50
|
+
|
|
51
|
+
const handleSelect = useCallback(
|
|
52
|
+
(optValue) => {
|
|
53
|
+
onChange(optValue || undefined);
|
|
54
|
+
setSearch("");
|
|
55
|
+
setOpen(false);
|
|
56
|
+
},
|
|
57
|
+
[onChange],
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div ref={dropdownRef} className="relative h-full">
|
|
62
|
+
<button
|
|
63
|
+
type="button"
|
|
64
|
+
disabled={disabled}
|
|
65
|
+
tabIndex={tabIndex}
|
|
66
|
+
onClick={() => setOpen((o) => !o)}
|
|
67
|
+
className="h-full flex items-center gap-0.5 px-2.5 border-r border-input bg-transparent hover:bg-muted/60 transition-colors disabled:cursor-not-allowed disabled:opacity-50 focus:outline-none shrink-0"
|
|
68
|
+
>
|
|
69
|
+
<FlagIcon country={value} />
|
|
70
|
+
<ChevronDown className="h-3 w-3 opacity-50" />
|
|
71
|
+
</button>
|
|
72
|
+
|
|
73
|
+
{open && (
|
|
74
|
+
<div className="absolute top-full left-0 z-50 mt-1 w-[280px] rounded-md border border-input bg-popover text-popover-foreground shadow-md">
|
|
75
|
+
<div className="flex items-center border-b border-input px-3">
|
|
76
|
+
<Search className="h-4 w-4 shrink-0 opacity-50 mr-2" />
|
|
77
|
+
<input
|
|
78
|
+
autoFocus
|
|
79
|
+
className="flex h-10 w-full bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground"
|
|
80
|
+
placeholder="Search country..."
|
|
81
|
+
value={search}
|
|
82
|
+
onChange={(e) => setSearch(e.target.value)}
|
|
83
|
+
/>
|
|
84
|
+
</div>
|
|
85
|
+
<div className="max-h-[250px] overflow-y-auto">
|
|
86
|
+
{filtered.length === 0 ? (
|
|
87
|
+
<div className="py-6 text-center text-sm text-muted-foreground">No country found.</div>
|
|
88
|
+
) : (
|
|
89
|
+
filtered.map((opt) => {
|
|
90
|
+
let callingCode = null;
|
|
91
|
+
if (opt.value) {
|
|
92
|
+
try {
|
|
93
|
+
callingCode = getCountryCallingCode(opt.value);
|
|
94
|
+
} catch {
|
|
95
|
+
callingCode = null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return (
|
|
99
|
+
<div
|
|
100
|
+
key={opt.value ?? "__intl__"}
|
|
101
|
+
onClick={() => handleSelect(opt.value)}
|
|
102
|
+
className="flex items-center gap-2 px-3 py-2 cursor-pointer hover:bg-accent hover:text-accent-foreground"
|
|
103
|
+
>
|
|
104
|
+
<span className="w-6 flex items-center justify-center">
|
|
105
|
+
<FlagIcon country={opt.value} />
|
|
106
|
+
</span>
|
|
107
|
+
<span className="flex-1 truncate text-sm">{opt.label}</span>
|
|
108
|
+
{callingCode && <span className="text-xs text-muted-foreground">+{callingCode}</span>}
|
|
109
|
+
{opt.value === value && <Check className="h-3.5 w-3.5 text-primary ml-0.5 shrink-0" />}
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
})
|
|
113
|
+
)}
|
|
114
|
+
</div>
|
|
115
|
+
</div>
|
|
116
|
+
)}
|
|
117
|
+
</div>
|
|
118
|
+
);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const CustomPhoneInput = forwardRef(({ className, ...props }, ref) => (
|
|
122
|
+
<input
|
|
123
|
+
ref={ref}
|
|
124
|
+
{...props}
|
|
125
|
+
className={cn(
|
|
126
|
+
"flex-1 h-full bg-transparent px-3 py-1 text-sm outline-none min-w-0",
|
|
127
|
+
"placeholder:text-muted-foreground",
|
|
128
|
+
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
129
|
+
className,
|
|
130
|
+
)}
|
|
131
|
+
/>
|
|
132
|
+
));
|
|
133
|
+
CustomPhoneInput.displayName = "CustomPhoneInput";
|
|
134
|
+
|
|
135
|
+
const PhoneInputComponent = forwardRef((props, ref) => {
|
|
136
|
+
const {
|
|
137
|
+
value = "",
|
|
138
|
+
onChange,
|
|
139
|
+
label,
|
|
140
|
+
placeholder = "Enter phone number",
|
|
141
|
+
defaultCountry = "PK",
|
|
142
|
+
className,
|
|
143
|
+
inputClassName = "h-9",
|
|
144
|
+
error,
|
|
145
|
+
required,
|
|
146
|
+
onValidationChange,
|
|
147
|
+
...rest
|
|
148
|
+
} = props;
|
|
149
|
+
|
|
150
|
+
const [internalError, setInternalError] = useState("");
|
|
151
|
+
const containerRef = useRef(null);
|
|
152
|
+
const isValidRef = useRef(false);
|
|
153
|
+
|
|
154
|
+
const isValid = useMemo(() => {
|
|
155
|
+
if (!value) return false;
|
|
156
|
+
try {
|
|
157
|
+
return isValidPhoneNumber(value);
|
|
158
|
+
} catch {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
}, [value]);
|
|
162
|
+
|
|
163
|
+
useEffect(() => {
|
|
164
|
+
isValidRef.current = isValid;
|
|
165
|
+
}, [isValid]);
|
|
166
|
+
|
|
167
|
+
useEffect(() => {
|
|
168
|
+
const container = containerRef.current;
|
|
169
|
+
if (!container) return;
|
|
170
|
+
const input = container.querySelector("input[type='tel']") || container.querySelector("input");
|
|
171
|
+
if (!input) return;
|
|
172
|
+
const handleKeyDown = (e) => {
|
|
173
|
+
if (isValidRef.current && /^\d$/.test(e.key)) e.preventDefault();
|
|
174
|
+
};
|
|
175
|
+
input.addEventListener("keydown", handleKeyDown);
|
|
176
|
+
return () => input.removeEventListener("keydown", handleKeyDown);
|
|
177
|
+
}, []);
|
|
178
|
+
|
|
179
|
+
const handlePhoneChange = useCallback(
|
|
180
|
+
(phoneValue) => {
|
|
181
|
+
onChange?.(phoneValue ?? "");
|
|
182
|
+
if (!phoneValue) {
|
|
183
|
+
setInternalError("");
|
|
184
|
+
onValidationChange?.(true, phoneValue);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
const valid = isValidPhoneNumber(phoneValue);
|
|
189
|
+
setInternalError(valid ? "" : "Please enter a valid phone number");
|
|
190
|
+
onValidationChange?.(valid, phoneValue);
|
|
191
|
+
} catch {
|
|
192
|
+
setInternalError("Please enter a valid phone number");
|
|
193
|
+
onValidationChange?.(false, phoneValue);
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
[onChange, onValidationChange],
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
const displayError = error || internalError;
|
|
200
|
+
|
|
201
|
+
return (
|
|
202
|
+
<div ref={containerRef} className={cn("w-full", className)}>
|
|
203
|
+
{label && (
|
|
204
|
+
<label className="mb-1.5 block text-xs sm:text-sm font-medium leading-none">
|
|
205
|
+
{label}
|
|
206
|
+
{required && <span className="text-destructive"> *</span>}
|
|
207
|
+
</label>
|
|
208
|
+
)}
|
|
209
|
+
|
|
210
|
+
<div
|
|
211
|
+
className={cn(
|
|
212
|
+
"flex rounded-md border border-input bg-transparent shadow-xs transition-[color,box-shadow] dark:bg-input/30",
|
|
213
|
+
inputClassName,
|
|
214
|
+
"focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50",
|
|
215
|
+
displayError && "border-destructive focus-within:ring-destructive/20 dark:focus-within:ring-destructive/40",
|
|
216
|
+
)}
|
|
217
|
+
>
|
|
218
|
+
<PhoneInput
|
|
219
|
+
ref={ref}
|
|
220
|
+
international
|
|
221
|
+
defaultCountry={defaultCountry}
|
|
222
|
+
value={value}
|
|
223
|
+
onChange={handlePhoneChange}
|
|
224
|
+
placeholder={placeholder}
|
|
225
|
+
limitMaxLength
|
|
226
|
+
inputComponent={CustomPhoneInput}
|
|
227
|
+
countrySelectComponent={CountrySelect}
|
|
228
|
+
className="flex w-full h-full [&_.PhoneInputCountry]:m-0 [&_.PhoneInputCountry]:p-0 [&_.PhoneInputCountry]:h-full [&_.PhoneInputCountry]:flex [&_.PhoneInputCountry]:items-stretch"
|
|
229
|
+
{...rest}
|
|
230
|
+
/>
|
|
231
|
+
</div>
|
|
232
|
+
|
|
233
|
+
{displayError && <p className="mt-1.5 text-xs text-destructive">{displayError}</p>}
|
|
234
|
+
|
|
235
|
+
{!displayError && isValid && <p className="mt-1.5 text-xs text-green-600">✓ Valid phone number</p>}
|
|
236
|
+
</div>
|
|
237
|
+
);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
PhoneInputComponent.displayName = "PhoneInputComponent";
|
|
241
|
+
|
|
242
|
+
export default PhoneInputComponent;
|