@brightlocal/ui-components 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/LICENSE +21 -0
- package/README.md +349 -0
- package/dist/accordion.cjs +1 -0
- package/dist/accordion.d.ts +32 -0
- package/dist/accordion.d.ts.map +1 -0
- package/dist/accordion.js +107 -0
- package/dist/alert-dialog.cjs +1 -0
- package/dist/alert-dialog.d.ts +15 -0
- package/dist/alert-dialog.d.ts.map +1 -0
- package/dist/alert-dialog.js +146 -0
- package/dist/alert.cjs +1 -0
- package/dist/alert.d.ts +16 -0
- package/dist/alert.d.ts.map +1 -0
- package/dist/alert.js +174 -0
- package/dist/aspect-ratio.cjs +1 -0
- package/dist/aspect-ratio.d.ts +10 -0
- package/dist/aspect-ratio.d.ts.map +1 -0
- package/dist/aspect-ratio.js +22 -0
- package/dist/avatar.cjs +1 -0
- package/dist/avatar.d.ts +11 -0
- package/dist/avatar.d.ts.map +1 -0
- package/dist/avatar.js +46 -0
- package/dist/badge.cjs +1 -0
- package/dist/badge.d.ts +19 -0
- package/dist/badge.d.ts.map +1 -0
- package/dist/badge.js +50 -0
- package/dist/blocks/data-table.cjs +1 -0
- package/dist/blocks/data-table.d.ts +303 -0
- package/dist/blocks/data-table.d.ts.map +1 -0
- package/dist/blocks/data-table.js +555 -0
- package/dist/blocks/sidebar.cjs +1 -0
- package/dist/blocks/sidebar.d.ts +744 -0
- package/dist/blocks/sidebar.d.ts.map +1 -0
- package/dist/blocks/sidebar.js +839 -0
- package/dist/breadcrumb.cjs +1 -0
- package/dist/breadcrumb.d.ts +12 -0
- package/dist/breadcrumb.d.ts.map +1 -0
- package/dist/breadcrumb.js +102 -0
- package/dist/button.cjs +1 -0
- package/dist/button.d.ts +73 -0
- package/dist/button.d.ts.map +1 -0
- package/dist/button.js +195 -0
- package/dist/calendar.cjs +1 -0
- package/dist/calendar.d.ts +28 -0
- package/dist/calendar.d.ts.map +1 -0
- package/dist/calendar.js +191 -0
- package/dist/card.cjs +1 -0
- package/dist/card.d.ts +28 -0
- package/dist/card.d.ts.map +1 -0
- package/dist/card.js +131 -0
- package/dist/carousel.cjs +1 -0
- package/dist/carousel.d.ts +21 -0
- package/dist/carousel.d.ts.map +1 -0
- package/dist/carousel.js +185 -0
- package/dist/centred-layout.cjs +1 -0
- package/dist/centred-layout.d.ts +49 -0
- package/dist/centred-layout.d.ts.map +1 -0
- package/dist/centred-layout.js +35 -0
- package/dist/chart.cjs +10 -0
- package/dist/chart.d.ts +83 -0
- package/dist/chart.d.ts.map +1 -0
- package/dist/chart.js +254 -0
- package/dist/checkbox.cjs +1 -0
- package/dist/checkbox.d.ts +15 -0
- package/dist/checkbox.d.ts.map +1 -0
- package/dist/checkbox.js +96 -0
- package/dist/collapsible.cjs +1 -0
- package/dist/collapsible.d.ts +18 -0
- package/dist/collapsible.d.ts.map +1 -0
- package/dist/collapsible.js +58 -0
- package/dist/combobox.cjs +1 -0
- package/dist/combobox.d.ts +65 -0
- package/dist/combobox.d.ts.map +1 -0
- package/dist/combobox.js +203 -0
- package/dist/command.cjs +1 -0
- package/dist/command.d.ts +44 -0
- package/dist/command.d.ts.map +1 -0
- package/dist/command.js +166 -0
- package/dist/context-menu.cjs +1 -0
- package/dist/context-menu.d.ts +42 -0
- package/dist/context-menu.d.ts.map +1 -0
- package/dist/context-menu.js +226 -0
- package/dist/date-picker.cjs +1 -0
- package/dist/date-picker.d.ts +54 -0
- package/dist/date-picker.d.ts.map +1 -0
- package/dist/date-picker.js +95 -0
- package/dist/dialog.cjs +1 -0
- package/dist/dialog.d.ts +20 -0
- package/dist/dialog.d.ts.map +1 -0
- package/dist/dialog.js +130 -0
- package/dist/drawer.cjs +1 -0
- package/dist/drawer.d.ts +22 -0
- package/dist/drawer.d.ts.map +1 -0
- package/dist/drawer.js +149 -0
- package/dist/dropdown-menu.cjs +1 -0
- package/dist/dropdown-menu.d.ts +253 -0
- package/dist/dropdown-menu.d.ts.map +1 -0
- package/dist/dropdown-menu.js +325 -0
- package/dist/field.cjs +1 -0
- package/dist/field.d.ts +21 -0
- package/dist/field.d.ts.map +1 -0
- package/dist/field.js +148 -0
- package/dist/global-layout.cjs +1 -0
- package/dist/global-layout.d.ts +60 -0
- package/dist/global-layout.d.ts.map +1 -0
- package/dist/global-layout.js +87 -0
- package/dist/hooks/use-mobile.cjs +1 -0
- package/dist/hooks/use-mobile.d.ts +2 -0
- package/dist/hooks/use-mobile.d.ts.map +1 -0
- package/dist/hooks/use-mobile.js +16 -0
- package/dist/hover-card.cjs +1 -0
- package/dist/hover-card.d.ts +43 -0
- package/dist/hover-card.d.ts.map +1 -0
- package/dist/hover-card.js +71 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +412 -0
- package/dist/input-base.cjs +1 -0
- package/dist/input-base.d.ts +28 -0
- package/dist/input-base.d.ts.map +1 -0
- package/dist/input-base.js +56 -0
- package/dist/input-group.cjs +1 -0
- package/dist/input-group.d.ts +26 -0
- package/dist/input-group.d.ts.map +1 -0
- package/dist/input-group.js +116 -0
- package/dist/input-otp.cjs +1 -0
- package/dist/input-otp.d.ts +45 -0
- package/dist/input-otp.d.ts.map +1 -0
- package/dist/input-otp.js +186 -0
- package/dist/input-password.cjs +1 -0
- package/dist/input-password.d.ts +38 -0
- package/dist/input-password.d.ts.map +1 -0
- package/dist/input-password.js +101 -0
- package/dist/input.cjs +1 -0
- package/dist/input.d.ts +32 -0
- package/dist/input.d.ts.map +1 -0
- package/dist/input.js +146 -0
- package/dist/label.cjs +1 -0
- package/dist/label.d.ts +12 -0
- package/dist/label.d.ts.map +1 -0
- package/dist/label.js +34 -0
- package/dist/lib/utils.cjs +1 -0
- package/dist/lib/utils.d.ts +3 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/utils.js +8 -0
- package/dist/link.cjs +1 -0
- package/dist/link.d.ts +29 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.figma.d.ts +2 -0
- package/dist/link.figma.d.ts.map +1 -0
- package/dist/link.js +71 -0
- package/dist/logo.cjs +1 -0
- package/dist/logo.d.ts +25 -0
- package/dist/logo.d.ts.map +1 -0
- package/dist/logo.js +71 -0
- package/dist/menubar.cjs +1 -0
- package/dist/menubar.d.ts +106 -0
- package/dist/menubar.d.ts.map +1 -0
- package/dist/menubar.js +270 -0
- package/dist/navigation-menu.cjs +1 -0
- package/dist/navigation-menu.d.ts +108 -0
- package/dist/navigation-menu.d.ts.map +1 -0
- package/dist/navigation-menu.js +175 -0
- package/dist/pagination.cjs +1 -0
- package/dist/pagination.d.ts +81 -0
- package/dist/pagination.d.ts.map +1 -0
- package/dist/pagination.js +216 -0
- package/dist/popover.cjs +1 -0
- package/dist/popover.d.ts +8 -0
- package/dist/popover.d.ts.map +1 -0
- package/dist/popover.js +44 -0
- package/dist/progress.cjs +1 -0
- package/dist/progress.d.ts +29 -0
- package/dist/progress.d.ts.map +1 -0
- package/dist/progress.js +64 -0
- package/dist/radio-group.cjs +1 -0
- package/dist/radio-group.d.ts +33 -0
- package/dist/radio-group.d.ts.map +1 -0
- package/dist/radio-group.js +193 -0
- package/dist/rating.cjs +1 -0
- package/dist/rating.d.ts +22 -0
- package/dist/rating.d.ts.map +1 -0
- package/dist/rating.js +51 -0
- package/dist/resizable.cjs +1 -0
- package/dist/resizable.d.ts +14 -0
- package/dist/resizable.d.ts.map +1 -0
- package/dist/resizable.js +46 -0
- package/dist/scroll-area.cjs +1 -0
- package/dist/scroll-area.d.ts +12 -0
- package/dist/scroll-area.d.ts.map +1 -0
- package/dist/scroll-area.js +67 -0
- package/dist/select.cjs +1 -0
- package/dist/select.d.ts +77 -0
- package/dist/select.d.ts.map +1 -0
- package/dist/select.js +259 -0
- package/dist/separator.cjs +1 -0
- package/dist/separator.d.ts +14 -0
- package/dist/separator.d.ts.map +1 -0
- package/dist/separator.js +43 -0
- package/dist/sheet.cjs +1 -0
- package/dist/sheet.d.ts +38 -0
- package/dist/sheet.d.ts.map +1 -0
- package/dist/sheet.js +190 -0
- package/dist/skeleton.cjs +1 -0
- package/dist/skeleton.d.ts +5 -0
- package/dist/skeleton.d.ts.map +1 -0
- package/dist/skeleton.js +15 -0
- package/dist/slider.cjs +1 -0
- package/dist/slider.d.ts +19 -0
- package/dist/slider.d.ts.map +1 -0
- package/dist/slider.js +69 -0
- package/dist/sonner.cjs +1 -0
- package/dist/sonner.d.ts +188 -0
- package/dist/sonner.d.ts.map +1 -0
- package/dist/sonner.js +150 -0
- package/dist/split-layout.cjs +1 -0
- package/dist/split-layout.d.ts +42 -0
- package/dist/split-layout.d.ts.map +1 -0
- package/dist/split-layout.js +108 -0
- package/dist/switch.cjs +1 -0
- package/dist/switch.d.ts +43 -0
- package/dist/switch.d.ts.map +1 -0
- package/dist/switch.figma.d.ts +2 -0
- package/dist/switch.figma.d.ts.map +1 -0
- package/dist/switch.js +110 -0
- package/dist/table.cjs +1 -0
- package/dist/table.d.ts +95 -0
- package/dist/table.d.ts.map +1 -0
- package/dist/table.js +164 -0
- package/dist/tabs.cjs +1 -0
- package/dist/tabs.d.ts +29 -0
- package/dist/tabs.d.ts.map +1 -0
- package/dist/tabs.js +96 -0
- package/dist/textarea.cjs +1 -0
- package/dist/textarea.d.ts +23 -0
- package/dist/textarea.d.ts.map +1 -0
- package/dist/textarea.js +47 -0
- package/dist/toggle-group.cjs +1 -0
- package/dist/toggle-group.d.ts +25 -0
- package/dist/toggle-group.d.ts.map +1 -0
- package/dist/toggle-group.js +74 -0
- package/dist/toggle.cjs +1 -0
- package/dist/toggle.d.ts +44 -0
- package/dist/toggle.d.ts.map +1 -0
- package/dist/toggle.figma.d.ts +2 -0
- package/dist/toggle.figma.d.ts.map +1 -0
- package/dist/toggle.js +57 -0
- package/dist/tooltip.cjs +1 -0
- package/dist/tooltip.d.ts +66 -0
- package/dist/tooltip.d.ts.map +1 -0
- package/dist/tooltip.js +78 -0
- package/package.json +184 -0
- package/src/blocks/data-table.tsx +1166 -0
- package/src/blocks/sidebar.tsx +1830 -0
- package/src/components/.gitkeep +0 -0
- package/src/components/accordion.tsx +142 -0
- package/src/components/alert-dialog.tsx +157 -0
- package/src/components/alert.tsx +199 -0
- package/src/components/aspect-ratio.tsx +30 -0
- package/src/components/avatar.tsx +56 -0
- package/src/components/badge.tsx +72 -0
- package/src/components/breadcrumb.tsx +109 -0
- package/src/components/button.tsx +296 -0
- package/src/components/calendar.tsx +234 -0
- package/src/components/card.tsx +158 -0
- package/src/components/carousel.tsx +256 -0
- package/src/components/centred-layout.tsx +91 -0
- package/src/components/chart.tsx +456 -0
- package/src/components/checkbox.tsx +132 -0
- package/src/components/collapsible.tsx +68 -0
- package/src/components/combobox.tsx +321 -0
- package/src/components/command.tsx +220 -0
- package/src/components/context-menu.tsx +305 -0
- package/src/components/date-picker.tsx +155 -0
- package/src/components/dialog.tsx +152 -0
- package/src/components/drawer.tsx +168 -0
- package/src/components/dropdown-menu.tsx +575 -0
- package/src/components/field.tsx +155 -0
- package/src/components/global-layout.tsx +153 -0
- package/src/components/hover-card.tsx +121 -0
- package/src/components/input-base.tsx +86 -0
- package/src/components/input-group.tsx +158 -0
- package/src/components/input-otp.tsx +327 -0
- package/src/components/input-password.tsx +191 -0
- package/src/components/input.tsx +220 -0
- package/src/components/label.tsx +42 -0
- package/src/components/link.figma.tsx +71 -0
- package/src/components/link.tsx +105 -0
- package/src/components/logo.tsx +97 -0
- package/src/components/menubar.tsx +379 -0
- package/src/components/navigation-menu.tsx +324 -0
- package/src/components/pagination.tsx +368 -0
- package/src/components/popover.tsx +46 -0
- package/src/components/progress.tsx +100 -0
- package/src/components/radio-group.tsx +281 -0
- package/src/components/rating.tsx +87 -0
- package/src/components/resizable.tsx +66 -0
- package/src/components/scroll-area.tsx +72 -0
- package/src/components/select.tsx +442 -0
- package/src/components/separator.tsx +53 -0
- package/src/components/sheet.tsx +220 -0
- package/src/components/skeleton.tsx +17 -0
- package/src/components/slider.tsx +86 -0
- package/src/components/sonner.tsx +396 -0
- package/src/components/split-layout.tsx +155 -0
- package/src/components/switch.figma.tsx +115 -0
- package/src/components/switch.tsx +173 -0
- package/src/components/table.tsx +277 -0
- package/src/components/tabs.tsx +142 -0
- package/src/components/textarea.tsx +77 -0
- package/src/components/toggle-group.tsx +109 -0
- package/src/components/toggle.figma.tsx +81 -0
- package/src/components/toggle.tsx +103 -0
- package/src/components/tooltip.tsx +156 -0
- package/src/hooks/.gitkeep +0 -0
- package/src/hooks/use-mobile.ts +21 -0
- package/src/index.ts +67 -0
- package/src/lib/utils.ts +6 -0
- package/src/types/external-modules.d.ts +16 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import {
|
|
5
|
+
OTPInput,
|
|
6
|
+
OTPInputContext,
|
|
7
|
+
type RenderProps,
|
|
8
|
+
REGEXP_ONLY_DIGITS,
|
|
9
|
+
REGEXP_ONLY_CHARS,
|
|
10
|
+
REGEXP_ONLY_DIGITS_AND_CHARS,
|
|
11
|
+
} from "input-otp";
|
|
12
|
+
import { MinusIcon } from "lucide-react";
|
|
13
|
+
|
|
14
|
+
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
15
|
+
import { Label } from "@brightlocal/ui-components/label";
|
|
16
|
+
|
|
17
|
+
type InputOTPProps = React.ComponentProps<typeof OTPInput> & {
|
|
18
|
+
containerClassName?: string;
|
|
19
|
+
trackingEl?: string;
|
|
20
|
+
dataHook: string;
|
|
21
|
+
trackingLabel?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const InputOTP = React.forwardRef<HTMLInputElement, InputOTPProps>(
|
|
25
|
+
(
|
|
26
|
+
{ trackingEl, trackingLabel, dataHook, containerClassName, ...props },
|
|
27
|
+
ref
|
|
28
|
+
) => {
|
|
29
|
+
// Ensure we always have an accessible label if none is provided
|
|
30
|
+
const hasAccessibleLabel = props.id || props["aria-label"];
|
|
31
|
+
const effectiveAriaLabel = !hasAccessibleLabel
|
|
32
|
+
? "One-time password"
|
|
33
|
+
: props["aria-label"];
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<OTPInput
|
|
37
|
+
ref={ref}
|
|
38
|
+
aria-label={effectiveAriaLabel}
|
|
39
|
+
className="disabled:cursor-not-allowed"
|
|
40
|
+
containerClassName={cn(
|
|
41
|
+
"group flex items-center gap-2 has-disabled:opacity-50 w-fit",
|
|
42
|
+
containerClassName
|
|
43
|
+
)}
|
|
44
|
+
data-hook={dataHook}
|
|
45
|
+
data-slot="input-otp"
|
|
46
|
+
data-tracking-el={trackingEl}
|
|
47
|
+
data-tracking-label={trackingLabel}
|
|
48
|
+
{...props}
|
|
49
|
+
/>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
InputOTP.displayName = "InputOTP";
|
|
55
|
+
|
|
56
|
+
type InputOTPGroupProps = React.ComponentProps<"div">;
|
|
57
|
+
|
|
58
|
+
function InputOTPGroup({ className, ...props }: InputOTPGroupProps) {
|
|
59
|
+
return (
|
|
60
|
+
<div
|
|
61
|
+
className={cn("flex items-center rounded-md shadow-xs", className)}
|
|
62
|
+
data-slot="input-otp-group"
|
|
63
|
+
{...props}
|
|
64
|
+
/>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type InputOTPSlotProps = React.ComponentProps<"div"> & {
|
|
69
|
+
index: number;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
function InputOTPSlot({ index, className, ...props }: InputOTPSlotProps) {
|
|
73
|
+
const inputOTPContext = React.useContext(
|
|
74
|
+
OTPInputContext
|
|
75
|
+
) as RenderProps | null;
|
|
76
|
+
const { char, hasFakeCaret, isActive } = inputOTPContext?.slots[index] ?? {};
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<div
|
|
80
|
+
className={cn(
|
|
81
|
+
// Base styles
|
|
82
|
+
"relative flex h-9 w-9 items-center justify-center",
|
|
83
|
+
"border-input bg-background border-y border-r",
|
|
84
|
+
"text-foreground text-sm font-normal transition-all outline-none",
|
|
85
|
+
"first:rounded-l-md first:border-l last:rounded-r-md",
|
|
86
|
+
// Focus state
|
|
87
|
+
"data-[active=true]:border-ring data-[active=true]:z-10",
|
|
88
|
+
"data-[active=true]:shadow-default",
|
|
89
|
+
// Error state
|
|
90
|
+
"group-has-[input[aria-invalid=true]]:border-destructive",
|
|
91
|
+
"group-has-[input[aria-invalid=true]]:data-[active=true]:shadow-destructive",
|
|
92
|
+
"group-has-[input[aria-invalid=true]]:data-[active=true]:border-destructive",
|
|
93
|
+
|
|
94
|
+
// Dark mode
|
|
95
|
+
"dark:bg-input/30",
|
|
96
|
+
className
|
|
97
|
+
)}
|
|
98
|
+
data-active={isActive}
|
|
99
|
+
data-slot="input-otp-slot"
|
|
100
|
+
{...props}
|
|
101
|
+
>
|
|
102
|
+
{char}
|
|
103
|
+
{hasFakeCaret && (
|
|
104
|
+
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
|
|
105
|
+
<div className="animate-caret-blink bg-foreground h-4 w-px duration-1000" />
|
|
106
|
+
</div>
|
|
107
|
+
)}
|
|
108
|
+
</div>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function InputOTPSeparator({ ...props }: React.ComponentProps<"div">) {
|
|
113
|
+
return (
|
|
114
|
+
<div data-slot="input-otp-separator" role="separator" {...props}>
|
|
115
|
+
<MinusIcon />
|
|
116
|
+
</div>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface InputOTPFieldProps {
|
|
121
|
+
id?: string;
|
|
122
|
+
label?: string;
|
|
123
|
+
dataHook: string;
|
|
124
|
+
trackingEl?: string;
|
|
125
|
+
trackingLabel?: string;
|
|
126
|
+
children?: React.ReactNode;
|
|
127
|
+
maxLength: number;
|
|
128
|
+
containerClassName?: string;
|
|
129
|
+
inputMode?: "numeric" | "text" | "none" | "tel" | "url" | "email" | "decimal";
|
|
130
|
+
pasteTransformer?: (p: string) => string;
|
|
131
|
+
pattern?: string;
|
|
132
|
+
disabled?: boolean;
|
|
133
|
+
className?: string;
|
|
134
|
+
"aria-label"?: string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const InputOTPField = React.forwardRef<HTMLInputElement, InputOTPFieldProps>(
|
|
138
|
+
(
|
|
139
|
+
{
|
|
140
|
+
label,
|
|
141
|
+
id,
|
|
142
|
+
dataHook,
|
|
143
|
+
trackingEl,
|
|
144
|
+
trackingLabel,
|
|
145
|
+
containerClassName,
|
|
146
|
+
children,
|
|
147
|
+
"aria-label": ariaLabel,
|
|
148
|
+
...props
|
|
149
|
+
},
|
|
150
|
+
ref
|
|
151
|
+
) => {
|
|
152
|
+
const generatedId = React.useId();
|
|
153
|
+
const inputId = id || generatedId;
|
|
154
|
+
|
|
155
|
+
// Ensure we always have an accessible label
|
|
156
|
+
const effectiveAriaLabel =
|
|
157
|
+
ariaLabel || (!label ? "One-time password" : undefined);
|
|
158
|
+
|
|
159
|
+
return (
|
|
160
|
+
<div className="w-fit">
|
|
161
|
+
{label && (
|
|
162
|
+
<Label
|
|
163
|
+
className="mb-2"
|
|
164
|
+
data-hook={dataHook ? `${dataHook}-label` : undefined}
|
|
165
|
+
htmlFor={inputId}
|
|
166
|
+
>
|
|
167
|
+
{label}
|
|
168
|
+
</Label>
|
|
169
|
+
)}
|
|
170
|
+
<InputOTP
|
|
171
|
+
ref={ref}
|
|
172
|
+
aria-label={effectiveAriaLabel}
|
|
173
|
+
containerClassName={containerClassName}
|
|
174
|
+
dataHook={dataHook}
|
|
175
|
+
id={label ? inputId : undefined}
|
|
176
|
+
trackingEl={trackingEl}
|
|
177
|
+
trackingLabel={trackingLabel}
|
|
178
|
+
{...props}
|
|
179
|
+
>
|
|
180
|
+
{children}
|
|
181
|
+
</InputOTP>
|
|
182
|
+
</div>
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
InputOTPField.displayName = "InputOTPField";
|
|
188
|
+
|
|
189
|
+
interface VariantInputOTPProps extends Omit<
|
|
190
|
+
InputOTPFieldProps,
|
|
191
|
+
"children" | "maxLength"
|
|
192
|
+
> {
|
|
193
|
+
maxLength?: number;
|
|
194
|
+
dataHook: string;
|
|
195
|
+
trackingEl?: string;
|
|
196
|
+
trackingLabel?: string;
|
|
197
|
+
disabled?: boolean;
|
|
198
|
+
autoFocus?: boolean;
|
|
199
|
+
pattern?: string;
|
|
200
|
+
"aria-label"?: string;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const InputOTPDigitsOnly = React.forwardRef<
|
|
204
|
+
HTMLInputElement,
|
|
205
|
+
VariantInputOTPProps
|
|
206
|
+
>(({ maxLength = 6, pattern = REGEXP_ONLY_DIGITS, ...props }, ref) => {
|
|
207
|
+
return (
|
|
208
|
+
<InputOTPField
|
|
209
|
+
ref={ref}
|
|
210
|
+
inputMode="numeric"
|
|
211
|
+
maxLength={maxLength}
|
|
212
|
+
pasteTransformer={(p: string) =>
|
|
213
|
+
p.replace(/\D+/g, "").slice(0, maxLength)
|
|
214
|
+
}
|
|
215
|
+
pattern={pattern}
|
|
216
|
+
{...props}
|
|
217
|
+
>
|
|
218
|
+
<InputOTPGroup>
|
|
219
|
+
{Array.from({ length: maxLength }, (_, i) => (
|
|
220
|
+
<InputOTPSlot key={i} index={i} />
|
|
221
|
+
))}
|
|
222
|
+
</InputOTPGroup>
|
|
223
|
+
</InputOTPField>
|
|
224
|
+
);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
InputOTPDigitsOnly.displayName = "InputOTPDigitsOnly";
|
|
228
|
+
|
|
229
|
+
const InputOTPWithSpacing = React.forwardRef<
|
|
230
|
+
HTMLInputElement,
|
|
231
|
+
VariantInputOTPProps
|
|
232
|
+
>(({ maxLength = 4, ...props }, ref) => {
|
|
233
|
+
return (
|
|
234
|
+
<InputOTPField
|
|
235
|
+
ref={ref}
|
|
236
|
+
containerClassName="gap-2"
|
|
237
|
+
maxLength={maxLength}
|
|
238
|
+
{...props}
|
|
239
|
+
>
|
|
240
|
+
{Array.from({ length: maxLength }, (_, i) => (
|
|
241
|
+
<InputOTPSlot
|
|
242
|
+
key={i}
|
|
243
|
+
className="rounded-md border-l shadow-xs"
|
|
244
|
+
index={i}
|
|
245
|
+
/>
|
|
246
|
+
))}
|
|
247
|
+
</InputOTPField>
|
|
248
|
+
);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
InputOTPWithSpacing.displayName = "InputOTPWithSpacing";
|
|
252
|
+
|
|
253
|
+
const InputOTPSimple = React.forwardRef<HTMLInputElement, VariantInputOTPProps>(
|
|
254
|
+
({ maxLength = 6, ...props }, ref) => {
|
|
255
|
+
const firstGroupLength = Math.ceil(maxLength / 2);
|
|
256
|
+
const secondGroupLength = maxLength - firstGroupLength;
|
|
257
|
+
|
|
258
|
+
return (
|
|
259
|
+
<InputOTPField ref={ref} maxLength={maxLength} {...props}>
|
|
260
|
+
<InputOTPGroup>
|
|
261
|
+
{Array.from({ length: firstGroupLength }, (_, i) => (
|
|
262
|
+
<InputOTPSlot key={i} index={i} />
|
|
263
|
+
))}
|
|
264
|
+
</InputOTPGroup>
|
|
265
|
+
<InputOTPSeparator />
|
|
266
|
+
<InputOTPGroup>
|
|
267
|
+
{Array.from({ length: secondGroupLength }, (_, i) => (
|
|
268
|
+
<InputOTPSlot
|
|
269
|
+
key={i + firstGroupLength}
|
|
270
|
+
index={i + firstGroupLength}
|
|
271
|
+
/>
|
|
272
|
+
))}
|
|
273
|
+
</InputOTPGroup>
|
|
274
|
+
</InputOTPField>
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
InputOTPSimple.displayName = "InputOTPSimple";
|
|
280
|
+
|
|
281
|
+
const InputOTPWithSeparator = React.forwardRef<
|
|
282
|
+
HTMLInputElement,
|
|
283
|
+
VariantInputOTPProps
|
|
284
|
+
>(({ maxLength = 6, ...props }, ref) => {
|
|
285
|
+
const groupSize = 2;
|
|
286
|
+
const groups = Math.ceil(maxLength / groupSize);
|
|
287
|
+
|
|
288
|
+
return (
|
|
289
|
+
<InputOTPField ref={ref} maxLength={maxLength} {...props}>
|
|
290
|
+
{Array.from({ length: groups }, (_, groupIndex) => {
|
|
291
|
+
const startIndex = groupIndex * groupSize;
|
|
292
|
+
const endIndex = Math.min(startIndex + groupSize, maxLength);
|
|
293
|
+
const groupLength = endIndex - startIndex;
|
|
294
|
+
|
|
295
|
+
return (
|
|
296
|
+
<React.Fragment key={groupIndex}>
|
|
297
|
+
{groupIndex > 0 && <InputOTPSeparator />}
|
|
298
|
+
<InputOTPGroup>
|
|
299
|
+
{Array.from({ length: groupLength }, (_, i) => (
|
|
300
|
+
<InputOTPSlot key={startIndex + i} index={startIndex + i} />
|
|
301
|
+
))}
|
|
302
|
+
</InputOTPGroup>
|
|
303
|
+
</React.Fragment>
|
|
304
|
+
);
|
|
305
|
+
})}
|
|
306
|
+
</InputOTPField>
|
|
307
|
+
);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
InputOTPWithSeparator.displayName = "InputOTPWithSeparator";
|
|
311
|
+
|
|
312
|
+
export {
|
|
313
|
+
InputOTPDigitsOnly,
|
|
314
|
+
InputOTPWithSpacing,
|
|
315
|
+
InputOTPSimple,
|
|
316
|
+
InputOTPWithSeparator,
|
|
317
|
+
REGEXP_ONLY_DIGITS,
|
|
318
|
+
REGEXP_ONLY_CHARS,
|
|
319
|
+
REGEXP_ONLY_DIGITS_AND_CHARS,
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
export type {
|
|
323
|
+
VariantInputOTPProps,
|
|
324
|
+
InputOTPProps,
|
|
325
|
+
InputOTPGroupProps,
|
|
326
|
+
InputOTPSlotProps,
|
|
327
|
+
};
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { Eye, EyeOff } from "lucide-react";
|
|
5
|
+
|
|
6
|
+
import { Progress } from "@brightlocal/ui-components/progress";
|
|
7
|
+
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
8
|
+
import {
|
|
9
|
+
InputGroup,
|
|
10
|
+
InputGroupInput,
|
|
11
|
+
InputGroupAddon,
|
|
12
|
+
InputGroupButton,
|
|
13
|
+
} from "@brightlocal/ui-components/input-group";
|
|
14
|
+
|
|
15
|
+
type PasswordStrength = "meter" | "weak" | "ok" | "good" | "great";
|
|
16
|
+
|
|
17
|
+
interface PasswordStrengthConfig {
|
|
18
|
+
label: string;
|
|
19
|
+
value: number;
|
|
20
|
+
color: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const PASSWORD_STRENGTH: Record<PasswordStrength, PasswordStrengthConfig> = {
|
|
24
|
+
meter: { label: "Meter", value: 0, color: "bg-muted" },
|
|
25
|
+
weak: { label: "Weak", value: 25, color: "bg-red-500" },
|
|
26
|
+
ok: { label: "Ok", value: 50, color: "bg-orange-500" },
|
|
27
|
+
good: { label: "Good", value: 75, color: "bg-green-500" },
|
|
28
|
+
great: { label: "Great", value: 100, color: "bg-green-600" },
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
interface InputPasswordProps extends Omit<
|
|
32
|
+
React.ComponentProps<"input">,
|
|
33
|
+
"type"
|
|
34
|
+
> {
|
|
35
|
+
/**
|
|
36
|
+
* Show password strength indicator below the input
|
|
37
|
+
*/
|
|
38
|
+
showStrength?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Callback when password strength changes
|
|
41
|
+
*/
|
|
42
|
+
onStrengthChange?: (strength: PasswordStrength) => void;
|
|
43
|
+
/**
|
|
44
|
+
* Required data-hook attribute for testing
|
|
45
|
+
*/
|
|
46
|
+
dataHook: string;
|
|
47
|
+
/**
|
|
48
|
+
* Optional tracking element identifier
|
|
49
|
+
*/
|
|
50
|
+
trackingEl?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Optional tracking label
|
|
53
|
+
*/
|
|
54
|
+
trackingLabel?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Calculate password strength based on various criteria
|
|
59
|
+
*/
|
|
60
|
+
function calculatePasswordStrength(password: string): PasswordStrength {
|
|
61
|
+
if (!password || password.length === 0) {
|
|
62
|
+
return "meter";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let score = 0;
|
|
66
|
+
|
|
67
|
+
// Length check
|
|
68
|
+
if (password.length >= 8) score += 1;
|
|
69
|
+
if (password.length >= 12) score += 1;
|
|
70
|
+
|
|
71
|
+
// Character variety checks
|
|
72
|
+
if (/[a-z]/.test(password)) score += 1; // lowercase
|
|
73
|
+
if (/[A-Z]/.test(password)) score += 1; // uppercase
|
|
74
|
+
if (/[0-9]/.test(password)) score += 1; // numbers
|
|
75
|
+
if (/[^a-zA-Z0-9]/.test(password)) score += 1; // special characters
|
|
76
|
+
|
|
77
|
+
// Determine strength based on score
|
|
78
|
+
if (score <= 2) return "weak";
|
|
79
|
+
if (score <= 3) return "ok";
|
|
80
|
+
if (score <= 4) return "good";
|
|
81
|
+
return "great";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const InputPassword = React.forwardRef<HTMLInputElement, InputPasswordProps>(
|
|
85
|
+
(
|
|
86
|
+
{
|
|
87
|
+
className,
|
|
88
|
+
showStrength = true,
|
|
89
|
+
onStrengthChange,
|
|
90
|
+
dataHook,
|
|
91
|
+
trackingEl,
|
|
92
|
+
trackingLabel,
|
|
93
|
+
onChange,
|
|
94
|
+
value,
|
|
95
|
+
id,
|
|
96
|
+
...props
|
|
97
|
+
},
|
|
98
|
+
ref
|
|
99
|
+
) => {
|
|
100
|
+
const [showPassword, setShowPassword] = React.useState(false);
|
|
101
|
+
const [strength, setStrength] = React.useState<PasswordStrength>("meter");
|
|
102
|
+
const [internalValue, setInternalValue] = React.useState("");
|
|
103
|
+
|
|
104
|
+
const generatedId = React.useId();
|
|
105
|
+
const inputId = id || generatedId;
|
|
106
|
+
const currentValue = value !== undefined ? String(value) : internalValue;
|
|
107
|
+
|
|
108
|
+
// Calculate password strength with debounce
|
|
109
|
+
React.useEffect(() => {
|
|
110
|
+
const timeoutId = setTimeout(() => {
|
|
111
|
+
const newStrength = calculatePasswordStrength(currentValue);
|
|
112
|
+
setStrength(newStrength);
|
|
113
|
+
onStrengthChange?.(newStrength);
|
|
114
|
+
}, 180);
|
|
115
|
+
|
|
116
|
+
return () => clearTimeout(timeoutId);
|
|
117
|
+
}, [currentValue, onStrengthChange]);
|
|
118
|
+
|
|
119
|
+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
120
|
+
setInternalValue(e.target.value);
|
|
121
|
+
onChange?.(e);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const togglePasswordVisibility = () => {
|
|
125
|
+
setShowPassword((prev) => !prev);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const strengthConfig = PASSWORD_STRENGTH[strength];
|
|
129
|
+
|
|
130
|
+
return (
|
|
131
|
+
<div className="w-full">
|
|
132
|
+
<InputGroup className={className}>
|
|
133
|
+
<InputGroupInput
|
|
134
|
+
ref={ref}
|
|
135
|
+
data-hook={dataHook}
|
|
136
|
+
data-tracking-el={trackingEl}
|
|
137
|
+
data-tracking-label={trackingLabel}
|
|
138
|
+
id={inputId}
|
|
139
|
+
type={showPassword ? "text" : "password"}
|
|
140
|
+
value={value}
|
|
141
|
+
onChange={handleChange}
|
|
142
|
+
{...props}
|
|
143
|
+
/>
|
|
144
|
+
<InputGroupAddon align="inline-end">
|
|
145
|
+
<InputGroupButton
|
|
146
|
+
aria-label={showPassword ? "Hide password" : "Show password"}
|
|
147
|
+
data-hook={dataHook ? `${dataHook}-toggle` : undefined}
|
|
148
|
+
disabled={props.disabled}
|
|
149
|
+
size="icon"
|
|
150
|
+
variant="ghost"
|
|
151
|
+
onClick={togglePasswordVisibility}
|
|
152
|
+
>
|
|
153
|
+
{showPassword ? (
|
|
154
|
+
<EyeOff className="h-4 w-4" />
|
|
155
|
+
) : (
|
|
156
|
+
<Eye className="h-4 w-4" />
|
|
157
|
+
)}
|
|
158
|
+
</InputGroupButton>
|
|
159
|
+
</InputGroupAddon>
|
|
160
|
+
</InputGroup>
|
|
161
|
+
|
|
162
|
+
{showStrength && (
|
|
163
|
+
<div className="mt-1 space-y-2">
|
|
164
|
+
<div className="flex items-center gap-2">
|
|
165
|
+
<Progress
|
|
166
|
+
className={cn("h-2 flex-1")}
|
|
167
|
+
dataHook={
|
|
168
|
+
dataHook
|
|
169
|
+
? `${dataHook}-strength-progress`
|
|
170
|
+
: "password-strength-progress"
|
|
171
|
+
}
|
|
172
|
+
value={strengthConfig.value}
|
|
173
|
+
/>
|
|
174
|
+
<span
|
|
175
|
+
className="text-foreground w-10 shrink-0 text-right text-sm font-medium whitespace-nowrap"
|
|
176
|
+
data-hook={dataHook ? `${dataHook}-strength-label` : undefined}
|
|
177
|
+
>
|
|
178
|
+
{strengthConfig.label}
|
|
179
|
+
</span>
|
|
180
|
+
</div>
|
|
181
|
+
</div>
|
|
182
|
+
)}
|
|
183
|
+
</div>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
InputPassword.displayName = "InputPassword";
|
|
189
|
+
|
|
190
|
+
export { InputPassword, calculatePasswordStrength, PASSWORD_STRENGTH };
|
|
191
|
+
export type { InputPasswordProps, PasswordStrength };
|