@skalfa/skalfa-app 1.0.0 → 1.0.1

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.
Files changed (87) hide show
  1. package/.env.example +43 -43
  2. package/.github/workflows/publish.yml +39 -0
  3. package/CONTRIBUTING.md +45 -0
  4. package/LICENSE +21 -0
  5. package/README.md +91 -28
  6. package/app/auth/edit/page.tsx +65 -65
  7. package/app/auth/login/page.tsx +63 -63
  8. package/app/auth/me/page.tsx +58 -58
  9. package/app/auth/register/page.tsx +69 -69
  10. package/app/auth/verify/page.tsx +53 -53
  11. package/app/dashboard/user/page.tsx +76 -76
  12. package/app/layout.tsx +37 -37
  13. package/app/manifest.ts +25 -0
  14. package/app/page.tsx +13 -13
  15. package/barrels.json +5 -5
  16. package/blueprints/starter.blueprint.json +102 -102
  17. package/bun.lock +916 -0
  18. package/components/base.components/chip/Chip.component.tsx +39 -39
  19. package/components/base.components/document/DocumentViewer.component.tsx +163 -163
  20. package/components/base.components/document/ExportExcel.component.tsx +340 -340
  21. package/components/base.components/document/ImportExcel.component.tsx +315 -315
  22. package/components/base.components/document/PrintTable.component.tsx +204 -204
  23. package/components/base.components/document/RenderPDF.component.tsx +415 -415
  24. package/components/base.components/input/Checkbox.component.tsx +109 -109
  25. package/components/base.components/input/Input.component.tsx +332 -332
  26. package/components/base.components/input/InputCheckbox.component.tsx +174 -174
  27. package/components/base.components/input/InputCurrency.component.tsx +163 -163
  28. package/components/base.components/input/InputDate.component.tsx +352 -352
  29. package/components/base.components/input/InputDatetime.component.tsx +260 -260
  30. package/components/base.components/input/InputDocument.component.tsx +351 -351
  31. package/components/base.components/input/InputImage.component.tsx +533 -533
  32. package/components/base.components/input/InputMap.component.tsx +317 -317
  33. package/components/base.components/input/InputNumber.component.tsx +192 -192
  34. package/components/base.components/input/InputOtp.component.tsx +169 -169
  35. package/components/base.components/input/InputPassword.component.tsx +236 -236
  36. package/components/base.components/input/InputRadio.component.tsx +175 -175
  37. package/components/base.components/input/InputTime.component.tsx +275 -275
  38. package/components/base.components/input/InputValues.component.tsx +68 -68
  39. package/components/base.components/input/Radio.component.tsx +102 -102
  40. package/components/base.components/input/Select.component.tsx +541 -541
  41. package/components/base.components/modal/BottomSheet.component.tsx +245 -245
  42. package/components/base.components/supervision/FormSupervision.component.tsx +433 -433
  43. package/components/base.components/supervision/TableSupervision.component.tsx +697 -697
  44. package/components/base.components/table/ControlBar.component.tsx +497 -497
  45. package/components/base.components/table/FilterComponent.tsx +518 -518
  46. package/components/base.components/table/Table.component.tsx +469 -469
  47. package/components/base.components/typography/TypographyArticle.component.tsx +26 -26
  48. package/components/base.components/typography/TypographyColumn.component.tsx +20 -20
  49. package/components/base.components/typography/TypographyContent.component.tsx +20 -20
  50. package/components/base.components/typography/TypographyTips.component.tsx +20 -20
  51. package/components/base.components/wrap/Draggable.component.tsx +303 -303
  52. package/components/base.components/wrap/IDBProvider.tsx +12 -12
  53. package/components/base.components/wrap/Image.component.tsx +9 -9
  54. package/components/base.components/wrap/ShortcutProvider.tsx +57 -57
  55. package/components/base.components/wrap/Swipe.component.tsx +93 -93
  56. package/components/index.ts +2 -2
  57. package/contexts/AppProvider.tsx +11 -11
  58. package/contexts/Auth.context.tsx +64 -64
  59. package/contexts/Toggle.context.tsx +44 -44
  60. package/next.config.ts +15 -1
  61. package/package.json +14 -13
  62. package/public/204.svg +19 -19
  63. package/public/500.svg +39 -39
  64. package/public/icon-192.png +0 -0
  65. package/public/icon-512.png +0 -0
  66. package/public/images/logo-fill.png +0 -0
  67. package/public/images/logo-full-fill.png +0 -0
  68. package/public/images/logo-full.png +0 -0
  69. package/public/images/logo.png +0 -0
  70. package/schema/idb/app.schema.ts +8 -8
  71. package/src-tauri/Cargo.toml +14 -0
  72. package/src-tauri/build.rs +3 -0
  73. package/src-tauri/capabilities/default.json +11 -0
  74. package/src-tauri/icons/128x128.png +0 -0
  75. package/src-tauri/icons/128x128@2x.png +0 -0
  76. package/src-tauri/icons/32x32.png +0 -0
  77. package/src-tauri/icons/icon.icns +0 -0
  78. package/src-tauri/icons/icon.ico +0 -0
  79. package/src-tauri/src/main.rs +7 -0
  80. package/src-tauri/tauri.conf.json +36 -0
  81. package/styles/globals.css +231 -231
  82. package/styles/tailwind.safelist +68 -68
  83. package/utils/commands/barrels.ts +27 -27
  84. package/utils/commands/light.ts +21 -21
  85. package/utils/commands/logger.ts +42 -42
  86. package/utils/commands/stubs/table-blueprint.stub +12 -12
  87. package/utils/commands/use-pdf.ts +29 -29
