@solexllc/usx-react 0.8.0 → 0.9.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.
Files changed (58) hide show
  1. package/README.md +15 -0
  2. package/dist/index.cjs +3173 -3238
  3. package/dist/index.js +3174 -3239
  4. package/package.json +4 -4
  5. package/src/components/checkbox/Checkbox.tsx +9 -4
  6. package/src/components/checkbox/checkbox.django.html +2 -2
  7. package/src/components/checkbox/config.json +9 -0
  8. package/src/components/data-table/DataTable.tsx +177 -0
  9. package/src/components/data-table/README.md +42 -0
  10. package/src/components/data-table/TableBody.tsx +123 -0
  11. package/src/components/{table → data-table}/TableCell.tsx +6 -3
  12. package/src/components/data-table/TableContext.tsx +49 -0
  13. package/src/components/{table → data-table}/TableFoot.tsx +8 -7
  14. package/src/components/data-table/TableGroup.tsx +62 -0
  15. package/src/components/data-table/TableHead.tsx +126 -0
  16. package/src/components/data-table/TableRow.tsx +24 -0
  17. package/src/components/data-table/cells.ts +49 -0
  18. package/src/components/data-table/columns.ts +15 -0
  19. package/src/components/data-table/config.json +375 -0
  20. package/src/components/data-table/data-table.django-snapshot.html +13560 -0
  21. package/src/components/data-table/data-table.django.html +13 -0
  22. package/src/components/data-table/data-table.html +34 -0
  23. package/src/components/data-table/data-table.react-snapshot.html +12033 -0
  24. package/src/components/{table → data-table}/table-body.django.html +12 -12
  25. package/src/components/{table → data-table}/table-foot.django.html +6 -6
  26. package/src/components/data-table/table-group.django.html +24 -0
  27. package/src/components/data-table/table-head.django.html +21 -0
  28. package/src/components/data-table/table-row.django.html +57 -0
  29. package/src/components/{table → data-table}/types.ts +48 -5
  30. package/src/components/pagination/Pagination.tsx +7 -1
  31. package/src/components/pagination/config.json +8 -0
  32. package/src/components/pagination/navigation/PaginationNavigation.tsx +10 -0
  33. package/src/components/pagination/step-options/PaginationStepOptions.tsx +3 -0
  34. package/src/components/radio-buttons/RadioButtons.tsx +37 -27
  35. package/src/components/radio-buttons/config.json +19 -1
  36. package/src/components/radio-buttons/radio-buttons.django-snapshot.html +28 -60
  37. package/src/components/radio-buttons/radio-buttons.django.html +16 -28
  38. package/src/components/table/Table.tsx +64 -334
  39. package/src/components/table/config.json +21 -339
  40. package/src/components/table/table.django-snapshot.html +712 -1482
  41. package/src/components/table/table.django.html +14 -59
  42. package/src/components/table/table.html +7 -2364
  43. package/src/components/table/table.react-snapshot.html +711 -2085
  44. package/src/index.js +1 -0
  45. package/src/components/table/TableBody.tsx +0 -256
  46. package/src/components/table/TableContext.tsx +0 -87
  47. package/src/components/table/TableGroup.tsx +0 -69
  48. package/src/components/table/TableHead.tsx +0 -139
  49. package/src/components/table/TableRow.tsx +0 -56
  50. package/src/components/table/hooks/useRowDetails.ts +0 -34
  51. package/src/components/table/hooks/useTableGroups.ts +0 -61
  52. package/src/components/table/hooks/useTableSelect.ts +0 -82
  53. package/src/components/table/hooks/useTableSort.ts +0 -60
  54. package/src/components/table/table-group.django.html +0 -40
  55. package/src/components/table/table-head.django.html +0 -85
  56. package/src/components/table/table-row.django.html +0 -98
  57. package/src/components/table/table_helper.py +0 -298
  58. /package/src/components/{table → data-table}/table-detail-row.django.html +0 -0
