@jsenv/navi 0.10.2 → 0.11.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 (207) hide show
  1. package/dist/jsenv_navi.js +13838 -23291
  2. package/dist/jsenv_navi.js.map +1281 -0
  3. package/package.json +6 -8
  4. package/index.js +0 -122
  5. package/src/action_private_properties.js +0 -11
  6. package/src/action_proxy_test.html +0 -353
  7. package/src/action_run_states.js +0 -5
  8. package/src/actions.js +0 -1401
  9. package/src/browser_integration/browser_integration.js +0 -216
  10. package/src/browser_integration/document_back_and_forward.js +0 -17
  11. package/src/browser_integration/document_loading_signal.js +0 -100
  12. package/src/browser_integration/document_state_signal.js +0 -9
  13. package/src/browser_integration/document_url_signal.js +0 -9
  14. package/src/browser_integration/use_is_visited.js +0 -19
  15. package/src/browser_integration/via_history.js +0 -232
  16. package/src/browser_integration/via_navigation.js +0 -168
  17. package/src/components/action_execution/form_context.js +0 -5
  18. package/src/components/action_execution/render_actionable_component.jsx +0 -29
  19. package/src/components/action_execution/use_action.js +0 -99
  20. package/src/components/action_execution/use_execute_action.js +0 -193
  21. package/src/components/action_execution/use_run_on_mount.js +0 -9
  22. package/src/components/action_renderer.jsx +0 -125
  23. package/src/components/callout/callout.js +0 -990
  24. package/src/components/callout/callout_demo.html +0 -201
  25. package/src/components/callout/test_dynamic_positioning.html +0 -161
  26. package/src/components/callout/test_html_document_iframe.html +0 -182
  27. package/src/components/demos/0_button_demo.html +0 -707
  28. package/src/components/demos/10_column_reordering_debug.html +0 -277
  29. package/src/components/demos/11_table_selection_debug.html +0 -432
  30. package/src/components/demos/1_checkbox_demo.html +0 -754
  31. package/src/components/demos/2_input_textual_demo.html +0 -286
  32. package/src/components/demos/3_radio_demo.html +0 -874
  33. package/src/components/demos/4_select_demo.html +0 -100
  34. package/src/components/demos/5_list_scrollable_demo.html +0 -153
  35. package/src/components/demos/6_tablist_demo.html +0 -77
  36. package/src/components/demos/7_table_selection_demo.html +0 -176
  37. package/src/components/demos/8_table_fixed_headers_demo.html +0 -584
  38. package/src/components/demos/9_table_column_drag_demo.html +0 -325
  39. package/src/components/demos/action/0_button_demo.html +0 -204
  40. package/src/components/demos/action/10_shortcuts_demo.html +0 -189
  41. package/src/components/demos/action/11_nested_shortcuts_demo.xhtml +0 -401
  42. package/src/components/demos/action/1_input_text_demo.html +0 -876
  43. package/src/components/demos/action/2_form_multiple.html +0 -303
  44. package/src/components/demos/action/3_details_demo.html +0 -203
  45. package/src/components/demos/action/4_input_checkbox_demo.html +0 -731
  46. package/src/components/demos/action/5_input_checkbox_state_demo.html +0 -270
  47. package/src/components/demos/action/6_checkbox_list_demo.html +0 -341
  48. package/src/components/demos/action/7_radio_list_demo.html +0 -357
  49. package/src/components/demos/action/8_editable_demo.html +0 -431
  50. package/src/components/demos/action/9_link_demo.html +0 -194
  51. package/src/components/demos/demo.md +0 -0
  52. package/src/components/demos/route/basic/basic.html +0 -14
  53. package/src/components/demos/route/basic/basic_route_demo.jsx +0 -224
  54. package/src/components/demos/route/multi/multi.html +0 -14
  55. package/src/components/demos/route/multi/multi_route_demo.jsx +0 -277
  56. package/src/components/demos/ui_transition/0_action_renderer_ui_transition_demo.html +0 -695
  57. package/src/components/demos/ui_transition/1_nested_ui_transition_demo.html +0 -429
  58. package/src/components/demos/ui_transition/2_height_transition_test.html +0 -295
  59. package/src/components/details/details.jsx +0 -245
  60. package/src/components/details/summary_marker.jsx +0 -141
  61. package/src/components/edition/editable.jsx +0 -186
  62. package/src/components/error_boundary_context.js +0 -9
  63. package/src/components/field/README.md +0 -247
  64. package/src/components/field/button.jsx +0 -429
  65. package/src/components/field/checkbox_list.jsx +0 -185
  66. package/src/components/field/collect_form_element_values.js +0 -82
  67. package/src/components/field/custom_field.js +0 -106
  68. package/src/components/field/form.jsx +0 -209
  69. package/src/components/field/input.jsx +0 -16
  70. package/src/components/field/input_checkbox.jsx +0 -434
  71. package/src/components/field/input_radio.jsx +0 -432
  72. package/src/components/field/input_textual.jsx +0 -389
  73. package/src/components/field/label.jsx +0 -46
  74. package/src/components/field/radio_list.jsx +0 -183
  75. package/src/components/field/select.jsx +0 -256
  76. package/src/components/field/use_action_events.js +0 -132
  77. package/src/components/field/use_form_events.js +0 -59
  78. package/src/components/field/use_ui_state_controller.js +0 -506
  79. package/src/components/item_tracker/README.md +0 -461
  80. package/src/components/item_tracker/use_isolated_item_tracker.jsx +0 -209
  81. package/src/components/item_tracker/use_isolated_item_tracker_demo.html +0 -148
  82. package/src/components/item_tracker/use_isolated_item_tracker_demo.jsx +0 -460
  83. package/src/components/item_tracker/use_item_tracker.jsx +0 -143
  84. package/src/components/item_tracker/use_item_tracker_demo.html +0 -207
  85. package/src/components/item_tracker/use_item_tracker_demo.jsx +0 -216
  86. package/src/components/keyboard_shortcuts/active_keyboard_shortcuts.jsx +0 -87
  87. package/src/components/keyboard_shortcuts/aria_key_shortcuts.js +0 -61
  88. package/src/components/keyboard_shortcuts/keyboard_key_meta.js +0 -17
  89. package/src/components/keyboard_shortcuts/keyboard_shortcuts.js +0 -371
  90. package/src/components/keyboard_shortcuts/os.js +0 -9
  91. package/src/components/layout/demos/demo_flex.html +0 -638
  92. package/src/components/layout/demos/demo_layout_style_buttons.html +0 -351
  93. package/src/components/layout/demos/demo_layout_style_input.html +0 -226
  94. package/src/components/layout/demos/demo_layout_style_text.html +0 -514
  95. package/src/components/layout/flex.jsx +0 -109
  96. package/src/components/layout/layout_context.jsx +0 -3
  97. package/src/components/layout/spacing.jsx +0 -20
  98. package/src/components/layout/use_layout_style.js +0 -249
  99. package/src/components/link/link.jsx +0 -267
  100. package/src/components/link/link_with_icon.jsx +0 -52
  101. package/src/components/loader/loader_background.jsx +0 -372
  102. package/src/components/loader/loading_spinner.jsx +0 -68
  103. package/src/components/loader/network_speed.js +0 -83
  104. package/src/components/loader/rectangle_loading.jsx +0 -244
  105. package/src/components/props_composition/demos/demo_with_props_style.html +0 -81
  106. package/src/components/props_composition/with_props_class_name.js +0 -37
  107. package/src/components/props_composition/with_props_style.js +0 -26
  108. package/src/components/route.jsx +0 -19
  109. package/src/components/selection/selection.jsx +0 -1583
  110. package/src/components/svg/font_sized_svg.jsx +0 -59
  111. package/src/components/svg/icon_and_text.jsx +0 -21
  112. package/src/components/svg/svg_mask_overlay.jsx +0 -105
  113. package/src/components/table/drag/table_drag.jsx +0 -506
  114. package/src/components/table/resize/table_resize.jsx +0 -650
  115. package/src/components/table/resize/table_size.js +0 -43
  116. package/src/components/table/selection/table_selection.js +0 -106
  117. package/src/components/table/selection/table_selection.jsx +0 -203
  118. package/src/components/table/sticky/sticky_group.js +0 -354
  119. package/src/components/table/sticky/table_sticky.js +0 -25
  120. package/src/components/table/sticky/table_sticky.jsx +0 -501
  121. package/src/components/table/table.jsx +0 -721
  122. package/src/components/table/table_css.js +0 -211
  123. package/src/components/table/table_ui.jsx +0 -49
  124. package/src/components/table/use_cells_and_columns.js +0 -90
  125. package/src/components/table/use_object_array_to_cells.js +0 -46
  126. package/src/components/table/z_indexes.js +0 -23
  127. package/src/components/tablist/tablist.jsx +0 -99
  128. package/src/components/text/demos/demo_text_and_icon.html +0 -421
  129. package/src/components/text/overflow.jsx +0 -15
  130. package/src/components/text/text.jsx +0 -83
  131. package/src/components/text/text_and_count.jsx +0 -28
  132. package/src/components/ui_transition.jsx +0 -128
  133. package/src/components/use_auto_focus.js +0 -94
  134. package/src/components/use_batch_during_render.js +0 -33
  135. package/src/components/use_debounce_true.js +0 -31
  136. package/src/components/use_dependencies_diff.js +0 -35
  137. package/src/components/use_focus_group.js +0 -20
  138. package/src/components/use_initial_value.js +0 -78
  139. package/src/components/use_is_visited.js +0 -19
  140. package/src/components/use_ref_array.js +0 -38
  141. package/src/components/use_signal_sync.js +0 -50
  142. package/src/components/use_stable_callback.js +0 -68
  143. package/src/components/use_state_array.js +0 -47
  144. package/src/docs/actions.md +0 -250
  145. package/src/docs/demos/resource/action_status.jsx +0 -42
  146. package/src/docs/demos/resource/demo.md +0 -1
  147. package/src/docs/demos/resource/resource_demo_0.html +0 -84
  148. package/src/docs/demos/resource/resource_demo_10_post_gc.html +0 -364
  149. package/src/docs/demos/resource/resource_demo_11_describe_many.html +0 -362
  150. package/src/docs/demos/resource/resource_demo_2.html +0 -173
  151. package/src/docs/demos/resource/resource_demo_3_filtered_users.html +0 -415
  152. package/src/docs/demos/resource/resource_demo_4_details.html +0 -284
  153. package/src/docs/demos/resource/resource_demo_5_renderer_lazy.html +0 -115
  154. package/src/docs/demos/resource/resource_demo_6_gc.html +0 -217
  155. package/src/docs/demos/resource/resource_demo_7_child_gc.html +0 -240
  156. package/src/docs/demos/resource/resource_demo_8_proxy_gc.html +0 -319
  157. package/src/docs/demos/resource/resource_demo_9_describe_one.html +0 -472
  158. package/src/docs/demos/resource/tata.jsx +0 -3
  159. package/src/docs/demos/resource/toto.jsx +0 -3
  160. package/src/docs/demos/user_nav/user_nav.html +0 -12
  161. package/src/docs/demos/user_nav/user_nav.jsx +0 -330
  162. package/src/docs/resource_dependencies.md +0 -103
  163. package/src/docs/resource_with_params.md +0 -80
  164. package/src/navi_css_vars.js +0 -14
  165. package/src/notes.md +0 -34
  166. package/src/route/route.js +0 -596
  167. package/src/route/route.xtest.html +0 -228
  168. package/src/store/array_signal_store.js +0 -537
  169. package/src/store/local_storage_signal.js +0 -17
  170. package/src/store/resource_graph.js +0 -1304
  171. package/src/store/tests/resource_graph_autoreload_demo.html +0 -12
  172. package/src/store/tests/resource_graph_autoreload_demo.jsx +0 -964
  173. package/src/store/tests/resource_graph_dependencies.test_manual.js +0 -95
  174. package/src/store/value_in_local_storage.js +0 -187
  175. package/src/symbol_object_signal.js +0 -1
  176. package/src/use_action_data.js +0 -10
  177. package/src/use_action_status.js +0 -47
  178. package/src/utils/add_many_event_listeners.js +0 -15
  179. package/src/utils/array_add_remove.js +0 -61
  180. package/src/utils/array_signal.js +0 -15
  181. package/src/utils/compare_two_js_values.js +0 -172
  182. package/src/utils/execute_with_cleanup.js +0 -21
  183. package/src/utils/get_caller_info.js +0 -85
  184. package/src/utils/is_signal.js +0 -20
  185. package/src/utils/js_value_weak_map.js +0 -162
  186. package/src/utils/js_value_weak_map_demo.html +0 -690
  187. package/src/utils/merge_two_js_values.js +0 -53
  188. package/src/utils/stringify_for_display.js +0 -131
  189. package/src/utils/weak_effect.js +0 -48
  190. package/src/validation/constraints/confirm_constraint.js +0 -14
  191. package/src/validation/constraints/create_unique_value_constraint.js +0 -27
  192. package/src/validation/constraints/native_constraints.js +0 -338
  193. package/src/validation/constraints/readonly_constraint.js +0 -41
  194. package/src/validation/constraints/same_as_constraint.js +0 -42
  195. package/src/validation/constraints/single_space_constraint.js +0 -13
  196. package/src/validation/custom_constraint_validation.js +0 -793
  197. package/src/validation/custom_message.js +0 -18
  198. package/src/validation/demos/browser_style.png +0 -0
  199. package/src/validation/demos/demo_same_as_constraint.html +0 -259
  200. package/src/validation/demos/form_validation_demo.html +0 -142
  201. package/src/validation/demos/form_validation_demo_preact.html +0 -87
  202. package/src/validation/demos/form_validation_native_popover_demo.html +0 -168
  203. package/src/validation/demos/form_validation_vs_native_demo.html +0 -172
  204. package/src/validation/hooks/use_constraints.js +0 -23
  205. package/src/validation/hooks/use_custom_validation_ref.js +0 -73
  206. package/src/validation/hooks/use_validation_message.js +0 -19
  207. package/src/validation/input_change_effect.js +0 -106
