-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b25227
commit 4946971
Showing
5 changed files
with
36 additions
and
31 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
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,8 @@ | ||
import type { Control } from './types'; | ||
|
||
export const controlDefaults: Required<Control> = { | ||
rows: 10, | ||
page: 1, | ||
field: 'createdAt', | ||
direction: 'desc', | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
import chunk from 'lodash/chunk'; | ||
import orderBy from 'lodash/orderBy'; | ||
|
||
export default <T extends Record<string, any>>( | ||
rows: T[], | ||
control = { rows: 10, page: 1, field: 'createdAt', direction: 'desc' } as any, | ||
) => { | ||
if (!rows?.length) return []; | ||
import type { Control } from '../../components/table/types'; | ||
import { controlDefaults } from '../../components/table/config'; | ||
|
||
let arr = [...rows]; | ||
export default <T>(rows: T[], control: Control) => { | ||
if (!rows?.length) return []; | ||
|
||
if (control.field && control.direction === 'asc') { | ||
arr = orderBy(arr, control.field, 'asc'); | ||
} | ||
const _rows = control.rows || controlDefaults.rows; | ||
const _page = control.page || controlDefaults.page; | ||
const _field = control.field || controlDefaults.field; | ||
const _direction = control.direction || controlDefaults.direction; | ||
|
||
if (control.field && control.direction === 'desc') { | ||
arr = orderBy(arr, control.field, 'desc'); | ||
} | ||
const ordered = orderBy(rows, _field, _direction as 'asc' | 'desc'); | ||
|
||
const chunked = chunk(arr, control.rows); | ||
const start = (_page - 1) * _rows; | ||
const pagedRows = ordered.slice(start, start + _rows); | ||
|
||
return chunked[control.page - 1]; | ||
return pagedRows; | ||
}; |