Skip to content

Commit 3fb552b

Browse files
authored
Merge pull request #58 from sakihet/update-card-form
replace CardForm input with textarea
2 parents 9300e5c + 0408928 commit 3fb552b

2 files changed

Lines changed: 53 additions & 15 deletions

File tree

src/components/CardForm.tsx

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef } from "preact/hooks"
1+
import { useEffect, useRef, useState } from "preact/hooks"
22
import { JSX } from "preact/jsx-runtime"
33
import { AddCardParams } from "./PageBoard"
44

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

16-
const handleSubmit = (e: JSX.TargetedEvent<HTMLFormElement>) => {
17-
e.preventDefault()
18-
if (inputElementCard.current?.value) {
19-
addCard({ listId: listId, cardName: inputElementCard.current?.value })
20-
inputElementCard.current.value = ''
18+
useEffect(() => {
19+
if (editing) {
20+
ref.current?.focus()
2121
}
22+
}, [editing])
23+
24+
const handleBlur = () => {
25+
setEditing(false)
26+
}
27+
const handleClick = () => {
28+
setEditing(true)
29+
}
30+
const handleKeyDown = (e: JSX.TargetedKeyboardEvent<HTMLTextAreaElement>) => {
31+
if (e.key === 'Enter' && !composing) {
32+
e.preventDefault()
33+
if (ref.current?.value) {
34+
addCard({ listId: listId, cardName: ref.current?.value })
35+
ref.current.value = ''
36+
}
37+
}
38+
}
39+
const handleCompositionStart = () => {
40+
setComposing(true)
41+
}
42+
const handleCompositionEnd = () => {
43+
setComposing(false)
2244
}
2345

2446
return (
25-
<form onSubmit={handleSubmit}>
26-
<input
27-
class="h-6 px-2 rounded-1 border-0"
28-
type="text"
29-
placeholder="Add a card"
30-
ref={inputElementCard}
31-
/>
32-
</form>
47+
<button
48+
class="h-8 w-full border-none text-left cursor-pointer"
49+
onClick={handleClick}
50+
>
51+
{editing
52+
?
53+
<textarea
54+
class="w-full border-none rounded-1 p-2 resize-none"
55+
onBlur={handleBlur}
56+
// @ts-ignore
57+
oncompositionstart={handleCompositionStart}
58+
oncompositionend={handleCompositionEnd}
59+
onKeyDown={handleKeyDown}
60+
ref={ref}
61+
/>
62+
:
63+
<div class="px-2">+ Add a card</div>
64+
}
65+
</button>
3366
)
3467
}

src/css/utility.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,8 @@
573573
.pre-wrap {
574574
white-space: pre-wrap;
575575
}
576+
577+
/* resize */
578+
.resize-none {
579+
resize: none;
580+
}

0 commit comments

Comments
 (0)