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

fix: allow sorting by german date format #2159

Merged
merged 8 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@types/lodash": "4.14.149",
"@types/resize-observer-browser": "^0.1.6",
"babel-loader": "^8.1.0",
"date-fns": "^2.6.0",
"date-fns": "^2.30.0",
"express": "^4.17.1",
"fast-glob": "^3.2.4",
"file-loader": "^6.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface Cell {
mobileTitle?: boolean;
resizable?: boolean;
sortable?: boolean;
sortBy?: 'number' | 'text';
sortBy?: 'number' | 'text' | 'date';
stretchWeight?: number;
textAlign?: 'left' | 'center' | 'right';
visible?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Cell } from './cell-interface';

export const DateCell: Cell = {
defaults: {
sortBy: 'text',
sortBy: 'date',
},
render: ({ content, isAutoWidthCheck }) => {
let value = content;
Expand Down
18 changes: 18 additions & 0 deletions packages/components/src/components/data-grid/data-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
import classNames from 'classnames';
import { emitEvent } from '../../utils/utils';

import { parse } from 'date-fns';

// [ ] add options to show nested content without the html column
// [ ] add options to pre-expand all html content
// [ ] Uber cell type where all options are available for user
Expand Down Expand Up @@ -413,10 +415,25 @@ export class DataGrid {
}

sortTable(sortDirection, type, columnIndex) {
const format = this.fields[columnIndex].format;
if (sortDirection === 'none') {
this.rows.sort((a, b) => {
return a.initialIndex - b.initialIndex;
});
} else if (type === 'date' && format) {
this.rows.sort((a, b) => {
const getDateObject = (dateString) => {
const parsed = parse(dateString, format, new Date());
return parsed;
};

const dateObjectA = getDateObject(a[columnIndex]);
const dateObjectB = getDateObject(b[columnIndex]);
// valueOf here for typescript to not complain about dateObjectA and dateObjectB not being numbers
return sortDirection === 'ascending'
? dateObjectA.valueOf() - dateObjectB.valueOf()
: dateObjectB.valueOf() - dateObjectA.valueOf();
});
} else {
switch (
(CELL_TYPES[type] &&
Expand All @@ -425,6 +442,7 @@ export class DataGrid {
CELL_DEFAULTS.sortBy
) {
case 'text':
case 'date':
if (sortDirection === 'ascending') {
this.rows.sort((a, b) => {
const textA = a[columnIndex].toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ Email label is partially beautified by automatically removing the protocol (`mai

Expected format: date/time `string`, eg `'10:23:00'`

##### Unique options, and their defaults

- `format?: string = 'MM.dd.yyyy'`
the date is parsed via date-fn, see [date-fn docs](https://date-fns.org/v2.30.0/docs/parse) for more info and additional formatting options, eg: `'dd.MM.yyyy'`
felix-ico marked this conversation as resolved.
Show resolved Hide resolved

<Canvas withSource="close">
<Story name="Date Cell">
{`<content>
Expand Down