@mdaushi/kinetics-core 0.1.0-a
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/controller.d.ts +37 -0
- package/dist/controller.js +97 -0
- package/dist/formatter.d.ts +5 -0
- package/dist/formatter.js +21 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -0
- package/dist/types.d.ts +55 -0
- package/dist/types.js +2 -0
- package/package.json +42 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { TableMeta, TableState } from "./types";
|
|
2
|
+
export interface TableControllerOptions {
|
|
3
|
+
url?: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class TableController {
|
|
6
|
+
private readonly state;
|
|
7
|
+
private readonly meta;
|
|
8
|
+
constructor(state: TableState, meta: TableMeta);
|
|
9
|
+
baseParams(): Record<string, unknown>;
|
|
10
|
+
/**
|
|
11
|
+
* Receive TanStack SortingState directly.
|
|
12
|
+
*/
|
|
13
|
+
resolveSort(sorting: Array<{
|
|
14
|
+
id: string;
|
|
15
|
+
desc: boolean;
|
|
16
|
+
}>): Record<string, unknown>;
|
|
17
|
+
resolveSearchParams(search: string): Record<string, unknown>;
|
|
18
|
+
resolveFilterParams(key: string, value: unknown): Record<string, unknown>;
|
|
19
|
+
resolvePageParams(pageIndex: number, pageSize: number): Record<string, unknown>;
|
|
20
|
+
/**
|
|
21
|
+
* Receive TanStack PaginationState directly.
|
|
22
|
+
*/
|
|
23
|
+
resolvePagination(pagination: {
|
|
24
|
+
pageIndex: number;
|
|
25
|
+
pageSize: number;
|
|
26
|
+
}): Record<string, unknown>;
|
|
27
|
+
/** TanStack SortingState from current server state */
|
|
28
|
+
toSorting(): Array<{
|
|
29
|
+
id: string;
|
|
30
|
+
desc: boolean;
|
|
31
|
+
}>;
|
|
32
|
+
/** TanStack PaginationState from current server state */
|
|
33
|
+
toPagination(): {
|
|
34
|
+
pageIndex: number;
|
|
35
|
+
pageSize: number;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// All the logic for constructing query params — no dependencies on React,
|
|
2
|
+
// Vue, or any other framework. The adapter (React/Vue) simply calls this method
|
|
3
|
+
// and passes the results to its respective router.
|
|
4
|
+
export class TableController {
|
|
5
|
+
constructor(state, meta) {
|
|
6
|
+
this.state = state;
|
|
7
|
+
this.meta = meta;
|
|
8
|
+
}
|
|
9
|
+
// Base params from current state
|
|
10
|
+
baseParams() {
|
|
11
|
+
return {
|
|
12
|
+
sort: this.state.sort,
|
|
13
|
+
direction: this.state.direction,
|
|
14
|
+
search: this.state.search,
|
|
15
|
+
filters: this.state.filters,
|
|
16
|
+
per_page: this.state.per_page,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
// Sort
|
|
20
|
+
// resolveSortParams(columnId: string | null): Record<string, unknown> {
|
|
21
|
+
// if (!columnId) {
|
|
22
|
+
// return { ...this.baseParams(), sort: null, direction: "asc", page: 1 };
|
|
23
|
+
// }
|
|
24
|
+
// const isSameColumn = this.state.sort === columnId;
|
|
25
|
+
// const direction =
|
|
26
|
+
// isSameColumn && this.state.direction === "asc" ? "desc" : "asc";
|
|
27
|
+
// return {
|
|
28
|
+
// ...this.baseParams(),
|
|
29
|
+
// sort: columnId,
|
|
30
|
+
// direction,
|
|
31
|
+
// page: 1,
|
|
32
|
+
// };
|
|
33
|
+
// }
|
|
34
|
+
/**
|
|
35
|
+
* Receive TanStack SortingState directly.
|
|
36
|
+
*/
|
|
37
|
+
resolveSort(sorting) {
|
|
38
|
+
const col = sorting[0] ?? null;
|
|
39
|
+
return {
|
|
40
|
+
...this.baseParams(),
|
|
41
|
+
sort: col?.id ?? null,
|
|
42
|
+
direction: col?.desc ? "desc" : "asc",
|
|
43
|
+
page: 1,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// Search
|
|
47
|
+
resolveSearchParams(search) {
|
|
48
|
+
return {
|
|
49
|
+
...this.baseParams(),
|
|
50
|
+
search,
|
|
51
|
+
page: 1,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
// Filter
|
|
55
|
+
resolveFilterParams(key, value) {
|
|
56
|
+
const filters = { ...this.state.filters };
|
|
57
|
+
if (value === null || value === "" || value === undefined) {
|
|
58
|
+
delete filters[key];
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
filters[key] = value;
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
...this.baseParams(),
|
|
65
|
+
filters,
|
|
66
|
+
page: 1,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
// Pagination
|
|
70
|
+
resolvePageParams(pageIndex, pageSize) {
|
|
71
|
+
return {
|
|
72
|
+
...this.baseParams(),
|
|
73
|
+
page: pageIndex + 1, // TanStack 0-based -> Laravel 1-based
|
|
74
|
+
per_page: pageSize,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Receive TanStack PaginationState directly.
|
|
79
|
+
*/
|
|
80
|
+
resolvePagination(pagination) {
|
|
81
|
+
return this.resolvePageParams(pagination.pageIndex, pagination.pageSize);
|
|
82
|
+
}
|
|
83
|
+
// Helpers
|
|
84
|
+
/** TanStack SortingState from current server state */
|
|
85
|
+
toSorting() {
|
|
86
|
+
if (!this.state.sort)
|
|
87
|
+
return [];
|
|
88
|
+
return [{ id: this.state.sort, desc: this.state.direction === "desc" }];
|
|
89
|
+
}
|
|
90
|
+
/** TanStack PaginationState from current server state */
|
|
91
|
+
toPagination() {
|
|
92
|
+
return {
|
|
93
|
+
pageIndex: this.meta.current_page - 1,
|
|
94
|
+
pageSize: this.meta.per_page,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats the value according to the column type to display as a string.
|
|
3
|
+
* Adapters (React/Vue) can use this directly or override it on a per-column basis.
|
|
4
|
+
*/
|
|
5
|
+
export function formatValue(value, type) {
|
|
6
|
+
if (value === null || value === undefined)
|
|
7
|
+
return "—";
|
|
8
|
+
switch (type) {
|
|
9
|
+
case "date":
|
|
10
|
+
return new Date(value).toLocaleDateString("id-ID");
|
|
11
|
+
case "datetime":
|
|
12
|
+
return new Date(value).toLocaleString("id-ID");
|
|
13
|
+
case "currency":
|
|
14
|
+
return new Intl.NumberFormat("id-ID", {
|
|
15
|
+
style: "currency",
|
|
16
|
+
currency: "IDR",
|
|
17
|
+
}).format(value);
|
|
18
|
+
default:
|
|
19
|
+
return String(value);
|
|
20
|
+
}
|
|
21
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { TableColumn, TableMeta, TableState, TableProps, TableAction, TableActionConfirm, TableActionGroup, ActionItem, } from "./types";
|
|
2
|
+
export { TableController } from "./controller";
|
|
3
|
+
export type { TableControllerOptions } from "./controller";
|
|
4
|
+
export { formatValue } from "./formatter";
|
package/dist/index.js
ADDED
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export interface TableColumn {
|
|
2
|
+
key: string;
|
|
3
|
+
label: string;
|
|
4
|
+
sortable: boolean;
|
|
5
|
+
searchable: boolean;
|
|
6
|
+
filterable: boolean;
|
|
7
|
+
filterOptions: string[] | Record<string, string>;
|
|
8
|
+
visible: boolean;
|
|
9
|
+
type: "text" | "date" | "datetime" | "currency" | "badge" | "actions" | string;
|
|
10
|
+
}
|
|
11
|
+
export interface TableMeta {
|
|
12
|
+
current_page: number;
|
|
13
|
+
last_page: number;
|
|
14
|
+
per_page: number;
|
|
15
|
+
total: number;
|
|
16
|
+
from: number | null;
|
|
17
|
+
to: number | null;
|
|
18
|
+
}
|
|
19
|
+
export interface TableState {
|
|
20
|
+
sort: string | null;
|
|
21
|
+
direction: "asc" | "desc";
|
|
22
|
+
search: string;
|
|
23
|
+
filters: Record<string, unknown>;
|
|
24
|
+
per_page: number;
|
|
25
|
+
}
|
|
26
|
+
export interface TableProps<TData = Record<string, unknown>> {
|
|
27
|
+
data: TData[];
|
|
28
|
+
columns: TableColumn[];
|
|
29
|
+
meta: TableMeta;
|
|
30
|
+
state: TableState;
|
|
31
|
+
}
|
|
32
|
+
export interface TableActionConfirm {
|
|
33
|
+
message: string;
|
|
34
|
+
}
|
|
35
|
+
export interface TableAction {
|
|
36
|
+
type: "action";
|
|
37
|
+
key: string;
|
|
38
|
+
label: string;
|
|
39
|
+
icon: string | null;
|
|
40
|
+
variant: "default" | "destructive";
|
|
41
|
+
href: string | null;
|
|
42
|
+
method: Method;
|
|
43
|
+
modal: boolean;
|
|
44
|
+
disabled: boolean;
|
|
45
|
+
confirm: TableActionConfirm | null;
|
|
46
|
+
meta: Record<string, unknown>;
|
|
47
|
+
}
|
|
48
|
+
export type Method = "get" | "post" | "put" | "patch" | "delete";
|
|
49
|
+
export interface TableActionGroup {
|
|
50
|
+
type: "group";
|
|
51
|
+
label: string;
|
|
52
|
+
icon: string | null;
|
|
53
|
+
actions: TableAction[];
|
|
54
|
+
}
|
|
55
|
+
export type ActionItem = TableAction | TableActionGroup;
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mdaushi/kinetics-core",
|
|
3
|
+
"version": "0.1.0-a",
|
|
4
|
+
"description": "Core utilities for Kinetics — Zero-Friction Tables for Inertia.js",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"inertia",
|
|
7
|
+
"laravel",
|
|
8
|
+
"datatable",
|
|
9
|
+
"tanstack-table",
|
|
10
|
+
"kinetics"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/mdaushi/kinetics",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/mdaushi/kinetics.git",
|
|
16
|
+
"directory": "packages/core"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"module": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"typescript": "^5.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsc",
|
|
40
|
+
"dev": "tsc --watch"
|
|
41
|
+
}
|
|
42
|
+
}
|