-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-grid): add the functionality to define a custom sorting al…
…gorithm (#371)
- Loading branch information
1 parent
540fa95
commit 4ac8ab8
Showing
10 changed files
with
261 additions
and
23 deletions.
There are no files selected for viewing
38 changes: 24 additions & 14 deletions
38
packages/dx-grid-core/src/plugins/sorting-state/computeds.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,35 @@ | ||
import mergeSort from '../../utils/merge-sort'; | ||
|
||
const createSortingCompare = (sorting, compareEqual, getCellValue) => (a, b) => { | ||
const inverse = sorting.direction === 'desc'; | ||
const { columnName } = sorting; | ||
const aValue = getCellValue(a, columnName); | ||
const bValue = getCellValue(b, columnName); | ||
|
||
if (aValue === bValue) { | ||
return (compareEqual && compareEqual(a, b)) || 0; | ||
} | ||
|
||
return (aValue < bValue) ^ inverse ? -1 : 1; // eslint-disable-line no-bitwise | ||
const defaultCompare = (a, b) => { | ||
if (a < b) return -1; | ||
if (a > b) return 1; | ||
return 0; | ||
}; | ||
|
||
export const sortedRows = (rows, sorting, getCellValue) => { | ||
export const sortedRows = (rows, sorting, getCellValue, getColumnCompare) => { | ||
if (!sorting.length) return rows; | ||
|
||
const compare = Array.from(sorting) | ||
.reverse() | ||
.reduce((prevCompare, columnSorting) => | ||
createSortingCompare(columnSorting, prevCompare, getCellValue), () => 0); | ||
.reduce( | ||
(prevCompare, columnSorting) => { | ||
const { columnName } = columnSorting; | ||
const inverse = columnSorting.direction === 'desc'; | ||
const columnCompare = (getColumnCompare && getColumnCompare(columnName)) || defaultCompare; | ||
|
||
return (aRow, bRow) => { | ||
const a = getCellValue(aRow, columnName); | ||
const b = getCellValue(bRow, columnName); | ||
const result = columnCompare(a, b); | ||
|
||
if (result !== 0) { | ||
return inverse ? -result : result; | ||
} | ||
return prevCompare(aRow, bRow); | ||
}; | ||
}, | ||
() => 0, | ||
); | ||
|
||
return mergeSort(Array.from(rows), compare); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/dx-react-demos/src/bootstrap3/sorting/local-custom-sorting.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from 'react'; | ||
import { | ||
SortingState, | ||
LocalSorting, | ||
} from '@devexpress/dx-react-grid'; | ||
import { | ||
Grid, | ||
TableView, | ||
TableHeaderRow, | ||
} from '@devexpress/dx-react-grid-bootstrap3'; | ||
|
||
import { | ||
generateRows, | ||
employeeTaskValues, | ||
} from '../../demo-data/generator'; | ||
|
||
const priorityWeights = { | ||
Low: 0, | ||
Normal: 1, | ||
High: 2, | ||
}; | ||
|
||
const comparePriority = (a, b) => { | ||
const priorityA = priorityWeights[a]; | ||
const priorityB = priorityWeights[b]; | ||
if (priorityA === priorityB) { | ||
return 0; | ||
} | ||
return (priorityA < priorityB) ? -1 : 1; | ||
}; | ||
|
||
const getColumnCompare = columnName => | ||
(columnName === 'priority' ? comparePriority : undefined); | ||
|
||
export default class Demo extends React.PureComponent { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
columns: [ | ||
{ name: 'subject', title: 'Subject', width: 300 }, | ||
{ name: 'startDate', title: 'Start Date' }, | ||
{ name: 'dueDate', title: 'Due Date' }, | ||
{ name: 'priority', title: 'Priority' }, | ||
], | ||
rows: generateRows({ | ||
columnValues: employeeTaskValues, | ||
length: 15, | ||
}), | ||
}; | ||
} | ||
render() { | ||
const { rows, columns } = this.state; | ||
|
||
return ( | ||
<Grid | ||
rows={rows} | ||
columns={columns} | ||
> | ||
<SortingState /> | ||
<LocalSorting | ||
getColumnCompare={getColumnCompare} | ||
/> | ||
<TableView /> | ||
<TableHeaderRow allowSorting /> | ||
</Grid> | ||
); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/dx-react-demos/src/bootstrap3/sorting/local-custom-sorting.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import Demo from './local-custom-sorting'; | ||
|
||
describe('BS3 sorting: custom local sorting demo', () => { | ||
it('should work', () => { | ||
mount( | ||
<Demo />, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/dx-react-demos/src/material-ui/sorting/local-custom-sorting.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from 'react'; | ||
import { | ||
SortingState, | ||
LocalSorting, | ||
} from '@devexpress/dx-react-grid'; | ||
import { | ||
Grid, | ||
TableView, | ||
TableHeaderRow, | ||
} from '@devexpress/dx-react-grid-material-ui'; | ||
|
||
import { | ||
generateRows, | ||
employeeTaskValues, | ||
} from '../../demo-data/generator'; | ||
|
||
const priorityWeights = { | ||
Low: 0, | ||
Normal: 1, | ||
High: 2, | ||
}; | ||
|
||
const comparePriority = (a, b) => { | ||
const priorityA = priorityWeights[a]; | ||
const priorityB = priorityWeights[b]; | ||
if (priorityA === priorityB) { | ||
return 0; | ||
} | ||
return (priorityA < priorityB) ? -1 : 1; | ||
}; | ||
|
||
const getColumnCompare = columnName => | ||
(columnName === 'priority' ? comparePriority : undefined); | ||
|
||
export default class Demo extends React.PureComponent { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
columns: [ | ||
{ name: 'subject', title: 'Subject', width: 300 }, | ||
{ name: 'startDate', title: 'Start Date' }, | ||
{ name: 'dueDate', title: 'Due Date' }, | ||
{ name: 'priority', title: 'Priority' }, | ||
], | ||
rows: generateRows({ | ||
columnValues: employeeTaskValues, | ||
length: 15, | ||
}), | ||
}; | ||
} | ||
render() { | ||
const { rows, columns } = this.state; | ||
|
||
return ( | ||
<Grid | ||
rows={rows} | ||
columns={columns} | ||
> | ||
<SortingState /> | ||
<LocalSorting | ||
getColumnCompare={getColumnCompare} | ||
/> | ||
<TableView /> | ||
<TableHeaderRow allowSorting /> | ||
</Grid> | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/dx-react-demos/src/material-ui/sorting/local-custom-sorting.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import injectTapEventPlugin from 'react-tap-event-plugin'; | ||
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles'; | ||
import Demo from './local-custom-sorting'; | ||
|
||
injectTapEventPlugin(); | ||
|
||
describe('MUI sorting: custom local sorting demo', () => { | ||
it('should work', () => { | ||
mount( | ||
<MuiThemeProvider theme={createMuiTheme()}> | ||
<Demo /> | ||
</MuiThemeProvider>, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters