@jsenv/navi 0.0.1 → 0.1.1

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 (139) hide show
  1. package/dist/jsenv_navi.js +22959 -0
  2. package/index.js +66 -16
  3. package/package.json +23 -11
  4. package/src/actions.js +50 -26
  5. package/src/browser_integration/browser_integration.js +31 -6
  6. package/src/browser_integration/via_history.js +42 -9
  7. package/src/components/action_execution/render_actionable_component.jsx +6 -4
  8. package/src/components/action_execution/use_action.js +51 -282
  9. package/src/components/action_execution/use_execute_action.js +106 -92
  10. package/src/components/action_execution/use_run_on_mount.js +9 -0
  11. package/src/components/action_renderer.jsx +21 -32
  12. package/src/components/demos/0_button_demo.html +574 -103
  13. package/src/components/demos/10_column_reordering_debug.html +277 -0
  14. package/src/components/demos/11_table_selection_debug.html +432 -0
  15. package/src/components/demos/1_checkbox_demo.html +579 -202
  16. package/src/components/demos/2_input_textual_demo.html +81 -138
  17. package/src/components/demos/3_radio_demo.html +0 -2
  18. package/src/components/demos/4_select_demo.html +19 -23
  19. package/src/components/demos/6_tablist_demo.html +77 -0
  20. package/src/components/demos/7_table_selection_demo.html +176 -0
  21. package/src/components/demos/8_table_fixed_headers_demo.html +584 -0
  22. package/src/components/demos/9_table_column_drag_demo.html +325 -0
  23. package/src/components/demos/action/0_button_demo.html +2 -4
  24. package/src/components/demos/action/1_input_text_demo.html +643 -222
  25. package/src/components/demos/action/3_details_demo.html +146 -115
  26. package/src/components/demos/action/4_input_checkbox_demo.html +442 -322
  27. package/src/components/demos/action/5_input_checkbox_state_demo.html +270 -0
  28. package/src/components/demos/action/6_checkbox_list_demo.html +304 -72
  29. package/src/components/demos/action/7_radio_list_demo.html +310 -170
  30. package/src/components/demos/action/{8_editable_text_demo.html → 8_editable_demo.html} +65 -76
  31. package/src/components/demos/action/9_link_demo.html +84 -62
  32. package/src/components/demos/ui_transition/0_action_renderer_ui_transition_demo.html +695 -0
  33. package/src/components/demos/ui_transition/1_nested_ui_transition_demo.html +429 -0
  34. package/src/components/demos/ui_transition/2_height_transition_test.html +295 -0
  35. package/src/components/details/details.jsx +62 -64
  36. package/src/components/edition/editable.jsx +186 -0
  37. package/src/components/field/README.md +247 -0
  38. package/src/components/{input → field}/button.jsx +151 -130
  39. package/src/components/field/checkbox_list.jsx +184 -0
  40. package/src/components/{collect_form_element_values.js → field/collect_form_element_values.js} +7 -4
  41. package/src/components/{input → field}/field_css.js +4 -1
  42. package/src/components/field/form.jsx +211 -0
  43. package/src/components/{input → field}/input.jsx +1 -0
  44. package/src/components/{input → field}/input_checkbox.jsx +132 -155
  45. package/src/components/{input → field}/input_radio.jsx +135 -46
  46. package/src/components/field/input_textual.jsx +418 -0
  47. package/src/components/field/label.jsx +32 -0
  48. package/src/components/field/radio_list.jsx +182 -0
  49. package/src/components/{input → field}/select.jsx +17 -32
  50. package/src/components/field/use_action_events.js +132 -0
  51. package/src/components/field/use_form_events.js +55 -0
  52. package/src/components/field/use_ui_state_controller.js +506 -0
  53. package/src/components/item_tracker/README.md +461 -0
  54. package/src/components/item_tracker/use_isolated_item_tracker.jsx +209 -0
  55. package/src/components/item_tracker/use_isolated_item_tracker_demo.html +148 -0
  56. package/src/components/item_tracker/use_isolated_item_tracker_demo.jsx +460 -0
  57. package/src/components/item_tracker/use_item_tracker.jsx +143 -0
  58. package/src/components/item_tracker/use_item_tracker_demo.html +207 -0
  59. package/src/components/item_tracker/use_item_tracker_demo.jsx +216 -0
  60. package/src/components/keyboard_shortcuts/active_keyboard_shortcuts.jsx +87 -0
  61. package/src/components/keyboard_shortcuts/aria_key_shortcuts.js +61 -0
  62. package/src/components/keyboard_shortcuts/keyboard_key_meta.js +17 -0
  63. package/src/components/keyboard_shortcuts/keyboard_shortcuts.js +371 -0
  64. package/src/components/link/link.jsx +65 -102
  65. package/src/components/link/link_with_icon.jsx +52 -0
  66. package/src/components/loader/loader_background.jsx +85 -64
  67. package/src/components/loader/rectangle_loading.jsx +38 -19
  68. package/src/components/route.jsx +8 -4
  69. package/src/components/selection/selection.jsx +1583 -0
  70. package/src/components/svg/font_sized_svg.jsx +45 -0
  71. package/src/components/svg/icon_and_text.jsx +21 -0
  72. package/src/components/svg/svg_mask_overlay.jsx +105 -0
  73. package/src/components/table/drag/table_drag.jsx +506 -0
  74. package/src/components/table/resize/table_resize.jsx +650 -0
  75. package/src/components/table/resize/table_size.js +43 -0
  76. package/src/components/table/selection/table_selection.js +106 -0
  77. package/src/components/table/selection/table_selection.jsx +203 -0
  78. package/src/components/table/sticky/sticky_group.js +354 -0
  79. package/src/components/table/sticky/table_sticky.js +25 -0
  80. package/src/components/table/sticky/table_sticky.jsx +501 -0
  81. package/src/components/table/table.jsx +721 -0
  82. package/src/components/table/table_css.js +211 -0
  83. package/src/components/table/table_ui.jsx +49 -0
  84. package/src/components/table/use_cells_and_columns.js +90 -0
  85. package/src/components/table/use_object_array_to_cells.js +46 -0
  86. package/src/components/table/z_indexes.js +23 -0
  87. package/src/components/tablist/tablist.jsx +99 -0
  88. package/src/components/text/overflow.jsx +15 -0
  89. package/src/components/text/text_and_count.jsx +28 -0
  90. package/src/components/ui_transition.jsx +128 -0
  91. package/src/components/use_auto_focus.js +58 -7
  92. package/src/components/use_batch_during_render.js +33 -0
  93. package/src/components/use_debounce_true.js +7 -7
  94. package/src/components/use_dependencies_diff.js +35 -0
  95. package/src/components/use_focus_group.js +4 -3
  96. package/src/components/use_initial_value.js +8 -34
  97. package/src/components/use_signal_sync.js +1 -1
  98. package/src/components/use_stable_callback.js +68 -0
  99. package/src/components/use_state_array.js +16 -9
  100. package/src/docs/actions.md +22 -0
  101. package/src/notes.md +33 -12
  102. package/src/route/route.js +97 -47
  103. package/src/store/resource_graph.js +2 -1
  104. package/src/store/tests/{resource_graph_dependencies.test.js → resource_graph_dependencies.test_manual.js} +13 -13
  105. package/src/utils/is_signal.js +20 -0
  106. package/src/utils/stringify_for_display.js +4 -23
  107. package/src/validation/constraints/confirm_constraint.js +14 -0
  108. package/src/validation/constraints/create_unique_value_constraint.js +27 -0
  109. package/src/validation/constraints/native_constraints.js +313 -0
  110. package/src/validation/constraints/readonly_constraint.js +36 -0
  111. package/src/validation/constraints/single_space_constraint.js +13 -0
  112. package/src/validation/custom_constraint_validation.js +599 -0
  113. package/src/validation/custom_message.js +18 -0
  114. package/src/validation/demos/browser_style.png +0 -0
  115. package/src/validation/demos/form_validation_demo.html +142 -0
  116. package/src/validation/demos/form_validation_demo_preact.html +87 -0
  117. package/src/validation/demos/form_validation_native_popover_demo.html +168 -0
  118. package/src/validation/demos/form_validation_vs_native_demo.html +172 -0
  119. package/src/validation/demos/validation_message_demo.html +203 -0
  120. package/src/validation/hooks/use_constraints.js +23 -0
  121. package/src/validation/hooks/use_custom_validation_ref.js +73 -0
  122. package/src/validation/hooks/use_validation_message.js +19 -0
  123. package/src/validation/validation_message.js +741 -0
  124. package/src/components/editable_text/editable_text.jsx +0 -96
  125. package/src/components/form.jsx +0 -144
  126. package/src/components/input/checkbox_list.jsx +0 -294
  127. package/src/components/input/field.jsx +0 -61
  128. package/src/components/input/input_textual.jsx +0 -338
  129. package/src/components/input/radio_list.jsx +0 -283
  130. package/src/components/input/use_form_event.js +0 -20
  131. package/src/components/input/use_on_change.js +0 -12
  132. package/src/components/selection/selection.js +0 -5
  133. package/src/components/selection/selection_context.jsx +0 -262
  134. package/src/components/shortcut/shortcut_context.jsx +0 -390
  135. package/src/components/use_action_events.js +0 -37
  136. package/src/utils/iterable_weak_set.js +0 -62
  137. /package/src/components/demos/action/{11_nested_shortcuts_demo.html → 11_nested_shortcuts_demo.xhtml} +0 -0
  138. /package/src/components/{shortcut → keyboard_shortcuts}/os.js +0 -0
  139. /package/src/route/{route.test.html → route.xtest.html} +0 -0
