Skip to content

Commit

Permalink
Fix formatting for Date objects in the grid (#2984)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Dec 11, 2023
1 parent c2be3d9 commit b025f81
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
14 changes: 9 additions & 5 deletions packages/toolpad-components/src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ function dateValueGetter({ value }: GridValueGetterParams<any, any>): Date | und
return undefined;
}

if (value instanceof Date) {
return value;
}

if (typeof value === 'number') {
return new Date(value);
}
Expand Down Expand Up @@ -430,22 +434,22 @@ export function parseColumns(columns: SerializableGridColumns): GridColDef[] {
};
}

if (column.type === 'date' && column.dateFormat) {
if (column.type === 'date') {
const format = createDateFormat(column.dateFormat);
return {
...customType,
...column,
valueFormatter: ({ value }) => {
try {
return format.format(value);
} catch (err) {
return 'Invalid';
} catch {
return 'Invalid Date';
}
},
};
}

if (column.type === 'dateTime' && column.dateTimeFormat) {
if (column.type === 'dateTime') {
const format = createDateFormat(column.dateTimeFormat);
return {
...customType,
Expand All @@ -454,7 +458,7 @@ export function parseColumns(columns: SerializableGridColumns): GridColDef[] {
try {
return format.format(value);
} catch {
return 'Invalid';
return 'Invalid Date';
}
},
};
Expand Down
8 changes: 7 additions & 1 deletion packages/toolpad-core/src/jsBrowserRuntime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function createBrowserRuntime(): JsRuntime {
(iframe.contentWindow as any).__SCOPE = globalScope;
(iframe.contentWindow as any).console = window.console;

return (iframe.contentWindow as any).eval(`
const result = (iframe.contentWindow as any).eval(`
(() => {
// See https://tc39.es/ecma262/multipage/global-object.html#sec-global-object
const ecmaGlobals = new Set([ 'globalThis', 'Infinity', 'NaN', 'undefined', 'eval', 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'decodeURI', 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', 'AggregateError', 'Array', 'ArrayBuffer', 'BigInt', 'BigInt64Array', 'BigUint64Array', 'Boolean', 'DataView', 'Date', 'Error', 'EvalError', 'FinalizationRegistry', 'Float32Array', 'Float64Array', 'Function', 'Int8Array', 'Int16Array', 'Int32Array', 'Map', 'Number', 'Object', 'Promise', 'Proxy', 'RangeError', 'ReferenceError', 'RegExp', 'Set', 'SharedArrayBuffer', 'String', 'Symbol', 'SyntaxError', 'TypeError', 'Uint8Array', 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array', 'URIError', 'WeakMap', 'WeakRef', 'WeakSet', 'Atomics', 'JSON', 'Math', 'Reflect' ]);
Expand Down Expand Up @@ -50,6 +50,12 @@ function createBrowserRuntime(): JsRuntime {
}
})()
`);

if (typeof result?.then === 'function') {
return Promise.resolve(result).then((value) => window.structuredClone(value));
}

return window.structuredClone(result);
}

function evaluateExpression(
Expand Down
1 change: 1 addition & 0 deletions test/integration/data-grid/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ test('Date columns', async ({ page }) => {
await expect(cellLocator(canvasGridLocator, 2, 3)).toHaveText('Invalid Date');
await expect(cellLocator(canvasGridLocator, 2, 4)).toHaveText('Invalid Date');
await expect(cellLocator(canvasGridLocator, 2, 5)).toHaveText('1/1/1970');
await expect(cellLocator(canvasGridLocator, 2, 6)).toHaveText('1/1/1970');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apiVersion: v1
kind: application
spec: {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/mui/mui-toolpad/v0.1.40/docs/schemas/v1/definitions.json#properties/Page

apiVersion: v1
kind: page
spec:
id: h0FFFmL
title: columns
content:
- component: DataGrid
Expand All @@ -21,10 +22,20 @@ spec:
width: 129
- field: date5
type: date
- field: date6
type: date
rows:
- date1: 2023-04-01
date2: 1234567
date3: not a date
date4: foo 1900
date5: '1234567'
$$jsExpression: |
[
{
date1: '2023-04-01',
date2: 1234567,
date3: 'not a date',
date4: 'foo 1900',
date5: '1234567',
date6: new Date(1234567),
},
]
display: shell
alias:
- h0FFFmL
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/mui/mui-toolpad/v0.1.40/docs/schemas/v1/definitions.json#properties/Page

apiVersion: v1
kind: page
spec:
id: 331kqzd
title: Page 1
content:
- component: PageRow
Expand Down Expand Up @@ -60,3 +61,5 @@ spec:
query:
function: getUsers
kind: local
alias:
- 331kqzd

0 comments on commit b025f81

Please sign in to comment.