UNPKG

4.14 kBTypeScriptView Raw
1/**
2 * @name useTable
3 * @author Fajar Rizky Hidayat <fajarrizkyhidayat@gmail.com>
4 */
5type UseTablePaginationProps = {
6 params?: {
7 page?: string;
8 perPage?: string;
9 };
10 /**
11 * @default 1
12 */
13 startPage?: number;
14 /**
15 * @default 10
16 */
17 perPage?: number;
18 /**
19 * @default
20 * ```
21 * [5, 10, 15, 25],
22 * ```
23 */
24 perPageOptions?: number[];
25 /**
26 * @name FromPage
27 * @description Get start
28 * @param total
29 * @param size
30 * @param df start page
31 * @default
32 * ```
33 * from: (total, page, size, df) => (page - 1) * size + (total === 0 ? 0 : df),
34 * ```
35 */
36 from?: (total: number, page: number, size: number, df: number) => number;
37 /**
38 * @name ToPage
39 * @description Get end
40 * @param total
41 * @param page
42 * @param size
43 * @default
44 * ```
45 * to: (total, page, size) => (total === 0 ? 0 : Math.min(total, page * size)),
46 * ```
47 */
48 to?: (total: number, page: number, size: number, df: number) => number;
49 /**
50 * @name LastPage
51 * @description Get Last Page Number
52 * @param total
53 * @param page
54 * @param size
55 * @default
56 * ```
57 * lastPage:(total, size) => Math.max(0, Math.ceil(total / size)),
58 * ```
59 */
60 lastPage?: (total: number, size: number) => number;
61 /**
62 *
63 * @param total
64 * @param page
65 * @param df
66 * @default
67 * ```
68 * disableFirst: (total, page, df) => total !== 0 && page === df,
69 * ```
70 */
71 disableFirst?: (total: number, page: number, df: number) => boolean;
72 /**
73 *
74 * @param total
75 * @param page
76 * @param lp
77 * @default
78 * ```
79 * disableLast: (total, page, lp) => total !== 0 && page === lp,
80 * ```
81 */
82 disableLast?: (total: number, page: number, lp: number) => boolean;
83};
84
85type UseTablePagination = {
86 total: number;
87 page: number;
88 from: number;
89 to: number;
90 lastPage: number;
91 perPageOptions: number[];
92 perPage: number;
93 text: string;
94 setPage: (value: number) => void;
95 setPerPage: (value: number) => void;
96 nextButton: () => React.DetailedHTMLProps<
97 React.ButtonHTMLAttributes<HTMLButtonElement>,
98 HTMLButtonElement
99 >;
100 backButton: () => React.DetailedHTMLProps<
101 React.ButtonHTMLAttributes<HTMLButtonElement>,
102 HTMLButtonElement
103 >;
104 firstButton: () => React.DetailedHTMLProps<
105 React.ButtonHTMLAttributes<HTMLButtonElement>,
106 HTMLButtonElement
107 >;
108 lastButton: () => React.DetailedHTMLProps<
109 React.ButtonHTMLAttributes<HTMLButtonElement>,
110 HTMLButtonElement
111 >;
112 onPerPageChange: (
113 event?: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>
114 ) => void;
115};
116
117interface UseTableProps<T = any> {
118 service: (event?: import(".").EventSend<T>) => T;
119 selector: (resp: any) => any;
120 total?: (resp: any) => number;
121 replaceUrl?: boolean;
122 cache?: {
123 key?: string;
124 /**
125 * @name ttl
126 * @description in seconds
127 * */
128 ttl?: number;
129 /**
130 * @name PeriodTime
131 * @description in ms
132 * */
133 periodTime?: number;
134 periodically?: boolean;
135 };
136 disabledOnDidMount?: boolean;
137 sort?: {
138 params?: {
139 order?: string;
140 orderBy?: string;
141 };
142 order?: "desc" | "asc";
143 orderBy?: string;
144 };
145 deps?: any[];
146 pagination?: UseTablePaginationProps;
147}
148
149type UseTable<T> = {
150 isEmpty: boolean;
151 loading: boolean;
152 total: number;
153 data: T[];
154 order: "desc" | "asc";
155 orderBy: string;
156 query: (key?: string, defaultValue?: any) => any;
157 setTotal: (value: number) => void;
158 setQuery: (
159 value:
160 | Record<string, any>
161 | ((value: Record<string, any>) => Record<string, any>)
162 ) => void;
163 onSort: (value: string) => void;
164 reload: () => void;
165 clear: (value?: { except?: string[]; only?: [string] }) => void;
166 remove: (key: string, resetPage?: boolean) => void;
167 add: (newValue: T, position?: "start" | "end" | number) => void;
168 update: (condition: (data: T) => boolean, newValue: Partial<T>) => void;
169 destroy: (condition: (data: T) => boolean) => void;
170 has: (key: string) => boolean;
171 cancel: () => void;
172 pagination: UseTablePagination;
173};
174
175export default function useTable<T, S = unknown>(
176 props: UseTableProps<S>
177): UseTable<T>;