@agilant/toga-blox 1.0.98 → 1.0.100

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,55 @@
1
+ import React from "react";
2
+ import { CellProps } from "react-table";
3
+ /** The shape your library expects for each column's config */
4
+ export interface SharedColumnConfig {
5
+ key: string;
6
+ type?: string;
7
+ linkClasses?: string;
8
+ leftIcon?: string;
9
+ rightIcon?: string;
10
+ leftImage?: string;
11
+ rightImage?: string;
12
+ transform?: (value: any) => any;
13
+ }
14
+ /** The shape of the columns you return to React Table */
15
+ export interface TableColumn {
16
+ Header: string | (() => JSX.Element);
17
+ accessor: string;
18
+ Cell: (props: CellProps<{
19
+ [key: string]: string | React.ReactElement;
20
+ }, string | React.ReactElement>) => JSX.Element;
21
+ sticky?: "left" | "right";
22
+ }
23
+ /**
24
+ * The "renderers" your app provides for icons, images, statuses, currencies, etc.
25
+ */
26
+ export interface Renderers {
27
+ renderIcon?: (iconName: string, style?: "solid" | "regular") => JSX.Element;
28
+ renderImage?: (props: {
29
+ imageUrl: string;
30
+ altText: string;
31
+ className?: string;
32
+ width?: number;
33
+ height?: number;
34
+ }) => JSX.Element;
35
+ renderStatus?: (value: string, extra?: any) => JSX.Element;
36
+ renderCurrency?: (value: string) => string;
37
+ }
38
+ /**
39
+ * The main function that shapes columns for React Table.
40
+ * - `columnTitles`: array of column headers
41
+ * - `accessors`: array of "col1", "col2", ...
42
+ * - `columnConfigs`: array of raw backend configs (with booleans)
43
+ * - `renderers`: your app's icon/image/status/currency callbacks
44
+ */
45
+ declare function formatTableData(columnTitles: string[], accessors: string[], columnConfigs: any[], renderers?: Renderers): TableColumn[];
46
+ /**
47
+ * Helper to generate "col1", "col2", "col3", etc.
48
+ */
49
+ declare function generateAccessors(columnTitles: string[]): string[];
50
+ export { formatTableData, generateAccessors };
51
+ declare const _default: {
52
+ formatTableData: typeof formatTableData;
53
+ generateAccessors: typeof generateAccessors;
54
+ };
55
+ export default _default;
@@ -0,0 +1,149 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ /**
3
+ * Convert your backend booleans (e.g. `hasStatus`, `hasCurrency`)
4
+ * into the library's expected fields (`type: "STATUS"`, `type: "CURRENCY"`, etc.).
5
+ */
6
+ function transformBackendConfig(backendConfig) {
7
+ console.log("[LIB] transformBackendConfig input:", backendConfig);
8
+ const finalConfig = {
9
+ key: backendConfig.key,
10
+ type: backendConfig.type ?? "DEFAULT",
11
+ linkClasses: backendConfig.linkClasses ?? "",
12
+ transform: backendConfig.transform,
13
+ };
14
+ if (backendConfig.hasStatus) {
15
+ finalConfig.type = "STATUS";
16
+ }
17
+ if (backendConfig.hasCurrency) {
18
+ finalConfig.type = "CURRENCY";
19
+ }
20
+ if (backendConfig.hasLeftIcon) {
21
+ finalConfig.leftIcon = "check"; // Or any icon name
22
+ }
23
+ if (backendConfig.hasRightIcon) {
24
+ finalConfig.rightIcon = "arrowRight"; // Or any icon name
25
+ }
26
+ if (backendConfig.hasLeftImage) {
27
+ finalConfig.leftImage = "https://example.com/placeholder.png";
28
+ }
29
+ if (backendConfig.hasRightImage) {
30
+ finalConfig.rightImage = "https://example.com/placeholder.png";
31
+ }
32
+ console.log("[LIB] transformBackendConfig output:", finalConfig);
33
+ return finalConfig;
34
+ }
35
+ /**
36
+ * The main function that shapes columns for React Table.
37
+ * - `columnTitles`: array of column headers
38
+ * - `accessors`: array of "col1", "col2", ...
39
+ * - `columnConfigs`: array of raw backend configs (with booleans)
40
+ * - `renderers`: your app's icon/image/status/currency callbacks
41
+ */
42
+ function formatTableData(columnTitles, accessors, columnConfigs, renderers = {}) {
43
+ /**
44
+ * Creates the "Cell" renderer for each column,
45
+ * checking icons/images or type fields,
46
+ * then calling the relevant `renderers` method.
47
+ */
48
+ function renderCellContent(config) {
49
+ return ({ value }) => {
50
+ // Log to see which type we ended up with, and what the cell value is
51
+ console.log("[LIB] renderCellContent -> config.type:", config.type, "value:", value);
52
+ // 1. Check icons/images
53
+ if (config.leftIcon && renderers.renderIcon) {
54
+ console.log("[LIB] using leftIcon:", config.leftIcon);
55
+ const icon = (_jsx("div", { className: "pr-2", children: renderers.renderIcon(config.leftIcon, "solid") }));
56
+ return renderContent(config, value, icon, "left");
57
+ }
58
+ if (config.rightIcon && renderers.renderIcon) {
59
+ console.log("[LIB] using rightIcon:", config.rightIcon);
60
+ const icon = (_jsx("div", { className: "pl-2", children: renderers.renderIcon(config.rightIcon, "solid") }));
61
+ return renderContent(config, value, icon, "right");
62
+ }
63
+ if (config.leftImage && renderers.renderImage) {
64
+ console.log("[LIB] using leftImage:", config.leftImage);
65
+ const image = (_jsx("div", { className: "pr-2", children: renderers.renderImage({
66
+ imageUrl: config.leftImage,
67
+ altText: "Image",
68
+ width: 30,
69
+ height: 30,
70
+ }) }));
71
+ return renderContent(config, value, image, "left");
72
+ }
73
+ if (config.rightImage && renderers.renderImage) {
74
+ console.log("[LIB] using rightImage:", config.rightImage);
75
+ const image = (_jsx("div", { className: "pl-2", children: renderers.renderImage({
76
+ imageUrl: config.rightImage,
77
+ altText: "Image",
78
+ width: 30,
79
+ height: 30,
80
+ }) }));
81
+ return renderContent(config, value, image, "right");
82
+ }
83
+ // 2. Check type
84
+ switch (config.type) {
85
+ case "STATUS":
86
+ console.log("[LIB] checking STATUS case, value:", value);
87
+ if (renderers.renderStatus && typeof value === "string") {
88
+ console.log("[LIB] calling renderers.renderStatus!");
89
+ return renderContent(config, value, renderers.renderStatus(value));
90
+ }
91
+ break;
92
+ case "CURRENCY":
93
+ console.log("[LIB] checking CURRENCY case, value:", value);
94
+ if (renderers.renderCurrency && typeof value === "string") {
95
+ console.log("[LIB] calling renderers.renderCurrency!");
96
+ const formatted = renderers.renderCurrency(value);
97
+ return renderContent(config, formatted);
98
+ }
99
+ break;
100
+ // Add other types if needed
101
+ default:
102
+ console.log("[LIB] type is:", config.type, "falling back to raw text");
103
+ }
104
+ // 3. Otherwise, raw text
105
+ return (_jsx("div", { style: { display: "flex", alignItems: "center" }, children: typeof value === "string" ? _jsx("p", { children: value }) : value }));
106
+ };
107
+ }
108
+ /**
109
+ * Helper that wraps the cell in <div> or <a>,
110
+ * optionally placing icons left/right.
111
+ */
112
+ function renderContent(config, value, content, position) {
113
+ return (_jsx("div", { style: { display: "flex", alignItems: "center" }, className: "flex w-full", children: config.linkClasses ? (_jsxs("a", { href: "#", className: `flex ${config.linkClasses}`, onClick: (e) => e.stopPropagation(), children: [position === "left" && content, typeof value === "string" ? _jsx("p", { children: value }) : value, position === "right" && content] })) : (_jsxs(_Fragment, { children: [position === "left" && content, typeof value === "string" ? _jsx("p", { children: value }) : value, position === "right" && content] })) }));
114
+ }
115
+ // Build columns, mapping each "raw config" through `transformBackendConfig`
116
+ const columns = columnTitles.map((title, i) => {
117
+ const accessor = accessors[i];
118
+ const key = `col${i + 1}`;
119
+ // 1) Find the raw config from your backend array
120
+ const rawBackendConfig = columnConfigs.find((c) => c.key === key) || {
121
+ key,
122
+ type: "DEFAULT",
123
+ };
124
+ console.log("[LIB] rawBackendConfig for", key, ":", rawBackendConfig);
125
+ // 2) Convert booleans => library fields
126
+ const config = transformBackendConfig(rawBackendConfig);
127
+ console.log("[LIB] final config for", key, ":", config);
128
+ return {
129
+ Header: title,
130
+ accessor,
131
+ Cell: ({ value }) => renderCellContent(config)({ value }),
132
+ };
133
+ });
134
+ console.log("[LIB] final columns:", columns);
135
+ return columns;
136
+ }
137
+ /**
138
+ * Helper to generate "col1", "col2", "col3", etc.
139
+ */
140
+ function generateAccessors(columnTitles) {
141
+ return columnTitles.map((_, index) => `col${index + 1}`);
142
+ }
143
+ // Named exports
144
+ export { formatTableData, generateAccessors };
145
+ // Default export object (so you can import the entire thing as default if you want)
146
+ export default {
147
+ formatTableData,
148
+ generateAccessors,
149
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@agilant/toga-blox",
3
3
  "private": false,
4
- "version": "1.0.98",
4
+ "version": "1.0.100",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",