浏览代码

update last item

feature/axios_provider
MSI\derek 5 个月前
父节点
当前提交
c55ec75a24
共有 5 个文件被更改,包括 113 次插入80 次删除
  1. +62
    -38
      src/components/CreateMaterial/MaterialDetails.tsx
  2. +50
    -41
      src/components/InputDataGrid/InputDataGrid.tsx
  3. +0
    -0
      src/components/InputDataGrid/backup
  4. +1
    -0
      src/components/InputDataGrid/index.ts
  5. +0
    -1
      src/components/useInputDataGrid/index.ts

+ 62
- 38
src/components/CreateMaterial/MaterialDetails.tsx 查看文件

@@ -11,19 +11,21 @@ import {
} from "@mui/material"; } from "@mui/material";
import { useFormContext } from "react-hook-form"; import { useFormContext } from "react-hook-form";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import useInputDataGrid from "../useInputDataGrid";
import { useMemo, useState } from "react";
import { GridColDef } from "@mui/x-data-grid";
import InputDataGrid from "../InputDataGrid";
import { useCallback, useMemo, useState } from "react";
import { GridColDef, GridRowModel } from "@mui/x-data-grid";
import { import {
InputGridProps,
InputDataGridProps, InputDataGridProps,
} from "../useInputDataGrid/useInputDataGrid";
TableRow,
} from "../InputDataGrid/InputDataGrid";
type Props = { type Props = {
isEditMode: boolean; isEditMode: boolean;
}; };
export type EntryError = {
[field in keyof CreateMaterialInputs]?: string;
};
export type EntryError =
| {
[field in keyof CreateMaterialInputs]?: string;
}
| undefined;


