"use client"; import BABox from "./BABox"; import BAComponentSwitcher, { formElement } from "./BAComponentSwitcher"; import React, { useState } from "react"; import BAButton from "./BAButton"; import BAIconButton from "./BAIconButton"; import { PlusOutlined, DeleteOutlined } from "@ant-design/icons"; import BAPera from "./BAPera"; import BALoader from "./BALoader"; type actionType = { icon: React.ReactNode; onClick: (row?: any, index?: number) => void; disabled?: boolean | ((row: any) => boolean); }; type propsType = { datasourse: any[]; cols: { key: string; label: string; displayField?: (data: any, index: number) => any; className?: string; HeaderField?: any; width?: any; element: formElement; }[]; loading?: boolean; onChange?: any; closeModal?: any; setDatasourse?: any; onAddRow?: any; onDeleteRow?: any; action?: actionType[]; disableAction?: boolean; updatedArr?: any[]; setUpdatedArr?: any; pageSize?: string | number; page?: string | number; disableAdd?: boolean; disableForm?: boolean; disabled?: (row: any) => void | boolean; }; export default function BAFormGrid(props: propsType) { const { datasourse, cols, loading, setDatasourse, onAddRow, onDeleteRow, action, disableAction, updatedArr, setUpdatedArr, disableForm, disableAdd } = props; const [rowObj, setRowObj] = useState({}); const addRow = () => { let obj = {}; if (onAddRow) { obj = onAddRow() || {}; } if (setDatasourse) setDatasourse([...datasourse, obj]); }; const deleteRow = (index: number) => { datasourse.splice(index, 1); if (onDeleteRow) { onDeleteRow(); } if (setDatasourse) setDatasourse([...datasourse]); }; return ( {loading ? Loading... : datasourse && datasourse.length > 0 ? ( <> {!disableAction && } {cols.map((col: any, index: number) => ( ))} {datasourse.map((row: any, rowIndex: number) => ( {!disableAction && ( )} {cols.map((col: any, colIndex: number) => ( ))} {row.expanded && ( )} ))} {!disableAdd && !disableAction && }
{col.label}
!disableForm && deleteRow(rowIndex)} /> {action && action?.map((item: actionType, key) => ( item.onClick(row, rowIndex)} /> ))} {col.displayField ? ( col.displayField(row, rowIndex) ) : col.element ? ( { datasourse[rowIndex] = { ...datasourse[rowIndex], [element.key]: val ? val[element.key] : "" }; setDatasourse([...datasourse]); if (updatedArr) { const existingIndex = updatedArr.findIndex( (item) => item.Seq_No === datasourse[rowIndex].Seq_No ); if (existingIndex === -1) updatedArr.push({ ...datasourse[rowIndex] }); else updatedArr[existingIndex] = datasourse[rowIndex]; setUpdatedArr([...updatedArr]); } }} rowIndex={rowIndex} /> ) : ( row[col.key] )}
{/* */} {row.expanded}
) : ( No Data Found )}
); }