"use client"; import React, {useEffect, useState} from "react"; import Paper from "@mui/material/Paper"; import Table from "@mui/material/Table"; import TableBody from "@mui/material/TableBody"; import TableCell from "@mui/material/TableCell"; import TableContainer from "@mui/material/TableContainer"; import TableHead from "@mui/material/TableHead"; import TablePagination from "@mui/material/TablePagination"; import TableRow from "@mui/material/TableRow"; import IconButton from "@mui/material/IconButton"; import EditIcon from "@mui/icons-material/Edit"; import SaveIcon from "@mui/icons-material/Save"; import CancelIcon from "@mui/icons-material/Close"; import DeleteIcon from "@mui/icons-material/Delete"; import TextField from "@mui/material/TextField"; import MultiSelect from "@/components/SearchBox/MultiSelect"; import { Collapse } from "@mui/material"; import TempInputGridForMockUp from "./TempInputGridForMockUp"; import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown"; import KeyboardArrowUpIcon from "@mui/icons-material/KeyboardArrowUp"; export interface ResultWithId { id: string | number; } interface BaseColumn { field: keyof T; label: string; type: string; options?: T[]; renderCell?: (T) => void; } interface ColumnWithAction extends BaseColumn { onClick: (item: T) => void; buttonIcon: React.ReactNode; buttonColor?: "inherit" | "default" | "primary" | "secondary"; } export type Column = | BaseColumn | ColumnWithAction; interface Props { items: T[], isMockUp?: Boolean, columns: Column[], noWrapper?: boolean, setPagingController: (value: { pageNum: number; pageSize: number; totalCount: number, index?: number}) => void, pagingController: { pageNum: number; pageSize: number; totalCount: number }, isAutoPaging: boolean } function EditableSearchResults({ index, items, isMockUp, columns, noWrapper, pagingController, setPagingController, isAutoPaging = true, isEdit = true, isHideButton = false, hasCollapse = false, }: Props) { const [page, setPage] = useState(0); const [rowsPerPage, setRowsPerPage] = useState(10); const [editingRowId, setEditingRowId] = useState(null); const [editedItems, setEditedItems] = useState(items); console.log(items) useEffect(()=>{ setEditedItems(items) },[items]) const handleChangePage = (_event: unknown, newPage: number) => { setPage(newPage); setPagingController({ ...pagingController, pageNum: newPage + 1 }, (index ?? -1)); }; const handleChangeRowsPerPage = (event: React.ChangeEvent) => { setRowsPerPage(+event.target.value); setPage(0); setPagingController({ ...pagingController, pageSize: +event.target.value, pageNum: 1 , index: index}); }; const handleEditClick = (id: number) => { setEditingRowId(id); }; const handleSaveClick = (item: T) => { setEditingRowId(null); // Call API or any save logic here setEditedItems((prev) => prev.map((row) => (row.id === item.id ? { ...row } : row)) ); }; const handleInputChange = (id: number, field: keyof T, value: string | number[]) => { setEditedItems((prev) => prev.map((item) => item.id === id ? { ...item, [field]: value } : item ) ); }; const handleDeleteClick = (id: number) => { // Implement delete logic here setEditedItems((prev) => prev.filter(item => item.id !== id)); }; useEffect(()=>{ console.log("[debug] isEdit in table", isEdit) //TODO: switch all record to not in edit mode and save the changes if (!isEdit) { editedItems.forEach(item => { // Call save logic here console.log("Saving item:", item); // Reset editing state if needed }); setEditingRowId(null); } },[isEdit]) function Row(props: { row: T }) { const { row } = props; const [open, setOpen] = useState(false); console.log(row) return ( <> { !isHideButton && {(editingRowId === row.id) ? ( <> handleSaveClick(row)}> setEditingRowId(null)}> { hasCollapse && setOpen(!open)} > {open ? : } } ) : ( <> handleEditClick(row.id as number)}> handleDeleteClick(row.id as number)}> { hasCollapse && setOpen(!open)} > {open ? : } } )} } {columns.map((column, idx) => { console.log(column) const columnName = column.field; return ( {editingRowId === row.id ? ( (() => { switch (column.type) { case 'input': console.log(column.type) return ( handleInputChange(row.id as number, columnName, e.target.value)} /> ); case 'multi-select': return ( handleInputChange(row.id as number, columnName, selectedValues)} /> ); case 'read-only': return ( {row[columnName] as string} ); default: return null; // Handle any default case if needed } })() ) : ( column.renderCell ? column.renderCell(row) : isEdit && handleEditClick(row.id as number)}> {row[columnName] as string} )} ); })} { hasCollapse &&
}
) } const table = ( <> {!isHideButton && Actions} {/* Action Column Header */} {columns.map((column, idx) => ( {column.label} ))} {(isAutoPaging ? editedItems.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) : editedItems).map((item) => ( ))}
); return noWrapper ? table : {table}; } export default EditableSearchResults;