-
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.
refactor(rect-grid): refactor table column reordering (#424)
fixes #154 BREAKING CHANGES: - The `ColumnOrderState` plugin has been renamed to `TableColumnReordering` and is now available via the `@devexpress/dx-react-grid-bootstrap3` and `@devexpress/dx-react-grid-material-ui` packages. - The `TableView` plugin's `allowColumnReordering` property has been removed and the `TableColumnReordering` plugin now depends on the `TableView` plugin. Thus, it is enough to link the `TableColumnReordering` plugin below the `TableView` plugin to enable column reordering. Before: ```jsx import { // ... ColumnOrderState } from '@devexpress/dx-react-grid'; // ... <ColumnOrderState defaultOrder={[/* ... */]} /> <TableView allowColumnReordering /> ``` After: ```jsx import { // ... TableColumnReordering } from '@devexpress/dx-react-grid-bootstrap3'; // } from '@devexpress/dx-react-grid-material-ui'; // ... <TableView /> <TableColumnReordering defaultOrder={[/* ... */]} /> ```
- Loading branch information
Showing
54 changed files
with
1,342 additions
and
829 deletions.
There are no files selected for viewing
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
11 changes: 0 additions & 11 deletions
11
packages/dx-grid-core/src/plugins/column-order-state/computeds.js
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
packages/dx-grid-core/src/plugins/column-order-state/computeds.test.js
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
packages/dx-grid-core/src/plugins/table-column-reordering/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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { TABLE_DATA_TYPE } from '../table-view/constants'; | ||
import { TABLE_REORDERING_TYPE } from './constants'; | ||
|
||
export const orderedColumns = (tableColumns, order) => { | ||
const result = Array.from(tableColumns); | ||
|
||
result.sort((a, b) => { | ||
if (a.type !== TABLE_DATA_TYPE || b.type !== TABLE_DATA_TYPE) return 0; | ||
|
||
const aPos = order.indexOf(a.column.name); | ||
const bPos = order.indexOf(b.column.name); | ||
return aPos - bPos; | ||
}); | ||
|
||
return result; | ||
}; | ||
|
||
export const tableHeaderRowsWithReordering = tableHeaderRows => [ | ||
{ | ||
key: TABLE_REORDERING_TYPE, | ||
type: TABLE_REORDERING_TYPE, | ||
height: 0, | ||
}, | ||
...tableHeaderRows, | ||
]; | ||
|
||
export const draftOrder = (order, sourceColumnIndex, targetColumnIndex) => { | ||
if (sourceColumnIndex === -1 | ||
|| targetColumnIndex === -1 | ||
|| sourceColumnIndex === targetColumnIndex) { | ||
return order; | ||
} | ||
|
||
const result = order.slice(); | ||
const sourceColumn = order[sourceColumnIndex]; | ||
result.splice(sourceColumnIndex, 1); | ||
result.splice(targetColumnIndex, 0, sourceColumn); | ||
|
||
return result; | ||
}; |
105 changes: 105 additions & 0 deletions
105
packages/dx-grid-core/src/plugins/table-column-reordering/computeds.test.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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import Immutable from 'seamless-immutable'; | ||
import { TABLE_DATA_TYPE } from '../table-view/constants'; | ||
import { TABLE_REORDERING_TYPE } from './constants'; | ||
import { | ||
orderedColumns, | ||
tableHeaderRowsWithReordering, | ||
draftOrder, | ||
} from './computeds'; | ||
|
||
describe('TableColumnReordering computeds', () => { | ||
describe('#orderedColumns', () => { | ||
it('should return columns in the order specified', () => { | ||
const tableColumns = [ | ||
{ type: TABLE_DATA_TYPE, column: { name: 'a' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'b', payload: {} } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'c' } }, | ||
]; | ||
const order = ['b', 'a', 'c']; | ||
|
||
const computed = orderedColumns(tableColumns, order); | ||
expect(computed) | ||
.toEqual([ | ||
{ type: TABLE_DATA_TYPE, column: { name: 'b', payload: {} } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'a' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'c' } }, | ||
]); | ||
expect(computed === tableColumns) | ||
.toBeFalsy(); | ||
}); | ||
|
||
it('should work with immutable columns', () => { | ||
const tableColumns = Immutable([ | ||
{ type: TABLE_DATA_TYPE, column: { name: 'a' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'b' } }, | ||
]); | ||
|
||
expect(orderedColumns(tableColumns, ['b', 'a'])) | ||
.toEqual([ | ||
{ type: TABLE_DATA_TYPE, column: { name: 'b' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'a' } }, | ||
]); | ||
}); | ||
|
||
it('should work correctly with non-data columns', () => { | ||
const tableColumns = [ | ||
{ type: 'test', column: { name: 'a' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'b' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'c' } }, | ||
]; | ||
|
||
expect(orderedColumns(tableColumns, ['c', 'a', 'b'])) | ||
.toEqual([ | ||
{ type: 'test', column: { name: 'a' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'c' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'b' } }, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('#tableHeaderRowsWithReordering', () => { | ||
it('should add a reordering row to the rows passed', () => { | ||
expect(tableHeaderRowsWithReordering([])) | ||
.toEqual([{ | ||
key: TABLE_REORDERING_TYPE, | ||
type: TABLE_REORDERING_TYPE, | ||
height: 0, | ||
}]); | ||
}); | ||
}); | ||
|
||
describe('#draftOrder', () => { | ||
it('should return reordered columns', () => { | ||
const columns = [ | ||
{ type: TABLE_DATA_TYPE, column: { name: 'a' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'b' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'c' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'd' } }, | ||
]; | ||
|
||
expect(draftOrder(columns, 1, 3)) | ||
.toEqual([ | ||
{ type: TABLE_DATA_TYPE, column: { name: 'a' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'c' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'd' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'b' } }, | ||
]); | ||
}); | ||
|
||
it('should return the array passed if no changes are possble', () => { | ||
const columns = [ | ||
{ type: TABLE_DATA_TYPE, column: { name: 'a' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'b' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'c' } }, | ||
{ type: TABLE_DATA_TYPE, column: { name: 'd' } }, | ||
]; | ||
|
||
expect(draftOrder(columns, -1, -1)) | ||
.toBe(columns); | ||
expect(draftOrder(columns, -1, 1)) | ||
.toBe(columns); | ||
expect(draftOrder(columns, 1, 1)) | ||
.toBe(columns); | ||
}); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/dx-grid-core/src/plugins/table-column-reordering/constants.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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const TABLE_REORDERING_TYPE = 'reordering'; |
2 changes: 1 addition & 1 deletion
2
...rc/plugins/column-order-state/reducers.js → ...ugins/table-column-reordering/reducers.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
10 changes: 5 additions & 5 deletions
10
...ugins/column-order-state/reducers.test.js → .../table-column-reordering/reducers.test.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
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
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
Oops, something went wrong.