feat: focus item form on creation of new checklist

This commit is contained in:
Henry
2025-08-13 12:52:17 +01:00
parent b4ba815e2d
commit 85fea3a4d2
3 changed files with 29 additions and 12 deletions

View File

@@ -17,7 +17,7 @@ interface NewChecklistFormInput {
} }
export function NewChecklistForm({ cardPublicId }: { cardPublicId: string }) { export function NewChecklistForm({ cardPublicId }: { cardPublicId: string }) {
const { closeModal } = useModal(); const { closeModal, setModalState } = useModal();
const { showPopup } = usePopup(); const { showPopup } = usePopup();
const utils = api.useUtils(); const utils = api.useUtils();
@@ -56,6 +56,9 @@ export function NewChecklistForm({ cardPublicId }: { cardPublicId: string }) {
}); });
return { previous }; return { previous };
}, },
onSuccess: (data) => {
setModalState("ADD_CHECKLIST", { createdChecklistId: data.publicId });
},
onError: (_error, vars, ctx) => { onError: (_error, vars, ctx) => {
if (ctx?.previous) if (ctx?.previous)
utils.card.byId.setData( utils.card.byId.setData(

View File

@@ -40,17 +40,12 @@ const NewChecklistItemForm = ({
const refocusEditable = () => { const refocusEditable = () => {
const el = editableRef.current; const el = editableRef.current;
if (!el) return; if (!el) return;
el.focus();
// hack to ensure the input is focused after creating new checklist item
setTimeout(() => { setTimeout(() => {
el.focus(); el.focus();
const range = document.createRange(); }, 100);
range.selectNodeContents(el);
range.collapse(false);
const sel = window.getSelection();
if (sel) {
sel.removeAllRanges();
sel.addRange(range);
}
}, 0);
}; };
const addChecklistItemMutation = api.checklist.createItem.useMutation({ const addChecklistItemMutation = api.checklist.createItem.useMutation({
@@ -150,6 +145,8 @@ const NewChecklistItemForm = ({
</label> </label>
<div className="flex-1 pr-7"> <div className="flex-1 pr-7">
<ContentEditable <ContentEditable
id={`checklist-item-input-${checklistPublicId}`}
tabIndex={0}
placeholder={t`Add an item...`} placeholder={t`Add an item...`}
html={title} html={title}
disabled={false} disabled={false}

View File

@@ -1,7 +1,7 @@
import Link from "next/link"; import Link from "next/link";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { t } from "@lingui/core/macro"; import { t } from "@lingui/core/macro";
import { useState } from "react"; import { useEffect, useState } from "react";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { HiPlus, HiXMark } from "react-icons/hi2"; import { HiPlus, HiXMark } from "react-icons/hi2";
import { IoChevronForwardSharp } from "react-icons/io5"; import { IoChevronForwardSharp } from "react-icons/io5";
@@ -140,7 +140,13 @@ export function CardRightPanel() {
export default function CardPage() { export default function CardPage() {
const router = useRouter(); const router = useRouter();
const utils = api.useUtils(); const utils = api.useUtils();
const { modalContentType, entityId, openModal } = useModal(); const {
modalContentType,
entityId,
openModal,
getModalState,
clearModalState,
} = useModal();
const { showPopup } = usePopup(); const { showPopup } = usePopup();
const { workspace } = useWorkspace(); const { workspace } = useWorkspace();
const [activeChecklistForm, setActiveChecklistForm] = useState<string | null>( const [activeChecklistForm, setActiveChecklistForm] = useState<string | null>(
@@ -192,6 +198,17 @@ export default function CardPage() {
}); });
}; };
// Open the new item form after creating a new checklist
useEffect(() => {
if (!card) return;
const state = getModalState("ADD_CHECKLIST");
const createdId: string | undefined = state?.createdChecklistId;
if (createdId) {
setActiveChecklistForm(createdId);
clearModalState("ADD_CHECKLIST");
}
}, [card, getModalState, clearModalState]);
if (!cardId) return <></>; if (!cardId) return <></>;
return ( return (