diff --git a/frontend/src/components/CheckInput.tsx b/frontend/src/components/CheckInput.tsx index 8094235..b0c08c7 100644 --- a/frontend/src/components/CheckInput.tsx +++ b/frontend/src/components/CheckInput.tsx @@ -15,9 +15,25 @@ type prop = { export const CheckInput = forwardRef(({ required, checkList, ...textFied }, ref) => { const [err, setErr] = useState(""); + const [value, setValue] = useState(""); + + const check = (value: string) => { + if (required && (!value || value == "")) { + setErr("此项必填") + return false + } + for (const v of checkList) { + if (!v.reg.test(value)) { + setErr(v.errMsg) + return false + } + } + setErr("") + return true + } const verify = () => { - return err == "" + return check(value) } useImperativeHandle(ref, () => { @@ -28,17 +44,8 @@ export const CheckInput = forwardRef(({ required, checkList, ...t const onChange = (event: React.ChangeEvent) => { const value = event.target.value - if (required && (!value || value == "")) { - setErr("此项必填") - return - } - for (const v of checkList) { - if (!v.reg.test(value)) { - setErr(v.errMsg) - return - } - } - setErr("") + setValue(value) + check(value) } @@ -48,6 +55,7 @@ export const CheckInput = forwardRef(({ required, checkList, ...t onChange={onChange} helperText={err} required={required} + value={value} {...textFied} /> }) diff --git a/frontend/src/views/Login.tsx b/frontend/src/views/Login.tsx index aaf5e85..cfcc7c6 100644 --- a/frontend/src/views/Login.tsx +++ b/frontend/src/views/Login.tsx @@ -37,7 +37,8 @@ export default function SignIn() { email: data.get('email')?.toString(), password: data.get('password')?.toString(), } - if (!Array.from(checkList.current.values()).every(v => v.verify())) { + if (!Array.from(checkList.current.values()).map(v => v.verify()).reduce((p, v) => (p == true) && (v == true))) { + setLoading(false) return } login(postData.email!, postData.password ?? ""). diff --git a/frontend/src/views/Register.tsx b/frontend/src/views/Register.tsx index 921b9e5..55fd6af 100644 --- a/frontend/src/views/Register.tsx +++ b/frontend/src/views/Register.tsx @@ -35,7 +35,8 @@ export default function SignUp() { password: data.get('password')?.toString(), username: data.get("username")?.toString() } - if (!Array.from(checkList.current.values()).every(v => v.verify())) { + if (!Array.from(checkList.current.values()).map(v => v.verify()).reduce((p, v) => (p == true) && (v == true))) { + setLoading(false) return } register(d.email ?? "", d.username ?? "", d.password ?? "").