From 9ff4921b6f6758df96fd77de8895915fcebd0893 Mon Sep 17 00:00:00 2001 From: "cyril.tsui" Date: Thu, 13 Feb 2025 16:42:38 +0800 Subject: [PATCH] update invoice search money format --- .../InvoiceSearch/InvoiceSearch.tsx | 65 +++++++++++++++++-- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/src/components/InvoiceSearch/InvoiceSearch.tsx b/src/components/InvoiceSearch/InvoiceSearch.tsx index f7c3abe..1e2a517 100644 --- a/src/components/InvoiceSearch/InvoiceSearch.tsx +++ b/src/components/InvoiceSearch/InvoiceSearch.tsx @@ -13,7 +13,7 @@ import { deleteInvoice, importIssuedInovice, importReceivedInovice, updateInvoic import { deleteDialog, errorDialogWithContent, successDialog } from "../Swal/CustomAlerts"; import { invoiceList, issuedInvoiceList, issuedInvoiceSearchForm, receivedInvoiceList, receivedInvoiceSearchForm } from "@/app/api/invoices"; import EditOutlinedIcon from '@mui/icons-material/EditOutlined'; -import { GridCellParams, GridColDef, GridEventListener, GridRowId, GridRowModes, GridRowModesModel } from "@mui/x-data-grid"; +import { GridCellParams, GridColDef, GridEventListener, GridRenderEditCellParams, GridRowId, GridRowModes, GridRowModesModel, GridValueFormatterParams } from "@mui/x-data-grid"; import { useGridApiRef } from "@mui/x-data-grid"; import StyledDataGrid from "../StyledDataGrid"; @@ -22,8 +22,11 @@ import CreateInvoiceModal from "./CreateInvoiceModal"; import { ProjectResult } from "@/app/api/projects"; import { IMPORT_INVOICE, IMPORT_RECEIPT } from "@/middleware"; import InvoiceSearchLoading from "./InvoiceSearchLoading"; +import { NumberFormatValues, NumericFormat } from "react-number-format"; - +interface CustomMoneyComponentProps { + params: GridRenderEditCellParams; +} interface Props { invoices: invoiceList[]; @@ -287,6 +290,48 @@ const InvoiceSearch: React.FC & SubComponents = ({ invoices, projects, ab // setSelectedRow([]); }; + // Money format : 000,000,000.00 + const CustomMoneyFormat = useCallback((value: number) => { + if (value) { + return moneyFormatter.format(value); + } else { + return "" + } + }, []) + + const CustomMoneyComponent: React.FC = ({ params }) => { + const { id, value, field } = params; + const ref = React.useRef(); + + const handleValueChange = (newValue: NumberFormatValues) => { + apiRef.current.setEditCellValue({ id, field, value: newValue.value }); + }; + + return { + console.log(values) + handleValueChange(values) + }} + customInput={TextField} + thousandSeparator + valueIsNumericString + decimalScale={2} + fixedDecimalScale + value={value} + inputRef={ref} + InputProps={{ + sx: { + '& .MuiInputBase-input': { + textAlign: 'right', + mb: 2 + } + } + }} + />; + } + const combinedColumns = useMemo[]>( () => [ { @@ -329,7 +374,13 @@ const InvoiceSearch: React.FC & SubComponents = ({ invoices, projects, ab headerName: t("Amount (HKD)"), editable: true, flex: 0.5, - type: 'number' + type: 'number', + renderEditCell: (params: GridRenderEditCellParams) => { + return + }, + valueFormatter: (params: GridValueFormatterParams) => { + return CustomMoneyFormat(params.value as number) + } }, { field: "receiptDate", @@ -351,7 +402,13 @@ const InvoiceSearch: React.FC & SubComponents = ({ invoices, projects, ab headerName: t("Actual Received Amount (HKD)"), editable: true, flex: 0.5, - type: 'number' + type: 'number', + renderEditCell: (params: GridRenderEditCellParams) => { + return + }, + valueFormatter: (params: GridValueFormatterParams) => { + return CustomMoneyFormat(params.value as number) + } }, ], [t]