const MaterialDetails: React.FC<Props> = ({ isEditMode }) => { const MaterialDetails: React.FC<Props> = ({ isEditMode }) => {
const { const {
@@ -43,35 +45,51 @@ const MaterialDetails: React.FC<Props> = ({ isEditMode }) => {
setError, setError,
clearErrors, clearErrors,
} = useFormContext<CreateMaterialInputs>(); } = useFormContext<CreateMaterialInputs>();
const columns = useMemo<GridColDef[]>(() => [], []);
const validationTest = (): EntryError => {
const typeColumns = useMemo<GridColDef[]>(() => [
{
field: "type",
headerName: 'type',
flex: 1,
editable: true,
}
], []);
const uomColumns = useMemo<GridColDef[]>(() => [
{
field: "uom",
headerName: 'uom',
flex: 1,
editable: true,
}
], []);
const validationTest = useCallback((newRow: GridRowModel<TableRow<Partial<CreateMaterialInputs>, EntryError>>): EntryError => {
const error: EntryError = {}; const error: EntryError = {};
return error;
};
const MaterialTypeInputGridProps: InputDataGridProps<
Partial<CreateMaterialInputs>,
EntryError
> = {
_formKey: "type",
columns,
validation: validationTest,
};
const MaterialUomInputGridProps: InputDataGridProps<
Partial<CreateMaterialInputs>,
EntryError
> = {
_formKey: "uom",
columns,
validation: validationTest,
};
const { DataGrid: MaterialTypeInputGrid } = useInputDataGrid(
MaterialTypeInputGridProps
);
const { DataGrid: MaterialUomInputGrid } = useInputDataGrid(
MaterialUomInputGridProps
);
console.log(newRow)
return Object.keys(error).length > 0 ? error : undefined;
// return undefined
}, []);
// const MaterialTypeInputGridProps: InputDataGridProps<
// Partial<CreateMaterialInputs>,
// EntryError
// > = {
// _formKey: "type",
// columns: typeColumns,
// validateRow: validationTest,
// };
// const MaterialUomInputGridProps: InputDataGridProps<
// Partial<CreateMaterialInputs>,
// EntryError
// > = {
// _formKey: "uom",
// columns: uomColumns,
// validateRow: validationTest,
// };
// const MaterialTypeInputGrid = useInputDataGrid(
// MaterialTypeInputGridProps
// );
// const MaterialUomInputGrid = useInputDataGrid(
// MaterialUomInputGridProps
// );


const test: InputGridProps = {}


return ( return (
<Card sx={{ display: "block" }}> <Card sx={{ display: "block" }}>
@@ -213,12 +231,18 @@ const MaterialDetails: React.FC<Props> = ({ isEditMode }) => {
/> />
</Grid> </Grid>
<Grid item xs={6}> <Grid item xs={6}>
<MaterialTypeInputGrid
// {...test}
<InputDataGrid<CreateMaterialInputs, EntryError>
_formKey={"type"}
columns={typeColumns}
validateRow={validationTest}
/> />
</Grid> </Grid>
<Grid item xs={6}> <Grid item xs={6}>
<MaterialUomInputGrid />
<InputDataGrid<CreateMaterialInputs, EntryError>
_formKey={"uom"}
columns={uomColumns}
validateRow={validationTest}
/>
</Grid> </Grid>
</Grid> </Grid>
</Box> </Box>


src/components/useInputDataGrid/useInputDataGrid.tsx → src/components/InputDataGrid/InputDataGrid.tsx 查看文件

@@ -1,3 +1,4 @@
"use client"
import { import {
Dispatch, Dispatch,
SetStateAction, SetStateAction,
@@ -12,6 +13,8 @@ import {
GridActionsCellItem, GridActionsCellItem,
GridCellParams, GridCellParams,
GridColDef, GridColDef,
GridEventListener,
GridRowEditStopReasons,
GridRowId, GridRowId,
GridRowIdGetter, GridRowIdGetter,
GridRowModel, GridRowModel,
@@ -32,10 +35,10 @@ import { useTranslation } from "react-i18next";
interface ResultWithId { interface ResultWithId {
id: string | number; id: string | number;
} }
export type InputGridProps = {
[key: string]: any
}
type TableRow<T, E> = Partial<
// export type InputGridProps = {
// [key: string]: any
// }
export type TableRow<T, E> = Partial<
T & { T & {
_isNew: boolean; _isNew: boolean;
_error: E; _error: E;
@@ -45,7 +48,7 @@ type TableRow<T, E> = Partial<
export type InputDataGridProps<T, E> = { export type InputDataGridProps<T, E> = {
_formKey: keyof T; _formKey: keyof T;
columns: GridColDef[]; columns: GridColDef[];
validation: () => E;
validateRow: (newRow: GridRowModel<TableRow<T, E>>) => E;
}; };


export class ProcessRowUpdateError<T, E> extends Error { export class ProcessRowUpdateError<T, E> extends Error {
@@ -60,15 +63,11 @@ export class ProcessRowUpdateError<T, E> extends Error {
} }
} }


function useInputDataGrid<T, E>({
function InputDataGrid<T, E>({
_formKey, _formKey,
columns, columns,
validation,
}: InputDataGridProps<T, E>): {
DataGrid: React.FC;
rowModesModel: GridRowModesModel;
setRowModesModel: Dispatch<SetStateAction<GridRowModesModel>>;
} {
validateRow,
}: InputDataGridProps<T, E>) {
const { const {
t, t,
// i18n: { language }, // i18n: { language },
@@ -96,7 +95,7 @@ function useInputDataGrid<T, E>({
[id]: { mode: GridRowModes.View }, [id]: { mode: GridRowModes.View },
})); }));
}, },
[setRowModesModel]
[]
); );
const onProcessRowUpdateError = useCallback( const onProcessRowUpdateError = useCallback(
(updateError: ProcessRowUpdateError<T, E>) => { (updateError: ProcessRowUpdateError<T, E>) => {
@@ -108,14 +107,18 @@ function useInputDataGrid<T, E>({
[apiRef, rowModesModel] [apiRef, rowModesModel]
); );


console.log("asdasdasd")
const processRowUpdate = useCallback( const processRowUpdate = useCallback(
( (
newRow: GridRowModel<TableRow<T, E>>,
originalRow: GridRowModel<TableRow<T, E>>
newRow: GridValidRowModel,
originalRow: GridValidRowModel,
// GridRowModel<TableRow<T, E>>,
// originalRow: GridRowModel<TableRow<T, E>>
) => { ) => {
///////////////// /////////////////
// validation here // validation here
// const errors = validation(); //repace true with validation method
// const errors = validateRow(newRow);
console.log(newRow)
// if (errors) { // if (errors) {
// throw new ProcessRowUpdateError( // throw new ProcessRowUpdateError(
// originalRow, // originalRow,
@@ -128,19 +131,18 @@ function useInputDataGrid<T, E>({
const rowToSave = { const rowToSave = {
...updatedRow, ...updatedRow,
} as TableRow<T, E>; /// test } as TableRow<T, E>; /// test
setRows((rw) =>
rw.map((r) => (getRowId(r) === getRowId(originalRow) ? rowToSave : r))
);
console.log(rowToSave)
// setRows((rw) =>
// rw.map((r) => (getRowId(r) === getRowId(originalRow) ? rowToSave : r))
// );
return rowToSave; return rowToSave;
}, },
[rows]
[validateRow, getRowId]
); );


const addRow = useCallback(() => { const addRow = useCallback(() => {
const newEntry = { id: Date.now(), _isNew: true } as TableRow<T, E>; const newEntry = { id: Date.now(), _isNew: true } as TableRow<T, E>;
setRows((prev) => [...prev, newEntry]); setRows((prev) => [...prev, newEntry]);
// console.log(newEntry)
setRowModesModel((model) => ({ setRowModesModel((model) => ({
...model, ...model,
[getRowId(newEntry)]: { [getRowId(newEntry)]: {
@@ -173,18 +175,14 @@ function useInputDataGrid<T, E>({
}, },
[setRowModesModel, rows] [setRowModesModel, rows]
); );
useEffect(() => {
console.log(rows)
}, [rows])
const handleDelete = useCallback( const handleDelete = useCallback(
(id: GridRowId) => () => { (id: GridRowId) => () => {
setRows((prevRows) => prevRows.filter((row) => getRowId(row) !== id)); setRows((prevRows) => prevRows.filter((row) => getRowId(row) !== id));
}, },
[] []
); );
if (columns) {


}
const _columns = useMemo<GridColDef[]>( const _columns = useMemo<GridColDef[]>(
() => [ () => [
...columns, ...columns,
@@ -230,20 +228,29 @@ function useInputDataGrid<T, E>({
}, },
}, },
], ],
[rowModesModel, handleSave, handleCancel, handleDelete]
[columns,rowModesModel, handleSave, handleCancel, handleDelete]
); );


// const processRowUpdate = (newRow: GridRowModel) => {
// const updatedRow = { ...newRow, isNew: false };
// console.log("asdasdasdsda")
// // setRows(rows.map((row) => (row.id === newRow.id ? updatedRow : row)));
// return updatedRow;
// };

// sync useForm // sync useForm
useEffect(() => { useEffect(() => {
console.log(formKey)
console.log(rows)
setValue(formKey, rows); setValue(formKey, rows);
}, [rows]);
}, [formKey, rows]);


const footer = ( const footer = (
<Box display="flex" gap={2} alignItems="center"> <Box display="flex" gap={2} alignItems="center">
<Button <Button
disableRipple disableRipple
variant="outlined" variant="outlined"
startIcon={<Add />}
startIcon={<Add />}
onClick={addRow} onClick={addRow}
size="small" size="small"
> >
@@ -260,10 +267,16 @@ function useInputDataGrid<T, E>({
</Button> </Button>
</Box> </Box>
); );
const DataGrid: React.FC<InputGridProps> = (props) => (
// const handleRowEditStop: GridEventListener<'rowEditStop'> = (params, event) => {
// if (params.reason === GridRowEditStopReasons.rowFocusOut) {
// event.defaultMuiPrevented = true;
// }
// };

return (
<StyledDataGrid <StyledDataGrid
{...props}
getRowId={getRowId as GridRowIdGetter<GridValidRowModel>}
// {...props}
// getRowId={getRowId as GridRowIdGetter<GridValidRowModel>}
apiRef={apiRef} apiRef={apiRef}
rows={rows} rows={rows}
columns={_columns} columns={_columns}
@@ -281,9 +294,10 @@ function useInputDataGrid<T, E>({
}, },
}} }}
disableColumnMenu disableColumnMenu
processRowUpdate={processRowUpdate}
// onRowEditStop={handleRowEditStop}
rowModesModel={rowModesModel} rowModesModel={rowModesModel}
onRowModesModelChange={setRowModesModel} onRowModesModelChange={setRowModesModel}
processRowUpdate={processRowUpdate as any}
onProcessRowUpdateError={onProcessRowUpdateError} onProcessRowUpdateError={onProcessRowUpdateError}
getCellClassName={(params: GridCellParams<TableRow<T, E>>) => { getCellClassName={(params: GridCellParams<TableRow<T, E>>) => {
let classname = ""; let classname = "";
@@ -300,12 +314,7 @@ function useInputDataGrid<T, E>({
footer: { child: footer }, footer: { child: footer },
}} }}
/> />
);
return {
DataGrid,
rowModesModel,
setRowModesModel,
};
)
} }
const FooterToolbar: React.FC<FooterPropsOverrides> = ({ child }) => { const FooterToolbar: React.FC<FooterPropsOverrides> = ({ child }) => {
return <GridToolbarContainer sx={{ p: 2 }}>{child}</GridToolbarContainer>; return <GridToolbarContainer sx={{ p: 2 }}>{child}</GridToolbarContainer>;
@@ -323,4 +332,4 @@ const NoRowsOverlay: React.FC = () => {
</Box> </Box>
); );
}; };
export default useInputDataGrid;
export default InputDataGrid;

src/components/useInputDataGrid/backup → src/components/InputDataGrid/backup 查看文件


+ 1
- 0
src/components/InputDataGrid/index.ts 查看文件

@@ -0,0 +1 @@
export { default } from "./InputDataGrid";

+ 0
- 1
src/components/useInputDataGrid/index.ts 查看文件

@@ -1 +0,0 @@
export { default } from "./useInputDataGrid";

正在加载...
取消
保存