@@ -0,0 +1,721 @@
1
+ /**
2
+ * Table Component with Custom Border and Selection System
3
+ *
4
+ * PROBLEM: We want to draw selected table cells with a clear visual perimeter
5
+ *
6
+ * ATTEMPTED SOLUTIONS & THEIR ISSUES:
7
+ *
8
+ * 1. Drawing selection outside the table:
9
+ * - z-index issues: Hard to ensure selection appears above all table elements
10
+ * - Performance issues: Constant recalculation during resizing, scrolling, etc.
11
+ * - Positioning complexity: Managing absolute positioning relative to table cells
12
+ *
13
+ * 2. Using native CSS table cell borders:
14
+ * - Border rendering artifacts: CSS borders are not rendered as straight lines,
15
+ * making selection perimeter imperfect (especially with thick borders)
16
+ * - Border-collapse compatibility: Native border-collapse causes sticky elements
17
+ * to lose borders while scrolling in some browsers
18
+ * - Dimension changes: Custom border-collapse (manually disabling adjacent borders)
19
+ * changes cell dimensions, making selection outline visible and inconsistent
20
+ *
21
+ * SOLUTION: Custom border system using box-shadow
22
+ *
23
+ * KEY PRINCIPLES:
24
+ * - Use inset box-shadow to ensure borders appear above table cell backgrounds
25
+ * - Use ::before pseudo-elements with position: absolute for flexible positioning
26
+ * - Each cell draws its own borders independently (no border-collapse by default)
27
+ * - Selection borders override table borders using higher CSS specificity
28
+ * - Sticky borders use thicker box-shadows in accent color (yellow)
29
+ *
30
+ * TECHNICAL IMPLEMENTATION:
31
+ * - All borders use inset box-shadow with specific directional mapping:
32
+ * * Top: inset 0 1px 0 0
33
+ * * Right: inset -1px 0 0 0
34
+ * * Bottom: inset 0 -1px 0 0
35
+ * * Left: inset 1px 0 0 0
36
+ * - Selection borders (blue) override table borders (red) in same pseudo-element
37
+ * - Sticky borders replace regular borders with thicker colored variants
38
+ * - Border-collapse mode available as optional feature for future use
39
+ *
40
+ * Note how border disappear for sticky elements when using border-collapse (https://bugzilla.mozilla.org/show_bug.cgi?id=1727594)
41
+ *
42
+ * Next steps:
43
+ *
44
+ * - Mettre le sticky again dans les tables cells (mais les suivantes pour avoir l'effet d'ombre)
45
+ *
46
+ * - Can add a column (+ button at the end of table headers)
47
+ * - Can add a row (+ button at the end of the row number column )
48
+ * - Delete a row (how?)
49
+ * - Delete a column (how?)
50
+ * - Update table column info (I guess a down arrow icon which opens a meny when clicked for instance)
51
+ */
52
+
53
+ import { useActiveElement } from "@jsenv/dom";
54
+ import { createContext, toChildArray } from "preact";
55
+ import { forwardRef } from "preact/compat";
56
+ import { useContext, useId, useImperativeHandle, useRef } from "preact/hooks";
57
+
58
+ import { Editable, useEditionController } from "../edition/editable.jsx";
59
+ import { createIsolatedItemTracker } from "../item_tracker/use_isolated_item_tracker.jsx";
60
+ import { createItemTracker } from "../item_tracker/use_item_tracker.jsx";
61
+ import { useKeyboardShortcuts } from "../keyboard_shortcuts/keyboard_shortcuts.js";
62
+ import {
63
+ createSelectionKeyboardShortcuts,
64
+ useSelectableElement,
65
+ } from "../selection/selection.jsx";
66
+ import { useFocusGroup } from "../use_focus_group.js";
67
+ import {
68
+ initDragTableColumnViaPointer,
69
+ TableColumnDropPreview,
70
+ TableDragCloneContainer,
71
+ TableDragContext,
72
+ useTableDragContextValue,
73
+ } from "./drag/table_drag.jsx";
74
+ import {
75
+ TableCellColumnResizeHandles,
76
+ TableCellRowResizeHandles,
77
+ TableColumnResizer,
78
+ TableRowResizer,
79
+ } from "./resize/table_resize.jsx";
80
+ import {
81
+ TableSizeContext,
82
+ useTableSizeContextValue,
83
+ } from "./resize/table_size.js";
84
+ import {
85
+ parseTableSelectionValue,
86
+ stringifyTableSelectionValue,
87
+ TableSelectionContext,
88
+ useTableSelectionContextValue,
89
+ } from "./selection/table_selection.js";
90
+ import { useTableSelectionController } from "./selection/table_selection.jsx";
91
+ import { useStickyGroup } from "./sticky/sticky_group.js";
92
+ import {
93
+ TableStickyContext,
94
+ useTableStickyContextValue,
95
+ } from "./sticky/table_sticky.js";
96
+ import "./sticky/table_sticky.jsx";
97
+ import { TableStickyFrontier } from "./sticky/table_sticky.jsx";
98
+ import "./table_css.js";
99
+ import { TableUI } from "./table_ui.jsx";
100
+
101
+ const [useColumnTrackerProviders, useRegisterColumn, useColumnByIndex] =
102
+ createIsolatedItemTracker();
103
+ const [useRowTrackerProvider, useRegisterRow, useRowByIndex] =
104
+ createItemTracker();
105
+
106
+ const ColumnProducerProviderContext = createContext();
107
+ const ColumnConsumerProviderContext = createContext();
108
+ const ColumnContext = createContext();
109
+ const RowContext = createContext();
110
+ const ColumnIndexContext = createContext();
111
+ const RowIndexContext = createContext();
112
+
113
+ const TableSectionContext = createContext();
114
+ const useIsInTableHead = () => useContext(TableSectionContext) === "head";
115
+
116
+ export const Table = forwardRef((props, ref) => {
117
+ const tableDefaultId = `table-${useId()}`;
118
+ const {
119
+ id = tableDefaultId,
120
+ selection = [],
121
+ selectionColor,
122
+ onSelectionChange,
123
+ onColumnSizeChange,
124
+ onRowSizeChange,
125
+ borderCollapse = true,
126
+ stickyLeftFrontierColumnIndex = -1,
127
+ onStickyLeftFrontierChange,
128
+ stickyTopFrontierRowIndex = -1,
129
+ onStickyTopFrontierChange,
130
+ onColumnOrderChange,
131
+ maxWidth,
132
+ maxHeight,
133
+ overflow,
134
+ children,
135
+ } = props;
136
+
137
+ const innerRef = useRef();
138
+ useImperativeHandle(ref, () => innerRef.current);
139
+ const tableContainerRef = useRef();
140
+ const tableUIRef = useRef();
141
+
142
+ const [ColumnProducerProvider, ColumnConsumerProvider, columns] =
143
+ useColumnTrackerProviders();
144
+ const RowTrackerProvider = useRowTrackerProvider();
145
+ const rows = RowTrackerProvider.items;
146
+
147
+ // selection
148
+ const selectionController = useTableSelectionController({
149
+ tableRef: innerRef,
150
+ selection,
151
+ onSelectionChange,
152
+ selectionColor,
153
+ });
154
+ const selectionContextValue = useTableSelectionContextValue(
155
+ selection,
156
+ selectionController,
157
+ );
158
+
159
+ useFocusGroup(innerRef);
160
+
161
+ // sticky
162
+ useStickyGroup(tableContainerRef, {
163
+ elementSelector: ".navi_table",
164
+ elementReceivingCumulativeStickyPositionRef: tableUIRef,
165
+ });
166
+ const stickyContextValue = useTableStickyContextValue({
167
+ stickyLeftFrontierColumnIndex,
168
+ stickyTopFrontierRowIndex,
169
+ onStickyLeftFrontierChange,
170
+ onStickyTopFrontierChange,
171
+ });
172
+
173
+ // drag columns
174
+ const tableRootRef = useRef();
175
+ const setColumnOrder = (columnIdsNewOrder) => {
176
+ // the code below ensures we re-render the selection when column are re-ordered
177
+ // forcing each previously selected <td> to unselect and newly selected <td> to be selected
178
+ // (because the corresponding DOM node is now different)
179
+ onSelectionChange?.([...selection]);
180
+ onColumnOrderChange?.(columnIdsNewOrder);
181
+ };
182
+ const tableDragCloneContainerRef = useRef();
183
+ const tableColumnDropPreviewRef = useRef();
184
+ const dragContextValue = useTableDragContextValue({
185
+ tableDragCloneContainerRef,
186
+ tableColumnDropPreviewRef,
187
+ setColumnOrder,
188
+ columns,
189
+ canChangeColumnOrder: Boolean(onColumnOrderChange),
190
+ });
191
+
192
+ useKeyboardShortcuts(innerRef, [
193
+ ...createSelectionKeyboardShortcuts(selectionController, {
194
+ toggleEnabled: true,
195
+ enabled: () => dragContextValue.grabTarget === null,
196
+ }),
197
+ {
198
+ key: "enter",
199
+ description: "Edit table cell content",
200
+ enabled: () => dragContextValue.grabTarget === null,
201
+ handler: () => {
202
+ // Find the currently focused cell
203
+ const activeCell = document.activeElement.closest("td");
204
+ if (!activeCell) {
205
+ return false;
206
+ }
207
+ activeCell.dispatchEvent(
208
+ new CustomEvent("editrequested", { bubbles: false }),
209
+ );
210
+ return true;
211
+ },
212
+ },
213
+ {
214
+ key: "a-z",
215
+ description: "Start editing table cell content",
216
+ enabled: () => dragContextValue.grabTarget === null,
217
+ handler: (e) => {
218
+ const activeCell = document.activeElement.closest("td");
219
+ if (!activeCell) {
220
+ return false;
221
+ }
222
+ activeCell.dispatchEvent(
223
+ new CustomEvent("editrequested", {
224
+ bubbles: false,
225
+ detail: { initialValue: e.key },
226
+ }),
227
+ );
228
+ return true;
229
+ },
230
+ },
231
+ ]);
232
+
233
+ // resizing
234
+ const columnResizerRef = useRef();
235
+ const rowResizerRef = useRef();
236
+ const tableSizeContextValue = useTableSizeContextValue({
237
+ onColumnSizeChange,
238
+ onRowSizeChange,
239
+ columns,
240
+ rows,
241
+ columnResizerRef,
242
+ rowResizerRef,
243
+ });
244
+
245
+ return (
246
+ <div
247
+ ref={tableRootRef}
248
+ className="navi_table_root"
249
+ style={{
250
+ overflow,
251
+ "--table-max-width": maxWidth ? `${maxWidth}px` : undefined,
252
+ "--table-max-height": maxHeight ? `${maxHeight}px` : undefined,
253
+ }}
254
+ >
255
+ <div ref={tableContainerRef} className="navi_table_container">
256
+ <table
257
+ ref={innerRef}
258
+ id={id}
259
+ className="navi_table"
260
+ aria-multiselectable="true"
261
+ data-multiselection={selection.length > 1 ? "" : undefined}
262
+ data-border-collapse={borderCollapse ? "" : undefined}
263
+ data-droppable=""
264
+ >
265
+ <TableSizeContext.Provider value={tableSizeContextValue}>
266
+ <TableSelectionContext.Provider value={selectionContextValue}>
267
+ <TableDragContext.Provider value={dragContextValue}>
268
+ <TableStickyContext.Provider value={stickyContextValue}>
269
+ <ColumnProducerProviderContext.Provider
270
+ value={ColumnProducerProvider}
271
+ >
272
+ <ColumnConsumerProviderContext.Provider
273
+ value={ColumnConsumerProvider}
274
+ >
275
+ <RowTrackerProvider>{children}</RowTrackerProvider>
276
+ </ColumnConsumerProviderContext.Provider>
277
+ </ColumnProducerProviderContext.Provider>
278
+ </TableStickyContext.Provider>
279
+ </TableDragContext.Provider>
280
+ </TableSelectionContext.Provider>
281
+ </TableSizeContext.Provider>
282
+ </table>
283
+ <TableUI ref={tableUIRef} tableRef={innerRef} tableId={id}>
284
+ <TableStickyContext.Provider value={stickyContextValue}>
285
+ <TableStickyFrontier tableRef={innerRef} />
286
+ </TableStickyContext.Provider>
287
+ <TableColumnResizer ref={columnResizerRef} />
288
+ <TableRowResizer ref={rowResizerRef} />
289
+ <TableDragCloneContainer
290
+ ref={tableDragCloneContainerRef}
291
+ tableId={id}
292
+ />
293
+ <TableColumnDropPreview ref={tableColumnDropPreviewRef} />
294
+ </TableUI>
295
+ </div>
296
+ </div>
297
+ );
298
+ });
299
+ export const Colgroup = ({ children }) => {
300
+ const ColumnProducerProvider = useContext(ColumnProducerProviderContext);
301
+ return (
302
+ <colgroup className="navi_colgroup">
303
+ <ColumnProducerProvider>{children}</ColumnProducerProvider>
304
+ </colgroup>
305
+ );
306
+ };
307
+ export const Col = ({ id, width, immovable, backgroundColor }) => {
308
+ const columnIndex = useRegisterColumn({
309
+ id,
310
+ width,
311
+ immovable,
312
+ backgroundColor,
313
+ });
314
+ const { stickyLeftFrontierColumnIndex } = useContext(TableStickyContext);
315
+ const isStickyLeft = columnIndex <= stickyLeftFrontierColumnIndex;
316
+
317
+ return (
318
+ <col
319
+ className="navi_col"
320
+ id={id}
321
+ data-sticky-left={isStickyLeft ? "" : undefined}
322
+ data-drag-sticky-left-frontier={isStickyLeft ? "" : undefined}
323
+ data-drag-obstacle={immovable ? "move-column" : undefined}
324
+ style={{
325
+ minWidth: width ? `${width}px` : undefined,
326
+ maxWidth: width ? `${width}px` : undefined,
327
+ }}
328
+ />
329
+ );
330
+ };
331
+ export const Thead = ({ children }) => {
332
+ return (
333
+ <thead>
334
+ <TableSectionContext.Provider value="head">
335
+ {children}
336
+ </TableSectionContext.Provider>
337
+ </thead>
338
+ );
339
+ };
340
+ export const Tbody = ({ children }) => {
341
+ return (
342
+ <tbody>
343
+ <TableSectionContext.Provider value="body">
344
+ {children}
345
+ </TableSectionContext.Provider>
346
+ </tbody>
347
+ );
348
+ };
349
+ export const Tr = ({ id, height, children }) => {
350
+ if (!id) {
351
+ console.warn("<Tr /> must have an id prop to enable selection");
352
+ }
353
+ id = String(id); // we need strings as this value is going to be used in data attributes
354
+ // and when generating cell ids
355
+
356
+ const { selectedRowIds } = useContext(TableSelectionContext);
357
+ const { stickyTopFrontierRowIndex } = useContext(TableStickyContext);
358
+ const rowIndex = useRegisterRow({ id, height });
359
+ const row = useRowByIndex(rowIndex);
360
+ const ColumnConsumerProvider = useContext(ColumnConsumerProviderContext);
361
+
362
+ const isStickyTop = rowIndex <= stickyTopFrontierRowIndex;
363
+ const isStickyTopFrontier = rowIndex === stickyTopFrontierRowIndex;
364
+ const isRowSelected = selectedRowIds.includes(id);
365
+
366
+ children = toChildArray(children);
367
+
368
+ /* We use <TableRowCells> to be able to provide <ColumnConsumerProvider />
369
+ that is needed by useColumnByIndex */
370
+
371
+ return (
372
+ <tr
373
+ className="navi_tr"
374
+ data-row-id={id ? id : undefined}
375
+ aria-selected={isRowSelected}
376
+ data-sticky-top={isStickyTop ? "" : undefined}
377
+ data-drag-sticky-top-frontier={isStickyTopFrontier ? "" : undefined}
378
+ style={{
379
+ height: height ? `${height}px` : undefined,
380
+ maxHeight: height ? `${height}px` : undefined,
381
+ }}
382
+ >
383
+ <ColumnConsumerProvider>
384
+ <TableRowCells rowIndex={rowIndex} row={row}>
385
+ {children}
386
+ </TableRowCells>
387
+ </ColumnConsumerProvider>
388
+ </tr>
389
+ );
390
+ };
391
+
392
+ const TableRowCells = ({ children, rowIndex, row }) => {
393
+ return children.map((child, columnIndex) => {
394
+ const column = useColumnByIndex(columnIndex);
395
+ const columnId = column.id;
396
+
397
+ return (
398
+ <RowContext.Provider key={columnId} value={row}>
399
+ <RowIndexContext.Provider value={rowIndex}>
400
+ <ColumnIndexContext.Provider value={columnIndex}>
401
+ <ColumnContext.Provider value={column}>
402
+ {child}
403
+ </ColumnContext.Provider>
404
+ </ColumnIndexContext.Provider>
405
+ </RowIndexContext.Provider>
406
+ </RowContext.Provider>
407
+ );
408
+ });
409
+ };
410
+
411
+ export const TableCell = forwardRef((props, ref) => {
412
+ const column = useContext(ColumnContext);
413
+ const row = useContext(RowContext);
414
+ const columnIndex = useContext(ColumnIndexContext);
415
+ const rowIndex = useContext(RowIndexContext);
416
+ const {
417
+ className = "",
418
+ canSelectAll,
419
+ canDragColumn,
420
+ canResizeWidth,
421
+ canResizeHeight,
422
+ selectionImpact,
423
+ onClick,
424
+ action,
425
+ name,
426
+ valueSignal,
427
+ // appeareance
428
+ style,
429
+ cursor,
430
+ bold,
431
+ alignX = column.alignX,
432
+ alignY = column.alignY,
433
+ backgroundColor = column.backgroundColor || row.backgroundColor,
434
+ children,
435
+ } = props;
436
+ const cellRef = useRef();
437
+ const isFirstRow = rowIndex === 0;
438
+ const isFirstColumn = columnIndex === 0;
439
+
440
+ // editing
441
+ const editable = Boolean(action);
442
+ const { editing, startEditing, stopEditing } = useEditionController();
443
+ useImperativeHandle(ref, () => ({
444
+ startEditing,
445
+ stopEditing,
446
+ element: cellRef.current,
447
+ }));
448
+
449
+ // stickyness
450
+ const { stickyLeftFrontierColumnIndex, stickyTopFrontierRowIndex } =
451
+ useContext(TableStickyContext);
452
+ const stickyLeft = columnIndex <= stickyLeftFrontierColumnIndex;
453
+ const stickyTop = rowIndex <= stickyTopFrontierRowIndex;
454
+ const isStickyLeftFrontier = columnIndex === stickyLeftFrontierColumnIndex;
455
+ const isAfterStickyLeftFrontier =
456
+ columnIndex === stickyLeftFrontierColumnIndex + 1;
457
+ const isStickyTopFrontier = rowIndex === stickyTopFrontierRowIndex;
458
+ const isAfterStickyTopFrontier = rowIndex === stickyTopFrontierRowIndex + 1;
459
+
460
+ // selection
461
+ const rowId = row.id;
462
+ const columnId = column.id;
463
+ const selectionValue = stringifyTableSelectionValue("cell", {
464
+ rowId,
465
+ columnId,
466
+ });
467
+ const {
468
+ selection,
469
+ selectionController,
470
+ columnIdWithSomeSelectedCellSet,
471
+ rowIdWithSomeSelectedCellSet,
472
+ } = useContext(TableSelectionContext);
473
+ const innerSelectionImpact =
474
+ selectionImpact === undefined
475
+ ? isFirstRow && isFirstColumn && canSelectAll
476
+ ? (allValues) => {
477
+ const cells = allValues.filter(
478
+ (v) => parseTableSelectionValue(v).type === "cell",
479
+ );
480
+ return cells;
481
+ }
482
+ : isFirstRow
483
+ ? (allValues) => {
484
+ const columnCells = allValues.filter((v) => {
485
+ const selectionValueInfo = parseTableSelectionValue(v);
486
+ return (
487
+ selectionValueInfo.type === "cell" &&
488
+ selectionValueInfo.columnId === columnId
489
+ );
490
+ });
491
+ return columnCells;
492
+ }
493
+ : isFirstColumn
494
+ ? (allValues) => {
495
+ const rowCells = allValues.filter((v) => {
496
+ const selectionValueInfo = parseTableSelectionValue(v);
497
+ return (
498
+ selectionValueInfo.type === "cell" &&
499
+ selectionValueInfo.rowId === rowId
500
+ );
501
+ });
502
+ return rowCells;
503
+ }
504
+ : undefined
505
+ : selectionImpact;
506
+
507
+ const { selected } = useSelectableElement(cellRef, {
508
+ selection,
509
+ selectionController,
510
+ selectionImpact: innerSelectionImpact,
511
+ // value: selectionId,
512
+ });
513
+
514
+ // moving column
515
+ const {
516
+ tableDragCloneContainerRef,
517
+ tableColumnDropPreviewRef,
518
+ grabTarget,
519
+ grabColumn,
520
+ releaseColumn,
521
+ canChangeColumnOrder,
522
+ } = useContext(TableDragContext);
523
+ const columnGrabbed = grabTarget === `column:${columnIndex}`;
524
+
525
+ // resizing
526
+ const innerCanDragColumn =
527
+ canDragColumn === undefined
528
+ ? rowIndex === 0 && !column.immovable && Boolean(canChangeColumnOrder)
529
+ : canDragColumn;
530
+ const innerCanResizeWidth =
531
+ canResizeWidth === undefined ? rowIndex === 0 : canResizeWidth;
532
+ const innerCanResizeHeight =
533
+ canResizeHeight === undefined ? columnIndex === 0 : canResizeHeight;
534
+
535
+ // display
536
+ const isInTableHead = useIsInTableHead();
537
+ const innerStyle = {
538
+ ...style,
539
+ };
540
+
541
+ const columnContainsSelectedCell =
542
+ columnIdWithSomeSelectedCellSet.has(columnId);
543
+ const rowContainsSelectedCell = rowIdWithSomeSelectedCellSet.has(rowId);
544
+ const containSelectedCell =
545
+ (isFirstRow && columnContainsSelectedCell) ||
546
+ (isFirstColumn && rowContainsSelectedCell);
547
+
548
+ // appeareance
549
+ const innerBackgroundColor =
550
+ backgroundColor || containSelectedCell
551
+ ? "var(--selection-background-color)"
552
+ : isFirstColumn
553
+ ? "#F8F8F8"
554
+ : isFirstRow
555
+ ? "#d3e7ff"
556
+ : "white";
557
+ if (innerBackgroundColor) {
558
+ innerStyle["--background-color"] = innerBackgroundColor;
559
+ }
560
+ if (cursor) {
561
+ innerStyle.cursor = cursor;
562
+ }
563
+ const columnWidth = column.width;
564
+ if (columnWidth !== undefined) {
565
+ innerStyle.minWidth = `${columnWidth}px`;
566
+ innerStyle.width = `${columnWidth}px`;
567
+ innerStyle.maxWidth = `${columnWidth}px`;
568
+ }
569
+ const rowHeight = row.height;
570
+ if (rowHeight !== undefined) {
571
+ innerStyle.maxHeight = `${rowHeight}px`;
572
+ }
573
+ const innerAlignX = alignX || isFirstRow ? "center" : "start";
574
+ const innerAlignY = alignY || isFirstColumn ? "center" : "start";
575
+ const innerBold = bold || containSelectedCell;
576
+ if (innerBold) {
577
+ innerStyle.fontWeight = "bold";
578
+ }
579
+
580
+ const activeElement = useActiveElement();
581
+ const TagName = isInTableHead ? "th" : "td";
582
+
583
+ return (
584
+ <TagName
585
+ className={["navi_table_cell", ...className.split(" ")].join(" ")}
586
+ ref={cellRef}
587
+ style={innerStyle}
588
+ data-align-x={innerAlignX}
589
+ data-align-y={innerAlignY}
590
+ // we use [data-focus] so that the attribute can be copied
591
+ // to the dragged cell copies
592
+ data-focus={activeElement === cellRef.current ? "" : undefined}
593
+ data-first-row={isFirstRow ? "" : undefined}
594
+ data-first-column={isFirstColumn ? "" : undefined}
595
+ data-sticky-left={stickyLeft ? "" : undefined}
596
+ data-sticky-top={stickyTop ? "" : undefined}
597
+ data-sticky-left-frontier={
598
+ stickyLeft && isStickyLeftFrontier ? "" : undefined
599
+ }
600
+ data-sticky-top-frontier={
601
+ stickyTop && isStickyTopFrontier ? "" : undefined
602
+ }
603
+ data-after-sticky-left-frontier={
604
+ isAfterStickyLeftFrontier ? "" : undefined
605
+ }
606
+ data-after-sticky-top-frontier={isAfterStickyTopFrontier ? "" : undefined}
607
+ tabIndex={-1}
608
+ data-height-xxs={
609
+ rowHeight !== undefined && rowHeight < 42 ? "" : undefined
610
+ }
611
+ data-width-xxs={
612
+ columnWidth !== undefined && columnWidth < 42 ? "" : undefined
613
+ }
614
+ data-selection-name={isInTableHead ? "column" : "cell"}
615
+ data-selection-keyboard-toggle
616
+ aria-selected={selected}
617
+ data-value={selectionValue}
618
+ data-editing={editing ? "" : undefined}
619
+ data-grabbed={columnGrabbed ? "" : undefined}
620
+ onClick={onClick}
621
+ onPointerDown={(e) => {
622
+ if (!innerCanDragColumn) {
623
+ return;
624
+ }
625
+ if (e.button !== 0) {
626
+ return;
627
+ }
628
+ initDragTableColumnViaPointer(e, {
629
+ tableDragCloneContainer: tableDragCloneContainerRef.current,
630
+ dropPreview: tableColumnDropPreviewRef.current,
631
+ onGrab: () => grabColumn(columnIndex),
632
+ onDrag: () => {},
633
+ onRelease: (_, newColumnIndex) =>
634
+ releaseColumn(columnIndex, newColumnIndex),
635
+ });
636
+ }}
637
+ onDoubleClick={(e) => {
638
+ if (!editable) {
639
+ return;
640
+ }
641
+ startEditing(e);
642
+ }}
643
+ oneditrequested={(e) => {
644
+ if (!editable) {
645
+ return;
646
+ }
647
+ startEditing(e);
648
+ }}
649
+ >
650
+ {editable ? (
651
+ <Editable
652
+ editing={editing}
653
+ onEditEnd={stopEditing}
654
+ value={children}
655
+ action={action}
656
+ name={name}
657
+ valueSignal={valueSignal}
658
+ height="100%"
659
+ width="100%"
660
+ >
661
+ {children}
662
+ </Editable>
663
+ ) : (
664
+ children
665
+ )}
666
+ {innerCanResizeWidth && (
667
+ <TableCellColumnResizeHandles
668
+ columnIndex={columnIndex}
669
+ columnMinWidth={column.minWidth}
670
+ columnMaxWidth={column.maxWidth}
671
+ />
672
+ )}
673
+ {innerCanResizeHeight && (
674
+ <TableCellRowResizeHandles
675
+ rowIndex={rowIndex}
676
+ rowMinHeight={row.minHeight}
677
+ rowMaxHeight={row.maxHeight}
678
+ />
679
+ )}
680
+ {isInTableHead && (
681
+ <span className="navi_table_cell_content_bold_clone" aria-hidden="true">
682
+ {children}
683
+ </span>
684
+ )}
685
+ <div
686
+ className="navi_table_cell_foreground"
687
+ data-visible={columnGrabbed ? "" : undefined}
688
+ ></div>
689
+ </TagName>
690
+ );
691
+ });
692
+
693
+ export const RowNumberCol = ({
694
+ width = 50,
695
+ minWidth = 30,
696
+ maxWidth = 100,
697
+ immovable = true,
698
+ ...rest
699
+ }) => {
700
+ return (
701
+ <Col
702
+ id="row_number"
703
+ width={width}
704
+ minWidth={minWidth}
705
+ maxWidth={maxWidth}
706
+ immovable={immovable}
707
+ {...rest}
708
+ />
709
+ );
710
+ };
711
+ export const RowNumberTableCell = (props) => {
712
+ const columnIndex = useContext(ColumnIndexContext);
713
+ const rowIndex = useContext(RowIndexContext);
714
+ const isTopLeftCell = columnIndex === 0 && rowIndex === 0;
715
+
716
+ return (
717
+ <TableCell canSelectAll={isTopLeftCell} alignX="left" {...props}>
718
+ {isTopLeftCell ? "" : rowIndex}
719
+ </TableCell>
720
+ );
721
+ };