@brightlocal/ui-components 0.2.0 → 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/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.d.ts.map +1 -1
- 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.map +1 -1
- package/dist/combobox.js +5 -2
- 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 +426 -396
- 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.d.ts.map +1 -1
- package/dist/input-otp.cjs +1 -1
- package/dist/input-otp.d.ts +43 -36
- package/dist/input-otp.d.ts.map +1 -1
- package/dist/input-otp.js +140 -119
- package/dist/input-password.cjs +1 -1
- package/dist/input-password.d.ts +147 -19
- package/dist/input-password.d.ts.map +1 -1
- package/dist/input-password.js +197 -92
- package/dist/input.cjs +1 -1
- package/dist/input.js +1 -1
- 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/select.d.ts.map +1 -1
- 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.d.ts.map +1 -1
- 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 -2
- package/src/components/chip.tsx +249 -0
- package/src/components/combobox.tsx +17 -13
- 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 +4 -1
- package/src/components/input-otp.tsx +159 -91
- package/src/components/input-password.tsx +469 -129
- package/src/components/input.tsx +1 -1
- package/src/components/progress.tsx +21 -2
- package/src/components/select.tsx +1 -3
- package/src/components/slider.tsx +85 -15
- package/src/components/textarea.tsx +4 -1
- package/src/index.ts +3 -0
|
@@ -12,184 +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;
|
|
55
175
|
/**
|
|
56
176
|
* Whether the input has an error state
|
|
57
177
|
*/
|
|
58
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;
|
|
59
191
|
}
|
|
60
192
|
|
|
61
193
|
/**
|
|
62
|
-
*
|
|
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>
|
|
63
220
|
*/
|
|
64
|
-
function
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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");
|
|
68
239
|
|
|
69
|
-
|
|
240
|
+
const generatedId = React.useId();
|
|
241
|
+
const inputId = id || generatedId;
|
|
70
242
|
|
|
71
|
-
|
|
72
|
-
if (password.length >= 8) score += 1;
|
|
73
|
-
if (password.length >= 12) score += 1;
|
|
243
|
+
const password = value !== undefined ? value : internalPassword;
|
|
74
244
|
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
245
|
+
// Use provided levels or defaults
|
|
246
|
+
const levels = (strengthLevels ?? DEFAULT_STRENGTH_LEVELS) as Record<
|
|
247
|
+
string,
|
|
248
|
+
StrengthLevelConfig
|
|
249
|
+
>;
|
|
80
250
|
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (score <= 4) return "good";
|
|
85
|
-
return "great";
|
|
86
|
-
}
|
|
251
|
+
// Use provided calculator or default
|
|
252
|
+
const calculator =
|
|
253
|
+
calculateStrength ?? (calculatePasswordStrength as (password: string) => T);
|
|
87
254
|
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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,
|
|
94
290
|
dataHook,
|
|
95
291
|
trackingEl,
|
|
96
292
|
trackingLabel,
|
|
97
|
-
|
|
98
|
-
value,
|
|
99
|
-
id,
|
|
293
|
+
inputId,
|
|
100
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
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
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
|
+
>(
|
|
425
|
+
(
|
|
426
|
+
{
|
|
427
|
+
className,
|
|
428
|
+
showLabel = "Show password",
|
|
429
|
+
hideLabel = "Hide password",
|
|
430
|
+
size = "icon",
|
|
431
|
+
variant = "ghost",
|
|
101
432
|
...props
|
|
102
433
|
},
|
|
103
434
|
ref
|
|
104
435
|
) => {
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
const [internalValue, setInternalValue] = React.useState("");
|
|
436
|
+
const { showPassword, toggleVisibility, disabled, dataHook } =
|
|
437
|
+
useInputPasswordContext();
|
|
108
438
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
+
);
|
|
112
460
|
|
|
113
|
-
|
|
114
|
-
React.useEffect(() => {
|
|
115
|
-
const timeoutId = setTimeout(() => {
|
|
116
|
-
const newStrength = calculatePasswordStrength(currentValue);
|
|
117
|
-
setStrength(newStrength);
|
|
118
|
-
onStrengthChange?.(newStrength);
|
|
119
|
-
}, 180);
|
|
461
|
+
InputPasswordToggle.displayName = "InputPasswordToggle";
|
|
120
462
|
|
|
121
|
-
|
|
122
|
-
|
|
463
|
+
// ============================================================================
|
|
464
|
+
// InputPasswordStrength
|
|
465
|
+
// ============================================================================
|
|
123
466
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
+
}
|
|
128
478
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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();
|
|
132
490
|
|
|
133
|
-
|
|
491
|
+
// Get config for current strength level
|
|
492
|
+
const config = strengthLevels[strength] ?? {
|
|
493
|
+
label: strength,
|
|
494
|
+
value: 0,
|
|
495
|
+
color: "bg-muted",
|
|
496
|
+
};
|
|
134
497
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
data-hook={dataHook ? `${dataHook}-toggle` : undefined}
|
|
152
|
-
disabled={props.disabled}
|
|
153
|
-
size="icon"
|
|
154
|
-
variant="ghost"
|
|
155
|
-
onClick={togglePasswordVisibility}
|
|
156
|
-
>
|
|
157
|
-
{showPassword ? (
|
|
158
|
-
<EyeOff className="h-4 w-4" />
|
|
159
|
-
) : (
|
|
160
|
-
<Eye className="h-4 w-4" />
|
|
161
|
-
)}
|
|
162
|
-
</InputGroupButton>
|
|
163
|
-
</InputGroupAddon>
|
|
164
|
-
</InputGroup>
|
|
165
|
-
|
|
166
|
-
{showStrength && (
|
|
167
|
-
<div className="mt-1 space-y-2">
|
|
168
|
-
<div className="flex items-center gap-2">
|
|
169
|
-
<Progress
|
|
170
|
-
className={cn("h-2 flex-1")}
|
|
171
|
-
dataHook={
|
|
172
|
-
dataHook
|
|
173
|
-
? `${dataHook}-strength-progress`
|
|
174
|
-
: "password-strength-progress"
|
|
175
|
-
}
|
|
176
|
-
value={strengthConfig.value}
|
|
177
|
-
/>
|
|
178
|
-
<span
|
|
179
|
-
className="text-foreground w-10 shrink-0 text-right text-sm font-medium whitespace-nowrap"
|
|
180
|
-
data-hook={dataHook ? `${dataHook}-strength-label` : undefined}
|
|
181
|
-
>
|
|
182
|
-
{strengthConfig.label}
|
|
183
|
-
</span>
|
|
184
|
-
</div>
|
|
185
|
-
</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>
|
|
186
514
|
)}
|
|
187
515
|
</div>
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
516
|
+
</div>
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
InputPasswordStrength.displayName = "InputPasswordStrength";
|
|
191
521
|
|
|
192
|
-
|
|
522
|
+
// ============================================================================
|
|
523
|
+
// Exports
|
|
524
|
+
// ============================================================================
|
|
525
|
+
|
|
526
|
+
export {
|
|
527
|
+
InputPasswordRoot,
|
|
528
|
+
InputPasswordField,
|
|
529
|
+
InputPasswordToggle,
|
|
530
|
+
InputPasswordStrength,
|
|
531
|
+
calculatePasswordStrength,
|
|
532
|
+
DEFAULT_STRENGTH_LEVELS,
|
|
533
|
+
};
|
|
193
534
|
|
|
194
|
-
export {
|
|
195
|
-
export type { InputPasswordProps, PasswordStrength };
|
|
535
|
+
export type { DefaultPasswordStrength, StrengthLevelConfig };
|
package/src/components/input.tsx
CHANGED
|
@@ -119,7 +119,7 @@ export const Input = React.forwardRef<HTMLInputElement, InputProps>(
|
|
|
119
119
|
className={cn(
|
|
120
120
|
inputVariants({ hasIcon, hasSecondaryIcon }),
|
|
121
121
|
hasError &&
|
|
122
|
-
"border-red-600/40 dark:border-red-400/60
|
|
122
|
+
"focus-visible:border-destructive focus-visible:shadow-destructive border-red-600/40 dark:border-red-400/60",
|
|
123
123
|
className
|
|
124
124
|
)}
|
|
125
125
|
data-hook={dataHook}
|