|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useEffect, useState } from "react"; |
| 4 | +import { X } from "@phosphor-icons/react"; |
| 5 | +import ModalTextField from "./ModalTextField"; |
| 6 | + |
| 7 | +interface CommentModalProps { |
| 8 | + isOpen: boolean; |
| 9 | + title: string; |
| 10 | + initialValue?: string; |
| 11 | + placeholder?: string; |
| 12 | + onClose: () => void; // invoked on cancel/close (no value change) |
| 13 | + onSave: (value: string) => void; // invoked when clicking Save |
| 14 | + // Optional props for enhanced functionality |
| 15 | + useModalTextField?: boolean; // Use ModalTextField instead of textarea |
| 16 | + width?: string; // Custom width (default: max-w-lg) |
| 17 | + saveButtonText?: string; // Custom save button text |
| 18 | + cancelButtonText?: string; // Custom cancel button text |
| 19 | + fieldName?: string; // Name attribute for ModalTextField |
| 20 | + onFieldChange?: (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void; // For external state management |
| 21 | +} |
| 22 | + |
| 23 | +export default function CommentModal({ |
| 24 | + isOpen, |
| 25 | + title, |
| 26 | + initialValue = "", |
| 27 | + placeholder = "Comment about this item here...", |
| 28 | + onClose, |
| 29 | + onSave, |
| 30 | + useModalTextField = false, |
| 31 | + width = "max-w-lg", |
| 32 | + saveButtonText = "Save", |
| 33 | + cancelButtonText = "Cancel", |
| 34 | + fieldName = "comments", |
| 35 | + onFieldChange, |
| 36 | +}: CommentModalProps) { |
| 37 | + const [value, setValue] = useState(initialValue); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + // keep draft in sync when opening for a different item |
| 41 | + if (isOpen) setValue(initialValue ?? ""); |
| 42 | + }, [isOpen, initialValue]); |
| 43 | + |
| 44 | + const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { |
| 45 | + setValue(e.target.value); |
| 46 | + if (onFieldChange) { |
| 47 | + onFieldChange(e); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + if (!isOpen) return null; |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="fixed inset-0 flex items-center justify-center bg-black/50 z-50"> |
| 55 | + <div className={`relative bg-white rounded-2xl p-6 w-full ${width} shadow-lg text-[#22070B]`}> |
| 56 | + <button |
| 57 | + onClick={onClose} |
| 58 | + aria-label="Close" |
| 59 | + className="absolute top-4 right-4 text-2xl text-gray-500 hover:text-gray-700" |
| 60 | + > |
| 61 | + <X className="size-6" /> |
| 62 | + </button> |
| 63 | + <h2 className="text-[20px] font-semibold leading-[28px] mb-2">{title}</h2> |
| 64 | + <p className="text-[16px] leading-[24px] mb-2">Comments</p> |
| 65 | + |
| 66 | + {useModalTextField ? ( |
| 67 | + <ModalTextField |
| 68 | + name={fieldName} |
| 69 | + value={value} |
| 70 | + onChange={handleInputChange} |
| 71 | + placeholder={placeholder} |
| 72 | + /> |
| 73 | + ) : ( |
| 74 | + <textarea |
| 75 | + value={value} |
| 76 | + onChange={handleInputChange} |
| 77 | + placeholder={placeholder} |
| 78 | + className="w-full min-h-[150px] rounded-[4px] border border-[rgba(34,7,11,0.05)] bg-[rgba(34,7,11,0.05)] text-[16px] p-3 focus:outline-none focus:ring-1 focus:ring-[rgba(34,7,11,0.1)] resize-none" |
| 79 | + /> |
| 80 | + )} |
| 81 | + |
| 82 | + <div className="mt-6 flex justify-end gap-4"> |
| 83 | + <button |
| 84 | + onClick={onClose} |
| 85 | + className="bg-white hover:bg-gray-100 text-[#EF3340] border-2 border-[#EF3340] font-semibold py-2 px-4 rounded-md" |
| 86 | + > |
| 87 | + {cancelButtonText} |
| 88 | + </button> |
| 89 | + <button |
| 90 | + onClick={() => onSave(value)} |
| 91 | + className="bg-[#EF3340] hover:bg-[#a32027] text-white font-semibold py-2 px-4 rounded-md" |
| 92 | + > |
| 93 | + {saveButtonText} |
| 94 | + </button> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +} |
0 commit comments