Fix useForm Hook Types

This commit is contained in:
奇趣保罗 2024-03-04 17:00:59 +08:00
parent 3ef694a468
commit b04b6c7ab8
5 changed files with 45 additions and 48 deletions

View File

@ -1,5 +1,5 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";
const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { token, siteUrl } = await chrome.storage.local.get(["token", "siteUrl"]);
@ -22,5 +22,5 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
res.send(addReq);
}
}
export default handler;

View File

@ -1,5 +1,5 @@
import type { PlasmoCSConfig } from "plasmo"
export const config: PlasmoCSConfig = {
matches: ["<all_urls>"],
world: "MAIN"

View File

@ -1,27 +1,30 @@
import { useEffect, useRef, type FormEvent } from "react";
import { useRef, type InputHTMLAttributes } from "react";
type Values = Record<string, string | number>;
type defaultValues = Record<string, any>;
interface Props {
initialValues?: Values;
onSubmit?: (values: Values) => void;
interface Props<T extends defaultValues = defaultValues> {
initialValues?: Partial<T>;
}
type a = (values: Values) => void;
type InputElements =
| HTMLInputElement
| HTMLSelectElement
| HTMLTextAreaElement
;
interface UseFormReturns {
getValue: () => Values;
setValue: (name: string, value: string | number) => void;
setValues: (values: Values) => void;
bindInput: (name: string) => any;
onSubmit: (a: a) => (ev: FormEvent) => void;
interface UseFormReturns<T extends defaultValues> {
getValue: () => T;
setValue: (name: keyof T, value: string | number) => void;
setValues: (values: Partial<T>) => void;
bindInput: (name: keyof T) => InputHTMLAttributes<InputElements>;
// onSubmit: (values: T) => (ev: React.FormEvent) => void
onSubmit: (fn: (values: T) => void) => (ev: React.FormEvent) => void
}
function useForm({ initialValues }: Props) {
const formRef = useRef<HTMLFormElement>();
const inputs = useRef({});
const values = useRef({ ...initialValues });
const inst = useRef<UseFormReturns>();
function useForm<T extends defaultValues>({ initialValues }: Props<T>) {
const inputs = useRef<Partial<Record<keyof T, InputElements>>>({});
const values = useRef<T>({ ...initialValues } as T);
const inst = useRef<UseFormReturns<T>>();
if (!inst.current) {
const getValue = () => {
@ -29,15 +32,15 @@ function useForm({ initialValues }: Props) {
}
const setValue = (name, value) => {
console.log(inputs.current[name]);
console.log(inputs.current[name], name);
if (inputs.current[name]) {
values.current[name] = value;
values.current[name as keyof T] = value;
inputs.current[name].value = value;
}
}
const setValues = (values: Values) => {
const setValues = (values) => {
Object.keys(values).forEach((key) => {
setValue(key, values[key]);
});
@ -45,29 +48,32 @@ function useForm({ initialValues }: Props) {
const bindInput = (name: string) => {
console.log('bindinput')
return {
name,
id: name,
ref: (target) => {
console.log('ref', target);
if (target) {
inputs.current[name] = target;
inputs.current[name as keyof T] = target;
}
return undefined;
},
defaultValue: initialValues[name],
onChange: (ev) => {
values.current[ev.target.name] = ev.target.value;
const { name, value } = ev.target;
values.current[name as keyof T] = value;
},
}
}
const onSubmit = (fn) => (ev: SubmitEvent) => {
ev.preventDefault();
fn(getValue());
const onSubmit = (fn: (values: T) => void) => {
return (ev: React.FormEvent) => {
ev.preventDefault();
fn(getValue());
}
}
inst.current = {
@ -79,16 +85,6 @@ function useForm({ initialValues }: Props) {
};
}
useEffect(() => {
console.log('form', formRef.current);
// formRef.current.onsubmit = (ev: SubmitEvent) => {
// ev.preventDefault();
// console.log(t.current?.getValue());
// console.log(inputs);
// }
}, []);
return inst.current;
}

View File

@ -22,7 +22,7 @@
margin-right: .5em;
}
}
.item:hover {
background-color: #eee;
}
@ -53,7 +53,7 @@
}
}
}
.body {
padding: 1em;
}

View File

@ -15,6 +15,10 @@ interface FormValue {
link: string;
desc: string;
tips: string;
author: string;
image: string;
sitename: string;
tags: string;
}
const getInfo = async () => {
@ -50,10 +54,7 @@ function Read({ onBack }: ReadProps) {
const [count, setCount] = useState(0);
const { bindInput, setValues, onSubmit } = useForm<FormValue>({
initialValues: {
test: 'test',
},
onSubmit: (values) => {
submitForm(values);
tags: "",
},
});