diff --git a/.eslintrc.js b/.eslintrc.js index 12f954b96..7a98b162f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -39,9 +39,10 @@ module.exports = { // react "react/prop-types": OFF, "react/no-unescaped-entities": OFF, - "react/jsx-curly-brace-presence": "warn", + "react/jsx-curly-brace-presence": WARN, // jsx-ally "jsx-a11y/no-onchange": WARN, + // import "import/no-anonymous-default-export": OFF, // next "@next/next/no-img-element": OFF, diff --git a/src/components/Admonition.tsx b/src/components/Admonition.tsx index edb3fd063..b1ad3b1ae 100644 --- a/src/components/Admonition.tsx +++ b/src/components/Admonition.tsx @@ -1,3 +1,4 @@ +import type { ReactNode } from "react" import styles from "./Admonition.module.css" import { cva } from "class-variance-authority" @@ -100,7 +101,17 @@ const admonition = cva(styles.admonition, { }, }) -export const Admonition = ({ type, title, children }) => { +type AdmonitionType = keyof typeof svgMap + +export const Admonition = ({ + type, + title, + children, +}: { + type: AdmonitionType + title: string + children: ReactNode +}) => { return (
diff --git a/src/components/ApiGallery.tsx b/src/components/ApiGallery.tsx index a3a1b9ca4..d4e2623fc 100644 --- a/src/components/ApiGallery.tsx +++ b/src/components/ApiGallery.tsx @@ -1,4 +1,5 @@ import { useEffect } from "react" +import type { MouseEventHandler } from "react" import Link from "next/link" import Footer from "./Footer" import typographyStyles from "../styles/typography.module.css" @@ -10,8 +11,8 @@ import { useRouter } from "next/router" export default function ApiGallery() { const router = useRouter() - const onChange = (e) => { - const version = parseInt(e.target.value) + const onChange: MouseEventHandler = (e) => { + const version = parseInt((e.target as HTMLElement).getAttribute("value")!) if (version !== 7) { router.push(`https://legacy.react-hook-form.com/v${version}/api`) diff --git a/src/components/BuilderPage.tsx b/src/components/BuilderPage.tsx index f23d43e59..981472643 100644 --- a/src/components/BuilderPage.tsx +++ b/src/components/BuilderPage.tsx @@ -2,9 +2,10 @@ import { useState, useRef, useEffect, memo, RefObject } from "react" import { useRouter } from "next/router" import { Animate } from "react-simple-animate" import { useForm } from "react-hook-form" +import type { SubmitHandler } from "react-hook-form" import SortableContainer from "./SortableContainer" import { useStateMachine } from "little-state-machine" -import type { GlobalState } from "little-state-machine" +import type { GlobalState, FormDataItem } from "little-state-machine" import colors from "../styles/colors" import generateCode from "./logic/generateCode" import copyClipBoard from "./utils/copyClipBoard" @@ -27,7 +28,9 @@ const errorStyle = { background: colors.errorPink, } -const defaultValue = { +type FormFieldDefinitionItem = Partial & { toggle?: boolean } + +const defaultValue: FormFieldDefinitionItem = { max: undefined, min: undefined, pattern: undefined, @@ -36,7 +39,7 @@ const defaultValue = { required: undefined, name: "", type: "", - options: [], + options: "", } function BuilderPage({ @@ -61,41 +64,43 @@ function BuilderPage({ } }, }) - const [editFormData, setFormData] = useState(defaultValue) + const [editFormData, setEditFormData] = useState(defaultValue) const { register, handleSubmit, watch, setValue, reset, formState } = - useForm() + useForm() const errors = formState.errors const [editIndex, setEditIndex] = useState(-1) const copyFormData = useRef([]) const closeButton = useRef(null) const [showValidation, toggleValidation] = useState(false) - const onSubmit = (data) => { + + const onSubmit: SubmitHandler = (data) => { toggleValidation(false) if (editIndex >= 0) { - formData[editIndex] = data + formData[editIndex] = data as FormDataItem updateFormData([...formData.filter((formInput) => formInput)]) - setFormData(defaultValue) + setEditFormData(defaultValue) setEditIndex(-1) } else { updateFormData([...formData, ...[data]]) } reset() } + const form = useRef(null) const type = watch("type") const shouldToggleOn = - editFormData.max || - editFormData.min || - editFormData.pattern || - editFormData.maxLength || - editFormData.minLength || + !!editFormData.max || + !!editFormData.min || + !!editFormData.pattern || + !!editFormData.maxLength || + !!editFormData.minLength || editFormData.required copyFormData.current = formData const editIndexRef = useRef(null) editIndexRef.current = editIndex const router = useRouter() - const validate = (value) => { + const validate = (value: unknown) => { return ( !Object.values(copyFormData.current).find( (data) => data.name === value @@ -118,7 +123,7 @@ function BuilderPage({ }, [editFormData.type, setValue]) useEffect(() => { - setValue("required", editFormData.required) + setValue("required", !!editFormData.required) }, [editFormData.required, editIndex, setValue]) const child = ( @@ -140,15 +145,12 @@ function BuilderPage({

diff --git a/src/components/DevTools.tsx b/src/components/DevTools.tsx index 5f96e667d..1b9aac681 100644 --- a/src/components/DevTools.tsx +++ b/src/components/DevTools.tsx @@ -20,6 +20,7 @@ import type { DevtoolUIProps } from "@hookform/devtools/dist/devToolUI" const DevTool = dynamic( () => + // @ts-expect-error no types are available import("@hookform/devtools/dist/index.cjs.development").then( (mod) => mod.DevTool ), @@ -37,7 +38,7 @@ export default function DevTools() { const { control } = methods - const onSubmit = (data) => console.log(data) + const onSubmit = (data: unknown) => console.log(data) return (
diff --git a/src/components/Form.tsx b/src/components/Form.tsx index 0226712ca..c00078131 100644 --- a/src/components/Form.tsx +++ b/src/components/Form.tsx @@ -1,14 +1,14 @@ import { memo } from "react" import { Animate } from "react-simple-animate" import { useStateMachine } from "little-state-machine" -import FormFields from "./FormFields" -import goToBuilder from "./utils/goToBuilder" -import { FieldValues, UseFormReturn } from "react-hook-form" +import type { FieldValues, UseFormReturn } from "react-hook-form" import home from "../data/home" import generic from "../data/generic" import buttonStyles from "../styles/button.module.css" import containerStyles from "../styles/container.module.css" import typographyStyles from "../styles/typography.module.css" +import FormFields from "./FormFields" +import goToBuilder from "./utils/goToBuilder" import styles from "./Form.module.css" const animationProps = { @@ -76,7 +76,7 @@ function Form({ Example - +