diff --git a/src/components/CardForm.tsx b/src/components/CardForm.tsx index b0c5df2..588972e 100644 --- a/src/components/CardForm.tsx +++ b/src/components/CardForm.tsx @@ -1,4 +1,4 @@ -import { useRef } from "preact/hooks" +import { useEffect, useRef, useState } from "preact/hooks" import { JSX } from "preact/jsx-runtime" import { AddCardParams } from "./PageBoard" @@ -11,24 +11,57 @@ export default function CardForm( addCard: (params: AddCardParams) => void } ) { - const inputElementCard = useRef(null) + const [editing, setEditing] = useState(false) + const [composing, setComposing] = useState(false) + const ref = useRef(null) - const handleSubmit = (e: JSX.TargetedEvent) => { - e.preventDefault() - if (inputElementCard.current?.value) { - addCard({ listId: listId, cardName: inputElementCard.current?.value }) - inputElementCard.current.value = '' + useEffect(() => { + if (editing) { + ref.current?.focus() } + }, [editing]) + + const handleBlur = () => { + setEditing(false) + } + const handleClick = () => { + setEditing(true) + } + const handleKeyDown = (e: JSX.TargetedKeyboardEvent) => { + if (e.key === 'Enter' && !composing) { + e.preventDefault() + if (ref.current?.value) { + addCard({ listId: listId, cardName: ref.current?.value }) + ref.current.value = '' + } + } + } + const handleCompositionStart = () => { + setComposing(true) + } + const handleCompositionEnd = () => { + setComposing(false) } return ( -
- -
+