@mkt-loitd/react-table-grid-custom 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -38,8 +38,8 @@ interface IReactTableGridCustom<T = unknown, SR = unknown, K extends Key = Key>
38
38
  TableLogo?: string;
39
39
  }
40
40
 
41
- declare const ReactTableGridCustomInner: <T, SR = unknown, K extends Key = Key>({ classNamePaginationTable, classNameWapperTable, hiddenPagination, hiddenSTT, data, page, pageSize, total, onChange, setConfigPagination, columns, rowKeyGetter, selectedRows, hiddenPaginationText, paginationText, listPageSize, fetching, TableLogo, ...spread }: IReactTableGridCustom<T, SR, K>) => JSX.Element;
42
- declare const ReactTableGridCustom: typeof ReactTableGridCustomInner;
41
+ type ReactTableGridCustomComponent = <T, SR = unknown, K extends Key = Key>(props: IReactTableGridCustom<T, SR, K>) => JSX.Element;
42
+ declare const ReactTableGridCustom: ReactTableGridCustomComponent;
43
43
 
44
44
  interface IPaginationParams {
45
45
  pageSize?: number;
package/dist/index.d.ts CHANGED
@@ -38,8 +38,8 @@ interface IReactTableGridCustom<T = unknown, SR = unknown, K extends Key = Key>
38
38
  TableLogo?: string;
39
39
  }
40
40
 
41
- declare const ReactTableGridCustomInner: <T, SR = unknown, K extends Key = Key>({ classNamePaginationTable, classNameWapperTable, hiddenPagination, hiddenSTT, data, page, pageSize, total, onChange, setConfigPagination, columns, rowKeyGetter, selectedRows, hiddenPaginationText, paginationText, listPageSize, fetching, TableLogo, ...spread }: IReactTableGridCustom<T, SR, K>) => JSX.Element;
42
- declare const ReactTableGridCustom: typeof ReactTableGridCustomInner;
41
+ type ReactTableGridCustomComponent = <T, SR = unknown, K extends Key = Key>(props: IReactTableGridCustom<T, SR, K>) => JSX.Element;
42
+ declare const ReactTableGridCustom: ReactTableGridCustomComponent;
43
43
 
44
44
  interface IPaginationParams {
45
45
  pageSize?: number;
package/dist/index.js CHANGED
@@ -25,7 +25,12 @@ __export(index_exports, {
25
25
  module.exports = __toCommonJS(index_exports);
26
26
 
27
27
  // src/ReactTableGridCustom.tsx
28
- var import_core2 = require("@mantine/core");
28
+ var import_core = require("@mantine/core");
29
+ var import_lodash = require("lodash");
30
+ var import_react2 = require("react");
31
+ var import_react_data_grid = require("react-data-grid");
32
+ var import_styles = require("react-data-grid/lib/styles.css");
33
+ var import_react_i18next2 = require("react-i18next");
29
34
 
30
35
  // src/helpers/table.ts
31
36
  var calculatorTotalPage = ({ total = 0, pageSize = 0 }) => {
@@ -53,65 +58,20 @@ function cn(...inputs) {
53
58
  return (0, import_tailwind_merge.twMerge)((0, import_clsx.clsx)(inputs));
54
59
  }
55
60
 
56
- // src/ReactTableGridCustom.tsx
57
- var import_lodash = require("lodash");
58
- var import_react3 = require("react");
59
- var import_react_data_grid = require("react-data-grid");
60
- var import_styles = require("react-data-grid/lib/styles.css");
61
- var import_react_i18next2 = require("react-i18next");
62
-
63
- // src/component/ComboboxCustom.tsx
64
- var import_core = require("@mantine/core");
61
+ // src/hooks/useTranslationTable.ts
65
62
  var import_react = require("react");
66
- var import_jsx_runtime = require("react/jsx-runtime");
67
- var ComboboxCustom = ({ options, onChange, value }) => {
68
- const combobox = (0, import_core.useCombobox)({
69
- onDropdownClose: () => combobox.resetSelectedOption()
70
- });
71
- const newOptions = (0, import_react.useMemo)(() => {
72
- return (options != null ? options : []).map((item) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
73
- import_core.Combobox.Option,
74
- {
75
- className: "page_size-table",
76
- value: item,
77
- disabled: item === value,
78
- children: item
79
- },
80
- item
81
- ));
82
- }, [options, value]);
83
- return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
84
- import_core.Combobox,
85
- {
86
- size: "sm",
87
- store: combobox,
88
- onOptionSubmit: (val) => {
89
- onChange && onChange(val);
90
- combobox.closeDropdown();
91
- },
92
- children: [
93
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.Combobox.Target, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
94
- import_core.InputBase,
95
- {
96
- component: "button",
97
- type: "button",
98
- pointer: true,
99
- rightSectionPointerEvents: "none",
100
- onClick: () => combobox.toggleDropdown(),
101
- className: "w-[70px]",
102
- classNames: { input: "custom_input_table" },
103
- children: value || /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.Input.Placeholder, { children: "Pick value" })
104
- }
105
- ) }),
106
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.Combobox.Dropdown, { className: "w-[70px]", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.Combobox.Options, { children: newOptions }) })
107
- ]
108
- }
109
- );
63
+ var import_react_i18next = require("react-i18next");
64
+ var useTranslationTable = (column) => {
65
+ const { i18n, t } = (0, import_react_i18next.useTranslation)();
66
+ const columnTranslation = (0, import_react.useMemo)(() => {
67
+ return column.map((item) => ({ ...item, name: t(`${item == null ? void 0 : item.name}`) }));
68
+ }, [i18n == null ? void 0 : i18n.language, column]);
69
+ return columnTranslation;
110
70
  };
