import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import * as z from 'zod' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form' import { Input } from '@/components/ui/input' import type { PasswordChangeRequest } from '@/types/auth' const passwordChangeSchema = z.object({ current_password: z.string().min(1, '请输入当前密码'), new_password: z .string() .min(8, '密码至少8个字符') .regex(/[A-Z]/, '密码必须包含至少一个大写字母') .regex(/[a-z]/, '密码必须包含至少一个小写字母') .regex(/\d/, '密码必须包含至少一个数字'), confirm_password: z.string().min(1, '请确认新密码'), }).refine((data) => data.new_password === data.confirm_password, { message: '两次输入的密码不一致', path: ['confirm_password'], }) type PasswordChangeFormValues = z.infer interface ChangePasswordDialogProps { open: boolean onOpenChange: (open: boolean) => void onSubmit: (data: PasswordChangeRequest) => Promise isLoading: boolean } export function ChangePasswordDialog({ open, onOpenChange, onSubmit, isLoading, }: ChangePasswordDialogProps) { const form = useForm({ resolver: zodResolver(passwordChangeSchema), defaultValues: { current_password: '', new_password: '', confirm_password: '', }, }) const handleSubmit = async (data: PasswordChangeFormValues) => { await onSubmit(data) form.reset() } const handleOpenChange = (open: boolean) => { if (!open && !isLoading) { form.reset() } onOpenChange(open) } return ( 修改密码 请输入当前密码和新密码。新密码至少8个字符,包含大小写字母和数字。
( 当前密码 )} /> ( 新密码 )} /> ( 确认新密码 )} />
) }