tinky-table 0.1.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.
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TableCell = TableCell;
4
+ var jsx_runtime_1 = require("react/jsx-runtime");
5
+ /**
6
+ * @fileoverview TableCell component for rendering individual table cells.
7
+ * @module tinky-table/TableCell
8
+ */
9
+ var tinky_1 = require("tinky");
10
+ /**
11
+ * TableCell renders an individual cell in the table.
12
+ *
13
+ * The cell width is handled by the parent Grid layout.
14
+ * Text truncation is handled by tinky's Text component wrap property.
15
+ *
16
+ * @param props - Cell properties
17
+ * @returns The rendered cell
18
+ *
19
+ * @example
20
+ * ```tsx
21
+ * <TableCell width={20} align="right" isLast={false} borderChar="│">
22
+ * 123.45
23
+ * </TableCell>
24
+ * ```
25
+ */
26
+ function TableCell(_a) {
27
+ var children = _a.children, _b = _a.align, align = _b === void 0 ? "left" : _b, _c = _a.isLast, isLast = _c === void 0 ? false : _c, _d = _a.borderChar, borderChar = _d === void 0 ? "│" : _d;
28
+ // Map align to justifyContent
29
+ var justifyContent = align === "right"
30
+ ? "flex-end"
31
+ : align === "center"
32
+ ? "center"
33
+ : "flex-start";
34
+ return ((0, jsx_runtime_1.jsxs)(tinky_1.Box, { flexDirection: "row", children: [(0, jsx_runtime_1.jsx)(tinky_1.Box, { flexGrow: 1, justifyContent: justifyContent, children: (0, jsx_runtime_1.jsx)(tinky_1.Text, { wrap: "truncate", children: children }) }), !isLast && (0, jsx_runtime_1.jsx)(tinky_1.Text, { children: borderChar })] }));
35
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @fileoverview TableHeader component for rendering table headers.
3
+ * @module tinky-table/TableHeader
4
+ */
5
+ import { type TableHeaderProps } from "../types.js";
6
+ /**
7
+ * TableHeader renders the header row of the table.
8
+ *
9
+ * Displays column titles with optional sort indicators.
10
+ * Column widths are handled by the parent Grid layout.
11
+ *
12
+ * @param props - Header properties
13
+ * @returns The rendered header row
14
+ *
15
+ * @example
16
+ * ```tsx
17
+ * <TableHeader
18
+ * columns={columns}
19
+ * columnWidths={[20, 15, 10]}
20
+ * borderChar="│"
21
+ * sortState={{ key: 'name', direction: 'asc' }}
22
+ * />
23
+ * ```
24
+ */
25
+ export declare function TableHeader({ columns, borderChar, sortState, style, }: TableHeaderProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.TableHeader = TableHeader;
15
+ var jsx_runtime_1 = require("react/jsx-runtime");
16
+ /**
17
+ * @fileoverview TableHeader component for rendering table headers.
18
+ * @module tinky-table/TableHeader
19
+ */
20
+ var tinky_1 = require("tinky");
21
+ /**
22
+ * Renders the sort indicator arrow.
23
+ *
24
+ * @param direction - Current sort direction
25
+ * @returns Sort indicator string
26
+ */
27
+ /**
28
+ * Returns the visual character for the sort direction.
29
+ *
30
+ * @param direction - The current direction of sorting ('asc', 'desc', or null).
31
+ * @returns The character representing the sort order (e.g. " ▲" or " ▼").
32
+ */
33
+ function getSortIndicator(direction) {
34
+ switch (direction) {
35
+ case "asc":
36
+ return " ▲";
37
+ case "desc":
38
+ return " ▼";
39
+ default:
40
+ return "";
41
+ }
42
+ }
43
+ /**
44
+ * TableHeader renders the header row of the table.
45
+ *
46
+ * Displays column titles with optional sort indicators.
47
+ * Column widths are handled by the parent Grid layout.
48
+ *
49
+ * @param props - Header properties
50
+ * @returns The rendered header row
51
+ *
52
+ * @example
53
+ * ```tsx
54
+ * <TableHeader
55
+ * columns={columns}
56
+ * columnWidths={[20, 15, 10]}
57
+ * borderChar="│"
58
+ * sortState={{ key: 'name', direction: 'asc' }}
59
+ * />
60
+ * ```
61
+ */
62
+ function TableHeader(_a) {
63
+ var columns = _a.columns, _b = _a.borderChar, borderChar = _b === void 0 ? "│" : _b, sortState = _a.sortState, style = _a.style;
64
+ return ((0, jsx_runtime_1.jsx)(tinky_1.Box, __assign({ flexDirection: "row" }, style, { children: columns.map(function (col, index) {
65
+ var _a;
66
+ var isLast = index === columns.length - 1;
67
+ // Get title as string
68
+ var titleText = String((_a = col.title) !== null && _a !== void 0 ? _a : col.key);
69
+ // Add sort indicator if this column is sorted
70
+ var sortIndicator = (sortState === null || sortState === void 0 ? void 0 : sortState.key) === col.key
71
+ ? getSortIndicator(sortState.direction)
72
+ : "";
73
+ // Map align to justifyContent
74
+ var justifyContent = col.align === "right"
75
+ ? "flex-end"
76
+ : col.align === "center"
77
+ ? "center"
78
+ : "flex-start";
79
+ return ((0, jsx_runtime_1.jsxs)(tinky_1.Box, { flexDirection: "row", flexGrow: 1, children: [(0, jsx_runtime_1.jsx)(tinky_1.Box, { flexGrow: 1, justifyContent: justifyContent, children: (0, jsx_runtime_1.jsxs)(tinky_1.Text, { bold: true, wrap: "truncate", children: [titleText, sortIndicator] }) }), !isLast && (0, jsx_runtime_1.jsx)(tinky_1.Text, { children: borderChar })] }, col.key));
80
+ }) })));
81
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @fileoverview TableRow component for rendering table rows.
3
+ * @module tinky-table/TableRow
4
+ */
5
+ import { type TableRowProps } from "../types.js";
6
+ /**
7
+ * TableRow renders a single row in the table.
8
+ *
9
+ * Uses flexbox layout to arrange cells horizontally.
10
+ *
11
+ * @param props - Row properties
12
+ * @returns The rendered row
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * <TableRow columnWidths={[20, 15, 10]}>
17
+ * <TableCell>Alice</TableCell>
18
+ * <TableCell>alice@example.com</TableCell>
19
+ * <TableCell>28</TableCell>
20
+ * </TableRow>
21
+ * ```
22
+ */
23
+ export declare function TableRow({ children, style }: TableRowProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.TableRow = TableRow;
15
+ var jsx_runtime_1 = require("react/jsx-runtime");
16
+ /**
17
+ * @fileoverview TableRow component for rendering table rows.
18
+ * @module tinky-table/TableRow
19
+ */
20
+ var tinky_1 = require("tinky");
21
+ /**
22
+ * TableRow renders a single row in the table.
23
+ *
24
+ * Uses flexbox layout to arrange cells horizontally.
25
+ *
26
+ * @param props - Row properties
27
+ * @returns The rendered row
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * <TableRow columnWidths={[20, 15, 10]}>
32
+ * <TableCell>Alice</TableCell>
33
+ * <TableCell>alice@example.com</TableCell>
34
+ * <TableCell>28</TableCell>
35
+ * </TableRow>
36
+ * ```
37
+ */
38
+ function TableRow(_a) {
39
+ var children = _a.children, style = _a.style;
40
+ return ((0, jsx_runtime_1.jsx)(tinky_1.Box, __assign({ flexDirection: "row" }, style, { children: children })));
41
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * tinky-table - A feature-rich Table component for tinky.
3
+ *
4
+ * @packageDocumentation
5
+ * @module tinky-table
6
+ *
7
+ * @example
8
+ * ```tsx
9
+ * import { Table } from 'tinky-table';
10
+ *
11
+ * const data = [
12
+ * { name: 'Alice', age: 28 },
13
+ * { name: 'Bob', age: 32 },
14
+ * ];
15
+ *
16
+ * const columns = [
17
+ * { key: 'name', title: 'Name', width: 20 },
18
+ * { key: 'age', title: 'Age', width: 10, align: 'right' },
19
+ * ];
20
+ *
21
+ * <Table data={data} columns={columns} />
22
+ * ```
23
+ */
24
+ export { Table } from "./components/Table.js";
25
+ export { TableCell } from "./components/TableCell.js";
26
+ export { TableRow } from "./components/TableRow.js";
27
+ export { TableHeader } from "./components/TableHeader.js";
28
+ export type { TableProps, ColumnDef, ColumnAlign, ColumnWidth, SortDirection, TableBorderStyle, RowData, PaginationConfig, TableCellProps, TableRowProps, TableHeaderProps, BorderChars, } from "./types.js";
29
+ export type { Styles } from "tinky";
30
+ export { getBorderChars, renderSeparator } from "./utils/border.js";
31
+ export { columnsToGridTemplate, type GridTemplateValue, } from "./utils/column-width.js";
package/lib/index.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ /**
3
+ * tinky-table - A feature-rich Table component for tinky.
4
+ *
5
+ * @packageDocumentation
6
+ * @module tinky-table
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * import { Table } from 'tinky-table';
11
+ *
12
+ * const data = [
13
+ * { name: 'Alice', age: 28 },
14
+ * { name: 'Bob', age: 32 },
15
+ * ];
16
+ *
17
+ * const columns = [
18
+ * { key: 'name', title: 'Name', width: 20 },
19
+ * { key: 'age', title: 'Age', width: 10, align: 'right' },
20
+ * ];
21
+ *
22
+ * <Table data={data} columns={columns} />
23
+ * ```
24
+ */
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.columnsToGridTemplate = exports.renderSeparator = exports.getBorderChars = exports.TableHeader = exports.TableRow = exports.TableCell = exports.Table = void 0;
27
+ // Main component
28
+ var Table_js_1 = require("./components/Table.js");
29
+ Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return Table_js_1.Table; } });
30
+ // Sub-components
31
+ var TableCell_js_1 = require("./components/TableCell.js");
32
+ Object.defineProperty(exports, "TableCell", { enumerable: true, get: function () { return TableCell_js_1.TableCell; } });
33
+ var TableRow_js_1 = require("./components/TableRow.js");
34
+ Object.defineProperty(exports, "TableRow", { enumerable: true, get: function () { return TableRow_js_1.TableRow; } });
35
+ var TableHeader_js_1 = require("./components/TableHeader.js");
36
+ Object.defineProperty(exports, "TableHeader", { enumerable: true, get: function () { return TableHeader_js_1.TableHeader; } });
37
+ // Utilities
38
+ var border_js_1 = require("./utils/border.js");
39
+ Object.defineProperty(exports, "getBorderChars", { enumerable: true, get: function () { return border_js_1.getBorderChars; } });
40
+ Object.defineProperty(exports, "renderSeparator", { enumerable: true, get: function () { return border_js_1.renderSeparator; } });
41
+ var column_width_js_1 = require("./utils/column-width.js");
42
+ Object.defineProperty(exports, "columnsToGridTemplate", { enumerable: true, get: function () { return column_width_js_1.columnsToGridTemplate; } });
package/lib/types.d.ts ADDED
@@ -0,0 +1,276 @@
1
+ /**
2
+ * @fileoverview Type definitions for tinky-table component.
3
+ * @module tinky-table/types
4
+ */
5
+ import { type ReactNode } from "react";
6
+ import { type Styles } from "tinky";
7
+ /**
8
+ * Column alignment options.
9
+ */
10
+ export type ColumnAlign = "left" | "center" | "right";
11
+ /**
12
+ * Column width specification.
13
+ * - `number`: Fixed width in characters
14
+ * - `${number}%`: Percentage of table width
15
+ * - `'auto'`: Automatically calculated based on content and available space
16
+ */
17
+ export type ColumnWidth = number | `${number}%` | "auto";
18
+ /**
19
+ * Sort direction for sortable columns.
20
+ */
21
+ export type SortDirection = "asc" | "desc" | null;
22
+ /**
23
+ * Border style options for the table.
24
+ */
25
+ export type TableBorderStyle = "single" | "double" | "round" | "bold" | "singleDouble" | "doubleSingle" | "classic" | "none";
26
+ /**
27
+ * Column definition for the table.
28
+ * @template T - The type of data in each row
29
+ */
30
+ export interface ColumnDef<T> {
31
+ /**
32
+ * Unique identifier for the column.
33
+ */
34
+ readonly key: string;
35
+ /**
36
+ * Header title to display.
37
+ */
38
+ readonly title: ReactNode;
39
+ /**
40
+ * The key in the data object to extract the cell's value from.
41
+ *
42
+ * Useful when the column `key` does not match the property name in the data object.
43
+ * If not specified, the column's `key` will be used as the property identifier.
44
+ */
45
+ readonly dataKey?: keyof T;
46
+ /**
47
+ * Column width.
48
+ * @default 'auto'
49
+ */
50
+ readonly width?: ColumnWidth;
51
+ /**
52
+ * Minimum column width in characters.
53
+ */
54
+ readonly minWidth?: number;
55
+ /**
56
+ * Maximum column width in characters.
57
+ */
58
+ readonly maxWidth?: number;
59
+ /**
60
+ * Text alignment within the column.
61
+ * @default 'left'
62
+ */
63
+ readonly align?: ColumnAlign;
64
+ /**
65
+ * Whether this column is sortable.
66
+ * @default false
67
+ */
68
+ readonly sortable?: boolean;
69
+ /**
70
+ * Custom render function for the cell content.
71
+ *
72
+ * Allows rendering complex React nodes (components, styled text, etc.) instead of
73
+ * just the raw value.
74
+ *
75
+ * @param value - The value extracted from the row using key/dataKey.
76
+ * @param row - The complete data object for the current row.
77
+ * @param rowIndex - The index of the row in the current page/dataset.
78
+ * @returns A ReactNode to be rendered within the cell.
79
+ */
80
+ readonly render?: (value: unknown, row: T, rowIndex: number) => ReactNode;
81
+ }
82
+ /**
83
+ * Base type constraint for row data.
84
+ */
85
+ export type RowData = Record<string, unknown>;
86
+ /**
87
+ * Pagination configuration.
88
+ */
89
+ export interface PaginationConfig {
90
+ /**
91
+ * Current page (1-indexed).
92
+ */
93
+ readonly page: number;
94
+ /**
95
+ * Number of rows per page.
96
+ */
97
+ readonly pageSize: number;
98
+ /**
99
+ * Total number of rows.
100
+ */
101
+ readonly total: number;
102
+ /**
103
+ * Callback when page changes.
104
+ */
105
+ readonly onChange?: (page: number) => void;
106
+ }
107
+ /**
108
+ * Props for the Table component.
109
+ * @template T - The type of data in each row
110
+ */
111
+ export interface TableProps<T extends RowData> {
112
+ /**
113
+ * Array of data to display in the table.
114
+ */
115
+ readonly data: T[];
116
+ /**
117
+ * Column definitions.
118
+ */
119
+ readonly columns: ColumnDef<T>[];
120
+ /**
121
+ * The visual style of the table borders.
122
+ *
123
+ * Options:
124
+ * - `'single'`: Thin single lines (default).
125
+ * - `'double'`: Double lines.
126
+ * - `'round'`: Rounded corners with single lines.
127
+ * - `'bold'`: Thick/Bold lines.
128
+ * - `'classic'`: ASCII characters (+, -, |).
129
+ * - `'none'`: No visible borders.
130
+ *
131
+ * @default 'single'
132
+ */
133
+ readonly borderStyle?: TableBorderStyle;
134
+ /**
135
+ * The total width of the table.
136
+ *
137
+ * - `number`: Fixed width in characters.
138
+ * - `'auto'`: Expands to fill the available terminal width.
139
+ * - `${number}%`: Percentage of the parent container's width.
140
+ *
141
+ * @default 'auto'
142
+ */
143
+ readonly width?: number | `${number}%` | "auto";
144
+ /**
145
+ * Whether to show the table header.
146
+ * @default true
147
+ */
148
+ readonly showHeader?: boolean;
149
+ /**
150
+ * Content to show when data is empty.
151
+ * @default 'No data'
152
+ */
153
+ readonly emptyText?: ReactNode;
154
+ /**
155
+ * Sort state for the table.
156
+ */
157
+ readonly sortState?: {
158
+ readonly key: string;
159
+ readonly direction: SortDirection;
160
+ };
161
+ /**
162
+ * Callback when sort state changes.
163
+ */
164
+ readonly onSortChange?: (key: string, direction: SortDirection) => void;
165
+ /**
166
+ * Pagination configuration.
167
+ */
168
+ readonly pagination?: PaginationConfig;
169
+ /**
170
+ * Custom style for the header row.
171
+ */
172
+ readonly headerStyle?: Partial<Styles>;
173
+ /**
174
+ * Custom style function for data rows.
175
+ * @param row - The row data
176
+ * @param index - The row index
177
+ * @returns Style object for the row
178
+ */
179
+ readonly rowStyle?: (row: T, index: number) => Partial<Styles>;
180
+ /**
181
+ * Custom style for cells.
182
+ */
183
+ readonly cellStyle?: Partial<Styles>;
184
+ }
185
+ /**
186
+ * Props for TableCell component.
187
+ */
188
+ export interface TableCellProps {
189
+ /**
190
+ * Cell content.
191
+ */
192
+ readonly children: ReactNode;
193
+ /**
194
+ * Cell width in characters.
195
+ */
196
+ readonly width: number;
197
+ /**
198
+ * Text alignment.
199
+ * @default 'left'
200
+ */
201
+ readonly align?: ColumnAlign;
202
+ /**
203
+ * Whether this is the last cell in the row.
204
+ * @default false
205
+ */
206
+ readonly isLast?: boolean;
207
+ /**
208
+ * Border character for column separator.
209
+ */
210
+ readonly borderChar?: string;
211
+ }
212
+ /**
213
+ * Props for TableRow component.
214
+ */
215
+ export interface TableRowProps {
216
+ /**
217
+ * Row content (TableCell components).
218
+ */
219
+ readonly children: ReactNode;
220
+ /**
221
+ * Column widths for grid layout.
222
+ */
223
+ readonly columnWidths: number[];
224
+ /**
225
+ * Custom row style.
226
+ */
227
+ readonly style?: Partial<Styles>;
228
+ }
229
+ /**
230
+ * Props for TableHeader component.
231
+ */
232
+ export interface TableHeaderProps {
233
+ /**
234
+ * Column definitions.
235
+ */
236
+ readonly columns: ColumnDef<RowData>[];
237
+ /**
238
+ * Calculated column widths.
239
+ */
240
+ readonly columnWidths: number[];
241
+ /**
242
+ * Border character for column separator.
243
+ */
244
+ readonly borderChar?: string;
245
+ /**
246
+ * Current sort state.
247
+ */
248
+ readonly sortState?: {
249
+ readonly key: string;
250
+ readonly direction: SortDirection;
251
+ };
252
+ /**
253
+ * Custom header style.
254
+ */
255
+ readonly style?: Partial<Styles>;
256
+ }
257
+ /**
258
+ * Border characters for table rendering.
259
+ */
260
+ export interface BorderChars {
261
+ readonly top: string;
262
+ readonly bottom: string;
263
+ readonly left: string;
264
+ readonly right: string;
265
+ readonly topLeft: string;
266
+ readonly topRight: string;
267
+ readonly bottomLeft: string;
268
+ readonly bottomRight: string;
269
+ readonly cross: string;
270
+ readonly topCross: string;
271
+ readonly bottomCross: string;
272
+ readonly leftCross: string;
273
+ readonly rightCross: string;
274
+ readonly horizontal: string;
275
+ readonly vertical: string;
276
+ }
package/lib/types.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ /**
3
+ * @fileoverview Type definitions for tinky-table component.
4
+ * @module tinky-table/types
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @fileoverview Border character mappings for different table styles.
3
+ * @module tinky-table/utils/border
4
+ */
5
+ import { type BorderChars, type TableBorderStyle } from "../types.js";
6
+ /**
7
+ * Gets the border characters for a given table border style.
8
+ *
9
+ * @param style - The border style name
10
+ * @returns The corresponding border character set
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const chars = getBorderChars('single');
15
+ * console.log(chars.topLeft); // '┌'
16
+ * ```
17
+ */
18
+ export declare function getBorderChars(style: TableBorderStyle): BorderChars;
19
+ /**
20
+ * Renders a horizontal separator line for the table.
21
+ *
22
+ * @param columnWidths - Array of column widths
23
+ * @param chars - Border character set
24
+ * @param position - Position of the separator ('top' | 'middle' | 'bottom')
25
+ * @returns The formatted separator string
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * const sep = renderSeparator([10, 15, 8], chars, 'top');
30
+ * // Returns: '┌──────────┬───────────────┬────────┐'
31
+ * ```
32
+ */
33
+ export declare function renderSeparator(columnWidths: number[], chars: BorderChars, position: "top" | "middle" | "bottom"): string;