111
- var ComboboxCustom_default = ComboboxCustom;
71
+ var useTranslationTable_default = useTranslationTable;
112
72
 
113
73
  // src/component/Icons.tsx
114
- var import_jsx_runtime2 = require("react/jsx-runtime");
74
+ var import_jsx_runtime = require("react/jsx-runtime");
115
75
  var LoadingIcon = ({
116
76
  isSpin = false,
117
77
  h = 30,
@@ -119,7 +79,7 @@ var LoadingIcon = ({
119
79
  size = 30,
120
80
  className
121
81
  }) => {
122
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
82
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
123
83
  "svg",
124
84
  {
125
85
  className: `${isSpin ? "animate-spin" : ""} ${className != null ? className : ""}`,
@@ -130,7 +90,7 @@ var LoadingIcon = ({
130
90
  height: size || h,
131
91
  fill: "currentColor",
132
92
  "aria-hidden": "true",
133
- children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("path", { d: "M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z" })
93
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { d: "M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z" })
134
94
  }
135
95
  );
136
96
  };
@@ -140,7 +100,7 @@ var ArrowIcon = ({
140
100
  size,
141
101
  className
142
102
  }) => {
143
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
103
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
144
104
  "svg",
145
105
  {
146
106
  className,
@@ -151,7 +111,7 @@ var ArrowIcon = ({
151
111
  height: size != null ? size : h,
152
112
  width: size != null ? size : w,
153
113
  xmlns: "http://www.w3.org/2000/svg",
154
- children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
114
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
155
115
  "path",
156
116
  {
157
117
  fillRule: "evenodd",
@@ -163,35 +123,23 @@ var ArrowIcon = ({
163
123
  };
164
124
 
165
125
  // src/component/RenderSortStatus.tsx
166
- var import_jsx_runtime3 = require("react/jsx-runtime");
126
+ var import_jsx_runtime2 = require("react/jsx-runtime");
167
127
  var RenderSortStatus = ({ sortDirection, priority }) => {
168
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
169
- sortDirection !== void 0 && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
128
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
129
+ sortDirection !== void 0 && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
170
130
  ArrowIcon,
171
131
  {
172
132
  className: cn(sortDirection === "DESC" && "-rotate-180", "transition-transform"),
173
133
  size: 20
174
134
  }
175
135
  ),
176
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { children: priority })
136
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: priority })
177
137
  ] });
178
138
  };
179
139
  var RenderSortStatus_default = RenderSortStatus;
180
140
 
181
- // src/hooks/useTranslationTable.ts
182
- var import_react2 = require("react");
183
- var import_react_i18next = require("react-i18next");
184
- var useTranslationTable = (column) => {
185
- const { i18n, t } = (0, import_react_i18next.useTranslation)();
186
- const columnTranslation = (0, import_react2.useMemo)(() => {
187
- return column.map((item) => ({ ...item, name: t(`${item == null ? void 0 : item.name}`) }));
188
- }, [i18n == null ? void 0 : i18n.language, column]);
189
- return columnTranslation;
190
- };
191
- var useTranslationTable_default = useTranslationTable;
192
-
193
141
  // src/ReactTableGridCustom.tsx
194
- var import_jsx_runtime4 = require("react/jsx-runtime");
142
+ var import_jsx_runtime3 = require("react/jsx-runtime");
195
143
  var ReactTableGridCustomInner = ({
196
144
  classNamePaginationTable,
197
145
  classNameWapperTable,
@@ -214,197 +162,134 @@ var ReactTableGridCustomInner = ({
214
162
  ...spread
215
163
  }) => {
216
164
  const { t } = (0, import_react_i18next2.useTranslation)();
217
- const [isLoading, setIsLoading] = (0, import_react3.useState)(true);
218
- const tableRef = (0, import_react3.useRef)(null);
219
- const [containerWidth, setContainerWidth] = (0, import_react3.useState)(0);
220
- const [sortColumns, setSortColumns] = (0, import_react3.useState)([]);
165
+ const tableRef = (0, import_react2.useRef)(null);
166
+ const [isLoading, setIsLoading] = (0, import_react2.useState)(true);
167
+ const [containerWidth, setContainerWidth] = (0, import_react2.useState)(0);
168
+ const [sortColumns, setSortColumns] = (0, import_react2.useState)([]);
221
169
  const isSelectRow = selectedRows !== void 0;
222
- const maxPage = (0, import_react3.useMemo)(
223
- () => !hiddenPagination ? calculatorTotalPage({
224
- total,
225
- pageSize
226
- }) : 0,
170
+ const maxPage = (0, import_react2.useMemo)(
171
+ () => !hiddenPagination ? calculatorTotalPage({ total, pageSize }) : 0,
227
172
  [pageSize, total, hiddenPagination]
228
173
  );
229
- const toInPagination = (0, import_react3.useMemo)(() => {
230
- const initPage = {
231
- from: 0,
232
- to: 0
233
- };
234
- if (!hiddenPaginationText && pageSize && page) {
235
- const from = STT(
236
- {
237
- page,
238
- pageSize
239
- },
240
- 0
241
- );
174
+ const toInPagination = (0, import_react2.useMemo)(() => {
175
+ if (!hiddenPaginationText && page && pageSize) {
176
+ const from = STT({ page, pageSize }, 0);
242
177
  return {
243
178
  from,
244
- to: maxPage === page ? total : page * pageSize
179
+ to: maxPage === page ? total != null ? total : 0 : page * pageSize
245
180
  };
246
181
  }
247
- return initPage;
248
- }, [pageSize, page, hiddenPaginationText, maxPage, total]);
249
- const columnTranslation = useTranslationTable_default(columns);
250
- (0, import_react3.useEffect)(() => {
251
- var _a;
252
- setIsLoading(false);
253
- if ((_a = tableRef.current) == null ? void 0 : _a.element) {
254
- const width = tableRef.current.element.offsetWidth;
255
- if (width) setContainerWidth(width);
256
- }
257
- }, []);
258
- const newColumns = (0, import_react3.useMemo)(() => {
259
- let columnsCustom = [...columnTranslation];
260
- const adjustedContainerWidth = containerWidth - 80 + 35;
261
- columnsCustom = columnsCustom.map((col) => {
262
- const newCol = { ...col };
263
- delete newCol.maxWidth;
264
- newCol.width = 200;
265
- return newCol;
266
- });
267
- const totalMaxWidth = columnsCustom.reduce((sum, col) => {
268
- var _a;
269
- return sum + ("width" in col ? Number((_a = col == null ? void 0 : col.width) != null ? _a : 200) : 0);
270
- }, 0);
271
- if (totalMaxWidth < adjustedContainerWidth && columnsCustom.length > 0) {
272
- const evenWidth = Math.floor(adjustedContainerWidth / columnsCustom.length);
273
- columnsCustom = columnsCustom.map((col) => {
274
- const newCol = { ...col };
275
- delete newCol.maxWidth;
276
- newCol.width = evenWidth;
277
- return newCol;
278
- });
279
- }
182
+ return { from: 0, to: 0 };
183
+ }, [page, pageSize, hiddenPaginationText, maxPage, total]);
184
+ const columnTranslation = useTranslationTable_default(
185
+ columns
186
+ );
187
+ const newColumns = (0, import_react2.useMemo)(() => {
188
+ let cols = [
189
+ ...columnTranslation
190
+ ];
191
+ const adjustedWidth = containerWidth - 45;
192
+ cols = cols.map(
193
+ (col) => "key" in col ? {
194
+ ...col,
195
+ width: 200,
196
+ maxWidth: void 0
197
+ } : col
198
+ );
280
199
  if (isSelectRow) {
281
- columnsCustom.unshift(import_react_data_grid.SelectColumn);
200
+ cols.unshift(import_react_data_grid.SelectColumn);
282
201
  }
283
- if (!hiddenSTT || !hiddenSTT && page && pageSize) {
284
- columnsCustom.unshift({
202
+ if (!hiddenSTT) {
203
+ const sttColumn = {
285
204
  key: "index",
286
205
  name: "STT",
287
206
  width: 80,
288
207
  renderCell: ({ rowIdx }) => STT({ page, pageSize }, rowIdx)
289
- });
208
+ };
209
+ cols.unshift(sttColumn);
290
210
  }
291
- return columnsCustom;
292
- }, [hiddenSTT, isSelectRow, page, pageSize, columnTranslation, containerWidth]);
293
- const customRowKeyGetter = (0, import_react3.useMemo)(() => {
294
- return (row) => {
211
+ return cols;
212
+ }, [
213
+ columnTranslation,
214
+ containerWidth,
215
+ hiddenSTT,
216
+ isSelectRow,
217
+ page,
218
+ pageSize
219
+ ]);
220
+ const customRowKeyGetter = (0, import_react2.useCallback)(
221
+ (row) => {
295
222
  if (typeof rowKeyGetter === "function") {
296
223
  return rowKeyGetter(row);
297
224
  }
298
- return (0, import_lodash.get)(row, rowKeyGetter);
299
- };
300
- }, [rowKeyGetter]);
301
- const handlePageChange = (0, import_react3.useCallback)(
302
- (page2) => {
303
- if (onChange) {
304
- onChange(page2);
305
- return;
306
- }
307
- if (setConfigPagination) {
308
- setConfigPagination((prev) => ({ ...prev, page: page2 }));
225
+ if (typeof rowKeyGetter === "string") {
226
+ return (0, import_lodash.get)(row, rowKeyGetter);
309
227
  }
228
+ throw new Error(
229
+ "rowKeyGetter must be a string or a function"
230
+ );
310
231
  },
311
- [setConfigPagination, onChange]
232
+ [rowKeyGetter]
312
233
  );
313
- const sortedRows = (0, import_react3.useMemo)(() => {
314
- var _a, _b, _c;
315
- if (sortColumns.length === 0) return data;
316
- const direction = (_b = (_a = sortColumns[0]) == null ? void 0 : _a.direction) == null ? void 0 : _b.toLocaleLowerCase();
317
- const columnKey = (_c = sortColumns[0]) == null ? void 0 : _c.columnKey;
318
- return (0, import_lodash.orderBy)(data, columnKey, direction);
234
+ const sortedRows = (0, import_react2.useMemo)(() => {
235
+ if (!sortColumns.length) return data;
236
+ const { columnKey, direction } = sortColumns[0];
237
+ if (!direction) return data;
238
+ const order = direction === "ASC" ? "asc" : "desc";
239
+ const iteratee = typeof columnKey === "string" ? columnKey : String(columnKey);
240
+ return (0, import_lodash.orderBy)(data, [iteratee], [order]);
319
241
  }, [data, sortColumns]);
320
- (0, import_react3.useEffect)(() => {
321
- if (page && page > maxPage && maxPage > 0) {
322
- handlePageChange(1);
242
+ (0, import_react2.useEffect)(() => {
243
+ var _a;
244
+ setIsLoading(false);
245
+ if ((_a = tableRef.current) == null ? void 0 : _a.element) {
246
+ setContainerWidth(tableRef.current.element.offsetWidth);
323
247
  }
324
- }, [page, maxPage, handlePageChange]);
325
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
248
+ }, []);
249
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
326
250
  "div",
327
251
  {
328
252
  className: cn(
329
- "wapper_table flex-1 rounded-md overflow-hidden relative h-full flex flex-col min-h-0",
253
+ "wapper_table flex flex-col h-full min-h-0",
330
254
  classNameWapperTable
331
255
  ),
332
256
  children: [
333
- /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "relative wapper_table_empty wrapperDatagrid flex flex-col flex-1", children: [
334
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
335
- import_react_data_grid.DataGrid,
336
- {
337
- ref: tableRef,
338
- "aria-rowcount": sortedRows == null ? void 0 : sortedRows.length,
339
- selectedRows,
340
- rows: sortedRows,
341
- rowKeyGetter: rowKeyGetter && isSelectRow ? customRowKeyGetter : void 0,
342
- columns: newColumns,
343
- renderers: {
344
- renderSortStatus: RenderSortStatus_default,
345
- renderCheckbox({ onChange: onChange2, checked, ...spread2 }) {
346
- const handleChange = (e) => {
347
- onChange2(e.target.checked, e.nativeEvent.shiftKey);
348
- };
349
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_core2.Checkbox, { color: "indigo", checked: !!checked, onChange: handleChange, ...spread2 });
350
- }
351
- },
352
- className: "fill-grid flex-1 h-full min-h-0",
353
- defaultColumnOptions: {
354
- // minWidth: 200,
355
- // maxWidth: 200,
356
- renderCell: ({ column, row }) => {
357
- const value = (0, import_lodash.get)(row, column.key);
358
- return [null, void 0, ""].includes(value) ? "-" : value;
359
- }
360
- },
361
- sortColumns,
362
- onSortColumnsChange: setSortColumns,
363
- ...spread
364
- }
365
- ),
366
- total === 0 && TableLogo && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "no_result absolute left-1/2 top-1/2 -translate-y-1/2 -translate-x-1/2 select-none", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "flex flex-col justify-center", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("img", { src: TableLogo, alt: "", className: "size-32" }) }) })
367
- ] }),
368
- !hiddenPagination && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
369
- "div",
257
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
258
+ import_react_data_grid.DataGrid,
370
259
  {
371
- className: cn(
372
- "flex justify-between items-center flex-wrap wapper_pagination p-[10px] pl-2 mt-auto",
373
- classNamePaginationTable
374
- ),
375
- children: [
376
- !hiddenPaginationText && page && pageSize && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "text-sm", children: paginationText ? paginationText({ ...toInPagination, total }) : total ? `${t("display")} ${toInPagination == null ? void 0 : toInPagination.from} ${t("to")} ${toInPagination == null ? void 0 : toInPagination.to} ${t("in")} ${total} ${t("data")}` : "..." }),
377
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
378
- ComboboxCustom_default,
379
- {
380
- options: listPageSize,
381
- value: pageSize == null ? void 0 : pageSize.toString(),
382
- onChange: (value) => {
383
- if (setConfigPagination) {
384
- setConfigPagination((prev) => ({ ...prev, pageSize: Number(value), page: 1 }));
260
+ ref: tableRef,
261
+ rows: sortedRows,
262
+ columns: newColumns,
263
+ selectedRows,
264
+ rowKeyGetter: isSelectRow ? customRowKeyGetter : void 0,
265
+ sortColumns,
266
+ onSortColumnsChange: setSortColumns,
267
+ renderers: {
268
+ renderSortStatus: RenderSortStatus_default,
269
+ renderCheckbox({ onChange: onChange2, checked, ...props }) {
270
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
271
+ import_core.Checkbox,
272
+ {
273
+ ...props,
274
+ checked: !!checked,
275
+ onChange: (e) => {
276
+ const nativeEvent = e.nativeEvent;
277
+ const shiftKey = nativeEvent instanceof MouseEvent ? nativeEvent.shiftKey : false;
278
+ onChange2(e.target.checked, shiftKey);
385
279
  }
386
280
  }
387
- }
388
- ),
389
- !!maxPage && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
390
- import_core2.Pagination,
391
- {
392
- color: "indigo",
393
- total: maxPage,
394
- size: "sm",
395
- value: page,
396
- onChange: handlePageChange
397
- }
398
- )
399
- ]
281
+ );
282
+ }
283
+ },
284
+ ...spread
400
285
  }
401
286
  ),
402
- (fetching || isLoading) && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "absolute inset-0 bg-gray-50/45 flex justify-center items-center", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "border-[2px] rounded-full border-gray-200 shadow-sm", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(LoadingIcon, { isSpin: true }) }) })
287
+ (fetching || isLoading) && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "absolute inset-0 flex items-center justify-center", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(LoadingIcon, { isSpin: true }) })
403
288
  ]
