diff --git a/background/messages/read.ts b/background/messages/read.ts index ad469fa..fdc6ad2 100644 --- a/background/messages/read.ts +++ b/background/messages/read.ts @@ -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; diff --git a/contents/invokeFetch.ts b/contents/invokeFetch.ts index 8b2e29f..3a88449 100644 --- a/contents/invokeFetch.ts +++ b/contents/invokeFetch.ts @@ -1,5 +1,5 @@ import type { PlasmoCSConfig } from "plasmo" - + export const config: PlasmoCSConfig = { matches: [""], world: "MAIN" diff --git a/hooks/useForm.ts b/hooks/useForm.ts index de71f8f..015651d 100644 --- a/hooks/useForm.ts +++ b/hooks/useForm.ts @@ -1,27 +1,30 @@ -import { useEffect, useRef, type FormEvent } from "react"; +import { useRef, type InputHTMLAttributes } from "react"; -type Values = Record; +type defaultValues = Record; -interface Props { - initialValues?: Values; - onSubmit?: (values: Values) => void; +interface Props { + initialValues?: Partial; } -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 { + getValue: () => T; + setValue: (name: keyof T, value: string | number) => void; + setValues: (values: Partial) => void; + bindInput: (name: keyof T) => InputHTMLAttributes; + // onSubmit: (values: T) => (ev: React.FormEvent) => void + onSubmit: (fn: (values: T) => void) => (ev: React.FormEvent) => void } -function useForm({ initialValues }: Props) { - const formRef = useRef(); - const inputs = useRef({}); - const values = useRef({ ...initialValues }); - const inst = useRef(); +function useForm({ initialValues }: Props) { + const inputs = useRef>>({}); + const values = useRef({ ...initialValues } as T); + const inst = useRef>(); 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; } diff --git a/popup/popup.module.less b/popup/popup.module.less index 01501f9..1417e01 100644 --- a/popup/popup.module.less +++ b/popup/popup.module.less @@ -22,7 +22,7 @@ margin-right: .5em; } } - + .item:hover { background-color: #eee; } @@ -53,7 +53,7 @@ } } } - + .body { padding: 1em; } diff --git a/popup/read.tsx b/popup/read.tsx index 4ced8ad..fe22229 100644 --- a/popup/read.tsx +++ b/popup/read.tsx @@ -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({ initialValues: { - test: 'test', - }, - onSubmit: (values) => { - submitForm(values); + tags: "", }, });