tablero 1.0.0
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/README.md +181 -0
- package/dist/core.d.mts +447 -0
- package/dist/core.d.ts +447 -0
- package/dist/core.js +426 -0
- package/dist/core.js.map +1 -0
- package/dist/core.mjs +376 -0
- package/dist/core.mjs.map +1 -0
- package/dist/react.d.mts +117 -0
- package/dist/react.d.ts +117 -0
- package/dist/react.js +543 -0
- package/dist/react.js.map +1 -0
- package/dist/react.mjs +541 -0
- package/dist/react.mjs.map +1 -0
- package/dist/ui.d.mts +160 -0
- package/dist/ui.d.ts +160 -0
- package/dist/ui.js +318 -0
- package/dist/ui.js.map +1 -0
- package/dist/ui.mjs +309 -0
- package/dist/ui.mjs.map +1 -0
- package/package.json +81 -0
package/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# tablero
|
|
2
|
+
|
|
3
|
+
A type-safe, framework-agnostic data table library with React bindings. Built with TypeScript for maximum type safety and developer experience.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🎯 **Type-safe** - Full TypeScript support with excellent type inference
|
|
8
|
+
- 🔄 **Framework-agnostic core** - Use the core logic with any framework
|
|
9
|
+
- ⚛️ **React hooks** - `useDataTable` hook for easy React integration
|
|
10
|
+
- 🎨 **Customizable UI** - Flexible, CSS-variable based styling
|
|
11
|
+
- 📊 **Sorting** - Single-column sorting (extensible to multi-column)
|
|
12
|
+
- 📄 **Pagination** - Client-side pagination with configurable page sizes
|
|
13
|
+
- 🔍 **Filtering** - Global and column-specific text filtering
|
|
14
|
+
- 📌 **Sticky headers & columns** - Keep headers and first column visible while scrolling
|
|
15
|
+
- 🔧 **Column resizing** - Resize columns with pointer-based interaction
|
|
16
|
+
- 🎭 **Custom renderers** - Customize cell, header, and row rendering
|
|
17
|
+
- ♿ **Accessible** - ARIA attributes and keyboard support
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install tablero
|
|
23
|
+
# or
|
|
24
|
+
pnpm add tablero
|
|
25
|
+
# or
|
|
26
|
+
yarn add tablero
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
### React Example
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { useDataTable } from "tablero/react";
|
|
35
|
+
import { DataTable } from "tablero/ui";
|
|
36
|
+
import { defineColumns, col } from "tablero/core";
|
|
37
|
+
import "tablero/dist/ui.css"; // Optional: import default styles
|
|
38
|
+
|
|
39
|
+
interface User {
|
|
40
|
+
id: number;
|
|
41
|
+
name: string;
|
|
42
|
+
email: string;
|
|
43
|
+
age: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const columns = defineColumns<User>()([
|
|
47
|
+
col("name", { header: "Name", sortable: true }),
|
|
48
|
+
col("email", { header: "Email", sortable: true, filter: "text" }),
|
|
49
|
+
col("age", { header: "Age", sortable: true }),
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
function MyTable() {
|
|
53
|
+
const table = useDataTable({
|
|
54
|
+
data: users,
|
|
55
|
+
columns,
|
|
56
|
+
pageSize: 10,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<DataTable
|
|
61
|
+
table={table}
|
|
62
|
+
stickyHeader
|
|
63
|
+
bordered
|
|
64
|
+
maxHeight={400}
|
|
65
|
+
/>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Core Usage (Framework-agnostic)
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import {
|
|
74
|
+
defineColumns,
|
|
75
|
+
col,
|
|
76
|
+
createColumns,
|
|
77
|
+
createInitialTableState,
|
|
78
|
+
applyFilters,
|
|
79
|
+
applySort,
|
|
80
|
+
getPaginatedData,
|
|
81
|
+
} from "tablero/core";
|
|
82
|
+
|
|
83
|
+
const columns = defineColumns<User>()([
|
|
84
|
+
col("name", { header: "Name", sortable: true }),
|
|
85
|
+
col("email", { header: "Email" }),
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
const runtimeColumns = createColumns(columns);
|
|
89
|
+
const state = createInitialTableState(columns.map((c) => c.id));
|
|
90
|
+
|
|
91
|
+
// Apply filters
|
|
92
|
+
const filtered = applyFilters(data, state.filtering, getValue);
|
|
93
|
+
|
|
94
|
+
// Apply sorting
|
|
95
|
+
const sorted = applySort(filtered, state.sorting, getValue);
|
|
96
|
+
|
|
97
|
+
// Paginate
|
|
98
|
+
const paginated = getPaginatedData(sorted, state.pagination);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Packages
|
|
102
|
+
|
|
103
|
+
This library is organized into three packages:
|
|
104
|
+
|
|
105
|
+
- **`tablero/core`** - Framework-agnostic core logic (state management, sorting, filtering, pagination)
|
|
106
|
+
- **`tablero/react`** - React hooks (`useDataTable`)
|
|
107
|
+
- **`tablero/ui`** - React UI components (`DataTable`, `TableHeader`, `TableBody`, `TableCell`)
|
|
108
|
+
|
|
109
|
+
## API Documentation
|
|
110
|
+
|
|
111
|
+
### Core API
|
|
112
|
+
|
|
113
|
+
See the [core package documentation](./packages/core/README.md) for detailed API documentation.
|
|
114
|
+
|
|
115
|
+
### React Hook
|
|
116
|
+
|
|
117
|
+
```tsx
|
|
118
|
+
const table = useDataTable({
|
|
119
|
+
data: TData[],
|
|
120
|
+
columns: ColumnDef<TData>[],
|
|
121
|
+
pageSize?: number,
|
|
122
|
+
state?: TableStateHandler | UncontrolledTableState,
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### DataTable Component
|
|
127
|
+
|
|
128
|
+
```tsx
|
|
129
|
+
<DataTable
|
|
130
|
+
table={table}
|
|
131
|
+
bordered={true}
|
|
132
|
+
stickyHeader={false}
|
|
133
|
+
stickyFirstColumn={false}
|
|
134
|
+
enableResizing={false}
|
|
135
|
+
maxHeight?: number | string
|
|
136
|
+
slots={{
|
|
137
|
+
loader?: React.ComponentType,
|
|
138
|
+
empty?: React.ComponentType<{ columns: Column[] }>,
|
|
139
|
+
error?: React.ComponentType<{ error: Error | string }>,
|
|
140
|
+
}}
|
|
141
|
+
renderCell?: (value, row, column) => React.ReactNode
|
|
142
|
+
renderHeader?: (column, sortState) => React.ReactNode
|
|
143
|
+
renderRow?: (row, index, cells) => React.ReactNode
|
|
144
|
+
/>
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Styling
|
|
148
|
+
|
|
149
|
+
The library uses CSS variables for easy theming. Import the default styles or create your own:
|
|
150
|
+
|
|
151
|
+
```css
|
|
152
|
+
:root {
|
|
153
|
+
--tablero-bg: #ffffff;
|
|
154
|
+
--tablero-header-bg: #f9fafb;
|
|
155
|
+
--tablero-border-color: #e5e7eb;
|
|
156
|
+
--tablero-border-width: 1px;
|
|
157
|
+
--tablero-hover-bg: #f3f4f6;
|
|
158
|
+
--tablero-text-color: #111827;
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## TypeScript Support
|
|
163
|
+
|
|
164
|
+
Full TypeScript support with excellent type inference:
|
|
165
|
+
|
|
166
|
+
```tsx
|
|
167
|
+
// Column keys are type-checked
|
|
168
|
+
const columns = defineColumns<User>()([
|
|
169
|
+
col("name", { ... }), // ✅ Type-safe
|
|
170
|
+
col("invalid", { ... }), // ❌ Type error
|
|
171
|
+
]);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## License
|
|
175
|
+
|
|
176
|
+
MIT
|
|
177
|
+
|
|
178
|
+
## Contributing
|
|
179
|
+
|
|
180
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
181
|
+
|
package/dist/core.d.mts
ADDED
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sorting functionality
|
|
3
|
+
*
|
|
4
|
+
* Single-column sorting implementation, extensible to multi-column sorting.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Sort direction type
|
|
8
|
+
*/
|
|
9
|
+
type SortDirection = 'asc' | 'desc' | null;
|
|
10
|
+
/**
|
|
11
|
+
* Sort state for single-column sorting
|
|
12
|
+
*
|
|
13
|
+
* TODO: Extend to multi-column sorting:
|
|
14
|
+
* - Change to: sorting: Array<{ columnId: string; direction: 'asc' | 'desc' }>
|
|
15
|
+
* - Add priority/order for multi-sort
|
|
16
|
+
*/
|
|
17
|
+
interface SortState {
|
|
18
|
+
/** Column ID to sort by, null if no sorting */
|
|
19
|
+
columnId: string | null;
|
|
20
|
+
/** Sort direction, null if no sorting */
|
|
21
|
+
direction: SortDirection;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Create initial sort state
|
|
25
|
+
*/
|
|
26
|
+
declare function createInitialSortState(): SortState;
|
|
27
|
+
/**
|
|
28
|
+
* Toggle sort state for a column
|
|
29
|
+
* Cycles through: none -> asc -> desc -> none
|
|
30
|
+
*/
|
|
31
|
+
declare function toggleSort(currentState: SortState, columnId: string): SortState;
|
|
32
|
+
/**
|
|
33
|
+
* Set sort state explicitly
|
|
34
|
+
*/
|
|
35
|
+
declare function setSort(columnId: string | null, direction: SortDirection): SortState;
|
|
36
|
+
/**
|
|
37
|
+
* Clear sort state
|
|
38
|
+
*/
|
|
39
|
+
declare function clearSort(): SortState;
|
|
40
|
+
/**
|
|
41
|
+
* Type guard to check if sorting is active
|
|
42
|
+
*/
|
|
43
|
+
declare function isSortActive(state: SortState): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Value comparator function type
|
|
46
|
+
*/
|
|
47
|
+
type CompareFn<T = unknown> = (a: T, b: T) => number;
|
|
48
|
+
/**
|
|
49
|
+
* Default comparator for primitive values
|
|
50
|
+
*/
|
|
51
|
+
declare function defaultCompare(a: unknown, b: unknown): number;
|
|
52
|
+
/**
|
|
53
|
+
* Create a sorted comparator function
|
|
54
|
+
*/
|
|
55
|
+
declare function createComparator<T>(direction: 'asc' | 'desc', compareFn?: CompareFn<T>): CompareFn<T>;
|
|
56
|
+
/**
|
|
57
|
+
* Sort data array based on sort state
|
|
58
|
+
*
|
|
59
|
+
* @param data - Array of data items
|
|
60
|
+
* @param sortState - Current sort state
|
|
61
|
+
* @param getValue - Function to extract value from data item for the sorted column
|
|
62
|
+
* @param compareFn - Optional custom comparator function
|
|
63
|
+
* @returns New sorted array (immutable)
|
|
64
|
+
*/
|
|
65
|
+
declare function applySort<T>(data: T[], sortState: SortState, getValue: (item: T, columnId: string) => unknown, compareFn?: CompareFn<unknown>): T[];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Pagination functionality
|
|
69
|
+
*
|
|
70
|
+
* Client-side pagination implementation.
|
|
71
|
+
*/
|
|
72
|
+
/**
|
|
73
|
+
* Pagination state
|
|
74
|
+
*/
|
|
75
|
+
interface PaginationState {
|
|
76
|
+
/** Current page index (0-based) */
|
|
77
|
+
pageIndex: number;
|
|
78
|
+
/** Number of items per page */
|
|
79
|
+
pageSize: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Create initial pagination state
|
|
83
|
+
*/
|
|
84
|
+
declare function createInitialPaginationState(pageSize?: number): PaginationState;
|
|
85
|
+
/**
|
|
86
|
+
* Calculate total number of pages
|
|
87
|
+
*/
|
|
88
|
+
declare function getPageCount(totalItems: number, pageSize: number): number;
|
|
89
|
+
/**
|
|
90
|
+
* Get the start index of the current page
|
|
91
|
+
*/
|
|
92
|
+
declare function getPageStartIndex(pageIndex: number, pageSize: number): number;
|
|
93
|
+
/**
|
|
94
|
+
* Get the end index of the current page (exclusive)
|
|
95
|
+
*/
|
|
96
|
+
declare function getPageEndIndex(pageIndex: number, pageSize: number, totalItems: number): number;
|
|
97
|
+
/**
|
|
98
|
+
* Check if page index is valid
|
|
99
|
+
*/
|
|
100
|
+
declare function isValidPageIndex(pageIndex: number, totalPages: number): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Clamp page index to valid range
|
|
103
|
+
*/
|
|
104
|
+
declare function clampPageIndex(pageIndex: number, totalPages: number): number;
|
|
105
|
+
/**
|
|
106
|
+
* Go to next page
|
|
107
|
+
*/
|
|
108
|
+
declare function goToNextPage(state: PaginationState, totalPages: number): PaginationState;
|
|
109
|
+
/**
|
|
110
|
+
* Go to previous page
|
|
111
|
+
*/
|
|
112
|
+
declare function goToPreviousPage(state: PaginationState, totalPages: number): PaginationState;
|
|
113
|
+
/**
|
|
114
|
+
* Go to specific page
|
|
115
|
+
*/
|
|
116
|
+
declare function goToPage(state: PaginationState, pageIndex: number, totalPages: number): PaginationState;
|
|
117
|
+
/**
|
|
118
|
+
* Change page size
|
|
119
|
+
*/
|
|
120
|
+
declare function setPageSize(state: PaginationState, pageSize: number, totalItems: number): PaginationState;
|
|
121
|
+
/**
|
|
122
|
+
* Get paginated slice of data
|
|
123
|
+
*
|
|
124
|
+
* @param data - Full data array
|
|
125
|
+
* @param pagination - Pagination state
|
|
126
|
+
* @returns Paginated data slice
|
|
127
|
+
*/
|
|
128
|
+
declare function getPaginatedData<T>(data: T[], pagination: PaginationState): T[];
|
|
129
|
+
/**
|
|
130
|
+
* Check if there is a next page
|
|
131
|
+
*/
|
|
132
|
+
declare function hasNextPage(pageIndex: number, totalPages: number): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Check if there is a previous page
|
|
135
|
+
*/
|
|
136
|
+
declare function hasPreviousPage(pageIndex: number): boolean;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Filtering functionality
|
|
140
|
+
*
|
|
141
|
+
* Basic text filtering implementation, extensible to advanced filtering.
|
|
142
|
+
*/
|
|
143
|
+
/**
|
|
144
|
+
* Filter state
|
|
145
|
+
*
|
|
146
|
+
* TODO: Extend to support:
|
|
147
|
+
* - Advanced filter types (number range, date range, etc.)
|
|
148
|
+
* - Filter operators (equals, contains, greater than, etc.)
|
|
149
|
+
* - Column-specific filter configurations
|
|
150
|
+
*/
|
|
151
|
+
interface FilterState {
|
|
152
|
+
/** Global text filter applied across all columns */
|
|
153
|
+
globalFilter: string;
|
|
154
|
+
/** Column-specific filters - maps column ID to filter value */
|
|
155
|
+
columnFilters: Record<string, string>;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Create initial filter state
|
|
159
|
+
*/
|
|
160
|
+
declare function createInitialFilterState(): FilterState;
|
|
161
|
+
/**
|
|
162
|
+
* Set global filter
|
|
163
|
+
*/
|
|
164
|
+
declare function setGlobalFilter(filter: string): FilterState;
|
|
165
|
+
/**
|
|
166
|
+
* Set column filter
|
|
167
|
+
*/
|
|
168
|
+
declare function setColumnFilter(state: FilterState, columnId: string, filter: string): FilterState;
|
|
169
|
+
/**
|
|
170
|
+
* Clear column filter
|
|
171
|
+
*/
|
|
172
|
+
declare function clearColumnFilter(state: FilterState, columnId: string): FilterState;
|
|
173
|
+
/**
|
|
174
|
+
* Clear all filters
|
|
175
|
+
*/
|
|
176
|
+
declare function clearAllFilters(): FilterState;
|
|
177
|
+
/**
|
|
178
|
+
* Check if any filter is active
|
|
179
|
+
*/
|
|
180
|
+
declare function isFilterActive(state: FilterState): boolean;
|
|
181
|
+
/**
|
|
182
|
+
* Value matcher function type
|
|
183
|
+
*/
|
|
184
|
+
type MatchFn = (value: unknown, filter: string) => boolean;
|
|
185
|
+
/**
|
|
186
|
+
* Default text matcher - case-insensitive substring match
|
|
187
|
+
*/
|
|
188
|
+
declare function defaultMatch(value: unknown, filter: string): boolean;
|
|
189
|
+
/**
|
|
190
|
+
* Apply filters to data array
|
|
191
|
+
*
|
|
192
|
+
* @param data - Array of data items
|
|
193
|
+
* @param filterState - Current filter state
|
|
194
|
+
* @param getValue - Function to extract value from data item for a column
|
|
195
|
+
* @param matchFn - Optional custom matcher function
|
|
196
|
+
* @returns Filtered data array (immutable)
|
|
197
|
+
*/
|
|
198
|
+
declare function applyFilters<T>(data: T[], filterState: FilterState, getValue: (item: T, columnId: string) => unknown, matchFn?: MatchFn): T[];
|
|
199
|
+
/**
|
|
200
|
+
* Get active filter count
|
|
201
|
+
*/
|
|
202
|
+
declare function getActiveFilterCount(state: FilterState): number;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Table state management
|
|
206
|
+
*
|
|
207
|
+
* Core state shape and management utilities for data tables.
|
|
208
|
+
* Supports both controlled and uncontrolled state patterns.
|
|
209
|
+
*/
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Core table state interface
|
|
213
|
+
*
|
|
214
|
+
* Designed to be extensible for:
|
|
215
|
+
* - Server-side mode (via mode: 'server' | 'client')
|
|
216
|
+
* - Multi-sort (sorting can be extended to array)
|
|
217
|
+
* - URL sync (state can be serialized/deserialized)
|
|
218
|
+
*/
|
|
219
|
+
interface TableState {
|
|
220
|
+
/** Sorting state - single column for now, extensible to array for multi-sort */
|
|
221
|
+
sorting: SortState;
|
|
222
|
+
/** Pagination state */
|
|
223
|
+
pagination: PaginationState;
|
|
224
|
+
/** Filtering state */
|
|
225
|
+
filtering: FilterState;
|
|
226
|
+
/** Column visibility state - maps column ID to visibility */
|
|
227
|
+
columnVisibility: Record<string, boolean>;
|
|
228
|
+
/** Column order - array of column IDs */
|
|
229
|
+
columnOrder: string[];
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Initial table state with defaults
|
|
233
|
+
*/
|
|
234
|
+
declare function createInitialTableState(columnIds: string[]): TableState;
|
|
235
|
+
/**
|
|
236
|
+
* Update table state immutably
|
|
237
|
+
* Returns a new state object with the update applied
|
|
238
|
+
*/
|
|
239
|
+
declare function updateTableState<T extends TableState>(state: T, updates: Partial<TableState>): T;
|
|
240
|
+
/**
|
|
241
|
+
* Update sorting state
|
|
242
|
+
*/
|
|
243
|
+
declare function updateSorting(state: TableState, sorting: SortState): TableState;
|
|
244
|
+
/**
|
|
245
|
+
* Update pagination state
|
|
246
|
+
*/
|
|
247
|
+
declare function updatePagination(state: TableState, pagination: Partial<PaginationState>): TableState;
|
|
248
|
+
/**
|
|
249
|
+
* Update filtering state
|
|
250
|
+
*/
|
|
251
|
+
declare function updateFiltering(state: TableState, filtering: Partial<FilterState>): TableState;
|
|
252
|
+
/**
|
|
253
|
+
* Update column visibility
|
|
254
|
+
*/
|
|
255
|
+
declare function updateColumnVisibility(state: TableState, columnId: string, visible: boolean): TableState;
|
|
256
|
+
/**
|
|
257
|
+
* Update column order
|
|
258
|
+
*/
|
|
259
|
+
declare function updateColumnOrder(state: TableState, columnOrder: string[]): TableState;
|
|
260
|
+
/**
|
|
261
|
+
* Controlled state handler type
|
|
262
|
+
* Used when state is managed externally
|
|
263
|
+
*/
|
|
264
|
+
type TableStateHandler = {
|
|
265
|
+
state: TableState;
|
|
266
|
+
setState: (state: TableState | ((prev: TableState) => TableState)) => void;
|
|
267
|
+
};
|
|
268
|
+
/**
|
|
269
|
+
* Uncontrolled state handler type
|
|
270
|
+
* Used when state is managed internally
|
|
271
|
+
*/
|
|
272
|
+
type UncontrolledTableState = {
|
|
273
|
+
initialState?: Partial<TableState>;
|
|
274
|
+
onStateChange?: (state: TableState) => void;
|
|
275
|
+
};
|
|
276
|
+
/**
|
|
277
|
+
* Check if state is controlled
|
|
278
|
+
*/
|
|
279
|
+
declare function isControlledState(handler: TableStateHandler | UncontrolledTableState): handler is TableStateHandler;
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Column definitions and utilities
|
|
283
|
+
*
|
|
284
|
+
* Type-safe column definition API with extensibility for:
|
|
285
|
+
* - Custom JSX renderers (via renderCell, renderHeader)
|
|
286
|
+
* - Grouped headers (via parentId)
|
|
287
|
+
* - Metadata extension (via meta)
|
|
288
|
+
*/
|
|
289
|
+
/**
|
|
290
|
+
* Filter type configuration
|
|
291
|
+
*
|
|
292
|
+
* TODO: Extend to support more filter types:
|
|
293
|
+
* - 'number' | 'date' | 'select' | 'range'
|
|
294
|
+
*/
|
|
295
|
+
type FilterType = "text" | "none";
|
|
296
|
+
/**
|
|
297
|
+
* Column accessor function type
|
|
298
|
+
* Extracts a value from a data item
|
|
299
|
+
*/
|
|
300
|
+
type AccessorFn<TData, TValue> = (data: TData) => TValue;
|
|
301
|
+
/**
|
|
302
|
+
* Column accessor - either a key path or a function
|
|
303
|
+
*/
|
|
304
|
+
type ColumnAccessor<TData, TValue> = keyof TData | AccessorFn<TData, TValue>;
|
|
305
|
+
/**
|
|
306
|
+
* Base column definition
|
|
307
|
+
*
|
|
308
|
+
* This is the serializable part of the column definition.
|
|
309
|
+
* Renderers and other runtime functions are kept separate for serialization.
|
|
310
|
+
*
|
|
311
|
+
* TODO: Add support for:
|
|
312
|
+
* - renderCell: (value: TValue, row: TData) => React.ReactNode
|
|
313
|
+
* - renderHeader: () => React.ReactNode
|
|
314
|
+
* - parentId?: string (for grouped headers)
|
|
315
|
+
* - meta?: Record<string, unknown> (for extensible metadata)
|
|
316
|
+
*/
|
|
317
|
+
interface ColumnDef<TData, TValue = unknown> {
|
|
318
|
+
/** Unique column identifier - must be stable */
|
|
319
|
+
id: string;
|
|
320
|
+
/** Column header label */
|
|
321
|
+
header?: string;
|
|
322
|
+
/** Whether column is sortable */
|
|
323
|
+
sortable?: boolean;
|
|
324
|
+
/** Filter type for this column */
|
|
325
|
+
filter?: FilterType;
|
|
326
|
+
/** Column width in pixels */
|
|
327
|
+
width?: number;
|
|
328
|
+
/** Minimum column width */
|
|
329
|
+
minWidth?: number;
|
|
330
|
+
/** Maximum column width */
|
|
331
|
+
maxWidth?: number;
|
|
332
|
+
/** Whether column is visible by default */
|
|
333
|
+
visible?: boolean;
|
|
334
|
+
/** Column alignment */
|
|
335
|
+
align?: "left" | "center" | "right";
|
|
336
|
+
/** Accessor function or key path to extract value */
|
|
337
|
+
accessor?: ColumnAccessor<TData, TValue>;
|
|
338
|
+
/** Custom metadata for extensibility */
|
|
339
|
+
meta?: Record<string, unknown>;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Runtime column representation
|
|
343
|
+
* Includes the definition plus computed properties
|
|
344
|
+
*/
|
|
345
|
+
interface Column<TData> {
|
|
346
|
+
/** Column definition */
|
|
347
|
+
def: ColumnDef<TData>;
|
|
348
|
+
/** Column ID (stable identifier) */
|
|
349
|
+
id: string;
|
|
350
|
+
/** Column header label */
|
|
351
|
+
header: string;
|
|
352
|
+
/** Whether column is sortable */
|
|
353
|
+
sortable: boolean;
|
|
354
|
+
/** Filter type */
|
|
355
|
+
filter: FilterType;
|
|
356
|
+
/** Column width */
|
|
357
|
+
width?: number;
|
|
358
|
+
/** Column alignment */
|
|
359
|
+
align: "left" | "center" | "right";
|
|
360
|
+
/** Accessor function to extract value from data */
|
|
361
|
+
accessor: AccessorFn<TData, unknown>;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Column definition builder options
|
|
365
|
+
*/
|
|
366
|
+
interface ColumnOptions<TData, TValue> {
|
|
367
|
+
header?: string;
|
|
368
|
+
sortable?: boolean;
|
|
369
|
+
filter?: FilterType;
|
|
370
|
+
width?: number;
|
|
371
|
+
minWidth?: number;
|
|
372
|
+
maxWidth?: number;
|
|
373
|
+
visible?: boolean;
|
|
374
|
+
align?: "left" | "center" | "right";
|
|
375
|
+
accessor?: ColumnAccessor<TData, TValue>;
|
|
376
|
+
meta?: Record<string, unknown>;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Create a column definition from a key
|
|
380
|
+
*
|
|
381
|
+
* @param key - Column key (must exist on TData)
|
|
382
|
+
* @param options - Column configuration options
|
|
383
|
+
* @returns Column definition
|
|
384
|
+
*/
|
|
385
|
+
declare function col<TData, TKey extends keyof TData>(key: TKey, options?: ColumnOptions<TData, TData[TKey]>): ColumnDef<TData, TData[TKey]>;
|
|
386
|
+
/**
|
|
387
|
+
* Create a column definition with a custom accessor
|
|
388
|
+
*
|
|
389
|
+
* @param id - Unique column identifier
|
|
390
|
+
* @param options - Column configuration options (must include accessor)
|
|
391
|
+
* @returns Column definition
|
|
392
|
+
*/
|
|
393
|
+
declare function colWithAccessor<TData, TValue>(id: string, options: ColumnOptions<TData, TValue> & {
|
|
394
|
+
accessor: ColumnAccessor<TData, TValue>;
|
|
395
|
+
}): ColumnDef<TData, TValue>;
|
|
396
|
+
/**
|
|
397
|
+
* Type-safe column definition array builder
|
|
398
|
+
*
|
|
399
|
+
* Provides type inference and validation for column definitions.
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* ```ts
|
|
403
|
+
* const columns = defineColumns<User>()([
|
|
404
|
+
* col("name", { header: "Name", sortable: true }),
|
|
405
|
+
* col("email", { header: "Email", filter: "text" }),
|
|
406
|
+
* ]);
|
|
407
|
+
* ```
|
|
408
|
+
*/
|
|
409
|
+
declare function defineColumns<TData>(): <TColumns extends readonly ColumnDef<TData>[]>(columns: TColumns) => TColumns;
|
|
410
|
+
/**
|
|
411
|
+
* Normalize column accessor to a function
|
|
412
|
+
*/
|
|
413
|
+
declare function normalizeAccessor<TData, TValue>(accessor: ColumnAccessor<TData, TValue>): AccessorFn<TData, TValue>;
|
|
414
|
+
/**
|
|
415
|
+
* Convert column definition to runtime column
|
|
416
|
+
*/
|
|
417
|
+
declare function createColumn<TData>(def: ColumnDef<TData>): Column<TData>;
|
|
418
|
+
/**
|
|
419
|
+
* Convert array of column definitions to runtime columns
|
|
420
|
+
*/
|
|
421
|
+
declare function createColumns<TData>(definitions: readonly ColumnDef<TData>[]): Column<TData>[];
|
|
422
|
+
/**
|
|
423
|
+
* Get column by ID
|
|
424
|
+
*/
|
|
425
|
+
declare function getColumnById<TData>(columns: Column<TData>[], id: string): Column<TData> | undefined;
|
|
426
|
+
/**
|
|
427
|
+
* Get visible columns
|
|
428
|
+
*/
|
|
429
|
+
declare function getVisibleColumns<TData>(columns: Column<TData>[], visibility: Record<string, boolean>): Column<TData>[];
|
|
430
|
+
/**
|
|
431
|
+
* Get columns in specified order
|
|
432
|
+
*/
|
|
433
|
+
declare function getOrderedColumns<TData>(columns: Column<TData>[], order: string[]): Column<TData>[];
|
|
434
|
+
/**
|
|
435
|
+
* Extract column IDs from definitions
|
|
436
|
+
*/
|
|
437
|
+
declare function getColumnIds<TData>(definitions: readonly ColumnDef<TData>[]): string[];
|
|
438
|
+
/**
|
|
439
|
+
* Validate column definitions
|
|
440
|
+
* Ensures all columns have unique IDs
|
|
441
|
+
*/
|
|
442
|
+
declare function validateColumns<TData>(definitions: readonly ColumnDef<TData>[]): {
|
|
443
|
+
valid: boolean;
|
|
444
|
+
errors: string[];
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
export { type AccessorFn, type Column, type ColumnAccessor, type ColumnDef, type ColumnOptions, type CompareFn, type FilterState, type FilterType, type MatchFn, type PaginationState, type SortDirection, type SortState, type TableState, type TableStateHandler, type UncontrolledTableState, applyFilters, applySort, clampPageIndex, clearAllFilters, clearColumnFilter, clearSort, col, colWithAccessor, createColumn, createColumns, createComparator, createInitialFilterState, createInitialPaginationState, createInitialSortState, createInitialTableState, defaultCompare, defaultMatch, defineColumns, getActiveFilterCount, getColumnById, getColumnIds, getOrderedColumns, getPageCount, getPageEndIndex, getPageStartIndex, getPaginatedData, getVisibleColumns, goToNextPage, goToPage, goToPreviousPage, hasNextPage, hasPreviousPage, isControlledState, isFilterActive, isSortActive, isValidPageIndex, normalizeAccessor, setColumnFilter, setGlobalFilter, setPageSize, setSort, toggleSort, updateColumnOrder, updateColumnVisibility, updateFiltering, updatePagination, updateSorting, updateTableState, validateColumns };
|