Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Refactor(useForm): remove unnecessary logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Jan 5, 2021
1 parent ffc18e9 commit d69188b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-seahorses-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

Refactor(useForm): remove unnecessary logic
17 changes: 3 additions & 14 deletions app/src/Playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import { useForm } from "react-cool-form";
export default () => {
const { form, getState, runValidation, field } = useForm({
defaultValues: { t1: "", t2: "" },
validate: async (values) => {
validate: (values) => {
const errors = {};
// eslint-disable-next-line compat/compat
await new Promise((r) => setTimeout(r, 2000));
// if (!values.t1.length) errors.t2 = "Form Required";
return errors;
},
Expand All @@ -15,7 +13,7 @@ export default () => {
console.log(
"LOG ===> ",
getState(
{ isValidating: "isValidating", errors: "errors" },
{ errors: "errors" },
{
filterUntouchedError: false,
}
Expand All @@ -24,16 +22,7 @@ export default () => {

return (
<form ref={form} noValidate>
<input
name="t1"
// ref={field(async (value) => {
// // eslint-disable-next-line compat/compat
// await new Promise((r) => setTimeout(r, 2000));
// // if (!value) return "Field Required";
// return false;
// })}
// required
/>
<input name="t1" ref={field((value) => false)} />
<input name="t2" required />
<input type="submit" />
<button
Expand Down
11 changes: 4 additions & 7 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,17 +398,14 @@ export default <V extends FormValues = FormValues>({
if (builtInValidationMode === false || !fieldsRef.current[name])
return undefined;

const {
field: { validationMessage, validity },
} = fieldsRef.current[name];
const { field } = fieldsRef.current[name];

if (builtInValidationMode === "message" && validationMessage)
return validationMessage;
if (builtInValidationMode === "message") return field.validationMessage;

// eslint-disable-next-line no-restricted-syntax
for (const k in validity) {
for (const k in field.validity) {
// @ts-expect-error
if (k !== "valid" && validity[k]) return k;
if (k !== "valid" && field.validity[k]) return k;
}

return undefined;
Expand Down

0 comments on commit d69188b

Please sign in to comment.