edit check list item

This commit is contained in:
hamid zarghami
2026-07-26 14:38:46 +03:30
parent cabf248c0e
commit 2f71b4c96c
4 changed files with 123 additions and 19 deletions
@@ -1,17 +1,83 @@
import { Popover, PopoverButton, PopoverPanel } from "@headlessui/react";
import { More } from "iconsax-react";
import { type FC } from "react";
import { type FC, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import TaskDetailLabelCheckbox from "../labels/TaskDetailLabelCheckbox";
type Props = {
title: string;
checked: boolean;
onToggle: () => void;
onEdit: () => void;
onSave: (title: string) => void;
onDelete: () => void;
isSaving?: boolean;
};
const ChecklistItem: FC<Props> = ({ title, checked, onToggle, onEdit, onDelete }) => {
const ChecklistItem: FC<Props> = ({ title, checked, onToggle, onSave, onDelete, isSaving = false }) => {
const { t } = useTranslation("global");
const [isEditing, setIsEditing] = useState(false);
const [editTitle, setEditTitle] = useState(title);
useEffect(() => {
if (!isEditing) {
setEditTitle(title);
}
}, [title, isEditing]);
const handleSave = () => {
const trimmed = editTitle.trim();
if (!trimmed || trimmed === title) {
setIsEditing(false);
setEditTitle(title);
return;
}
onSave(trimmed);
setIsEditing(false);
};
const handleCancel = () => {
setIsEditing(false);
setEditTitle(title);
};
if (isEditing) {
return (
<div className="flex gap-1 items-center">
<TaskDetailLabelCheckbox checked={checked} onToggle={onToggle} />
<div className="flex-1 flex gap-2 items-center">
<input
value={editTitle}
onChange={(e) => setEditTitle(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") handleSave();
if (e.key === "Escape") handleCancel();
}}
autoFocus
disabled={isSaving}
className="flex-1 h-[27px] px-2 text-xs bg-white/50 rounded outline-none"
/>
<button
type="button"
onClick={handleSave}
disabled={!editTitle.trim() || isSaving}
className="h-7 px-3 rounded-lg bg-[#292D32] text-white text-[11px] disabled:opacity-50"
>
{t("save")}
</button>
<button
type="button"
onClick={handleCancel}
disabled={isSaving}
className="h-7 px-3 rounded-lg bg-white/50 text-[#292D32] text-[11px] border border-white disabled:opacity-50"
>
{t("cancel")}
</button>
</div>
</div>
);
}
return (
<div className="flex gap-1 h-[27px] items-center">
<TaskDetailLabelCheckbox checked={checked} onToggle={onToggle} />
@@ -24,14 +90,32 @@ const ChecklistItem: FC<Props> = ({ title, checked, onToggle, onEdit, onDelete }
<More size={20} color="#000" />
</PopoverButton>
<PopoverPanel anchor="bottom end" className="z-[80] mt-1 rounded-[10px] bg-white shadow-md text-right p-3">
<button type="button" onClick={onEdit} className="w-full text-sm text-right">
ویرایش
</button>
<PopoverPanel anchor="bottom end" className="z-[80] mt-1 rounded-[10px] bg-white shadow-md text-right p-3">
{({ close }) => (
<>
<button
type="button"
onClick={() => {
close();
setIsEditing(true);
}}
className="w-full text-sm text-right"
>
ویرایش
</button>
<button type="button" onClick={onDelete} className="w-full mt-3 text-sm text-right text-[#FF0000]">
پاک کردن
</button>
<button
type="button"
onClick={() => {
close();
onDelete();
}}
className="w-full mt-3 text-sm text-right text-[#FF0000]"
>
پاک کردن
</button>
</>
)}
</PopoverPanel>
</Popover>
</div>