@brightlocal/ui-components 0.1.4 → 0.3.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/README.md +2 -1
- package/dist/alert.cjs +1 -1
- package/dist/alert.d.ts +10 -1
- package/dist/alert.d.ts.map +1 -1
- package/dist/alert.js +6 -2
- package/dist/card.cjs +1 -1
- package/dist/card.d.ts +6 -2
- package/dist/card.d.ts.map +1 -1
- package/dist/card.js +89 -73
- package/dist/centred-layout.cjs +1 -1
- package/dist/centred-layout.d.ts +49 -25
- package/dist/centred-layout.d.ts.map +1 -1
- package/dist/centred-layout.js +57 -32
- package/dist/checkbox.cjs +1 -1
- package/dist/checkbox.d.ts.map +1 -1
- package/dist/checkbox.js +19 -19
- package/dist/chip.cjs +1 -0
- package/dist/chip.d.ts +61 -0
- package/dist/chip.d.ts.map +1 -0
- package/dist/chip.js +165 -0
- package/dist/combobox.cjs +1 -1
- package/dist/combobox.d.ts +75 -59
- package/dist/combobox.d.ts.map +1 -1
- package/dist/combobox.js +244 -191
- package/dist/date-picker.cjs +1 -1
- package/dist/date-picker.d.ts +89 -52
- package/dist/date-picker.d.ts.map +1 -1
- package/dist/date-picker.js +228 -210
- package/dist/dropdown-menu.d.ts.map +1 -1
- package/dist/header.cjs +1 -0
- package/dist/header.d.ts +42 -0
- package/dist/header.d.ts.map +1 -0
- package/dist/header.js +22 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +428 -388
- package/dist/input-chip.cjs +1 -0
- package/dist/input-chip.d.ts +117 -0
- package/dist/input-chip.d.ts.map +1 -0
- package/dist/input-chip.js +284 -0
- package/dist/input-group.cjs +1 -1
- package/dist/input-group.d.ts +5 -0
- package/dist/input-group.d.ts.map +1 -1
- package/dist/input-group.js +47 -39
- package/dist/input-otp.cjs +1 -1
- package/dist/input-otp.d.ts +45 -32
- package/dist/input-otp.d.ts.map +1 -1
- package/dist/input-otp.js +138 -114
- package/dist/input-password.cjs +1 -1
- package/dist/input-password.d.ts +151 -19
- package/dist/input-password.d.ts.map +1 -1
- package/dist/input-password.js +197 -91
- package/dist/input.cjs +1 -1
- package/dist/input.d.ts +4 -0
- package/dist/input.d.ts.map +1 -1
- package/dist/input.js +26 -24
- package/dist/logo.cjs +1 -1
- package/dist/logo.js +13 -13
- package/dist/progress.cjs +1 -1
- package/dist/progress.d.ts +10 -0
- package/dist/progress.d.ts.map +1 -1
- package/dist/progress.js +45 -36
- package/dist/radio-group.cjs +1 -1
- package/dist/radio-group.d.ts +4 -0
- package/dist/radio-group.d.ts.map +1 -1
- package/dist/radio-group.js +49 -47
- package/dist/select.cjs +1 -1
- package/dist/select.d.ts +3 -1
- package/dist/select.d.ts.map +1 -1
- package/dist/select.js +57 -56
- package/dist/slider.cjs +1 -1
- package/dist/slider.d.ts +31 -3
- package/dist/slider.d.ts.map +1 -1
- package/dist/slider.js +53 -43
- package/dist/textarea.cjs +1 -1
- package/dist/textarea.d.ts +4 -0
- package/dist/textarea.d.ts.map +1 -1
- package/dist/textarea.js +19 -18
- package/dist/toggle.cjs +1 -1
- package/dist/toggle.d.ts.map +1 -1
- package/dist/toggle.js +17 -17
- package/package.json +1 -1
- package/src/components/alert.tsx +10 -1
- package/src/components/card.tsx +66 -38
- package/src/components/centred-layout.tsx +115 -38
- package/src/components/checkbox.tsx +2 -3
- package/src/components/chip.tsx +249 -0
- package/src/components/combobox.tsx +490 -298
- package/src/components/date-picker.tsx +410 -266
- package/src/components/dropdown-menu.tsx +6 -1
- package/src/components/header.tsx +69 -0
- package/src/components/input-chip.tsx +548 -0
- package/src/components/input-group.tsx +17 -2
- package/src/components/input-otp.tsx +161 -78
- package/src/components/input-password.tsx +473 -128
- package/src/components/input.tsx +11 -3
- package/src/components/logo.tsx +1 -1
- package/src/components/progress.tsx +21 -2
- package/src/components/radio-group.tsx +11 -0
- package/src/components/select.tsx +5 -2
- package/src/components/slider.tsx +85 -15
- package/src/components/textarea.tsx +15 -5
- package/src/components/toggle.tsx +13 -15
- package/src/index.ts +3 -0
|
@@ -12,179 +12,524 @@ import {
|
|
|
12
12
|
InputGroupButton,
|
|
13
13
|
} from "@brightlocal/ui-components/input-group";
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
// ============================================================================
|
|
16
|
+
// Types
|
|
17
|
+
// ============================================================================
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Default password strength levels (industry standard)
|
|
21
|
+
*/
|
|
22
|
+
type DefaultPasswordStrength = "meter" | "weak" | "ok" | "good" | "great";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Configuration for a single strength level
|
|
26
|
+
*/
|
|
27
|
+
interface StrengthLevelConfig {
|
|
18
28
|
label: string;
|
|
19
29
|
value: number;
|
|
20
30
|
color: string;
|
|
21
31
|
}
|
|
22
32
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Default strength levels configuration
|
|
35
|
+
*/
|
|
36
|
+
const DEFAULT_STRENGTH_LEVELS: Record<
|
|
37
|
+
DefaultPasswordStrength,
|
|
38
|
+
StrengthLevelConfig
|
|
39
|
+
> = {
|
|
40
|
+
meter: { label: "Meter", value: 0, color: "bg-green-500" },
|
|
41
|
+
weak: { label: "Weak", value: 25, color: "bg-green-500" },
|
|
42
|
+
ok: { label: "Ok", value: 50, color: "bg-green-500" },
|
|
27
43
|
good: { label: "Good", value: 75, color: "bg-green-500" },
|
|
28
|
-
great: { label: "Great", value: 100, color: "bg-green-
|
|
44
|
+
great: { label: "Great", value: 100, color: "bg-green-500" },
|
|
29
45
|
};
|
|
30
46
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Calculate password strength based on various criteria (default implementation)
|
|
49
|
+
* - Minimum 12 characters required for "ok" or better
|
|
50
|
+
*/
|
|
51
|
+
function calculatePasswordStrength(password: string): DefaultPasswordStrength {
|
|
52
|
+
if (!password || password.length === 0) {
|
|
53
|
+
return "meter";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Minimum 12 characters required for "ok" or better
|
|
57
|
+
if (password.length < 12) {
|
|
58
|
+
return "weak";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let score = 0;
|
|
62
|
+
|
|
63
|
+
// Length bonuses
|
|
64
|
+
if (password.length >= 16) score += 1;
|
|
65
|
+
|
|
66
|
+
// Character variety checks
|
|
67
|
+
if (/[a-z]/.test(password)) score += 1; // lowercase
|
|
68
|
+
if (/[A-Z]/.test(password)) score += 1; // uppercase
|
|
69
|
+
if (/[0-9]/.test(password)) score += 1; // numbers
|
|
70
|
+
if (/[^a-zA-Z0-9]/.test(password)) score += 1; // special characters
|
|
71
|
+
|
|
72
|
+
// Determine strength based on score
|
|
73
|
+
if (score <= 1) return "ok";
|
|
74
|
+
if (score <= 3) return "good";
|
|
75
|
+
return "great";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ============================================================================
|
|
79
|
+
// Context
|
|
80
|
+
// ============================================================================
|
|
81
|
+
|
|
82
|
+
interface InputPasswordContextValue {
|
|
83
|
+
// Visibility state
|
|
84
|
+
showPassword: boolean;
|
|
85
|
+
setShowPassword: (show: boolean) => void;
|
|
86
|
+
toggleVisibility: () => void;
|
|
87
|
+
|
|
88
|
+
// Password value and strength
|
|
89
|
+
password: string;
|
|
90
|
+
setPassword: (password: string) => void;
|
|
91
|
+
strength: string;
|
|
92
|
+
strengthLevels: Record<string, StrengthLevelConfig>;
|
|
93
|
+
|
|
94
|
+
// Disabled state
|
|
95
|
+
disabled?: boolean;
|
|
96
|
+
|
|
97
|
+
// Data attributes
|
|
98
|
+
dataHook: string;
|
|
99
|
+
trackingEl?: string;
|
|
100
|
+
trackingLabel?: string;
|
|
101
|
+
|
|
102
|
+
// Input ID for label association
|
|
103
|
+
inputId: string;
|
|
104
|
+
|
|
105
|
+
// Error state
|
|
106
|
+
error?: boolean;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const InputPasswordContext =
|
|
110
|
+
React.createContext<InputPasswordContextValue | null>(null);
|
|
111
|
+
|
|
112
|
+
function useInputPasswordContext() {
|
|
113
|
+
const context = React.useContext(InputPasswordContext);
|
|
114
|
+
if (!context) {
|
|
115
|
+
throw new Error(
|
|
116
|
+
"InputPassword compound components must be used within InputPasswordRoot"
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
return context;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ============================================================================
|
|
123
|
+
// InputPasswordRoot
|
|
124
|
+
// ============================================================================
|
|
125
|
+
|
|
126
|
+
export interface InputPasswordRootProps<
|
|
127
|
+
T extends string = DefaultPasswordStrength,
|
|
34
128
|
> {
|
|
35
129
|
/**
|
|
36
|
-
*
|
|
130
|
+
* Data hook for automated testing (REQUIRED)
|
|
37
131
|
*/
|
|
38
|
-
|
|
132
|
+
dataHook: string;
|
|
39
133
|
/**
|
|
40
|
-
*
|
|
134
|
+
* Tracking element identifier for analytics
|
|
41
135
|
*/
|
|
42
|
-
|
|
136
|
+
trackingEl?: string;
|
|
43
137
|
/**
|
|
44
|
-
*
|
|
138
|
+
* Tracking label for analytics context
|
|
45
139
|
*/
|
|
46
|
-
|
|
140
|
+
trackingLabel?: string;
|
|
47
141
|
/**
|
|
48
|
-
*
|
|
142
|
+
* Disabled state
|
|
49
143
|
*/
|
|
50
|
-
|
|
144
|
+
disabled?: boolean;
|
|
51
145
|
/**
|
|
52
|
-
*
|
|
146
|
+
* Controlled password value
|
|
53
147
|
*/
|
|
54
|
-
|
|
148
|
+
value?: string;
|
|
149
|
+
/**
|
|
150
|
+
* Callback fired when password changes
|
|
151
|
+
*/
|
|
152
|
+
onChange?: (value: string) => void;
|
|
153
|
+
/**
|
|
154
|
+
* Callback fired when password strength changes
|
|
155
|
+
*/
|
|
156
|
+
onStrengthChange?: (strength: T) => void;
|
|
157
|
+
/**
|
|
158
|
+
* Custom strength levels configuration.
|
|
159
|
+
* Define your own levels with custom keys, labels, values, and colors.
|
|
160
|
+
* @example
|
|
161
|
+
* strengthLevels={{
|
|
162
|
+
* empty: { label: "Enter password", value: 0, color: "bg-muted" },
|
|
163
|
+
* slow: { label: "Slow", value: 33, color: "bg-red-500" },
|
|
164
|
+
* medium: { label: "Medium", value: 66, color: "bg-yellow-500" },
|
|
165
|
+
* fast: { label: "Fast", value: 100, color: "bg-green-500" },
|
|
166
|
+
* }}
|
|
167
|
+
*/
|
|
168
|
+
strengthLevels?: Record<T, StrengthLevelConfig>;
|
|
169
|
+
/**
|
|
170
|
+
* Custom function to calculate password strength.
|
|
171
|
+
* Must return a key from strengthLevels (or default levels if not provided).
|
|
172
|
+
* @default calculatePasswordStrength (built-in)
|
|
173
|
+
*/
|
|
174
|
+
calculateStrength?: (password: string) => T;
|
|
175
|
+
/**
|
|
176
|
+
* Whether the input has an error state
|
|
177
|
+
*/
|
|
178
|
+
error?: boolean;
|
|
179
|
+
/**
|
|
180
|
+
* ID for the input element (for label association)
|
|
181
|
+
*/
|
|
182
|
+
id?: string;
|
|
183
|
+
/**
|
|
184
|
+
* Children components
|
|
185
|
+
*/
|
|
186
|
+
children: React.ReactNode;
|
|
187
|
+
/**
|
|
188
|
+
* Additional class name for the root container
|
|
189
|
+
*/
|
|
190
|
+
className?: string;
|
|
55
191
|
}
|
|
56
192
|
|
|
57
193
|
/**
|
|
58
|
-
*
|
|
194
|
+
* InputPasswordRoot - Root component that provides context for all InputPassword parts
|
|
195
|
+
*
|
|
196
|
+
* Generic type parameter T allows custom strength levels:
|
|
197
|
+
* @example
|
|
198
|
+
* // Using default levels
|
|
199
|
+
* <InputPasswordRoot dataHook="password">...</InputPasswordRoot>
|
|
200
|
+
*
|
|
201
|
+
* // Using custom levels
|
|
202
|
+
* type MyStrength = "empty" | "slow" | "medium" | "fast";
|
|
203
|
+
* <InputPasswordRoot<MyStrength>
|
|
204
|
+
* dataHook="password"
|
|
205
|
+
* strengthLevels={{
|
|
206
|
+
* empty: { label: "Enter password", value: 0, color: "bg-muted" },
|
|
207
|
+
* slow: { label: "Slow", value: 33, color: "bg-red-500" },
|
|
208
|
+
* medium: { label: "Medium", value: 66, color: "bg-yellow-500" },
|
|
209
|
+
* fast: { label: "Fast", value: 100, color: "bg-green-500" },
|
|
210
|
+
* }}
|
|
211
|
+
* calculateStrength={(password) => {
|
|
212
|
+
* if (!password) return "empty";
|
|
213
|
+
* if (password.length < 8) return "slow";
|
|
214
|
+
* if (password.length < 12) return "medium";
|
|
215
|
+
* return "fast";
|
|
216
|
+
* }}
|
|
217
|
+
* >
|
|
218
|
+
* ...
|
|
219
|
+
* </InputPasswordRoot>
|
|
59
220
|
*/
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
221
|
+
function InputPasswordRoot<T extends string = DefaultPasswordStrength>({
|
|
222
|
+
children,
|
|
223
|
+
dataHook,
|
|
224
|
+
trackingEl,
|
|
225
|
+
trackingLabel,
|
|
226
|
+
disabled,
|
|
227
|
+
value,
|
|
228
|
+
onChange,
|
|
229
|
+
onStrengthChange,
|
|
230
|
+
strengthLevels,
|
|
231
|
+
calculateStrength,
|
|
232
|
+
error,
|
|
233
|
+
id,
|
|
234
|
+
className,
|
|
235
|
+
}: InputPasswordRootProps<T>) {
|
|
236
|
+
const [showPassword, setShowPassword] = React.useState(false);
|
|
237
|
+
const [internalPassword, setInternalPassword] = React.useState("");
|
|
238
|
+
const [strength, setStrength] = React.useState<string>("meter");
|
|
64
239
|
|
|
65
|
-
|
|
240
|
+
const generatedId = React.useId();
|
|
241
|
+
const inputId = id || generatedId;
|
|
66
242
|
|
|
67
|
-
|
|
68
|
-
if (password.length >= 8) score += 1;
|
|
69
|
-
if (password.length >= 12) score += 1;
|
|
243
|
+
const password = value !== undefined ? value : internalPassword;
|
|
70
244
|
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
245
|
+
// Use provided levels or defaults
|
|
246
|
+
const levels = (strengthLevels ?? DEFAULT_STRENGTH_LEVELS) as Record<
|
|
247
|
+
string,
|
|
248
|
+
StrengthLevelConfig
|
|
249
|
+
>;
|
|
76
250
|
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
251
|
+
// Use provided calculator or default
|
|
252
|
+
const calculator =
|
|
253
|
+
calculateStrength ?? (calculatePasswordStrength as (password: string) => T);
|
|
254
|
+
|
|
255
|
+
const handleSetPassword = React.useCallback(
|
|
256
|
+
(newPassword: string) => {
|
|
257
|
+
if (value === undefined) {
|
|
258
|
+
setInternalPassword(newPassword);
|
|
259
|
+
}
|
|
260
|
+
onChange?.(newPassword);
|
|
261
|
+
},
|
|
262
|
+
[value, onChange]
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
const toggleVisibility = React.useCallback(() => {
|
|
266
|
+
setShowPassword((prev) => !prev);
|
|
267
|
+
}, []);
|
|
268
|
+
|
|
269
|
+
// Calculate password strength with debounce
|
|
270
|
+
React.useEffect(() => {
|
|
271
|
+
const timeoutId = setTimeout(() => {
|
|
272
|
+
const newStrength = calculator(password);
|
|
273
|
+
setStrength(newStrength);
|
|
274
|
+
onStrengthChange?.(newStrength);
|
|
275
|
+
}, 180);
|
|
276
|
+
|
|
277
|
+
return () => clearTimeout(timeoutId);
|
|
278
|
+
}, [password, calculator, onStrengthChange]);
|
|
279
|
+
|
|
280
|
+
const contextValue = React.useMemo<InputPasswordContextValue>(
|
|
281
|
+
() => ({
|
|
282
|
+
showPassword,
|
|
283
|
+
setShowPassword,
|
|
284
|
+
toggleVisibility,
|
|
285
|
+
password,
|
|
286
|
+
setPassword: handleSetPassword,
|
|
287
|
+
strength,
|
|
288
|
+
strengthLevels: levels,
|
|
289
|
+
disabled,
|
|
290
|
+
dataHook,
|
|
291
|
+
trackingEl,
|
|
292
|
+
trackingLabel,
|
|
293
|
+
inputId,
|
|
294
|
+
error,
|
|
295
|
+
}),
|
|
296
|
+
[
|
|
297
|
+
showPassword,
|
|
298
|
+
toggleVisibility,
|
|
299
|
+
password,
|
|
300
|
+
handleSetPassword,
|
|
301
|
+
strength,
|
|
302
|
+
levels,
|
|
303
|
+
disabled,
|
|
304
|
+
dataHook,
|
|
305
|
+
trackingEl,
|
|
306
|
+
trackingLabel,
|
|
307
|
+
inputId,
|
|
308
|
+
error,
|
|
309
|
+
]
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
return (
|
|
313
|
+
<InputPasswordContext.Provider value={contextValue}>
|
|
314
|
+
<div className={cn("w-full", className)}>{children}</div>
|
|
315
|
+
</InputPasswordContext.Provider>
|
|
316
|
+
);
|
|
82
317
|
}
|
|
83
318
|
|
|
84
|
-
|
|
319
|
+
InputPasswordRoot.displayName = "InputPasswordRoot";
|
|
320
|
+
|
|
321
|
+
// ============================================================================
|
|
322
|
+
// InputPasswordField
|
|
323
|
+
// ============================================================================
|
|
324
|
+
|
|
325
|
+
export interface InputPasswordFieldProps extends Omit<
|
|
326
|
+
React.ComponentProps<typeof InputGroupInput>,
|
|
327
|
+
"type"
|
|
328
|
+
> {
|
|
329
|
+
/**
|
|
330
|
+
* Additional class name for the InputGroup wrapper
|
|
331
|
+
*/
|
|
332
|
+
groupClassName?: string;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* InputPasswordField - The password input field with toggle button
|
|
337
|
+
* Wraps InputGroup with InputGroupInput and InputGroupAddon for the toggle
|
|
338
|
+
*/
|
|
339
|
+
const InputPasswordField = React.forwardRef<
|
|
340
|
+
HTMLInputElement,
|
|
341
|
+
InputPasswordFieldProps
|
|
342
|
+
>(({ className, groupClassName, onChange, ...props }, ref) => {
|
|
343
|
+
const {
|
|
344
|
+
showPassword,
|
|
345
|
+
toggleVisibility,
|
|
346
|
+
password,
|
|
347
|
+
setPassword,
|
|
348
|
+
disabled,
|
|
349
|
+
dataHook,
|
|
350
|
+
trackingEl,
|
|
351
|
+
trackingLabel,
|
|
352
|
+
inputId,
|
|
353
|
+
error,
|
|
354
|
+
} = useInputPasswordContext();
|
|
355
|
+
|
|
356
|
+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
357
|
+
setPassword(e.target.value);
|
|
358
|
+
onChange?.(e);
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
return (
|
|
362
|
+
<InputGroup className={groupClassName} dataHook={dataHook} error={error}>
|
|
363
|
+
<InputGroupInput
|
|
364
|
+
ref={ref}
|
|
365
|
+
className={className}
|
|
366
|
+
disabled={disabled}
|
|
367
|
+
id={inputId}
|
|
368
|
+
trackingEl={trackingEl}
|
|
369
|
+
trackingLabel={trackingLabel}
|
|
370
|
+
type={showPassword ? "text" : "password"}
|
|
371
|
+
value={password}
|
|
372
|
+
onChange={handleChange}
|
|
373
|
+
{...props}
|
|
374
|
+
/>
|
|
375
|
+
<InputGroupAddon align="inline-end">
|
|
376
|
+
<InputGroupButton
|
|
377
|
+
aria-label={showPassword ? "Hide password" : "Show password"}
|
|
378
|
+
data-hook={`${dataHook}-toggle`}
|
|
379
|
+
disabled={disabled}
|
|
380
|
+
size="icon"
|
|
381
|
+
variant="ghost"
|
|
382
|
+
onClick={toggleVisibility}
|
|
383
|
+
>
|
|
384
|
+
{showPassword ? (
|
|
385
|
+
<EyeOff className="h-4 w-4" />
|
|
386
|
+
) : (
|
|
387
|
+
<Eye className="h-4 w-4" />
|
|
388
|
+
)}
|
|
389
|
+
</InputGroupButton>
|
|
390
|
+
</InputGroupAddon>
|
|
391
|
+
</InputGroup>
|
|
392
|
+
);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
InputPasswordField.displayName = "InputPasswordField";
|
|
396
|
+
|
|
397
|
+
// ============================================================================
|
|
398
|
+
// InputPasswordToggle
|
|
399
|
+
// ============================================================================
|
|
400
|
+
|
|
401
|
+
export interface InputPasswordToggleProps extends Omit<
|
|
402
|
+
React.ComponentProps<typeof InputGroupButton>,
|
|
403
|
+
"onClick" | "aria-label"
|
|
404
|
+
> {
|
|
405
|
+
/**
|
|
406
|
+
* Custom aria-label for show state
|
|
407
|
+
* @default "Show password"
|
|
408
|
+
*/
|
|
409
|
+
showLabel?: string;
|
|
410
|
+
/**
|
|
411
|
+
* Custom aria-label for hide state
|
|
412
|
+
* @default "Hide password"
|
|
413
|
+
*/
|
|
414
|
+
hideLabel?: string;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* InputPasswordToggle - Standalone toggle button for password visibility
|
|
419
|
+
* Use this when you need custom placement of the toggle button
|
|
420
|
+
*/
|
|
421
|
+
const InputPasswordToggle = React.forwardRef<
|
|
422
|
+
HTMLButtonElement,
|
|
423
|
+
InputPasswordToggleProps
|
|
424
|
+
>(
|
|
85
425
|
(
|
|
86
426
|
{
|
|
87
427
|
className,
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
trackingLabel,
|
|
93
|
-
onChange,
|
|
94
|
-
value,
|
|
95
|
-
id,
|
|
428
|
+
showLabel = "Show password",
|
|
429
|
+
hideLabel = "Hide password",
|
|
430
|
+
size = "icon",
|
|
431
|
+
variant = "ghost",
|
|
96
432
|
...props
|
|
97
433
|
},
|
|
98
434
|
ref
|
|
99
435
|
) => {
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
const [internalValue, setInternalValue] = React.useState("");
|
|
436
|
+
const { showPassword, toggleVisibility, disabled, dataHook } =
|
|
437
|
+
useInputPasswordContext();
|
|
103
438
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
439
|
+
return (
|
|
440
|
+
<InputGroupButton
|
|
441
|
+
ref={ref}
|
|
442
|
+
aria-label={showPassword ? hideLabel : showLabel}
|
|
443
|
+
className={className}
|
|
444
|
+
data-hook={`${dataHook}-toggle`}
|
|
445
|
+
disabled={disabled}
|
|
446
|
+
size={size}
|
|
447
|
+
variant={variant}
|
|
448
|
+
onClick={toggleVisibility}
|
|
449
|
+
{...props}
|
|
450
|
+
>
|
|
451
|
+
{showPassword ? (
|
|
452
|
+
<EyeOff className="h-4 w-4" />
|
|
453
|
+
) : (
|
|
454
|
+
<Eye className="h-4 w-4" />
|
|
455
|
+
)}
|
|
456
|
+
</InputGroupButton>
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
);
|
|
107
460
|
|
|
108
|
-
|
|
109
|
-
React.useEffect(() => {
|
|
110
|
-
const timeoutId = setTimeout(() => {
|
|
111
|
-
const newStrength = calculatePasswordStrength(currentValue);
|
|
112
|
-
setStrength(newStrength);
|
|
113
|
-
onStrengthChange?.(newStrength);
|
|
114
|
-
}, 180);
|
|
461
|
+
InputPasswordToggle.displayName = "InputPasswordToggle";
|
|
115
462
|
|
|
116
|
-
|
|
117
|
-
|
|
463
|
+
// ============================================================================
|
|
464
|
+
// InputPasswordStrength
|
|
465
|
+
// ============================================================================
|
|
118
466
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
467
|
+
export interface InputPasswordStrengthProps {
|
|
468
|
+
/**
|
|
469
|
+
* Additional class name
|
|
470
|
+
*/
|
|
471
|
+
className?: string;
|
|
472
|
+
/**
|
|
473
|
+
* Whether to show the label next to the progress bar
|
|
474
|
+
* @default true
|
|
475
|
+
*/
|
|
476
|
+
showLabel?: boolean;
|
|
477
|
+
}
|
|
123
478
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
479
|
+
/**
|
|
480
|
+
* InputPasswordStrength - Password strength indicator
|
|
481
|
+
* Displays a progress bar and optional label showing password strength.
|
|
482
|
+
* Automatically uses the strength levels configured on InputPasswordRoot.
|
|
483
|
+
*/
|
|
484
|
+
function InputPasswordStrength({
|
|
485
|
+
className,
|
|
486
|
+
showLabel = true,
|
|
487
|
+
}: InputPasswordStrengthProps) {
|
|
488
|
+
const { strength, strengthLevels, dataHook, disabled } =
|
|
489
|
+
useInputPasswordContext();
|
|
127
490
|
|
|
128
|
-
|
|
491
|
+
// Get config for current strength level
|
|
492
|
+
const config = strengthLevels[strength] ?? {
|
|
493
|
+
label: strength,
|
|
494
|
+
value: 0,
|
|
495
|
+
color: "bg-muted",
|
|
496
|
+
};
|
|
129
497
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
data-hook={dataHook ? `${dataHook}-toggle` : undefined}
|
|
147
|
-
disabled={props.disabled}
|
|
148
|
-
size="icon"
|
|
149
|
-
variant="ghost"
|
|
150
|
-
onClick={togglePasswordVisibility}
|
|
151
|
-
>
|
|
152
|
-
{showPassword ? (
|
|
153
|
-
<EyeOff className="h-4 w-4" />
|
|
154
|
-
) : (
|
|
155
|
-
<Eye className="h-4 w-4" />
|
|
156
|
-
)}
|
|
157
|
-
</InputGroupButton>
|
|
158
|
-
</InputGroupAddon>
|
|
159
|
-
</InputGroup>
|
|
160
|
-
|
|
161
|
-
{showStrength && (
|
|
162
|
-
<div className="mt-1 space-y-2">
|
|
163
|
-
<div className="flex items-center gap-2">
|
|
164
|
-
<Progress
|
|
165
|
-
className={cn("h-2 flex-1")}
|
|
166
|
-
dataHook={
|
|
167
|
-
dataHook
|
|
168
|
-
? `${dataHook}-strength-progress`
|
|
169
|
-
: "password-strength-progress"
|
|
170
|
-
}
|
|
171
|
-
value={strengthConfig.value}
|
|
172
|
-
/>
|
|
173
|
-
<span
|
|
174
|
-
className="text-foreground w-10 shrink-0 text-right text-sm font-medium whitespace-nowrap"
|
|
175
|
-
data-hook={dataHook ? `${dataHook}-strength-label` : undefined}
|
|
176
|
-
>
|
|
177
|
-
{strengthConfig.label}
|
|
178
|
-
</span>
|
|
179
|
-
</div>
|
|
180
|
-
</div>
|
|
498
|
+
return (
|
|
499
|
+
<div className={cn("mt-1 space-y-2", className)}>
|
|
500
|
+
<div className={cn("flex items-center gap-2", disabled && "opacity-50")}>
|
|
501
|
+
<Progress
|
|
502
|
+
className="h-2 flex-1"
|
|
503
|
+
dataHook={`${dataHook}-strength-progress`}
|
|
504
|
+
indicatorClassName={config.color}
|
|
505
|
+
value={config.value}
|
|
506
|
+
/>
|
|
507
|
+
{showLabel && (
|
|
508
|
+
<span
|
|
509
|
+
className="text-foreground w-10 shrink-0 text-right text-sm font-medium whitespace-nowrap"
|
|
510
|
+
data-hook={`${dataHook}-strength-label`}
|
|
511
|
+
>
|
|
512
|
+
{config.label}
|
|
513
|
+
</span>
|
|
181
514
|
)}
|
|
182
515
|
</div>
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
516
|
+
</div>
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
InputPasswordStrength.displayName = "InputPasswordStrength";
|
|
186
521
|
|
|
187
|
-
|
|
522
|
+
// ============================================================================
|
|
523
|
+
// Exports
|
|
524
|
+
// ============================================================================
|
|
525
|
+
|
|
526
|
+
export {
|
|
527
|
+
InputPasswordRoot,
|
|
528
|
+
InputPasswordField,
|
|
529
|
+
InputPasswordToggle,
|
|
530
|
+
InputPasswordStrength,
|
|
531
|
+
calculatePasswordStrength,
|
|
532
|
+
DEFAULT_STRENGTH_LEVELS,
|
|
533
|
+
};
|
|
188
534
|
|
|
189
|
-
export {
|
|
190
|
-
export type { InputPasswordProps, PasswordStrength };
|
|
535
|
+
export type { DefaultPasswordStrength, StrengthLevelConfig };
|
package/src/components/input.tsx
CHANGED
|
@@ -78,6 +78,10 @@ export interface InputProps
|
|
|
78
78
|
* Required data-hook attribute for automated testing
|
|
79
79
|
*/
|
|
80
80
|
dataHook: string;
|
|
81
|
+
/**
|
|
82
|
+
* Whether the input has an error state
|
|
83
|
+
*/
|
|
84
|
+
error?: boolean;
|
|
81
85
|
/**
|
|
82
86
|
* Optional tracking element identifier for analytics
|
|
83
87
|
*/
|
|
@@ -95,23 +99,27 @@ export const Input = React.forwardRef<HTMLInputElement, InputProps>(
|
|
|
95
99
|
hasIcon = false,
|
|
96
100
|
hasSecondaryIcon = false,
|
|
97
101
|
dataHook,
|
|
102
|
+
error,
|
|
98
103
|
trackingEl,
|
|
99
104
|
trackingLabel,
|
|
100
105
|
...props
|
|
101
106
|
},
|
|
102
107
|
ref
|
|
103
108
|
) => {
|
|
104
|
-
//
|
|
109
|
+
// Support both error prop and aria-invalid for backwards compatibility
|
|
105
110
|
const hasError =
|
|
106
|
-
|
|
111
|
+
error ||
|
|
112
|
+
props["aria-invalid"] === true ||
|
|
113
|
+
props["aria-invalid"] === "true";
|
|
107
114
|
|
|
108
115
|
return (
|
|
109
116
|
<input
|
|
110
117
|
ref={ref}
|
|
118
|
+
aria-invalid={hasError ? "true" : undefined}
|
|
111
119
|
className={cn(
|
|
112
120
|
inputVariants({ hasIcon, hasSecondaryIcon }),
|
|
113
121
|
hasError &&
|
|
114
|
-
"
|
|
122
|
+
"focus-visible:border-destructive focus-visible:shadow-destructive border-red-600/40 dark:border-red-400/60",
|
|
115
123
|
className
|
|
116
124
|
)}
|
|
117
125
|
data-hook={dataHook}
|