@@ -1,298 +0,0 @@
1
- """
2
- table_helper.py — Server-side data utilities for the Django Table component.
3
-
4
- These functions replace the React hooks (useTableSort, useTableGroups, etc.).
5
- A view calls the relevant helpers before rendering, then passes the results as
6
- template context. Zero Django dependencies — importable and testable anywhere.
7
- """
8
-
9
- from __future__ import annotations
10
-
11
- import math
12
- from typing import Any
13
-
14
-
15
- # ── Sorting ───────────────────────────────────────────────────────────────────
16
-
17
- def _sort_key(row: dict, key: str):
18
- """
19
- Null-safe sort key matching useTableSort's defaultCompareFn:
20
- - None / missing values sort last.
21
- - Strings compared case-insensitively (localeCompare approximation).
22
- - Numbers compared numerically.
23
- """
24
- val = row.get(key)
25
- if val is None:
26
- return (1, '') # nulls last
27
- if isinstance(val, str):
28
- return (0, val.lower())
29
- return (0, val)
30
-
31
-
32
- def sort_data(data: list[dict], key: str, direction: str = 'asc') -> list[dict]:
33
- """
34
- Return a stable-sorted copy of *data* on *key*.
35
-
36
- direction: 'asc' (default) or 'desc'.
37
- Matches the sort + optional .reverse() in useTableSort.js.
38
- """
39
- sorted_rows = sorted(data, key=lambda row: _sort_key(row, key))
40
- if direction == 'desc':
41
- sorted_rows.reverse()
42
- return sorted_rows
43
-
44
-
45
- # ── Grouping ──────────────────────────────────────────────────────────────────
46
-
47
- def group_data(data: list[dict], field: str) -> list[dict]:
48
- """
49
- Group *data* by *field*, preserving insertion order.
50
-
51
- Returns:
52
- [{'key': group_value, 'rows': [...]}, ...]
53
-
54
- Matches the Map-based grouping in useTableGroups.js.
55
- Pass groupBy['property'] when the React groupBy prop is an object.
56
- """
57
- groups: dict[str, list] = {}
58
- for row in data:
59
- key = str(row.get(field, '__ungrouped__'))
60
- groups.setdefault(key, []).append(row)
61
- return [{'key': k, 'rows': v} for k, v in groups.items()]
62
-
63
-
64
- # ── Pagination ────────────────────────────────────────────────────────────────
65
-
66
- def paginate_table(data: list, page: int = 1, page_size: int = 10) -> dict:
67
- """
68
- Slice *data* for the requested *page*.
69
-
70
- Returns a dict suitable for spreading into template context:
71
- {
72
- 'rows': sliced list for this page,
73
- 'total': len(data),
74
- 'current_page': page (clamped),
75
- 'total_pages': ceil(total / page_size),
76
- 'page_size': page_size,
77
- }
78
-
79
- Pass 'rows' as 'data' and the rest into the pagination sub-template.
80
- """
81
- total = len(data)
82
- total_pages = max(1, math.ceil(total / page_size)) if total else 1
83
- page = max(1, min(page, total_pages))
84
- start = (page - 1) * page_size
85
- return {
86
- 'rows': data[start: start + page_size],
87
- 'total': total,
88
- 'current_page': page,
89
- 'total_pages': total_pages,
90
- 'page_size': page_size,
91
- }
92
-
93
-
94
- # ── Footer aggregation ────────────────────────────────────────────────────────
95
-
96
- def _aggregate(data: list[dict], key: str, agg_type: str) -> Any:
97
- """
98
- Compute an aggregate value for *key* across *data*.
99
- Matches the aggregate() helper in TableFoot.jsx.
100
- """
101
- vals = []
102
- for row in data:
103
- try:
104
- v = float(row[key])
105
- vals.append(v)
106
- except (KeyError, TypeError, ValueError):
107
- pass
108
-
109
- if not vals:
110
- return '--'
111
-
112
- if agg_type == 'sum':
113
- result = sum(vals)
114
- elif agg_type == 'avg':
115
- result = round(sum(vals) / len(vals), 2)
116
- elif agg_type == 'min':
117
- result = min(vals)
118
- elif agg_type == 'max':
119
- result = max(vals)
120
- elif agg_type == 'count':
121
- result = len(vals)
122
- else:
123
- return '--'
124
-
125
- # Return int when the result is a whole number
126
- return int(result) if result == int(result) else result
127
-
128
-
129
- def compute_footer(columns: list[dict], data: list[dict]) -> dict:
130
- """
131
- Return a {col_key: rendered_value} dict for the tfoot row.
132
-
133
- Each column may define:
134
- col['footer'] — a literal value or callable(data) -> value
135
- col['aggregate'] — one of 'sum', 'avg', 'min', 'max', 'count'
136
-
137
- Matches the logic in TableFoot.jsx.
138
- """
139
- result = {}
140
- for col in columns:
141
- key = col.get('key')
142
- if not key:
143
- continue
144
- if 'footer' in col:
145
- footer = col['footer']
146
- result[key] = footer(data) if callable(footer) else footer
147
- elif col.get('aggregate'):
148
- result[key] = _aggregate(data, key, col['aggregate'])
149
- return result
150
-
151
-
152
- # ── Header rows (multi-level) ─────────────────────────────────────────────────
153
-
154
- def _get_depth(columns: list[dict]) -> int:
155
- """Maximum nesting depth. Leaf columns count as depth 1."""
156
- max_d = 1
157
- for col in columns:
158
- if col.get('columns'):
159
- d = 1 + _get_depth(col['columns'])
160
- if d > max_d:
161
- max_d = d
162
- return max_d
163
-
164
-
165
- def _get_leaf_count(col: dict) -> int:
166
- """Number of leaf columns under *col* (or 1 if it is itself a leaf)."""
167
- if not col.get('columns'):
168
- return 1
169
- return sum(_get_leaf_count(c) for c in col['columns'])
170
-
171
-
172
- def build_header_rows(columns: list[dict]) -> list[list[dict]]:
173
- """
174
- Flatten a (possibly nested) column list into per-level header rows.
175
-
176
- Returns:
177
- [
178
- [{'col': {...}, 'row_span': 2, 'col_span': 1}, ...], # level 0
179
- [{'col': {...}, 'row_span': 1, 'col_span': 3}, ...], # level 1
180
- ]
181
-
182
- Pure Python port of buildHeaderRows() + traverse() in TableHead.jsx.
183
- """
184
- visible = [c for c in columns if not c.get('hidden')]
185
- total_depth = _get_depth(visible)
186
- rows: list[list] = [[] for _ in range(total_depth)]
187
-
188
- def traverse(cols: list[dict], level: int) -> None:
189
- for col in cols:
190
- is_leaf = not col.get('columns')
191
- row_span = (total_depth - level) if is_leaf else 1
192
- col_span = 1 if is_leaf else _get_leaf_count(col)
193
- rows[level].append({
194
- 'col': col,
195
- 'row_span': row_span,
196
- 'col_span': col_span,
197
- })
198
- if not is_leaf:
199
- traverse(col['columns'], level + 1)
200
-
201
- traverse(visible, 0)
202
- return rows
203
-
204
-
205
- # ── Leaf columns ──────────────────────────────────────────────────────────────
206
-
207
- def get_leaf_cols(columns: list[dict]) -> list[dict]:
208
- """
209
- Recursively collect only leaf columns (no 'columns' children).
210
- Skips hidden columns.
211
-
212
- Matches getLeafCols() in TableBody.jsx.
213
- """
214
- result = []
215
- for col in columns:
216
- if col.get('hidden'):
217
- continue
218
- if col.get('columns'):
219
- result.extend(get_leaf_cols(col['columns']))
220
- else:
221
- result.append(col)
222
- return result
223
-
224
-
225
- # ── Total column count ────────────────────────────────────────────────────────
226
-
227
- def total_col_count(
228
- columns: list[dict],
229
- selection_mode: str | None = None,
230
- row_details: bool = False,
231
- ) -> int:
232
- """
233
- Return the total <td>/<th> column count including auxiliary columns.
234
- Matches the totalCols useMemo in Table.jsx.
235
- """
236
- count = len(get_leaf_cols(columns))
237
- if selection_mode:
238
- count += 1
239
- if row_details:
240
- count += 1
241
- return count
242
-
243
-
244
- # ── Convenience: full table context builder ───────────────────────────────────
245
-
246
- def build_table_context(
247
- columns: list[dict],
248
- data: list[dict],
249
- *,
250
- sort: dict | None = None,
251
- group_by: str | None = None,
252
- selection_mode: str | None = None,
253
- row_details: bool = False,
254
- page: int = 1,
255
- page_size: int = 10,
256
- paginate: bool = False,
257
- ) -> dict:
258
- """
259
- Convenience wrapper: compute all derived context in one call.
260
-
261
- Returns a dict ready to unpack into template context (via **ctx or
262
- context.update(ctx)). Individual helpers can still be called separately
263
- for full control.
264
- """
265
- # Sort
266
- if sort and sort.get('key') and sort.get('direction'):
267
- data = sort_data(data, sort['key'], sort['direction'])
268
-
269
- # Header rows (multi-level)
270
- header_rows = build_header_rows(columns)
271
- leaf_cols = get_leaf_cols(columns)
272
- total_cols = total_col_count(columns, selection_mode, row_details)
273
-
274
- # Grouping
275
- groups = group_data(data, group_by) if group_by else None
276
-
277
- # Pagination
278
- pagination = None
279
- if paginate:
280
- pag = paginate_table(data, page=page, page_size=page_size)
281
- data = pag['rows']
282
- pagination = {
283
- 'current_page': pag['current_page'],
284
- 'total_pages': pag['total_pages'],
285
- 'total': pag['total'],
286
- 'page_size': pag['page_size'],
287
- }
288
-
289
- return {
290
- 'header_rows': header_rows,
291
- 'leaf_columns': leaf_cols,
292
- 'total_cols': total_cols,
293
- 'total_depth': len(header_rows),
294
- 'data': data,
295
- 'groups': groups,
296
- 'sort': sort or {},
297
- 'pagination': pagination,
298
- }