From 1ec5f6497b76ed0124711e2568dfa993787bb93e Mon Sep 17 00:00:00 2001 From: xmdhs Date: Fri, 22 Sep 2023 18:09:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E6=A0=A1=E9=AA=8C=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/CheckInput.tsx | 32 ++++++++++++++++---------- frontend/src/views/Login.tsx | 3 ++- frontend/src/views/Register.tsx | 3 ++- 3 files changed, 24 insertions(+), 14 deletions(-) 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 ?? "").