|
@@ -1,25 +1,27 @@
|
|
import { useMemo, useState } from 'react';
|
|
import { useMemo, useState } from 'react';
|
|
import { stableSort, getComparator } from '../utils/Sort';
|
|
import { stableSort, getComparator } from '../utils/Sort';
|
|
|
|
+import { ColDefinitionsType, SortType } from '../components/grid/Types';
|
|
|
|
|
|
export const usePaginationHook = (list: any[]) => {
|
|
export const usePaginationHook = (list: any[]) => {
|
|
const [currentPage, setCurrentPage] = useState(0);
|
|
const [currentPage, setCurrentPage] = useState(0);
|
|
const [pageSize, setPageSize] = useState(10);
|
|
const [pageSize, setPageSize] = useState(10);
|
|
const [orderBy, setOrderBy] = useState('');
|
|
const [orderBy, setOrderBy] = useState('');
|
|
const [order, setOrder] = useState<'asc' | 'desc'>('asc');
|
|
const [order, setOrder] = useState<'asc' | 'desc'>('asc');
|
|
|
|
+ const [sortType, setSortType] = useState<SortType>('number');
|
|
|
|
|
|
const total = list.length;
|
|
const total = list.length;
|
|
const { data, offset } = useMemo(() => {
|
|
const { data, offset } = useMemo(() => {
|
|
const offset = pageSize * currentPage;
|
|
const offset = pageSize * currentPage;
|
|
// only when user click sort, orderBy will have value
|
|
// only when user click sort, orderBy will have value
|
|
const sortList = orderBy
|
|
const sortList = orderBy
|
|
- ? stableSort(list, getComparator(order || 'asc', orderBy))
|
|
|
|
|
|
+ ? stableSort(list, getComparator(order || 'asc', orderBy, sortType))
|
|
: list;
|
|
: list;
|
|
const data = sortList.slice(offset, offset + pageSize);
|
|
const data = sortList.slice(offset, offset + pageSize);
|
|
return {
|
|
return {
|
|
offset,
|
|
offset,
|
|
data,
|
|
data,
|
|
};
|
|
};
|
|
- }, [pageSize, currentPage, orderBy, list, order]);
|
|
|
|
|
|
+ }, [pageSize, currentPage, orderBy, list, order, sortType]);
|
|
|
|
|
|
const handleCurrentPage = (page: number) => {
|
|
const handleCurrentPage = (page: number) => {
|
|
setCurrentPage(page);
|
|
setCurrentPage(page);
|
|
@@ -28,10 +30,15 @@ export const usePaginationHook = (list: any[]) => {
|
|
const handlePageSize = (size: number) => {
|
|
const handlePageSize = (size: number) => {
|
|
setPageSize(size);
|
|
setPageSize(size);
|
|
};
|
|
};
|
|
- const handleGridSort = (e: any, property: string) => {
|
|
|
|
|
|
+ const handleGridSort = (
|
|
|
|
+ e: any,
|
|
|
|
+ property: string,
|
|
|
|
+ col: ColDefinitionsType = {} as ColDefinitionsType
|
|
|
|
+ ) => {
|
|
const isAsc = orderBy === property && order === 'asc';
|
|
const isAsc = orderBy === property && order === 'asc';
|
|
setOrder(isAsc ? 'desc' : 'asc');
|
|
setOrder(isAsc ? 'desc' : 'asc');
|
|
setOrderBy(property);
|
|
setOrderBy(property);
|
|
|
|
+ setSortType(col!.sortType || 'number');
|
|
};
|
|
};
|
|
|
|
|
|
return {
|
|
return {
|