Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[data grid] Column definition undefined value #14446

Closed
oliviertassinari opened this issue Sep 1, 2024 · 2 comments · Fixed by #14456 · May be fixed by #15366
Closed

[data grid] Column definition undefined value #14446

oliviertassinari opened this issue Sep 1, 2024 · 2 comments · Fixed by #14456 · May be fixed by #15366
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module!

Comments

@oliviertassinari
Copy link
Member

oliviertassinari commented Sep 1, 2024

Steps to reproduce

Link to live example:

import * as React from "react";
import { DataGridPro } from "@mui/x-data-grid-pro";

export default function HeaderFilteringDataGridPro() {
  return (
    <div style={{ height: 400, width: "100%" }}>
      <DataGridPro
        columns={[
          {
            field: "actions",
            headerName: "Actions",
            type: "actions",
            width: 80,
            filterOperators: undefined, // <- the issue is here
            getActions: (params) => [<>action</>],
          },
        ]}
        headerFilters
      />
    </div>
  );
}

https://codesandbox.io/p/sandbox/wispy-paper-mfjzqs?file=%2Fsrc%2FDemo.tsx%3A1%2C1-23%2C1

Current behavior

It crashes

SCR-20240901-ovmg

filterOperators: undefined overrides the default values of the column type. It likely does the same with a bunch of other properties.

Expected behavior

It can be solved in two different ways. TypeScript flags filterOperators: undefined, as invalid. It's this issue: microsoft/TypeScript#13195 and this Stack Overflow question: https://stackoverflow.com/questions/67840542/object-with-optional-keys-but-with-a-mandatory-value-in-typescript. It can be enabled with https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes, but I suspect most projects are not using it, e.g. TypeScript maintainers consider it too much of a pain to have it in strict: microsoft/TypeScript#44421 (comment). So our only option seems to be ⬇️

Or the grid sets the default value for undefined values, like components do e.g.

function test(props) {
  const {
    open = true,
  } = props;
  console.log(open);
}
test({ open : true });
test({ open : false });
test({ open : null });
test({ open : undefined });
diff --git a/packages/x-data-grid/src/hooks/features/columns/gridColumnsUtils.ts b/packages/x-data-grid/src/hooks/features/columns/gridColumnsUtils.ts
index cd3c0d8d4..e9b3185cf 100644
--- a/packages/x-data-grid/src/hooks/features/columns/gridColumnsUtils.ts
+++ b/packages/x-data-grid/src/hooks/features/columns/gridColumnsUtils.ts
@@ -1,4 +1,5 @@
 import * as React from 'react';
+import resolveProps from '@mui/utils/resolveProps';
 import {
   GridColumnLookup,
   GridColumnsState,
@@ -379,11 +380,12 @@ export const createColumnsState = ({
       }
     });

-    columnsState.lookup[field] = {
-      ...existingState,
-      ...newColumn,
-      hasBeenResized,
-    };
+    columnsState.lookup[field] = resolveProps(existingState, { ...newColumn, hasBeenResized });
   });

Context

I saw this in https://mui.zendesk.com/agent/tickets/19977.

Your environment

"@mui/x-data-grid-pro": "7.15.0",

Search keywords: -

@oliviertassinari oliviertassinari added bug 🐛 Something doesn't work status: waiting for maintainer These issues haven't been looked at yet by a maintainer component: data grid This is the name of the generic UI component, not the React module! labels Sep 1, 2024
@michelengelen michelengelen changed the title [data grid] Column definition undefined value [data grid] Column definition undefined value Sep 2, 2024
@michelengelen michelengelen removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 2, 2024
@github-project-automation github-project-automation bot moved this to 🆕 Needs refinement in MUI X Data Grid Sep 2, 2024
@michelengelen
Copy link
Member

Thanks for opening a PR @sai6855 👍🏼

Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module!
Projects
Status: Done
2 participants