Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[DataGrid] Use unformatted number and boolean values for CSV serialization #7809

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ describe('<DataGridPro /> - Export', () => {
);
}
render(<TestCaseCSVExport />);
expect(apiRef.current.getDataAsCsv()).to.equal(['id,isAdmin', '0,Yes', '1,No'].join('\r\n'));
expect(apiRef.current.getDataAsCsv()).to.equal(
['id,isAdmin', '0,true', '1,false'].join('\r\n'),
);
});

it('should warn when a value of a field is an object and no `valueFormatter` is provided', () => {
Expand Down
6 changes: 2 additions & 4 deletions packages/grid/x-data-grid/src/colDef/gridBooleanColDef.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import { gridNumberComparator } from '../hooks/features/sorting/gridSortingUtils
import { getGridBooleanOperators } from './gridBooleanOperators';
import { GridValueFormatterParams } from '../models/params/gridCellParams';

function gridBooleanFormatter({ value, api }: GridValueFormatterParams) {
return value
? api.getLocaleText('booleanCellTrueLabel')
: api.getLocaleText('booleanCellFalseLabel');
function gridBooleanFormatter({ value }: GridValueFormatterParams) {
return String(value);
}

export const GRID_BOOLEAN_COL_DEF: GridColTypeDef<boolean | null, any> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ const serializeRow = (
objectFormattedValueWarning();
}
}
return serializeCellValue(cellParams.formattedValue, delimiterCharacter);
let valueToSerialize: any;
if (column.type === 'number') {
valueToSerialize = cellParams.value;
} else {
valueToSerialize = cellParams.formattedValue;
}
return serializeCellValue(valueToSerialize, delimiterCharacter);
});

interface BuildCSVOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {
export const useGridCsvExport = (apiRef: React.MutableRefObject<GridPrivateApiCommunity>): void => {
const logger = useGridLogger(apiRef, 'useGridCsvExport');

const getDataAsCsv = React.useCallback(
(options: GridCsvExportOptions = {}): string => {
const getDataAsCsv = React.useCallback<GridCsvExportApi['getDataAsCsv']>(
(options = {}) => {
logger.debug(`Get data as CSV`);

const exportedColumns = getColumnsToExport({
Expand Down