@@ -1,469 +1,469 @@
1
- "use client"
2
-
3
- import { isValidElement, ReactNode, useEffect, useMemo, useRef, useState } from "react";
4
- import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
5
- import { faArrowDownZA, faArrowUpAZ, faChevronLeft, faChevronRight } from "@fortawesome/free-solid-svg-icons";
6
- import { ApiFilterType, cn, pcn, useLazySearch, useResponsive } from "@utils";
7
- import { ControlBarComponent, ControlBarOptionType, PaginationComponent, PaginationProps, ScrollContainerComponent, FilterColumnOption, CheckboxComponent, SwipeComponent, SwipeActionType } from "@components";
8
-
9
-
10
-
11
- type CT = "controller-bar" | "head-column" | "column" | "row" | "floating-action" | "base";
12
-
13
- export interface TableColumnType {
14
- selector : string;
15
- label : string | ReactNode;
16
- width ?: string;
17
- sortable ?: boolean;
18
- searchable ?: boolean;
19
- filterable ?: boolean | {
20
- type : "text" | "number" | "currency" | "date";
21
- } | {
22
- type : "select";
23
- options : { label: string; value: any }[];
24
- };
25
- className ?: string;
26
- item ?: (data: any) => string | ReactNode;
27
- tip ?: string | ((data: any) => string);
28
- };
29
-
30
- export interface TableProps {
31
- id ?: string;
32
-
33
- controlBar ?: false | ControlBarOptionType[];
34
-
35
- columns : TableColumnType[];
36
- data : Record<string, any>[];
37
- pagination ?: PaginationProps | false;
38
-
39
- loading ?: boolean;
40
- sortBy ?: string[];
41
- onChangeSortBy ?: (sort: string[]) => void;
42
- search ?: string;
43
- onChangeSearch ?: (search: string) => void;
44
- searchableColumn ?: string[];
45
- onChangeSearchableColumn ?: (column: string) => void;
46
- filter ?: ApiFilterType[];
47
- onChangeFilter ?: (filters: ApiFilterType[]) => void;
48
- checks ?: (string | number)[];
49
- onChangeChecks ?: (checks: (string | number)[]) => void;
50
- actionBulking ?: ((checks: (string | number)[]) => ReactNode) | false
51
- focus ?: number | null,
52
- setFocus ?: (focus: number | null) => void,
53
-
54
- onRowClick ?: (data: Record<string, any>, key: number) => void;
55
- onRefresh ?: () => void;
56
-
57
- block ?: boolean;
58
- noIndex ?: boolean;
59
- responsiveControl ?: {
60
- mobile ?: {
61
- item ?: (item: Record<string, any>, key: number) => ReactNode,
62
- leftActionControl ?: Omit<SwipeActionType, "onAction"> & { onAction?: (item: Record<string, any>, key?: number) => void },
63
- rightActionControl ?: Omit<SwipeActionType, "onAction"> & { onAction?: (item: Record<string, any>, key?: number) => void },
64
- }
65
- }
66
-
67
- /** Use custom class with: "controller-bar::", "head-column::", "column::", "floating-action::", "row::". */
68
- className?: string;
69
- };
70
-
71
-
72
-
73
- export function TableComponent({
74
- id,
75
- controlBar,
76
- columns,
77
- data,
78
- pagination,
79
- loading,
80
-
81
- sortBy,
82
- onChangeSortBy,
83
- search,
84
- onChangeSearch,
85
- searchableColumn,
86
- onChangeSearchableColumn,
87
- filter,
88
- onChangeFilter,
89
- checks,
90
- onChangeChecks,
91
- actionBulking,
92
- focus,
93
-
94
- onRowClick,
95
- onRefresh,
96
-
97
- block,
98
- noIndex,
99
- responsiveControl,
100
-
101
- className = "",
102
- }: TableProps) {
103
- const [displayColumns, setDisplayColumns] = useState<string[]>([]);
104
- const [showFloatingAction, setShowFloatingAction] = useState(false);
105
- const [floatingActionActive, setFloatingActionActive] = useState<false | number>(false);
106
- const [keyword, setKeyword] = useState<string>("");
107
- const [keywordSearch] = useLazySearch(keyword);
108
- const { isSm } = useResponsive ();
109
-
110
- const actionColumnRef = useRef<HTMLDivElement>(null);
111
-
112
- useEffect(() => {
113
- if (columns) setDisplayColumns([...columns.map((column) => column.selector)]);
114
- }, [columns]);
115
-
116
-
117
- useEffect(() => {
118
- setKeyword(search || "");
119
- }, [search]);
120
-
121
- useEffect(() => {
122
- keywordSearch ? onChangeSearch?.(keywordSearch) : onChangeSearch?.("");
123
-
124
- if(pagination != false) {
125
- pagination?.onChange?.(pagination.totalRow, pagination.paginate, 1);
126
- }
127
- }, [keywordSearch]);
128
-
129
- const columnMapping = useMemo(() => {
130
- return ( columns?.filter((column) => displayColumns.includes(column.selector)) || []);
131
- }, [columns, displayColumns]);
132
-
133
-
134
-
135
- const styles = {
136
- head : "px-4 py-2.5 font-bold w-full flex justify-between gap-2 items-center text-sm text-foreground capitalize",
137
- column : cn("px-4 py-4 font-medium", pcn<CT>(className, "column")),
138
- row : "flex items-center gap-4 rounded-[4px] relative opacity-0 animate-intro-fade border-b hover:bg-light-primary/30",
139
- floatingAction : cn("sticky bg-background -right-5 z-30 cursor-pointer flex items-center shadow rounded-l-lg", pcn<CT>(className, "floating-action")),
140
- };
141
-
142
-
143
- const numberOfRow = (key: number) => pagination && (pagination?.page || 1) != 1 ? pagination?.paginate * ((pagination?.page || 1) - 1) + key + 1 : key + 1;
144
-
145
-
146
- function renderHead() {
147
- return (
148
- <>
149
- {columnMapping?.map((column, key) => {
150
- const sortColumn = sortBy?.find((e) => e.split(" ")?.at(0) == column.selector)?.split(" ")?.at(0) || "";
151
- const sortDirection = sortBy?.find((e) => e.split(" ")?.at(0) == column.selector)?.split(" ")?.at(1) || "";
152
-
153
- return (
154
- <div
155
- key={key}
156
- className={cn(
157
- styles?.head,
158
- column.sortable && "cursor-pointer",
159
- pcn<CT>(className, "head-column")
160
- )}
161
- style={{ width: column.width ? column.width : 200 }}
162
- onClick={() => column.sortable && onChangeSortBy?.([`${column.selector} ${sortDirection == "desc" ? "asc" : "desc"}`])}
163
- >
164
- {column.label}
165
-
166
- {!!sortColumn && (
167
- <FontAwesomeIcon
168
- icon={sortDirection == "desc" ? faArrowDownZA : faArrowUpAZ}
169
- className="text-light-foreground/70"
170
- />
171
- )}
172
- </div>
173
- );
174
- })}
175
- </>
176
- );
177
- }
178
-
179
-
180
- function renderItem(item: Record<string, any>, itemKey: number) {
181
- const itemMapping = columnMapping.map((column) => {
182
- if (column?.item) {
183
- return column.item(item);
184
- }
185
-
186
- const value = item[column.selector];
187
-
188
- if (isValidElement(value)) {
189
- return value;
190
- }
191
-
192
- if (value === null || value === undefined) {
193
- return "-";
194
- }
195
-
196
- if (typeof value === "object") {
197
- return JSON.stringify(value);
198
- }
199
-
200
- return value;
201
- });
202
-
203
- if(!isSm || !responsiveControl?.mobile) {
204
- return (
205
- <>
206
- {itemMapping?.map((one, key) => {
207
- const column = columnMapping?.[key];
208
-
209
- let title = one as string;
210
- if (column?.tip) {
211
- if (typeof column.tip === "string") {
212
- title = (item[column.tip as keyof object] as any)?.toString() || "-";
213
- } else if (typeof column.tip === "function") {
214
- title = column.tip(item);
215
- }
216
- }
217
- return (
218
- <div
219
- key={key}
220
- className={cn(styles.column , onRowClick && "cursor-pointer")}
221
- style={{ width: columnMapping?.at(key)?.width || 200 }}
222
- onClick={() => onRowClick?.(item, itemKey) }
223
- title={title}
224
- >
225
- {one}
226
- </div>
227
- );
228
- })}
229
- </>
230
- );
231
- } else {
232
-
233
- const { onAction: onLeftAction, ...restLeftAction } = responsiveControl?.mobile?.leftActionControl || {};
234
- const { onAction: onRightAction, ...restRightAction } = responsiveControl?.mobile?.rightActionControl || {};
235
-
236
- return (
237
- <SwipeComponent
238
- className="border-b py-2 px-2 bg-white"
239
- leftActionControl={!!responsiveControl?.mobile?.leftActionControl ? {
240
- ...restLeftAction,
241
- ...(onLeftAction ? { onAction: () => onLeftAction?.(item, itemKey)} : {})
242
- } : undefined}
243
- rightActionControl={!!responsiveControl?.mobile?.rightActionControl ? {
244
- ...restRightAction,
245
- ...(onRightAction ? { onAction: () => onRightAction?.(item, itemKey)} : {})
246
- } : undefined}
247
- >
248
- {responsiveControl?.mobile?.item ? responsiveControl?.mobile?.item(item, itemKey) : (
249
- <div onClick={() => onRowClick?.(item, itemKey) }>
250
- <p className="font-semibold">{Object.values(itemMapping)[0]}</p>
251
- <p className="text-sm">{Object.values(itemMapping)[1]}</p>
252
- </div>
253
- )}
254
- </SwipeComponent>
255
- )
256
- }
257
- }
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
- return (
270
- <div className={cn("relative", pcn<CT>(className, "base"))}>
271
- {controlBar != false && (
272
- <ControlBarComponent
273
- id={id}
274
- options={!controlBar ? ["SEARCH", "SELECTABLE", "REFRESH"] : controlBar}
275
- searchableOptions={columns?.filter((c: TableColumnType) => c.searchable)}
276
- onSearchable={(e) => onChangeSearchableColumn?.(String(e))}
277
- searchable={searchableColumn || []}
278
- onSearch={(e) => setKeyword(e)}
279
- search={keyword}
280
- selectableOptions={columns}
281
- onSelectable={(e) => setDisplayColumns(e)}
282
- selectable={displayColumns}
283
- sortableOptions={columns?.filter((c: TableColumnType) => c.sortable)}
284
- sort={sortBy}
285
- onSort={(sort) => onChangeSortBy?.(sort)}
286
- onRefresh={() => onRefresh?.()}
287
- filterableColumns={columns?.filter((c) => !!c?.filterable)?.map((c) => ({
288
- label: c.label,
289
- selector: c.selector,
290
- type: typeof c?.filterable == "object" ? c?.filterable?.type : "text",
291
- options: typeof c?.filterable == "object" && c?.filterable?.type == "select" ? c?.filterable?.options : undefined
292
- })) as FilterColumnOption[]}
293
- onFilter={(filters) => onChangeFilter?.(filters)}
294
- filter={filter}
295
- className={pcn<CT>(className, "controller-bar") || ""}
296
- />
297
- )}
298
-
299
- <div className="relative">
300
- <ScrollContainerComponent
301
- scrollFloating={!isSm && block}
302
- className="w-full"
303
- onScroll={(e) => {
304
- actionColumnRef.current?.clientWidth && e.scrollLeft &&
305
- setShowFloatingAction(e.scrollLeft + e.clientWidth <= e.scrollWidth - actionColumnRef.current?.clientWidth);
306
- }}
307
- footer={
308
- <>
309
- {block && pagination && (
310
- <>
311
- <div className="py-6"></div>
312
- <div className="my-2 absolute bottom-0 w-full">
313
- <PaginationComponent {...pagination} />
314
- </div>
315
- </>
316
- )}
317
- </>
318
- }
319
- >
320
- {loading ? (
321
- <>
322
- {
323
- // =========================>
324
- // ## When Loading
325
- // =========================>
326
- }
327
- <div className="w-max min-w-full">
328
- <div className="flex flex-col items-center justify-center gap-8 p-20">
329
- <h1 className="text-lg text-light-foreground animate-pulse">
330
- Memuat data...
331
- </h1>
332
- </div>
333
- </div>
334
- </>
335
- ) : !data || !data.length ? (
336
- <>
337
- {
338
- // =========================>
339
- // ## When Empty
340
- // =========================>
341
- }
342
- <div className="flex flex-col items-center justify-center gap-8 p-20 opacity-50">
343
- <h1 className="text-lg text-light-foreground">
344
- Belum Ada Data
345
- </h1>
346
- </div>
347
- </>
348
- ) : (
349
- <>
350
- {!isSm || !responsiveControl?.mobile ? (
351
- <>
352
- <div className="w-max min-w-full">
353
- {
354
- // =========================>
355
- // ## Head Column
356
- // =========================>
357
- }
358
- <div className={cn("flex gap-4", pcn<CT>(className, "row"))}>
359
- {!!actionBulking && (
360
- <div className={cn(styles.head, "w-max")}>
361
- <CheckboxComponent
362
- name="selected_table"
363
- className="w-5 h-5"
364
- checked={data.length > 0 && checks?.length === data.length}
365
- onChange={() => data.length > 0 && checks?.length === data.length ? onChangeChecks?.([]) : onChangeChecks?.(data.map((d) => d.id))}
366
- />
367
- </div>
368
- )}
369
- {!noIndex && <div className={cn(styles.head, "w-8", pcn<CT>(className, "head-column"))}>#</div>}
370
- {renderHead()}
371
- </div>
372
-
373
- {
374
- // =========================>
375
- // ## Body Column
376
- // =========================>
377
- }
378
- <div className={`flex flex-col gap-y-0.5`}>
379
- {data.map((item: Record<string, any>, key) => {
380
- return (
381
- <div
382
- style={{ animationDelay: `${(key + 1) * 0.05}s` }}
383
- className={cn(
384
- styles.row,
385
- key % 2 ? "bg-light-primary/10" : "bg-white",
386
- focus == key && "bg-light-primary/30",
387
- pcn<CT>(className, "row")
388
- )}
389
- key={key}
390
- >
391
- {!!actionBulking && (
392
- <div className={cn("w-max", styles.column)}>
393
- <CheckboxComponent
394
- name="selected_table"
395
- className="w-5 h-5"
396
- checked={checks?.includes(item?.id)}
397
- onChange={() => checks?.includes(item?.id) ? onChangeChecks?.(checks.filter((i) => i !== item?.id)) : onChangeChecks?.([...(checks || []), item?.id])}
398
- />
399
- </div>
400
- )}
401
- {!noIndex && <div className={cn("w-8", styles?.column)}>{numberOfRow(key)}</div>}
402
- {renderItem(item, key)}
403
- <div ref={actionColumnRef} className={cn(`flex-1 flex justify-end gap-2 px-4 py-2`)}>
404
- {item["action" as keyof object]}
405
- </div>
406
-
407
- {item["action" as keyof object] &&
408
- showFloatingAction && (
409
- <div
410
- className={styles.floatingAction}
411
- onClick={() =>
412
- floatingActionActive !== false &&
413
- floatingActionActive == key ? setFloatingActionActive(false) : setFloatingActionActive(key)
414
- }
415
- >
416
- <div className="pl-4 pr-7 py-2">
417
- <FontAwesomeIcon icon={floatingActionActive === false || floatingActionActive != key ? faChevronLeft : faChevronRight}/>
418
- </div>
419
-
420
- <div className={`py-1 flex gap-2 ${floatingActionActive !== false && floatingActionActive == key ? "w-max pl-2 pr-8" : "w-0"}`}>
421
- {item["action" as keyof object]}
422
- </div>
423
- </div>
424
- )}
425
- </div>
426
- );
427
- })}
428
- </div>
429
- </div>
430
- </>
431
- ) : (
432
- <>
433
- <div className={`w-full flex flex-col gap-y-0.5`}>
434
- {data.map((item: Record<string, any>, key) => {
435
- return (
436
- <div
437
- style={{ animationDelay: `${(key + 1) * 0.05}s` }}
438
- key={key}
439
- >
440
- {renderItem(item, key)}
441
- </div>
442
- )
443
- })}
444
- </div>
445
- </>
446
- )}
447
-
448
- </>
449
- )}
450
- </ScrollContainerComponent>
451
- </div>
452
-
453
- {!!actionBulking && !!checks?.length && (
454
- <div className="rounded-[6px] bg-white mt-4 w-full px-4 py-2 border flex justify-between items-center">
455
- <div className="text-sm font-semibold">{checks?.length} Data Terpilih</div>
456
- <div className="flex justify-end items-center gap-2">
457
- {actionBulking?.(checks)}
458
- </div>
459
- </div>
460
- )}
461
-
462
- {!block && pagination && (
463
- <div className="mt-4">
464
- <PaginationComponent {...pagination} />
465
- </div>
466
- )}
467
- </div>
468
- );
469
- }
1
+ "use client"
2
+
3
+ import { isValidElement, ReactNode, useEffect, useMemo, useRef, useState } from "react";
4
+ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
5
+ import { faArrowDownZA, faArrowUpAZ, faChevronLeft, faChevronRight } from "@fortawesome/free-solid-svg-icons";
6
+ import { ApiFilterType, cn, pcn, useLazySearch, useResponsive } from "@utils";
7
+ import { ControlBarComponent, ControlBarOptionType, PaginationComponent, PaginationProps, ScrollContainerComponent, FilterColumnOption, CheckboxComponent, SwipeComponent, SwipeActionType } from "@components";
8
+
9
+
10
+
11
+ type CT = "controller-bar" | "head-column" | "column" | "row" | "floating-action" | "base";
12
+
13
+ export interface TableColumnType {
14
+ selector : string;
15
+ label : string | ReactNode;
16
+ width ?: string;
17
+ sortable ?: boolean;
18
+ searchable ?: boolean;
19
+ filterable ?: boolean | {
20
+ type : "text" | "number" | "currency" | "date";
21
+ } | {
22
+ type : "select";
23
+ options : { label: string; value: any }[];
24
+ };
25
+ className ?: string;
26
+ item ?: (data: any) => string | ReactNode;
27
+ tip ?: string | ((data: any) => string);
28
+ };
29
+
30
+ export interface TableProps {
31
+ id ?: string;
32
+
33
+ controlBar ?: false | ControlBarOptionType[];
34
+
35
+ columns : TableColumnType[];
36
+ data : Record<string, any>[];
37
+ pagination ?: PaginationProps | false;
38
+
39
+ loading ?: boolean;
40
+ sortBy ?: string[];
41
+ onChangeSortBy ?: (sort: string[]) => void;
42
+ search ?: string;
43
+ onChangeSearch ?: (search: string) => void;
44
+ searchableColumn ?: string[];
45
+ onChangeSearchableColumn ?: (column: string) => void;
46
+ filter ?: ApiFilterType[];
47
+ onChangeFilter ?: (filters: ApiFilterType[]) => void;
48
+ checks ?: (string | number)[];
49
+ onChangeChecks ?: (checks: (string | number)[]) => void;
50
+ actionBulking ?: ((checks: (string | number)[]) => ReactNode) | false
51
+ focus ?: number | null,
52
+ setFocus ?: (focus: number | null) => void,
53
+
54
+ onRowClick ?: (data: Record<string, any>, key: number) => void;
55
+ onRefresh ?: () => void;
56
+
57
+ block ?: boolean;
58
+ noIndex ?: boolean;
59
+ responsiveControl ?: {
60
+ mobile ?: {
61
+ item ?: (item: Record<string, any>, key: number) => ReactNode,
62
+ leftActionControl ?: Omit<SwipeActionType, "onAction"> & { onAction?: (item: Record<string, any>, key?: number) => void },
63
+ rightActionControl ?: Omit<SwipeActionType, "onAction"> & { onAction?: (item: Record<string, any>, key?: number) => void },
64
+ }
65
+ }
66
+
67
+ /** Use custom class with: "controller-bar::", "head-column::", "column::", "floating-action::", "row::". */
68
+ className?: string;
69
+ };
70
+
71
+
72
+
73
+ export function TableComponent({
74
+ id,
75
+ controlBar,
76
+ columns,
77
+ data,
78
+ pagination,
79
+ loading,
80
+
81
+ sortBy,
82
+ onChangeSortBy,
83
+ search,
84
+ onChangeSearch,
85
+ searchableColumn,
86
+ onChangeSearchableColumn,
87
+ filter,
88
+ onChangeFilter,
89
+ checks,
90
+ onChangeChecks,
91
+ actionBulking,
92
+ focus,
93
+
94
+ onRowClick,
95
+ onRefresh,
96
+
97
+ block,
98
+ noIndex,
99
+ responsiveControl,
100
+
101
+ className = "",
102
+ }: TableProps) {
103
+ const [displayColumns, setDisplayColumns] = useState<string[]>([]);
104
+ const [showFloatingAction, setShowFloatingAction] = useState(false);
105
+ const [floatingActionActive, setFloatingActionActive] = useState<false | number>(false);
106
+ const [keyword, setKeyword] = useState<string>("");
107
+ const [keywordSearch] = useLazySearch(keyword);
108
+ const { isSm } = useResponsive ();
109
+
110
+ const actionColumnRef = useRef<HTMLDivElement>(null);
111
+
112
+ useEffect(() => {
113
+ if (columns) setDisplayColumns([...columns.map((column) => column.selector)]);
114
+ }, [columns]);
115
+
116
+
117
+ useEffect(() => {
118
+ setKeyword(search || "");
119
+ }, [search]);
120
+
121
+ useEffect(() => {
122
+ keywordSearch ? onChangeSearch?.(keywordSearch) : onChangeSearch?.("");
123
+
124
+ if(pagination != false) {
125
+ pagination?.onChange?.(pagination.totalRow, pagination.paginate, 1);
126
+ }
127
+ }, [keywordSearch]);
128
+
129
+ const columnMapping = useMemo(() => {
130
+ return ( columns?.filter((column) => displayColumns.includes(column.selector)) || []);
131
+ }, [columns, displayColumns]);
132
+
133
+
134
+
135
+ const styles = {
136
+ head : "px-4 py-2.5 font-bold w-full flex justify-between gap-2 items-center text-sm text-foreground capitalize",
137
+ column : cn("px-4 py-4 font-medium", pcn<CT>(className, "column")),
138
+ row : "flex items-center gap-4 rounded-[4px] relative opacity-0 animate-intro-fade border-b hover:bg-light-primary/30",
139
+ floatingAction : cn("sticky bg-background -right-5 z-30 cursor-pointer flex items-center shadow rounded-l-lg", pcn<CT>(className, "floating-action")),
140
+ };
141
+
142
+
143
+ const numberOfRow = (key: number) => pagination && (pagination?.page || 1) != 1 ? pagination?.paginate * ((pagination?.page || 1) - 1) + key + 1 : key + 1;
144
+
145
+
146
+ function renderHead() {
147
+ return (
148
+ <>
149
+ {columnMapping?.map((column, key) => {
150
+ const sortColumn = sortBy?.find((e) => e.split(" ")?.at(0) == column.selector)?.split(" ")?.at(0) || "";
151
+ const sortDirection = sortBy?.find((e) => e.split(" ")?.at(0) == column.selector)?.split(" ")?.at(1) || "";
152
+
153
+ return (
154
+ <div
155
+ key={key}
156
+ className={cn(
157
+ styles?.head,
158
+ column.sortable && "cursor-pointer",
159
+ pcn<CT>(className, "head-column")
160
+ )}
161
+ style={{ width: column.width ? column.width : 200 }}
162
+ onClick={() => column.sortable && onChangeSortBy?.([`${column.selector} ${sortDirection == "desc" ? "asc" : "desc"}`])}
163
+ >
164
+ {column.label}
165
+
166
+ {!!sortColumn && (
167
+ <FontAwesomeIcon
168
+ icon={sortDirection == "desc" ? faArrowDownZA : faArrowUpAZ}
169
+ className="text-light-foreground/70"
170
+ />
171
+ )}
172
+ </div>
173
+ );
174
+ })}
175
+ </>
176
+ );
177
+ }
178
+
179
+
180
+ function renderItem(item: Record<string, any>, itemKey: number) {
181
+ const itemMapping = columnMapping.map((column) => {
182
+ if (column?.item) {
183
+ return column.item(item);
184
+ }
185
+
186
+ const value = item[column.selector];
187
+
188
+ if (isValidElement(value)) {
189
+ return value;
190
+ }
191
+
192
+ if (value === null || value === undefined) {
193
+ return "-";
194
+ }
195
+
196
+ if (typeof value === "object") {
197
+ return JSON.stringify(value);
198
+ }
199
+
200
+ return value;
201
+ });
202
+
203
+ if(!isSm || !responsiveControl?.mobile) {
204
+ return (
205
+ <>
206
+ {itemMapping?.map((one, key) => {
207
+ const column = columnMapping?.[key];
208
+
209
+ let title = one as string;
210
+ if (column?.tip) {
211
+ if (typeof column.tip === "string") {
212
+ title = (item[column.tip as keyof object] as any)?.toString() || "-";
213
+ } else if (typeof column.tip === "function") {
214
+ title = column.tip(item);
215
+ }
216
+ }
217
+ return (
218
+ <div
219
+ key={key}
220
+ className={cn(styles.column , onRowClick && "cursor-pointer")}
221
+ style={{ width: columnMapping?.at(key)?.width || 200 }}
222
+ onClick={() => onRowClick?.(item, itemKey) }
223
+ title={title}
224
+ >
225
+ {one}
226
+ </div>
227
+ );
228
+ })}
229
+ </>
230
+ );
231
+ } else {
232
+
233
+ const { onAction: onLeftAction, ...restLeftAction } = responsiveControl?.mobile?.leftActionControl || {};
234
+ const { onAction: onRightAction, ...restRightAction } = responsiveControl?.mobile?.rightActionControl || {};
235
+
236
+ return (
237
+ <SwipeComponent
238
+ className="border-b py-2 px-2 bg-white"
239
+ leftActionControl={!!responsiveControl?.mobile?.leftActionControl ? {
240
+ ...restLeftAction,
241
+ ...(onLeftAction ? { onAction: () => onLeftAction?.(item, itemKey)} : {})
242
+ } : undefined}
243
+ rightActionControl={!!responsiveControl?.mobile?.rightActionControl ? {
244
+ ...restRightAction,
245
+ ...(onRightAction ? { onAction: () => onRightAction?.(item, itemKey)} : {})
246
+ } : undefined}
247
+ >
248
+ {responsiveControl?.mobile?.item ? responsiveControl?.mobile?.item(item, itemKey) : (
249
+ <div onClick={() => onRowClick?.(item, itemKey) }>
250
+ <p className="font-semibold">{Object.values(itemMapping)[0]}</p>
251
+ <p className="text-sm">{Object.values(itemMapping)[1]}</p>
252
+ </div>
253
+ )}
254
+ </SwipeComponent>
255
+ )
256
+ }
257
+ }
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
+ return (
270
+ <div className={cn("relative", pcn<CT>(className, "base"))}>
271
+ {controlBar != false && (
272
+ <ControlBarComponent
273
+ id={id}
274
+ options={!controlBar ? ["SEARCH", "SELECTABLE", "REFRESH"] : controlBar}
275
+ searchableOptions={columns?.filter((c: TableColumnType) => c.searchable)}
276
+ onSearchable={(e) => onChangeSearchableColumn?.(String(e))}
277
+ searchable={searchableColumn || []}
278
+ onSearch={(e) => setKeyword(e)}
279
+ search={keyword}
280
+ selectableOptions={columns}
281
+ onSelectable={(e) => setDisplayColumns(e)}
282
+ selectable={displayColumns}
283
+ sortableOptions={columns?.filter((c: TableColumnType) => c.sortable)}
284
+ sort={sortBy}
285
+ onSort={(sort) => onChangeSortBy?.(sort)}
286
+ onRefresh={() => onRefresh?.()}
287
+ filterableColumns={columns?.filter((c) => !!c?.filterable)?.map((c) => ({
288
+ label: c.label,
289
+ selector: c.selector,
290
+ type: typeof c?.filterable == "object" ? c?.filterable?.type : "text",
291
+ options: typeof c?.filterable == "object" && c?.filterable?.type == "select" ? c?.filterable?.options : undefined
292
+ })) as FilterColumnOption[]}
293
+ onFilter={(filters) => onChangeFilter?.(filters)}
294
+ filter={filter}
295
+ className={pcn<CT>(className, "controller-bar") || ""}
296
+ />
297
+ )}
298
+
299
+ <div className="relative">
300
+ <ScrollContainerComponent
301
+ scrollFloating={!isSm && block}
302
+ className="w-full"
303
+ onScroll={(e) => {
304
+ actionColumnRef.current?.clientWidth && e.scrollLeft &&
305
+ setShowFloatingAction(e.scrollLeft + e.clientWidth <= e.scrollWidth - actionColumnRef.current?.clientWidth);
306
+ }}
307
+ footer={
308
+ <>
309
+ {block && pagination && (
310
+ <>
311
+ <div className="py-6"></div>
312
+ <div className="my-2 absolute bottom-0 w-full">
313
+ <PaginationComponent {...pagination} />
314
+ </div>
315
+ </>
316
+ )}
317
+ </>
318
+ }
319
+ >
320
+ {loading ? (
321
+ <>
322
+ {
323
+ // =========================>
324
+ // ## When Loading
325
+ // =========================>
326
+ }
327
+ <div className="w-max min-w-full">
328
+ <div className="flex flex-col items-center justify-center gap-8 p-20">
329
+ <h1 className="text-lg text-light-foreground animate-pulse">
330
+ Memuat data...
331
+ </h1>
332
+ </div>
333
+ </div>
334
+ </>
335
+ ) : !data || !data.length ? (
336
+ <>
337
+ {
338
+ // =========================>
339
+ // ## When Empty
340
+ // =========================>
341
+ }
342
+ <div className="flex flex-col items-center justify-center gap-8 p-20 opacity-50">
343
+ <h1 className="text-lg text-light-foreground">
344
+ Belum Ada Data
345
+ </h1>
346
+ </div>
347
+ </>
348
+ ) : (
349
+ <>
350
+ {!isSm || !responsiveControl?.mobile ? (
351
+ <>
352
+ <div className="w-max min-w-full">
353
+ {
354
+ // =========================>
355
+ // ## Head Column
356
+ // =========================>
357
+ }
358
+ <div className={cn("flex gap-4", pcn<CT>(className, "row"))}>
359
+ {!!actionBulking && (
360
+ <div className={cn(styles.head, "w-max")}>
361
+ <CheckboxComponent
362
+ name="selected_table"
363
+ className="w-5 h-5"
364
+ checked={data.length > 0 && checks?.length === data.length}
365
+ onChange={() => data.length > 0 && checks?.length === data.length ? onChangeChecks?.([]) : onChangeChecks?.(data.map((d) => d.id))}
366
+ />
367
+ </div>
368
+ )}
369
+ {!noIndex && <div className={cn(styles.head, "w-8", pcn<CT>(className, "head-column"))}>#</div>}
370
+ {renderHead()}
371
+ </div>
372
+
373
+ {
374
+ // =========================>
375
+ // ## Body Column
376
+ // =========================>
377
+ }
378
+ <div className={`flex flex-col gap-y-0.5`}>
379
+ {data.map((item: Record<string, any>, key) => {
380
+ return (
381
+ <div
382
+ style={{ animationDelay: `${(key + 1) * 0.05}s` }}
383
+ className={cn(
384
+ styles.row,
385
+ key % 2 ? "bg-light-primary/10" : "bg-white",
386
+ focus == key && "bg-light-primary/30",
387
+ pcn<CT>(className, "row")
388
+ )}
389
+ key={key}
390
+ >
391
+ {!!actionBulking && (
392
+ <div className={cn("w-max", styles.column)}>
393
+ <CheckboxComponent
394
+ name="selected_table"
395
+ className="w-5 h-5"
396
+ checked={checks?.includes(item?.id)}
397
+ onChange={() => checks?.includes(item?.id) ? onChangeChecks?.(checks.filter((i) => i !== item?.id)) : onChangeChecks?.([...(checks || []), item?.id])}
398
+ />
399
+ </div>
400
+ )}
401
+ {!noIndex && <div className={cn("w-8", styles?.column)}>{numberOfRow(key)}</div>}
402
+ {renderItem(item, key)}
403
+ <div ref={actionColumnRef} className={cn(`flex-1 flex justify-end gap-2 px-4 py-2`)}>
404
+ {item["action" as keyof object]}
405
+ </div>
406
+
407
+ {item["action" as keyof object] &&
408
+ showFloatingAction && (
409
+ <div
410
+ className={styles.floatingAction}
411
+ onClick={() =>
412
+ floatingActionActive !== false &&
413
+ floatingActionActive == key ? setFloatingActionActive(false) : setFloatingActionActive(key)
414
+ }
415
+ >
416
+ <div className="pl-4 pr-7 py-2">
417
+ <FontAwesomeIcon icon={floatingActionActive === false || floatingActionActive != key ? faChevronLeft : faChevronRight}/>
418
+ </div>
419
+
420
+ <div className={`py-1 flex gap-2 ${floatingActionActive !== false && floatingActionActive == key ? "w-max pl-2 pr-8" : "w-0"}`}>
421
+ {item["action" as keyof object]}
422
+ </div>
423
+ </div>
424
+ )}
425
+ </div>
426
+ );
427
+ })}
428
+ </div>
429
+ </div>
430
+ </>
431
+ ) : (
432
+ <>
433
+ <div className={`w-full flex flex-col gap-y-0.5`}>
434
+ {data.map((item: Record<string, any>, key) => {
435
+ return (
436
+ <div
437
+ style={{ animationDelay: `${(key + 1) * 0.05}s` }}
438
+ key={key}
439
+ >
440
+ {renderItem(item, key)}
441
+ </div>
442
+ )
443
+ })}
444
+ </div>
445
+ </>
446
+ )}
447
+
448
+ </>
449
+ )}
450
+ </ScrollContainerComponent>
451
+ </div>
452
+
453
+ {!!actionBulking && !!checks?.length && (
454
+ <div className="rounded-[6px] bg-white mt-4 w-full px-4 py-2 border flex justify-between items-center">
455
+ <div className="text-sm font-semibold">{checks?.length} Data Terpilih</div>
456
+ <div className="flex justify-end items-center gap-2">
457
+ {actionBulking?.(checks)}
458
+ </div>
459
+ </div>
460
+ )}
461
+
462
+ {!block && pagination && (
463
+ <div className="mt-4">
464
+ <PaginationComponent {...pagination} />
465
+ </div>
466
+ )}
467
+ </div>
468
+ );
469
+ }