Skip to content

Commit f6e309c

Browse files
committed
lovely!
1 parent 4e5c9a7 commit f6e309c

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

app/routes/board.$id/column.tsx

+8-11
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,15 @@ export function Column({ name, columnId, items }: ColumnProps) {
107107
}
108108
/>
109109
))}
110-
111-
{edit && (
112-
<NewCard
113-
columnId={columnId}
114-
nextOrder={items.length}
115-
onAddCard={() => scrollList()}
116-
onComplete={() => setEdit(false)}
117-
/>
118-
)}
119110
</ul>
120-
121-
{!edit && (
111+
{edit ? (
112+
<NewCard
113+
columnId={columnId}
114+
nextOrder={items.length}
115+
onAddCard={() => scrollList()}
116+
onComplete={() => setEdit(false)}
117+
/>
118+
) : (
122119
<div className="p-2">
123120
<button
124121
type="button"

app/routes/board.$id/components.tsx

+14-29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { useFetcher } from "@remix-run/react";
2-
import {
3-
forwardRef,
4-
useEffect,
5-
useLayoutEffect,
6-
useRef,
7-
useState,
8-
} from "react";
2+
import { forwardRef, useRef, useState } from "react";
3+
import { flushSync } from "react-dom";
94

105
export let SaveButton = forwardRef<
116
HTMLButtonElement,
@@ -55,34 +50,20 @@ export function EditableText({
5550
let [edit, setEdit] = useState(false);
5651
let inputRef = useRef<HTMLInputElement>(null);
5752
let buttonRef = useRef<HTMLButtonElement>(null);
58-
let lastAction = useRef<"click" | "escape" | "submit" | "none">("none");
5953

6054
// optimistic update
6155
if (fetcher.formData?.has(fieldName)) {
6256
value = String(fetcher.formData.get("name"));
6357
}
6458

65-
useLayoutEffect(() => {
66-
switch (lastAction.current) {
67-
case "submit":
68-
case "escape": {
69-
buttonRef.current?.focus();
70-
break;
71-
}
72-
case "click": {
73-
inputRef.current?.select();
74-
break;
75-
}
76-
}
77-
lastAction.current = "none";
78-
}, [edit]);
79-
8059
return edit ? (
8160
<fetcher.Form
8261
method="post"
8362
onSubmit={() => {
84-
lastAction.current = "submit";
85-
setEdit(false);
63+
flushSync(() => {
64+
setEdit(false);
65+
});
66+
buttonRef.current?.focus();
8667
}}
8768
>
8869
{children}
@@ -95,8 +76,10 @@ export function EditableText({
9576
className={inputClassName}
9677
onKeyDown={(event) => {
9778
if (event.key === "Escape") {
98-
lastAction.current = "escape";
99-
setEdit(false);
79+
flushSync(() => {
80+
setEdit(false);
81+
});
82+
buttonRef.current?.focus();
10083
}
10184
}}
10285
onBlur={(event) => {
@@ -113,8 +96,10 @@ export function EditableText({
11396
type="button"
11497
ref={buttonRef}
11598
onClick={() => {
116-
lastAction.current = "click";
117-
setEdit(true);
99+
flushSync(() => {
100+
setEdit(true);
101+
});
102+
inputRef.current?.select();
118103
}}
119104
className={buttonClassName}
120105
>

0 commit comments

Comments
 (0)