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;
|
||||
|
||||
/** 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 {
|
||||
/** Selects whether the form changes credentials or only rehearses validation. */
|
||||
mode?: 'change' | 'rehearsal';
|
||||
@@ -57,6 +74,8 @@ export interface ChangePasswordFormProps {
|
||||
className?: string;
|
||||
renderActions?: (state: {
|
||||
canSubmit: boolean;
|
||||
/** Fields are filled enough to try saving; strength may still be unmet. */
|
||||
canAttempt: boolean;
|
||||
submitting: boolean;
|
||||
submitLabel: string;
|
||||
}) => ReactNode;
|
||||
@@ -118,12 +137,20 @@ export function ChangePasswordForm({
|
||||
[displayedRules],
|
||||
);
|
||||
const confirmMatches = newPassword.length > 0 && newPassword === confirmPassword;
|
||||
const canSubmit =
|
||||
const canAttempt =
|
||||
!submitting
|
||||
&& (rehearsing || effectiveOld.length > 0)
|
||||
&& valid
|
||||
&& confirmMatches
|
||||
&& (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.
|
||||
useEffect(() => {
|
||||
@@ -156,7 +183,12 @@ export function ChangePasswordForm({
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!canSubmit) return;
|
||||
if (!canAttempt) return;
|
||||
if (!valid) {
|
||||
setNewPasswordFocused(true);
|
||||
document.getElementById(`${idPrefix}-new`)?.focus();
|
||||
return;
|
||||
}
|
||||
setSubmitting(true);
|
||||
setError('');
|
||||
try {
|
||||
@@ -220,11 +252,12 @@ export function ChangePasswordForm({
|
||||
onChange={setNewPassword}
|
||||
onFocus={() => setNewPasswordFocused(true)}
|
||||
onBlur={() => setNewPasswordFocused(false)}
|
||||
validationKey={effectiveOld}
|
||||
validationKey={`${effectiveOld}\0${newPasswordFocused}\0${valid}\0${strengthError}\0${requirementHint ?? ''}`}
|
||||
reserveLines={2}
|
||||
validate={(value) => {
|
||||
if (value.length === 0) return '请输入新密码';
|
||||
if (!rehearsing && value === effectiveOld) return '新密码不能与当前密码相同';
|
||||
return null;
|
||||
return requirementHint;
|
||||
}}
|
||||
showValidIcon={false}
|
||||
endAdornment={(
|
||||
@@ -294,14 +327,14 @@ export function ChangePasswordForm({
|
||||
)}
|
||||
</AnimatePresence>
|
||||
|
||||
{renderActions ? renderActions({ canSubmit, submitting, submitLabel }) : (
|
||||
{renderActions ? renderActions({ canSubmit, canAttempt, submitting, submitLabel }) : (
|
||||
<div className="flex items-center gap-2 pt-1">
|
||||
{onCancel && (
|
||||
<Button type="button" variant="ghost" onClick={onCancel} className="h-10">
|
||||
取消
|
||||
</Button>
|
||||
)}
|
||||
<Button type="submit" disabled={!canSubmit} className="ml-auto h-10">
|
||||
<Button type="submit" disabled={!canAttempt} className="ml-auto h-10">
|
||||
{submitting ? (
|
||||
<>
|
||||
<Loader2 className="size-4 animate-spin" /> 提交中…
|
||||
|
||||
@@ -356,12 +356,12 @@ function PasswordStep({
|
||||
idPrefix="onboarding-cpw"
|
||||
submitLabel={mode === 'rehearsal' ? '完成演练' : '保存并完成'}
|
||||
className="gap-5"
|
||||
renderActions={({ canSubmit, submitting, submitLabel }) => (
|
||||
renderActions={({ canAttempt, submitting, submitLabel }) => (
|
||||
<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>
|
||||
<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 ? (
|
||||
<>
|
||||
<Loader2 className="size-4 animate-spin" /> 提交中…
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
PasswordStrength,
|
||||
type PasswordRule,
|
||||
} from '../src/components/interior/password-strength';
|
||||
import { passwordRequirementHint } from '../src/components/pages/change-password-form';
|
||||
|
||||
const RULES: readonly PasswordRule[] = [
|
||||
{ id: 'length', label: '长度不少于 10 位', test: (value) => value.length >= 10 },
|
||||
@@ -68,3 +69,54 @@ describe('password input guidance', () => {
|
||||
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