{
diff --git a/packages/grid/_modules_/grid/models/gridExport.ts b/packages/grid/_modules_/grid/models/gridExport.ts
index fbd0fee21f3f2..f7d1bd8881035 100644
--- a/packages/grid/_modules_/grid/models/gridExport.ts
+++ b/packages/grid/_modules_/grid/models/gridExport.ts
@@ -23,6 +23,16 @@ export interface GridExportCsvOptions {
* @default false
*/
utf8WithBom?: boolean;
+ /**
+ * The columns exported in the CSV.
+ * This should only be used if you want to restrict the columns exports.
+ */
+ fields?: string[];
+ /**
+ * If `true`, the hidden columns will also be exported.
+ * @default false
+ */
+ allColumns?: boolean;
}
/**
diff --git a/packages/grid/x-grid/src/tests/export.XGrid.test.tsx b/packages/grid/x-grid/src/tests/export.XGrid.test.tsx
index b23c58c1fff1d..cdfe15d860cab 100644
--- a/packages/grid/x-grid/src/tests/export.XGrid.test.tsx
+++ b/packages/grid/x-grid/src/tests/export.XGrid.test.tsx
@@ -1,4 +1,4 @@
-import { GridApiRef, useGridApiRef, XGrid } from '@material-ui/x-grid';
+import { GridApiRef, GridColumns, useGridApiRef, XGrid } from '@material-ui/x-grid';
import { expect } from 'chai';
import * as React from 'react';
import { createClientRenderStrictMode } from 'test/utils';
@@ -15,177 +15,341 @@ describe(' - Export', () => {
let apiRef: GridApiRef;
- const columns = [{ field: 'id' }, { field: 'brand', headerName: 'Brand' }];
-
- it('getDataAsCsv should work with basic strings', () => {
- const TestCaseCSVExport = () => {
- apiRef = useGridApiRef();
- return (
-
-
-
- );
- };
-
- render();
- expect(apiRef.current.getDataAsCsv()).to.equal(
- ['id,Brand', '0,Nike', '1,Adidas', '2,Puma'].join('\r\n'),
- );
- apiRef.current.updateRows([
- {
- id: 1,
- brand: 'Adidas,Reebok',
- },
- ]);
- expect(apiRef.current.getDataAsCsv()).to.equal(
- ['id,Brand', '0,Nike', '1,"Adidas,Reebok"', '2,Puma'].join('\r\n'),
- );
- });
+ const columns: GridColumns = [{ field: 'id' }, { field: 'brand', headerName: 'Brand' }];
+
+ describe('getDataAsCsv', () => {
+ it('should work with basic strings', () => {
+ const TestCaseCSVExport = () => {
+ apiRef = useGridApiRef();
+ return (
+
+
+
+ );
+ };
- it('getDataAsCsv should work with comma', () => {
- const TestCaseCSVExport = () => {
- apiRef = useGridApiRef();
- return (
-
-
-
+ render();
+ expect(apiRef.current.getDataAsCsv()).to.equal(
+ ['id,Brand', '0,Nike', '1,Adidas', '2,Puma'].join('\r\n'),
);
- };
+ apiRef.current.updateRows([
+ {
+ id: 1,
+ brand: 'Adidas,Reebok',
+ },
+ ]);
+ expect(apiRef.current.getDataAsCsv()).to.equal(
+ ['id,Brand', '0,Nike', '1,"Adidas,Reebok"', '2,Puma'].join('\r\n'),
+ );
+ });
- render();
- expect(apiRef.current.getDataAsCsv()).to.equal(
- ['id,Brand', '0,Nike', '1,"Adidas,Puma"'].join('\r\n'),
- );
- });
+ it('should work with comma', () => {
+ const TestCaseCSVExport = () => {
+ apiRef = useGridApiRef();
+ return (
+
+
+
+ );
+ };
- it('getDataAsCsv should apply valueFormatter correctly', () => {
- const TestCaseCSVExport = () => {
- apiRef = useGridApiRef();
- return (
-
- (params.value === 'Nike' ? 'Jordan' : params.value),
- },
- ]}
- rows={[
- {
- id: 0,
- brand: 'Nike',
- },
- {
- id: 1,
- brand: 'Adidas',
- },
- ]}
- />
-
+ render();
+ expect(apiRef.current.getDataAsCsv()).to.equal(
+ ['id,Brand', '0,Nike', '1,"Adidas,Puma"'].join('\r\n'),
);
- };
+ });
- render();
- expect(apiRef.current.getDataAsCsv()).to.equal(
- ['id,Brand', '0,Jordan', '1,Adidas'].join('\r\n'),
- );
- });
+ it('should apply valueFormatter correctly', () => {
+ const TestCaseCSVExport = () => {
+ apiRef = useGridApiRef();
+ return (
+
+ (params.value === 'Nike' ? 'Jordan' : params.value),
+ },
+ ]}
+ rows={[
+ {
+ id: 0,
+ brand: 'Nike',
+ },
+ {
+ id: 1,
+ brand: 'Adidas',
+ },
+ ]}
+ />
+
+ );
+ };
- it('getDataAsCsv should work with double quotes', () => {
- const TestCaseCSVExport = () => {
- apiRef = useGridApiRef();
- return (
-
-
-
+ render();
+ expect(apiRef.current.getDataAsCsv()).to.equal(
+ ['id,Brand', '0,Jordan', '1,Adidas'].join('\r\n'),
);
- };
+ });
- render();
- expect(apiRef.current.getDataAsCsv()).to.equal(
- ['id,Brand', '0,Nike', '1,Samsung 24"" (inches)'].join('\r\n'),
- );
- });
+ it('should work with double quotes', () => {
+ const TestCaseCSVExport = () => {
+ apiRef = useGridApiRef();
+ return (
+
+
+
+ );
+ };
- it('getDataAsCsv should allow to change the delimiter', () => {
- const TestCaseCSVExport = () => {
- apiRef = useGridApiRef();
- return (
-
-
-
+ render();
+ expect(apiRef.current.getDataAsCsv()).to.equal(
+ ['id,Brand', '0,Nike', '1,Samsung 24"" (inches)'].join('\r\n'),
);
- };
-
- render();
- expect(
- apiRef.current.getDataAsCsv({
- delimiter: ';',
- }),
- ).to.equal(['id;Brand', '0;Nike', '1;Adidas'].join('\r\n'));
+ });
+
+ it('should allow to change the delimiter', () => {
+ const TestCaseCSVExport = () => {
+ apiRef = useGridApiRef();
+ return (
+
+
+
+ );
+ };
+
+ render();
+ expect(
+ apiRef.current.getDataAsCsv({
+ delimiter: ';',
+ }),
+ ).to.equal(['id;Brand', '0;Nike', '1;Adidas'].join('\r\n'));
+ });
+
+ it('should not export hidden column', () => {
+ const TestCaseCSVExport = () => {
+ apiRef = useGridApiRef();
+ return (
+
+
+
+ );
+ };
+
+ render();
+ expect(apiRef.current.getDataAsCsv()).to.equal(['id', '0', '1'].join('\r\n'));
+ });
+
+ it('should export hidden column if params.allColumns = true', () => {
+ const TestCaseCSVExport = () => {
+ apiRef = useGridApiRef();
+ return (
+
+
+
+ );
+ };
+
+ render();
+ expect(
+ apiRef.current.getDataAsCsv({
+ allColumns: true,
+ }),
+ ).to.equal(['id,Brand', '0,Nike', '1,Adidas'].join('\r\n'));
+ });
+
+ it('should not export columns with column.disableExport = true', () => {
+ const TestCaseCSVExport = () => {
+ apiRef = useGridApiRef();
+ return (
+
+
+
+ );
+ };
+
+ render();
+ expect(
+ apiRef.current.getDataAsCsv({
+ fields: ['brand'],
+ }),
+ ).to.equal(['Brand', 'Nike', 'Adidas'].join('\r\n'));
+ });
+
+ it('should only export columns in params.fields if defined', () => {
+ const TestCaseCSVExport = () => {
+ apiRef = useGridApiRef();
+ return (
+
+
+
+ );
+ };
+
+ render();
+ expect(
+ apiRef.current.getDataAsCsv({
+ fields: ['brand'],
+ }),
+ ).to.equal(['Brand', 'Nike', 'Adidas'].join('\r\n'));
+ });
+
+ it('should export column defined in params.fields even if column.hide=true or column.disableExport=true', () => {
+ const TestCaseCSVExport = () => {
+ apiRef = useGridApiRef();
+ return (
+
+
+
+ );
+ };
+
+ render();
+ expect(
+ apiRef.current.getDataAsCsv({
+ fields: ['id', 'brand'],
+ }),
+ ).to.equal(['id,Brand', '0,Nike', '1,Adidas'].join('\r\n'));
+ });
});
});