@@ -1,506 +0,0 @@
1
- import { createPubSub } from "@jsenv/dom";
2
- import { createContext } from "preact";
3
- import {
4
- useContext,
5
- useLayoutEffect,
6
- useMemo,
7
- useRef,
8
- useState,
9
- } from "preact/hooks";
10
-
11
- import { useNavState } from "../../browser_integration/browser_integration.js";
12
- import { FormContext } from "../action_execution/form_context.js";
13
- import { useInitialValue } from "../use_initial_value.js";
14
-
15
- const DEBUG_UI_STATE_CONTROLLER = false;
16
- const DEBUG_UI_GROUP_STATE_CONTROLLER = false;
17
- const debugUIState = (...args) => {
18
- if (DEBUG_UI_STATE_CONTROLLER) {
19
- console.debug(...args);
20
- }
21
- };
22
- const debugUIGroup = (...args) => {
23
- if (DEBUG_UI_GROUP_STATE_CONTROLLER) {
24
- console.debug(...args);
25
- }
26
- };
27
-
28
- export const UIStateControllerContext = createContext();
29
- export const UIStateContext = createContext();
30
- export const ParentUIStateControllerContext = createContext();
31
-
32
- export const FieldNameContext = createContext();
33
- export const ReadOnlyContext = createContext();
34
- export const DisabledContext = createContext();
35
- export const RequiredContext = createContext();
36
- export const LoadingContext = createContext();
37
- export const LoadingElementContext = createContext();
38
-
39
- /**
40
- * UI State Controller Hook
41
- *
42
- * Manages the relationship between external state (props) and UI state (what user sees).
43
- * Allows UI state to diverge temporarily for responsive interactions, with mechanisms
44
- * to sync back to external state when needed.
45
- *
46
- * Key features:
47
- * - Immediate UI updates for responsive interactions
48
- * - State divergence with sync capabilities (resetUIState)
49
- * - Group integration for coordinated form inputs
50
- * - External control via custom events (onsetuistate/onresetuistate)
51
- * - Error recovery and form reset support
52
- *
53
- * See README.md for detailed usage examples and patterns.
54
- */
55
- export const useUIStateController = (
56
- props,
57
- componentType,
58
- {
59
- statePropName = "value",
60
- defaultStatePropName = "defaultValue",
61
- fallbackState = "",
62
- getStateFromProp = (prop) => prop,
63
- getPropFromState = (state) => state,
64
- } = {},
65
- ) => {
66
- const parentUIStateController = useContext(ParentUIStateControllerContext);
67
- const formContext = useContext(FormContext);
68
- const { id, name, onUIStateChange, action } = props;
69
- const uncontrolled = !formContext && !action;
70
- const [navState, setNavState] = useNavState(id);
71
-
72
- const uiStateControllerRef = useRef();
73
- const hasStateProp = Object.hasOwn(props, statePropName);
74
- const state = props[statePropName];
75
- const defaultState = props[defaultStatePropName];
76
- const stateInitial = useInitialValue(() => {
77
- if (hasStateProp) {
78
- // controlled by state prop ("value" or "checked")
79
- return getStateFromProp(state);
80
- }
81
- if (defaultState) {
82
- // not controlled but want an initial state (a value or being checked)
83
- return getStateFromProp(defaultState);
84
- }
85
- if (formContext && navState) {
86
- // not controlled but want to use value from nav state
87
- // (I think this should likely move earlier to win over the hasUIStateProp when it's undefined)
88
- return getStateFromProp(navState);
89
- }
90
- return getStateFromProp(fallbackState);
91
- });
92
-
93
- /**
94
- * This check is needed only for basic field because
95
- * When using action/form we consider the action/form code
96
- * will have a side effect that will re-render the component with the up-to-date state
97
- *
98
- * In practice we set the checked from the backend state
99
- * We use action to fetch the new state and update the local state
100
- * The component re-renders so it's the action/form that is considered as responsible
101
- * to update the state and as a result allowed to have "checked"/"value" prop without "onUIStateChange"
102
- */
103
- const readOnly =
104
- uncontrolled &&
105
- hasStateProp &&
106
- !onUIStateChange &&
107
- !parentUIStateController;
108
- if (readOnly && import.meta.dev) {
109
- console.warn(
110
- `"${componentType}" is controlled by "${statePropName}" prop. Replace it by "${defaultStatePropName}" or combine it with "onUIStateChange" to make field interactive.`,
111
- );
112
- }
113
-
114
- const [
115
- notifyParentAboutChildMount,
116
- notifyParentAboutChildUIStateChange,
117
- notifyParentAboutChildUnmount,
118
- ] = useParentControllerNotifiers(
119
- parentUIStateController,
120
- uiStateControllerRef,
121
- componentType,
122
- );
123
- useLayoutEffect(() => {
124
- notifyParentAboutChildMount();
125
- return notifyParentAboutChildUnmount;
126
- }, []);
127
-
128
- const existingUIStateController = uiStateControllerRef.current;
129
- if (existingUIStateController) {
130
- existingUIStateController._checkForUpdates({
131
- readOnly,
132
- name,
133
- onUIStateChange,
134
- getPropFromState,
135
- getStateFromProp,
136
- hasStateProp,
137
- stateInitial,
138
- state,
139
- });
140
- return existingUIStateController;
141
- }
142
- debugUIState(
143
- `Creating "${componentType}" ui state controller - initial state:`,
144
- JSON.stringify(stateInitial),
145
- );
146
- const [publishUIState, subscribeUIState] = createPubSub();
147
- const uiStateController = {
148
- _checkForUpdates: ({
149
- readOnly,
150
- name,
151
- onUIStateChange,
152
- getPropFromState,
153
- getStateFromProp,
154
- hasStateProp,
155
- stateInitial,
156
- state,
157
- }) => {
158
- uiStateController.readOnly = readOnly;
159
- uiStateController.name = name;
160
- uiStateController.onUIStateChange = onUIStateChange;
161
- uiStateController.getPropFromState = getPropFromState;
162
- uiStateController.getStateFromProp = getStateFromProp;
163
- uiStateController.stateInitial = stateInitial;
164
-
165
- if (hasStateProp) {
166
- uiStateController.hasStateProp = true;
167
- const currentState = uiStateController.state;
168
- if (state !== currentState) {
169
- uiStateController.state = state;
170
- uiStateController.setUIState(
171
- uiStateController.getPropFromState(state),
172
- new CustomEvent("state_prop"),
173
- );
174
- }
175
- } else if (uiStateController.hasStateProp) {
176
- uiStateController.hasStateProp = false;
177
- uiStateController.state = uiStateController.stateInitial;
178
- }
179
- },
180
-
181
- componentType,
182
- readOnly,
183
- name,
184
- hasStateProp,
185
- state: stateInitial,
186
- uiState: stateInitial,
187
- onUIStateChange,
188
- getPropFromState,
189
- getStateFromProp,
190
- setUIState: (prop, e) => {
191
- const newUIState = uiStateController.getStateFromProp(prop);
192
- if (formContext) {
193
- setNavState(prop);
194
- }
195
- const currentUIState = uiStateController.uiState;
196
- if (newUIState === currentUIState) {
197
- return;
198
- }
199
- debugUIState(
200
- `${componentType}.setUIState(${JSON.stringify(newUIState)}, "${e.type}") -> updating to ${JSON.stringify(newUIState)}`,
201
- );
202
- uiStateController.uiState = newUIState;
203
- publishUIState(newUIState);
204
- uiStateController.onUIStateChange?.(newUIState, e);
205
- notifyParentAboutChildUIStateChange(e);
206
- },
207
- resetUIState: (e) => {
208
- const currentState = uiStateController.state;
209
- uiStateController.setUIState(currentState, e);
210
- },
211
- actionEnd: () => {
212
- debugUIState(`"${componentType}" actionEnd called`);
213
- if (formContext) {
214
- setNavState(undefined);
215
- }
216
- },
217
- subscribe: subscribeUIState,
218
- };
219
- uiStateControllerRef.current = uiStateController;
220
- return uiStateController;
221
- };
222
-
223
- const NO_PARENT = [() => {}, () => {}, () => {}];
224
- const useParentControllerNotifiers = (
225
- parentUIStateController,
226
- uiStateControllerRef,
227
- componentType,
228
- ) => {
229
- return useMemo(() => {
230
- if (!parentUIStateController) {
231
- return NO_PARENT;
232
- }
233
-
234
- const parentComponentType = parentUIStateController.componentType;
235
- const notifyParentAboutChildMount = () => {
236
- const uiStateController = uiStateControllerRef.current;
237
- debugUIState(
238
- `"${componentType}" registering into "${parentComponentType}"`,
239
- );
240
- parentUIStateController.registerChild(uiStateController);
241
- };
242
-
243
- const notifyParentAboutChildUIStateChange = (e) => {
244
- const uiStateController = uiStateControllerRef.current;
245
- debugUIState(
246
- `"${componentType}" notifying "${parentComponentType}" of ui state change`,
247
- );
248
- parentUIStateController.onChildUIStateChange(uiStateController, e);
249
- };
250
-
251
- const notifyParentAboutChildUnmount = () => {
252
- const uiStateController = uiStateControllerRef.current;
253
- debugUIState(
254
- `"${componentType}" unregistering from "${parentComponentType}"`,
255
- );
256
- parentUIStateController.unregisterChild(uiStateController);
257
- };
258
-
259
- return [
260
- notifyParentAboutChildMount,
261
- notifyParentAboutChildUIStateChange,
262
- notifyParentAboutChildUnmount,
263
- ];
264
- }, []);
265
- };
266
-
267
- /**
268
- * UI Group State Controller Hook
269
- *
270
- * This hook manages a collection of child UI state controllers and aggregates their states
271
- * into a unified group state. It provides a way to coordinate multiple form inputs that
272
- * work together as a logical unit.
273
- *
274
- * What it provides:
275
- *
276
- * 1. **Child State Aggregation**:
277
- * - Collects state from multiple child UI controllers
278
- * - Combines them into a single meaningful group state
279
- * - Updates group state automatically when any child changes
280
- *
281
- * 2. **Child Filtering**:
282
- * - Can filter which child controllers to include based on component type
283
- * - Useful for mixed content where only specific inputs matter
284
- * - Enables type-safe aggregation patterns
285
- *
286
- * 3. **Group Operations**:
287
- * - Provides `resetUIState()` that cascades to all children
288
- * - Enables group-level operations like "clear all" or "reset form section"
289
- * - Maintains consistency across related inputs
290
- *
291
- * 4. **External State Management**:
292
- * - Notifies external code of group state changes via `onUIStateChange`
293
- * - Allows external systems to react to group-level state changes
294
- * - Supports complex form validation and submission logic
295
- *
296
- * Why use it:
297
- * - When you have multiple related inputs that should be treated as one logical unit
298
- * - For implementing checkbox lists, radio groups, or form sections
299
- * - When you need to perform operations on multiple inputs simultaneously
300
- * - To aggregate input states for validation or submission
301
- *
302
- * How it works:
303
- * - Child controllers automatically register themselves when mounted
304
- * - Group controller listens for child state changes and re-aggregates
305
- * - Custom aggregation function determines how child states combine
306
- * - Group state updates trigger notifications to external code
307
- *
308
- * @param {Object} props - Component props containing onUIStateChange callback
309
- * @param {string} componentType - Type identifier for this group controller
310
- * @param {Object} config - Configuration object
311
- * @param {string} [config.childComponentType] - Filter children by this type (e.g., "checkbox")
312
- * @param {Function} config.aggregateChildStates - Function to aggregate child states
313
- * @param {any} [config.emptyState] - State to use when no children have values
314
- * @returns {Object} UI group state controller
315
- *
316
- * Usage Examples:
317
- * - **Checkbox List**: Aggregates multiple checkboxes into array of checked values
318
- * - **Radio Group**: Manages radio buttons to ensure single selection
319
- * - **Form Section**: Groups related inputs for validation and reset operations
320
- * - **Dynamic Lists**: Handles variable number of repeated input groups
321
- */
322
-
323
- export const useUIGroupStateController = (
324
- props,
325
- componentType,
326
- { childComponentType, aggregateChildStates, emptyState = undefined },
327
- ) => {
328
- if (typeof aggregateChildStates !== "function") {
329
- throw new TypeError("aggregateChildStates must be a function");
330
- }
331
- const parentUIStateController = useContext(ParentUIStateControllerContext);
332
- const { onUIStateChange, name } = props;
333
- const childUIStateControllerArrayRef = useRef([]);
334
- const childUIStateControllerArray = childUIStateControllerArrayRef.current;
335
- const uiStateControllerRef = useRef();
336
-
337
- const groupIsRenderingRef = useRef(false);
338
- const pendingChangeRef = useRef(false);
339
- groupIsRenderingRef.current = true;
340
- pendingChangeRef.current = false;
341
-
342
- const [
343
- notifyParentAboutChildMount,
344
- notifyParentAboutChildUIStateChange,
345
- notifyParentAboutChildUnmount,
346
- ] = useParentControllerNotifiers(
347
- parentUIStateController,
348
- uiStateControllerRef,
349
- componentType,
350
- );
351
- useLayoutEffect(() => {
352
- notifyParentAboutChildMount();
353
- return notifyParentAboutChildUnmount;
354
- }, []);
355
-
356
- const onChange = (_, e) => {
357
- if (groupIsRenderingRef.current) {
358
- pendingChangeRef.current = true;
359
- return;
360
- }
361
- const newUIState = aggregateChildStates(
362
- childUIStateControllerArray,
363
- emptyState,
364
- );
365
- const uiStateController = uiStateControllerRef.current;
366
- uiStateController.setUIState(newUIState, e);
367
- };
368
-
369
- useLayoutEffect(() => {
370
- groupIsRenderingRef.current = false;
371
- if (pendingChangeRef.current) {
372
- pendingChangeRef.current = false;
373
- onChange(
374
- null,
375
- new CustomEvent(`${componentType}_batched_ui_state_update`),
376
- );
377
- }
378
- });
379
-
380
- const existingUIStateController = uiStateControllerRef.current;
381
- if (existingUIStateController) {
382
- existingUIStateController.name = name;
383
- existingUIStateController.onUIStateChange = onUIStateChange;
384
- return existingUIStateController;
385
- }
386
- debugUIGroup(
387
- childComponentType === "*"
388
- ? `Creating "${componentType}" ui state controller (monitoring all descendants ui state(s))"`
389
- : `Creating "${componentType}" ui state controller (monitoring "${childComponentType}" ui state(s))`,
390
- );
391
-
392
- const [publishUIState, subscribeUIState] = createPubSub();
393
- const isMonitoringChild = (childUIStateController) => {
394
- if (childComponentType === "*") {
395
- return true;
396
- }
397
- return childUIStateController.componentType === childComponentType;
398
- };
399
- const uiStateController = {
400
- componentType,
401
- name,
402
- onUIStateChange,
403
- uiState: emptyState,
404
- setUIState: (newUIState, e) => {
405
- const currentUIState = uiStateController.uiState;
406
- if (newUIState === currentUIState) {
407
- return;
408
- }
409
- uiStateController.uiState = newUIState;
410
- debugUIGroup(
411
- `${componentType}.setUIState(${JSON.stringify(newUIState)}, "${e.type}") -> updates from ${JSON.stringify(currentUIState)} to ${JSON.stringify(newUIState)}`,
412
- );
413
- publishUIState(newUIState);
414
- uiStateController.onUIStateChange?.(newUIState, e);
415
- notifyParentAboutChildUIStateChange(e);
416
- },
417
- registerChild: (childUIStateController) => {
418
- if (!isMonitoringChild(childUIStateController)) {
419
- return;
420
- }
421
- const childComponentType = childUIStateController.componentType;
422
- childUIStateControllerArray.push(childUIStateController);
423
- debugUIGroup(
424
- `${componentType}.registerChild("${childComponentType}") -> registered (total: ${childUIStateControllerArray.length})`,
425
- );
426
- onChange(
427
- childUIStateController,
428
- new CustomEvent(`${childComponentType}_mount`),
429
- );
430
- },
431
- onChildUIStateChange: (childUIStateController, e) => {
432
- if (!isMonitoringChild(childUIStateController)) {
433
- return;
434
- }
435
- debugUIGroup(
436
- `${componentType}.onChildUIStateChange("${childComponentType}") to ${JSON.stringify(
437
- childUIStateController.uiState,
438
- )}`,
439
- );
440
- onChange(childUIStateController, e);
441
- },
442
- unregisterChild: (childUIStateController) => {
443
- if (!isMonitoringChild(childUIStateController)) {
444
- return;
445
- }
446
- const childComponentType = childUIStateController.componentType;
447
- const index = childUIStateControllerArray.indexOf(childUIStateController);
448
- if (index === -1) {
449
- debugUIGroup(
450
- `${componentType}.unregisterChild("${childComponentType}") -> not found`,
451
- );
452
- return;
453
- }
454
- childUIStateControllerArray.splice(index, 1);
455
- debugUIGroup(
456
- `${componentType}.unregisterChild("${childComponentType}") -> unregisteed (remaining: ${childUIStateControllerArray.length})`,
457
- );
458
- onChange(
459
- childUIStateController,
460
- new CustomEvent(`${childComponentType}_unmount`),
461
- );
462
- },
463
- resetUIState: (e) => {
464
- // we should likely batch the changes that will be reported for performances
465
- for (const childUIStateController of childUIStateControllerArray) {
466
- childUIStateController.resetUIState(e);
467
- }
468
- },
469
- actionEnd: (e) => {
470
- for (const childUIStateController of childUIStateControllerArray) {
471
- childUIStateController.actionEnd(e);
472
- }
473
- },
474
- subscribe: subscribeUIState,
475
- };
476
- uiStateControllerRef.current = uiStateController;
477
- return uiStateController;
478
- };
479
-
480
- /**
481
- * Hook to track UI state from a UI state controller
482
- *
483
- * This hook allows external code to react to UI state changes without
484
- * causing the controller itself to re-create. It returns the current UI state
485
- * and will cause re-renders when the UI state changes.
486
- *
487
- * @param {Object} uiStateController - The UI state controller to track
488
- * @returns {any} The current UI state
489
- */
490
- export const useUIState = (uiStateController) => {
491
- const [trackedUIState, setTrackedUIState] = useState(
492
- uiStateController.uiState,
493
- );
494
-
495
- useLayoutEffect(() => {
496
- // Subscribe to UI state changes
497
- const unsubscribe = uiStateController.subscribe(setTrackedUIState);
498
-
499
- // Sync with current state in case it changed before subscription
500
- setTrackedUIState(uiStateController.uiState);
501
-
502
- return unsubscribe;
503
- }, [uiStateController]);
504
-
505
- return trackedUIState;
506
- };