fix(webui): explain unmet password rules after blur
The requirement list still opens only while the new-password field is focused. After blur, unmet rules stay visible as field text, and Save can be pressed to return focus to that field.
This commit is contained in:
3 files changed
+94
-9
No files matched your search
@@ -28,6 +28,23 @@ const EMPTY_RULES: readonly PasswordRule[] = [
|
|||||||
|
|
||||||
const STRENGTH_LABELS = ['未输入', '很弱', '较弱', '一般', '良好', '强'] as const;
|
const STRENGTH_LABELS = ['未输入', '很弱', '较弱', '一般', '良好', '强'] as const;
|
||||||
|
|
||||||
|
/** Shown under the new-password field only while the animated rule list is hidden. */
|
||||||
|
export function passwordRequirementHint(options: {
|
||||||
|
focused: boolean;
|
||||||
|
value: string;
|
||||||
|
checked: boolean;
|
||||||
|
valid: boolean;
|
||||||
|
rules: readonly PasswordRule[];
|
||||||
|
checkError: string;
|
||||||
|
}): string | null {
|
||||||
|
if (options.focused || options.value.length === 0) return null;
|
||||||
|
if (options.checkError) return `密码强度校验失败:${options.checkError}`;
|
||||||
|
if (!options.checked || options.valid) return null;
|
||||||
|
const unmet = options.rules.filter((rule) => !rule.ok);
|
||||||
|
if (unmet.length === 0) return '密码尚未满足全部要求';
|
||||||
|
return `仍需满足:${unmet.map((rule) => rule.label).join('、')}`;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ChangePasswordFormProps {
|
export interface ChangePasswordFormProps {
|
||||||
/** Selects whether the form changes credentials or only rehearses validation. */
|
/** Selects whether the form changes credentials or only rehearses validation. */
|
||||||
mode?: 'change' | 'rehearsal';
|
mode?: 'change' | 'rehearsal';
|
||||||
@@ -57,6 +74,8 @@ export interface ChangePasswordFormProps {
|
|||||||
className?: string;
|
className?: string;
|
||||||
renderActions?: (state: {
|
renderActions?: (state: {
|
||||||
canSubmit: boolean;
|
canSubmit: boolean;
|
||||||
|
/** Fields are filled enough to try saving; strength may still be unmet. */
|
||||||
|
canAttempt: boolean;
|
||||||
submitting: boolean;
|
submitting: boolean;
|
||||||
submitLabel: string;
|
submitLabel: string;
|
||||||
}) => ReactNode;
|
}) => ReactNode;
|
||||||
@@ -118,12 +137,20 @@ export function ChangePasswordForm({
|
|||||||
[displayedRules],
|
[displayedRules],
|
||||||
);
|
);
|
||||||
const confirmMatches = newPassword.length > 0 && newPassword === confirmPassword;
|
const confirmMatches = newPassword.length > 0 && newPassword === confirmPassword;
|
||||||
const canSubmit =
|
const canAttempt =
|
||||||
!submitting
|
!submitting
|
||||||
&& (rehearsing || effectiveOld.length > 0)
|
&& (rehearsing || effectiveOld.length > 0)
|
||||||
&& valid
|
|
||||||
&& confirmMatches
|
&& confirmMatches
|
||||||
&& (rehearsing || effectiveOld !== newPassword);
|
&& (rehearsing || effectiveOld !== newPassword);
|
||||||
|
const canSubmit = canAttempt && valid;
|
||||||
|
const requirementHint = passwordRequirementHint({
|
||||||
|
focused: newPasswordFocused,
|
||||||
|
value: newPassword,
|
||||||
|
checked: checkedCurrentPassword,
|
||||||
|
valid,
|
||||||
|
rules: displayedRules,
|
||||||
|
checkError: strengthError,
|
||||||
|
});
|
||||||
|
|
||||||
// Debounce the strength check so we don't slam the API on every keystroke.
|
// Debounce the strength check so we don't slam the API on every keystroke.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -156,7 +183,12 @@ export function ChangePasswordForm({
|
|||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (!canSubmit) return;
|
if (!canAttempt) return;
|
||||||
|
if (!valid) {
|
||||||
|
setNewPasswordFocused(true);
|
||||||
|
document.getElementById(`${idPrefix}-new`)?.focus();
|
||||||
|
return;
|
||||||
|
}
|
||||||
setSubmitting(true);
|
setSubmitting(true);
|
||||||
setError('');
|
setError('');
|
||||||
try {
|
try {
|
||||||
@@ -220,11 +252,12 @@ export function ChangePasswordForm({
|
|||||||
onChange={setNewPassword}
|
onChange={setNewPassword}
|
||||||
onFocus={() => setNewPasswordFocused(true)}
|
onFocus={() => setNewPasswordFocused(true)}
|
||||||
onBlur={() => setNewPasswordFocused(false)}
|
onBlur={() => setNewPasswordFocused(false)}
|
||||||
validationKey={effectiveOld}
|
validationKey={`${effectiveOld}\0${newPasswordFocused}\0${valid}\0${strengthError}\0${requirementHint ?? ''}`}
|
||||||
|
reserveLines={2}
|
||||||
validate={(value) => {
|
validate={(value) => {
|
||||||
if (value.length === 0) return '请输入新密码';
|
if (value.length === 0) return '请输入新密码';
|
||||||
if (!rehearsing && value === effectiveOld) return '新密码不能与当前密码相同';
|
if (!rehearsing && value === effectiveOld) return '新密码不能与当前密码相同';
|
||||||
return null;
|
return requirementHint;
|
||||||
}}
|
}}
|
||||||
showValidIcon={false}
|
showValidIcon={false}
|
||||||
endAdornment={(
|
endAdornment={(
|
||||||
@@ -294,14 +327,14 @@ export function ChangePasswordForm({
|
|||||||
)}
|
)}
|
||||||
</AnimatePresence>
|
</AnimatePresence>
|
||||||
|
|
||||||
{renderActions ? renderActions({ canSubmit, submitting, submitLabel }) : (
|
{renderActions ? renderActions({ canSubmit, canAttempt, submitting, submitLabel }) : (
|
||||||
<div className="flex items-center gap-2 pt-1">
|
<div className="flex items-center gap-2 pt-1">
|
||||||
{onCancel && (
|
{onCancel && (
|
||||||
<Button type="button" variant="ghost" onClick={onCancel} className="h-10">
|
<Button type="button" variant="ghost" onClick={onCancel} className="h-10">
|
||||||
取消
|
取消
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
<Button type="submit" disabled={!canSubmit} className="ml-auto h-10">
|
<Button type="submit" disabled={!canAttempt} className="ml-auto h-10">
|
||||||
{submitting ? (
|
{submitting ? (
|
||||||
<>
|
<>
|
||||||
<Loader2 className="size-4 animate-spin" /> 提交中…
|
<Loader2 className="size-4 animate-spin" /> 提交中…
|
||||||
|
|||||||
@@ -356,12 +356,12 @@ function PasswordStep({
|
|||||||
idPrefix="onboarding-cpw"
|
idPrefix="onboarding-cpw"
|
||||||
submitLabel={mode === 'rehearsal' ? '完成演练' : '保存并完成'}
|
submitLabel={mode === 'rehearsal' ? '完成演练' : '保存并完成'}
|
||||||
className="gap-5"
|
className="gap-5"
|
||||||
renderActions={({ canSubmit, submitting, submitLabel }) => (
|
renderActions={({ canAttempt, submitting, submitLabel }) => (
|
||||||
<div className="grid gap-3 pt-2 sm:flex sm:items-center">
|
<div className="grid gap-3 pt-2 sm:flex sm:items-center">
|
||||||
<Button type="button" variant="outline" onClick={back} disabled={submitting} className="h-11 w-full sm:w-auto">
|
<Button type="button" variant="outline" onClick={back} disabled={submitting} className="h-11 w-full sm:w-auto">
|
||||||
上一步
|
上一步
|
||||||
</Button>
|
</Button>
|
||||||
<Button type="submit" disabled={!canSubmit} className="h-11 w-full sm:ml-auto sm:min-w-36 sm:w-auto">
|
<Button type="submit" disabled={!canAttempt} className="h-11 w-full sm:ml-auto sm:min-w-36 sm:w-auto">
|
||||||
{submitting ? (
|
{submitting ? (
|
||||||
<>
|
<>
|
||||||
<Loader2 className="size-4 animate-spin" /> 提交中…
|
<Loader2 className="size-4 animate-spin" /> 提交中…
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
PasswordStrength,
|
PasswordStrength,
|
||||||
type PasswordRule,
|
type PasswordRule,
|
||||||
} from '../src/components/interior/password-strength';
|
} from '../src/components/interior/password-strength';
|
||||||
|
import { passwordRequirementHint } from '../src/components/pages/change-password-form';
|
||||||
|
|
||||||
const RULES: readonly PasswordRule[] = [
|
const RULES: readonly PasswordRule[] = [
|
||||||
{ id: 'length', label: '长度不少于 10 位', test: (value) => value.length >= 10 },
|
{ id: 'length', label: '长度不少于 10 位', test: (value) => value.length >= 10 },
|
||||||
@@ -68,3 +69,54 @@ describe('password input guidance', () => {
|
|||||||
expect(markup).toContain('role="status"');
|
expect(markup).toContain('role="status"');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('passwordRequirementHint', () => {
|
||||||
|
const rules = [
|
||||||
|
{ id: 'length', label: '长度不少于 10 位', ok: false },
|
||||||
|
{ id: 'upper', label: '包含大写字母', ok: true },
|
||||||
|
];
|
||||||
|
|
||||||
|
it('hides unmet-rule copy while the animated list is open', () => {
|
||||||
|
expect(passwordRequirementHint({
|
||||||
|
focused: true,
|
||||||
|
value: 'short',
|
||||||
|
checked: true,
|
||||||
|
valid: false,
|
||||||
|
rules,
|
||||||
|
checkError: '',
|
||||||
|
})).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('lists unmet rules after the new-password field loses focus', () => {
|
||||||
|
expect(passwordRequirementHint({
|
||||||
|
focused: false,
|
||||||
|
value: 'short',
|
||||||
|
checked: true,
|
||||||
|
valid: false,
|
||||||
|
rules,
|
||||||
|
checkError: '',
|
||||||
|
})).toBe('仍需满足:长度不少于 10 位');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not invent unmet rules while strength is still being checked', () => {
|
||||||
|
expect(passwordRequirementHint({
|
||||||
|
focused: false,
|
||||||
|
value: 'short',
|
||||||
|
checked: false,
|
||||||
|
valid: false,
|
||||||
|
rules,
|
||||||
|
checkError: '',
|
||||||
|
})).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('surfaces a failed strength check after blur', () => {
|
||||||
|
expect(passwordRequirementHint({
|
||||||
|
focused: false,
|
||||||
|
value: 'short',
|
||||||
|
checked: false,
|
||||||
|
valid: false,
|
||||||
|
rules,
|
||||||
|
checkError: '网络错误',
|
||||||
|
})).toBe('密码强度校验失败:网络错误');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user