404
289
  }
405
290
  );
406
291
  };
407
- var ReactTableGridCustom = (0, import_react3.memo)(
292
+ var ReactTableGridCustom = (0, import_react2.memo)(
408
293
  ReactTableGridCustomInner
409
294
  );
410
295
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.mjs CHANGED
@@ -1,5 +1,20 @@
1
1
  // src/ReactTableGridCustom.tsx
2
- import { Checkbox, Pagination } from "@mantine/core";
2
+ import { Checkbox } from "@mantine/core";
3
+ import { get, orderBy } from "lodash";
4
+ import {
5
+ memo,
6
+ useCallback,
7
+ useEffect,
8
+ useMemo as useMemo2,
9
+ useRef,
10
+ useState
11
+ } from "react";
12
+ import {
13
+ DataGrid,
14
+ SelectColumn
15
+ } from "react-data-grid";
16
+ import "react-data-grid/lib/styles.css";
17
+ import { useTranslation as useTranslation2 } from "react-i18next";
3
18
 
4
19
  // src/helpers/table.ts
5
20
  var calculatorTotalPage = ({ total = 0, pageSize = 0 }) => {
@@ -27,65 +42,20 @@ function cn(...inputs) {
27
42
  return twMerge(clsx(inputs));
28
43
  }
29
44
 
30
- // src/ReactTableGridCustom.tsx
31
- import { get, orderBy } from "lodash";
32
- import { memo, useCallback, useEffect, useMemo as useMemo3, useRef, useState } from "react";
33
- import { DataGrid, SelectColumn } from "react-data-grid";
34
- import "react-data-grid/lib/styles.css";
35
- import { useTranslation as useTranslation2 } from "react-i18next";
36
-
37
- // src/component/ComboboxCustom.tsx
38
- import { Combobox, Input, InputBase, useCombobox } from "@mantine/core";
45
+ // src/hooks/useTranslationTable.ts
39
46
  import { useMemo } from "react";
40
- import { jsx, jsxs } from "react/jsx-runtime";
41
- var ComboboxCustom = ({ options, onChange, value }) => {
42
- const combobox = useCombobox({
43
- onDropdownClose: () => combobox.resetSelectedOption()
44
- });
45
- const newOptions = useMemo(() => {
46
- return (options != null ? options : []).map((item) => /* @__PURE__ */ jsx(
47
- Combobox.Option,
48
- {
49
- className: "page_size-table",
50
- value: item,
51
- disabled: item === value,
52
- children: item
53
- },
54
- item
55
- ));
56
- }, [options, value]);
57
- return /* @__PURE__ */ jsxs(
58
- Combobox,
59
- {
60
- size: "sm",
61
- store: combobox,
62
- onOptionSubmit: (val) => {
63
- onChange && onChange(val);
64
- combobox.closeDropdown();
65
- },
66
- children: [
67
- /* @__PURE__ */ jsx(Combobox.Target, { children: /* @__PURE__ */ jsx(
68
- InputBase,
69
- {
70
- component: "button",
71
- type: "button",
72
- pointer: true,
73
- rightSectionPointerEvents: "none",
74
- onClick: () => combobox.toggleDropdown(),
75
- className: "w-[70px]",
76
- classNames: { input: "custom_input_table" },
77
- children: value || /* @__PURE__ */ jsx(Input.Placeholder, { children: "Pick value" })
78
- }
79
- ) }),
80
- /* @__PURE__ */ jsx(Combobox.Dropdown, { className: "w-[70px]", children: /* @__PURE__ */ jsx(Combobox.Options, { children: newOptions }) })
81
- ]
82
- }
83
- );
47
+ import { useTranslation } from "react-i18next";
48
+ var useTranslationTable = (column) => {
49
+ const { i18n, t } = useTranslation();
50
+ const columnTranslation = useMemo(() => {
51
+ return column.map((item) => ({ ...item, name: t(`${item == null ? void 0 : item.name}`) }));
52
+ }, [i18n == null ? void 0 : i18n.language, column]);
53
+ return columnTranslation;
84
54
  };
85
- var ComboboxCustom_default = ComboboxCustom;
55
+ var useTranslationTable_default = useTranslationTable;
86
56
 
87
57
  // src/component/Icons.tsx
88
- import { jsx as jsx2 } from "react/jsx-runtime";
58
+ import { jsx } from "react/jsx-runtime";
89
59
  var LoadingIcon = ({
90
60
  isSpin = false,
91
61
  h = 30,
@@ -93,7 +63,7 @@ var LoadingIcon = ({
93
63
  size = 30,
94
64
  className
95
65
  }) => {
96
- return /* @__PURE__ */ jsx2(
66
+ return /* @__PURE__ */ jsx(
97
67
  "svg",
98
68
  {
99
69
  className: `${isSpin ? "animate-spin" : ""} ${className != null ? className : ""}`,
@@ -104,7 +74,7 @@ var LoadingIcon = ({
104
74
  height: size || h,
105
75
  fill: "currentColor",
106
76
  "aria-hidden": "true",
107
- children: /* @__PURE__ */ jsx2("path", { d: "M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z" })
77
+ children: /* @__PURE__ */ jsx("path", { d: "M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z" })
108
78
  }
109
79
  );
110
80
  };
@@ -114,7 +84,7 @@ var ArrowIcon = ({
114
84
  size,
115
85
  className
116
86
  }) => {
117
- return /* @__PURE__ */ jsx2(
87
+ return /* @__PURE__ */ jsx(
118
88
  "svg",
119
89
  {
120
90
  className,
@@ -125,7 +95,7 @@ var ArrowIcon = ({
125
95
  height: size != null ? size : h,
126
96
  width: size != null ? size : w,
127
97
  xmlns: "http://www.w3.org/2000/svg",
128
- children: /* @__PURE__ */ jsx2(
98
+ children: /* @__PURE__ */ jsx(
129
99
  "path",
130
100
  {
131
101
  fillRule: "evenodd",
@@ -137,35 +107,23 @@ var ArrowIcon = ({
137
107
  };
138
108
 
139
109
  // src/component/RenderSortStatus.tsx
140
- import { Fragment, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
110
+ import { Fragment, jsx as jsx2, jsxs } from "react/jsx-runtime";
141
111
  var RenderSortStatus = ({ sortDirection, priority }) => {
142
- return /* @__PURE__ */ jsxs2(Fragment, { children: [
143
- sortDirection !== void 0 && /* @__PURE__ */ jsx3(
112
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
113
+ sortDirection !== void 0 && /* @__PURE__ */ jsx2(
144
114
  ArrowIcon,
145
115
  {
146
116
  className: cn(sortDirection === "DESC" && "-rotate-180", "transition-transform"),
147
117
  size: 20
148
118
  }
149
119
  ),
150
- /* @__PURE__ */ jsx3("span", { children: priority })
120
+ /* @__PURE__ */ jsx2("span", { children: priority })
151
121
  ] });
152
122
  };
153
123
  var RenderSortStatus_default = RenderSortStatus;
154
124
 
155
- // src/hooks/useTranslationTable.ts
156
- import { useMemo as useMemo2 } from "react";
157
- import { useTranslation } from "react-i18next";
158
- var useTranslationTable = (column) => {
159
- const { i18n, t } = useTranslation();
160
- const columnTranslation = useMemo2(() => {
161
- return column.map((item) => ({ ...item, name: t(`${item == null ? void 0 : item.name}`) }));
162
- }, [i18n == null ? void 0 : i18n.language, column]);
163
- return columnTranslation;
164
- };
165
- var useTranslationTable_default = useTranslationTable;
166
-
167
125
  // src/ReactTableGridCustom.tsx
168
- import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
126
+ import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
169
127
  var ReactTableGridCustomInner = ({
170
128
  classNamePaginationTable,
171
129
  classNameWapperTable,
@@ -188,192 +146,129 @@ var ReactTableGridCustomInner = ({
188
146
  ...spread
189
147
  }) => {
190
148
  const { t } = useTranslation2();
191
- const [isLoading, setIsLoading] = useState(true);
192
149
  const tableRef = useRef(null);
150
+ const [isLoading, setIsLoading] = useState(true);
193
151
  const [containerWidth, setContainerWidth] = useState(0);
194
152
  const [sortColumns, setSortColumns] = useState([]);
195
153
  const isSelectRow = selectedRows !== void 0;
196
- const maxPage = useMemo3(
197
- () => !hiddenPagination ? calculatorTotalPage({
198
- total,
199
- pageSize
200
- }) : 0,
154
+ const maxPage = useMemo2(
155
+ () => !hiddenPagination ? calculatorTotalPage({ total, pageSize }) : 0,
201
156
  [pageSize, total, hiddenPagination]
202
157
  );
203
- const toInPagination = useMemo3(() => {
204
- const initPage = {
205
- from: 0,
206
- to: 0
207
- };
208
- if (!hiddenPaginationText && pageSize && page) {
209
- const from = STT(
210
- {
211
- page,
212
- pageSize
213
- },
214
- 0
215
- );
158
+ const toInPagination = useMemo2(() => {
159
+ if (!hiddenPaginationText && page && pageSize) {
160
+ const from = STT({ page, pageSize }, 0);
216
161
  return {
217
162
  from,
218
- to: maxPage === page ? total : page * pageSize
163
+ to: maxPage === page ? total != null ? total : 0 : page * pageSize
219
164
  };
220
165
  }
221
- return initPage;
222
- }, [pageSize, page, hiddenPaginationText, maxPage, total]);
223
- const columnTranslation = useTranslationTable_default(columns);
224
- useEffect(() => {
225
- var _a;
226
- setIsLoading(false);
227
- if ((_a = tableRef.current) == null ? void 0 : _a.element) {
228
- const width = tableRef.current.element.offsetWidth;
229
- if (width) setContainerWidth(width);
230
- }
231
- }, []);
232
- const newColumns = useMemo3(() => {
233
- let columnsCustom = [...columnTranslation];
234
- const adjustedContainerWidth = containerWidth - 80 + 35;
235
- columnsCustom = columnsCustom.map((col) => {
236
- const newCol = { ...col };
237
- delete newCol.maxWidth;
238
- newCol.width = 200;
239
- return newCol;
240
- });
241
- const totalMaxWidth = columnsCustom.reduce((sum, col) => {
242
- var _a;
243
- return sum + ("width" in col ? Number((_a = col == null ? void 0 : col.width) != null ? _a : 200) : 0);
244
- }, 0);
245
- if (totalMaxWidth < adjustedContainerWidth && columnsCustom.length > 0) {
246
- const evenWidth = Math.floor(adjustedContainerWidth / columnsCustom.length);
247
- columnsCustom = columnsCustom.map((col) => {
248
- const newCol = { ...col };
249
- delete newCol.maxWidth;
250
- newCol.width = evenWidth;
251
- return newCol;
252
- });
253
- }
166
+ return { from: 0, to: 0 };
167
+ }, [page, pageSize, hiddenPaginationText, maxPage, total]);
168
+ const columnTranslation = useTranslationTable_default(
169
+ columns
170
+ );
171
+ const newColumns = useMemo2(() => {
172
+ let cols = [
173
+ ...columnTranslation
174
+ ];
175
+ const adjustedWidth = containerWidth - 45;
176
+ cols = cols.map(
177
+ (col) => "key" in col ? {
178
+ ...col,
179
+ width: 200,
180
+ maxWidth: void 0
181
+ } : col
182
+ );
254
183
  if (isSelectRow) {
255
- columnsCustom.unshift(SelectColumn);
184
+ cols.unshift(SelectColumn);
256
185
  }
257
- if (!hiddenSTT || !hiddenSTT && page && pageSize) {
258
- columnsCustom.unshift({
186
+ if (!hiddenSTT) {
187
+ const sttColumn = {
259
188
  key: "index",
260
189
  name: "STT",
261
190
  width: 80,
262
191
  renderCell: ({ rowIdx }) => STT({ page, pageSize }, rowIdx)
263
- });
192
+ };
193
+ cols.unshift(sttColumn);
264
194
  }
265
- return columnsCustom;
266
- }, [hiddenSTT, isSelectRow, page, pageSize, columnTranslation, containerWidth]);
267
- const customRowKeyGetter = useMemo3(() => {
268
- return (row) => {
195
+ return cols;
196
+ }, [
197
+ columnTranslation,
198
+ containerWidth,
199
+ hiddenSTT,
200
+ isSelectRow,
201
+ page,
202
+ pageSize
203
+ ]);
204
+ const customRowKeyGetter = useCallback(
205
+ (row) => {
269
206
  if (typeof rowKeyGetter === "function") {
270
207
  return rowKeyGetter(row);
271
208
  }
272
- return get(row, rowKeyGetter);
273
- };
274
- }, [rowKeyGetter]);
275
- const handlePageChange = useCallback(
276
- (page2) => {
277
- if (onChange) {
278
- onChange(page2);
279
- return;
280
- }
281
- if (setConfigPagination) {
282
- setConfigPagination((prev) => ({ ...prev, page: page2 }));
209
+ if (typeof rowKeyGetter === "string") {
210
+ return get(row, rowKeyGetter);
283
211
  }
212
+ throw new Error(
213
+ "rowKeyGetter must be a string or a function"
214
+ );
284
215
  },
285
- [setConfigPagination, onChange]
216
+ [rowKeyGetter]
286
217
  );
287
- const sortedRows = useMemo3(() => {
288
- var _a, _b, _c;
289
- if (sortColumns.length === 0) return data;
290
- const direction = (_b = (_a = sortColumns[0]) == null ? void 0 : _a.direction) == null ? void 0 : _b.toLocaleLowerCase();
291
- const columnKey = (_c = sortColumns[0]) == null ? void 0 : _c.columnKey;
292
- return orderBy(data, columnKey, direction);
218
+ const sortedRows = useMemo2(() => {
219
+ if (!sortColumns.length) return data;
220
+ const { columnKey, direction } = sortColumns[0];
221
+ if (!direction) return data;
222
+ const order = direction === "ASC" ? "asc" : "desc";
223
+ const iteratee = typeof columnKey === "string" ? columnKey : String(columnKey);
224
+ return orderBy(data, [iteratee], [order]);
293
225
  }, [data, sortColumns]);
294
226
  useEffect(() => {
295
- if (page && page > maxPage && maxPage > 0) {
296
- handlePageChange(1);
227
+ var _a;
228
+ setIsLoading(false);
229
+ if ((_a = tableRef.current) == null ? void 0 : _a.element) {
230
+ setContainerWidth(tableRef.current.element.offsetWidth);
297
231
  }
298
- }, [page, maxPage, handlePageChange]);
299
- return /* @__PURE__ */ jsxs3(
232
+ }, []);
233
+ return /* @__PURE__ */ jsxs2(
300
234
  "div",
301
235
  {
302
236
  className: cn(
303
- "wapper_table flex-1 rounded-md overflow-hidden relative h-full flex flex-col min-h-0",
237
+ "wapper_table flex flex-col h-full min-h-0",
304
238
  classNameWapperTable
305
239
  ),
306
240
  children: [
307
- /* @__PURE__ */ jsxs3("div", { className: "relative wapper_table_empty wrapperDatagrid flex flex-col flex-1", children: [
308
- /* @__PURE__ */ jsx4(
309
- DataGrid,
310
- {
311
- ref: tableRef,
312
- "aria-rowcount": sortedRows == null ? void 0 : sortedRows.length,
313
- selectedRows,
314
- rows: sortedRows,
315
- rowKeyGetter: rowKeyGetter && isSelectRow ? customRowKeyGetter : void 0,
316
- columns: newColumns,
317
- renderers: {
318
- renderSortStatus: RenderSortStatus_default,
319
- renderCheckbox({ onChange: onChange2, checked, ...spread2 }) {
320
- const handleChange = (e) => {
321
- onChange2(e.target.checked, e.nativeEvent.shiftKey);
322
- };
323
- return /* @__PURE__ */ jsx4(Checkbox, { color: "indigo", checked: !!checked, onChange: handleChange, ...spread2 });
324
- }
325
- },
326
- className: "fill-grid flex-1 h-full min-h-0",
327
- defaultColumnOptions: {
328
- // minWidth: 200,
329
- // maxWidth: 200,
330
- renderCell: ({ column, row }) => {
331
- const value = get(row, column.key);
332
- return [null, void 0, ""].includes(value) ? "-" : value;
333
- }
334
- },
335
- sortColumns,
336
- onSortColumnsChange: setSortColumns,
337
- ...spread
338
- }
339
- ),
340
- total === 0 && TableLogo && /* @__PURE__ */ jsx4("div", { className: "no_result absolute left-1/2 top-1/2 -translate-y-1/2 -translate-x-1/2 select-none", children: /* @__PURE__ */ jsx4("div", { className: "flex flex-col justify-center", children: /* @__PURE__ */ jsx4("img", { src: TableLogo, alt: "", className: "size-32" }) }) })
341
- ] }),
342
- !hiddenPagination && /* @__PURE__ */ jsxs3(
343
- "div",
241
+ /* @__PURE__ */ jsx3(
242
+ DataGrid,
344
243
  {
345
- className: cn(
346
- "flex justify-between items-center flex-wrap wapper_pagination p-[10px] pl-2 mt-auto",
347
- classNamePaginationTable
348
- ),
349
- children: [
350
- !hiddenPaginationText && page && pageSize && /* @__PURE__ */ jsx4("div", { className: "text-sm", children: paginationText ? paginationText({ ...toInPagination, total }) : total ? `${t("display")} ${toInPagination == null ? void 0 : toInPagination.from} ${t("to")} ${toInPagination == null ? void 0 : toInPagination.to} ${t("in")} ${total} ${t("data")}` : "..." }),
351
- /* @__PURE__ */ jsx4(
352
- ComboboxCustom_default,
353
- {
354
- options: listPageSize,
355
- value: pageSize == null ? void 0 : pageSize.toString(),
356
- onChange: (value) => {
357
- if (setConfigPagination) {
358
- setConfigPagination((prev) => ({ ...prev, pageSize: Number(value), page: 1 }));
244
+ ref: tableRef,
245
+ rows: sortedRows,
246
+ columns: newColumns,
247
+ selectedRows,
248
+ rowKeyGetter: isSelectRow ? customRowKeyGetter : void 0,
249
+ sortColumns,
250
+ onSortColumnsChange: setSortColumns,
251
+ renderers: {
252
+ renderSortStatus: RenderSortStatus_default,
253
+ renderCheckbox({ onChange: onChange2, checked, ...props }) {
254
+ return /* @__PURE__ */ jsx3(
255
+ Checkbox,
256
+ {
257
+ ...props,
258
+ checked: !!checked,
259
+ onChange: (e) => {
260
+ const nativeEvent = e.nativeEvent;
261
+ const shiftKey = nativeEvent instanceof MouseEvent ? nativeEvent.shiftKey : false;
262
+ onChange2(e.target.checked, shiftKey);
359
263
  }
360
264
  }
361
- }
362
- ),
363
- !!maxPage && /* @__PURE__ */ jsx4(
364
- Pagination,
365
- {
366
- color: "indigo",
367
- total: maxPage,
368
- size: "sm",
369
- value: page,
370
- onChange: handlePageChange
371
- }
372
- )
373
- ]
265
+ );
266
+ }
267
+ },
268
+ ...spread
374
269
  }
375
270
  ),
376
- (fetching || isLoading) && /* @__PURE__ */ jsx4("div", { className: "absolute inset-0 bg-gray-50/45 flex justify-center items-center", children: /* @__PURE__ */ jsx4("div", { className: "border-[2px] rounded-full border-gray-200 shadow-sm", children: /* @__PURE__ */ jsx4(LoadingIcon, { isSpin: true }) }) })
271
+ (fetching || isLoading) && /* @__PURE__ */ jsx3("div", { className: "absolute inset-0 flex items-center justify-center", children: /* @__PURE__ */ jsx3(LoadingIcon, { isSpin: true }) })
377
272
  ]
378
273
  }
379
274
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mkt-loitd/react-table-grid-custom",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "React Table Grid Custom component",
5
5
  "license": "ISC",
6
6
  "private": false,
@@ -12,6 +12,7 @@
12
12
  "types": "dist/index.d.ts",
13
13
  "exports": {
14
14
  ".": {
15
+ "types": "./dist/index.d.ts",
15
16
  "import": "./dist/index.js",
16
17
  "require": "./dist/index.cjs"
17
18
  }
@@ -28,9 +29,9 @@
28
29
  "release:patch": "npm version patch --no-git-tag-version"
29
30
  },
30
31
  "peerDependencies": {
32
+ "next": ">=13",
31
33
  "react": ">=18",
32
- "react-dom": ">=18",
33
- "next": ">=13"
34
+ "react-dom": ">=18"
34
35
  },
35
36
  "dependencies": {
36
37
  "@mantine/core": "^8.3.10",
@@ -49,7 +50,8 @@
49
50
  },
50
51
  "devDependencies": {
51
52
  "@types/lodash": "^4.17.21",
52
- "@types/react": "^18.2.66",
53
+ "@types/react": ">=18",
54
+ "@types/react-dom": ">=18",
53
55
  "tslib": "^2.8.1",
54
56
  "tsup": "^8.5.1",
55
57
  "typescript": "^5.9.3"