Skip to content

Commit

Permalink
Merge pull request #58 from sakihet/update-card-form
Browse files Browse the repository at this point in the history
replace CardForm input with textarea
  • Loading branch information
sakihet authored Nov 6, 2023
2 parents 9300e5c + 0408928 commit 3fb552b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
63 changes: 48 additions & 15 deletions src/components/CardForm.tsx
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -11,24 +11,57 @@ export default function CardForm(
addCard: (params: AddCardParams) => void
}
) {
const inputElementCard = useRef<HTMLInputElement>(null)
const [editing, setEditing] = useState<boolean>(false)
const [composing, setComposing] = useState<boolean>(false)
const ref = useRef<HTMLTextAreaElement>(null)

const handleSubmit = (e: JSX.TargetedEvent<HTMLFormElement>) => {
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<HTMLTextAreaElement>) => {
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 (
<form onSubmit={handleSubmit}>
<input
class="h-6 px-2 rounded-1 border-0"
type="text"
placeholder="Add a card"
ref={inputElementCard}
/>
</form>
<button
class="h-8 w-full border-none text-left cursor-pointer"
onClick={handleClick}
>
{editing
?
<textarea
class="w-full border-none rounded-1 p-2 resize-none"
onBlur={handleBlur}
// @ts-ignore
oncompositionstart={handleCompositionStart}
oncompositionend={handleCompositionEnd}
onKeyDown={handleKeyDown}
ref={ref}
/>
:
<div class="px-2">+ Add a card</div>
}
</button>
)
}
5 changes: 5 additions & 0 deletions src/css/utility.css
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,8 @@
.pre-wrap {
white-space: pre-wrap;
}

/* resize */
.resize-none {
resize: none;
}

0 comments on commit 3fb552b

Please sign in to comment.