@infinite-table/infinite-react 0.5.1 → 0.5.2

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 (282) hide show
  1. package/README.md +104 -0
  2. package/index.css +896 -0
  3. package/index.d.ts +2264 -0
  4. package/index.esm.css +896 -0
  5. package/index.esm.js +15404 -0
  6. package/index.js +15240 -0
  7. package/package.json +2 -94
  8. package/.vscode/settings.json +0 -3
  9. package/API.md +0 -0
  10. package/build/buildPackageJson.js +0 -43
  11. package/build/scripts/test.js +0 -37
  12. package/bundler.config.js +0 -25
  13. package/globals.d.ts +0 -7
  14. package/rollup.config.js +0 -15
  15. package/run-build-deepmap.js +0 -47
  16. package/run-build.js +0 -45
  17. package/run-dts-concat.js +0 -21
  18. package/src/components/CSSVariableWatch.tsx +0 -82
  19. package/src/components/DataSource/BooleanCollectionState.ts +0 -214
  20. package/src/components/DataSource/BooleanDeepCollectionState.ts +0 -208
  21. package/src/components/DataSource/DataSourceContext.ts +0 -20
  22. package/src/components/DataSource/GroupRowsState.ts +0 -74
  23. package/src/components/DataSource/Indexer.ts +0 -27
  24. package/src/components/DataSource/RowSelectionState.ts +0 -763
  25. package/src/components/DataSource/defaultFilterTypes.ts +0 -64
  26. package/src/components/DataSource/index.tsx +0 -108
  27. package/src/components/DataSource/privateHooks/getChangeDetect.ts +0 -7
  28. package/src/components/DataSource/privateHooks/useLoadData.ts +0 -783
  29. package/src/components/DataSource/publicHooks/useDataSource.ts +0 -19
  30. package/src/components/DataSource/publicHooks/useDataSourceActions.ts +0 -14
  31. package/src/components/DataSource/state/getInitialState.ts +0 -494
  32. package/src/components/DataSource/state/normalizeSortInfo.ts +0 -35
  33. package/src/components/DataSource/state/reducer.ts +0 -466
  34. package/src/components/DataSource/state/rowInfoStatus.ts +0 -27
  35. package/src/components/DataSource/types.ts +0 -548
  36. package/src/components/HeadlessTable/HeadlessTableWithPinnedContainers.tsx +0 -142
  37. package/src/components/HeadlessTable/MappedCells.ts +0 -251
  38. package/src/components/HeadlessTable/RawTable.tsx +0 -98
  39. package/src/components/HeadlessTable/ReactHeadlessTableRenderer.tsx +0 -1516
  40. package/src/components/HeadlessTable/index.tsx +0 -234
  41. package/src/components/InfiniteTable/InfiniteCls.css.ts +0 -164
  42. package/src/components/InfiniteTable/InfiniteTableContext.ts +0 -16
  43. package/src/components/InfiniteTable/api/getImperativeApi.ts +0 -391
  44. package/src/components/InfiniteTable/api/getSelectionApi.ts +0 -364
  45. package/src/components/InfiniteTable/api/type.ts +0 -11
  46. package/src/components/InfiniteTable/components/ActiveCellIndicator.css.ts +0 -51
  47. package/src/components/InfiniteTable/components/ActiveCellIndicator.tsx +0 -116
  48. package/src/components/InfiniteTable/components/ActiveRowIndicator.css.ts +0 -56
  49. package/src/components/InfiniteTable/components/ActiveRowIndicator.tsx +0 -94
  50. package/src/components/InfiniteTable/components/CheckBox.css.ts +0 -13
  51. package/src/components/InfiniteTable/components/CheckBox.tsx +0 -96
  52. package/src/components/InfiniteTable/components/FilterEditors.tsx +0 -52
  53. package/src/components/InfiniteTable/components/InfiniteTableBody/InfiniteTableBody.tsx +0 -27
  54. package/src/components/InfiniteTable/components/InfiniteTableBody/index.tsx +0 -1
  55. package/src/components/InfiniteTable/components/InfiniteTableFooter/InfiniteTableFooter.tsx +0 -31
  56. package/src/components/InfiniteTable/components/InfiniteTableFooter/index.tsx +0 -1
  57. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableColumnHeaderFilter.tsx +0 -102
  58. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeader.tsx +0 -163
  59. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderCell.tsx +0 -574
  60. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderGroup.tsx +0 -79
  61. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderTypes.tsx +0 -25
  62. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderWrapper.tsx +0 -185
  63. package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/GroupResizeHandle.tsx +0 -159
  64. package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/ResizeHandle.css.ts +0 -124
  65. package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/columnResizer.ts +0 -166
  66. package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/index.tsx +0 -146
  67. package/src/components/InfiniteTable/components/InfiniteTableHeader/buildColumnAndGroupTree.ts +0 -240
  68. package/src/components/InfiniteTable/components/InfiniteTableHeader/header.css.ts +0 -358
  69. package/src/components/InfiniteTable/components/InfiniteTableHeader/index.tsx +0 -1
  70. package/src/components/InfiniteTable/components/InfiniteTableHeader/useColumnGroupResizeHandle.tsx +0 -114
  71. package/src/components/InfiniteTable/components/InfiniteTableHeader/useColumnResizeHandle.tsx +0 -146
  72. package/src/components/InfiniteTable/components/InfiniteTableLicenseFooter/footer.css.ts +0 -7
  73. package/src/components/InfiniteTable/components/InfiniteTableLicenseFooter/index.tsx +0 -110
  74. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableCell.tsx +0 -120
  75. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableCellTypes.ts +0 -74
  76. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableColumnCell.tsx +0 -457
  77. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRow.tsx +0 -107
  78. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRowClassName.ts +0 -6
  79. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRowTypes.ts +0 -32
  80. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRowUnvirtualized.tsx +0 -75
  81. package/src/components/InfiniteTable/components/InfiniteTableRow/columnRendering.tsx +0 -263
  82. package/src/components/InfiniteTable/components/InfiniteTableRow/index.tsx +0 -1
  83. package/src/components/InfiniteTable/components/InfiniteTableRow/row.css.ts +0 -74
  84. package/src/components/InfiniteTable/components/InfiniteTableRow/useRowDOMProps.ts +0 -199
  85. package/src/components/InfiniteTable/components/LoadMask.css.ts +0 -34
  86. package/src/components/InfiniteTable/components/LoadMask.tsx +0 -27
  87. package/src/components/InfiniteTable/components/ScrollbarPlaceholder.tsx +0 -56
  88. package/src/components/InfiniteTable/components/cell.css.ts +0 -192
  89. package/src/components/InfiniteTable/components/icons/ArrowDown.tsx +0 -14
  90. package/src/components/InfiniteTable/components/icons/ArrowUp.tsx +0 -14
  91. package/src/components/InfiniteTable/components/icons/ExpanderIcon.css.ts +0 -31
  92. package/src/components/InfiniteTable/components/icons/ExpanderIcon.tsx +0 -63
  93. package/src/components/InfiniteTable/components/icons/Icon.tsx +0 -18
  94. package/src/components/InfiniteTable/components/icons/InfiniteTableIconClassName.ts +0 -1
  95. package/src/components/InfiniteTable/components/icons/LoadingIcon.css.ts +0 -8
  96. package/src/components/InfiniteTable/components/icons/LoadingIcon.tsx +0 -45
  97. package/src/components/InfiniteTable/components/icons/MenuIcon.tsx +0 -50
  98. package/src/components/InfiniteTable/components/icons/SortIcon.tsx +0 -113
  99. package/src/components/InfiniteTable/eventHandlers/eventHandlerTypes.ts +0 -57
  100. package/src/components/InfiniteTable/eventHandlers/index.ts +0 -95
  101. package/src/components/InfiniteTable/eventHandlers/keyboardNavigation.ts +0 -232
  102. package/src/components/InfiniteTable/eventHandlers/keyboardSelection.ts +0 -110
  103. package/src/components/InfiniteTable/eventHandlers/onCellClick.ts +0 -94
  104. package/src/components/InfiniteTable/eventHandlers/onKeyDown.ts +0 -25
  105. package/src/components/InfiniteTable/hooks/adjustColumnOrderForAllColumns.ts +0 -70
  106. package/src/components/InfiniteTable/hooks/reorderColumnsOnDrag.ts +0 -476
  107. package/src/components/InfiniteTable/hooks/useAutoSizeColumns.ts +0 -206
  108. package/src/components/InfiniteTable/hooks/useCellClassName.ts +0 -80
  109. package/src/components/InfiniteTable/hooks/useCellRendering.tsx +0 -217
  110. package/src/components/InfiniteTable/hooks/useColumnGroups.ts +0 -71
  111. package/src/components/InfiniteTable/hooks/useColumnMenu.ts +0 -72
  112. package/src/components/InfiniteTable/hooks/useColumnPointerEvents.ts +0 -258
  113. package/src/components/InfiniteTable/hooks/useColumnRowspan.ts +0 -43
  114. package/src/components/InfiniteTable/hooks/useColumnSizeFn.ts +0 -24
  115. package/src/components/InfiniteTable/hooks/useColumnsWhen.ts +0 -582
  116. package/src/components/InfiniteTable/hooks/useComputed.ts +0 -186
  117. package/src/components/InfiniteTable/hooks/useComputedVisibleColumns.ts +0 -219
  118. package/src/components/InfiniteTable/hooks/useDOMProps.ts +0 -306
  119. package/src/components/InfiniteTable/hooks/useEffectWithRaf.ts +0 -47
  120. package/src/components/InfiniteTable/hooks/useInfiniteTable.ts +0 -10
  121. package/src/components/InfiniteTable/hooks/useInfiniteTableState.ts +0 -11
  122. package/src/components/InfiniteTable/hooks/useInternalProps.ts +0 -8
  123. package/src/components/InfiniteTable/hooks/useLicense/LicenseDetails.ts +0 -13
  124. package/src/components/InfiniteTable/hooks/useLicense/crc32.ts +0 -56
  125. package/src/components/InfiniteTable/hooks/useLicense/decode.ts +0 -147
  126. package/src/components/InfiniteTable/hooks/useLicense/useLicense.ts +0 -39
  127. package/src/components/InfiniteTable/hooks/useRerenderOnKeyChange.ts +0 -21
  128. package/src/components/InfiniteTable/hooks/useRowHover.ts +0 -18
  129. package/src/components/InfiniteTable/hooks/useScrollToActiveCell.ts +0 -48
  130. package/src/components/InfiniteTable/hooks/useScrollToActiveRow.ts +0 -44
  131. package/src/components/InfiniteTable/hooks/useScrollbars.ts +0 -51
  132. package/src/components/InfiniteTable/hooks/useToggleGroupRow.ts +0 -56
  133. package/src/components/InfiniteTable/hooks/useUnpinnedRendering.tsx +0 -165
  134. package/src/components/InfiniteTable/hooks/useYourBrain.ts +0 -55
  135. package/src/components/InfiniteTable/index.tsx +0 -352
  136. package/src/components/InfiniteTable/internalProps.ts +0 -4
  137. package/src/components/InfiniteTable/state/computeColumnGroupsDepths.ts +0 -29
  138. package/src/components/InfiniteTable/state/getColumnVisibilityForHideEmptyGroupColumns.ts +0 -127
  139. package/src/components/InfiniteTable/state/getInitialState.ts +0 -375
  140. package/src/components/InfiniteTable/theme.css.ts +0 -556
  141. package/src/components/InfiniteTable/types/InfiniteTableAction.ts +0 -6
  142. package/src/components/InfiniteTable/types/InfiniteTableActionType.ts +0 -12
  143. package/src/components/InfiniteTable/types/InfiniteTableColumn.ts +0 -478
  144. package/src/components/InfiniteTable/types/InfiniteTableComputedValues.ts +0 -43
  145. package/src/components/InfiniteTable/types/InfiniteTableContextValue.ts +0 -12
  146. package/src/components/InfiniteTable/types/InfiniteTableInternalProps.ts +0 -3
  147. package/src/components/InfiniteTable/types/InfiniteTableProps.ts +0 -511
  148. package/src/components/InfiniteTable/types/InfiniteTableState.ts +0 -195
  149. package/src/components/InfiniteTable/types/Utility.ts +0 -35
  150. package/src/components/InfiniteTable/types/index.ts +0 -96
  151. package/src/components/InfiniteTable/utilities.css.ts +0 -179
  152. package/src/components/InfiniteTable/utils/MultiRowSelector.ts +0 -111
  153. package/src/components/InfiniteTable/utils/RenderHookComponentForInfinite.tsx +0 -52
  154. package/src/components/InfiniteTable/utils/adjustColumnOrderForPinning.ts +0 -33
  155. package/src/components/InfiniteTable/utils/assignFiltered.ts +0 -29
  156. package/src/components/InfiniteTable/utils/defaultGetColumContextMenuItems.tsx +0 -108
  157. package/src/components/InfiniteTable/utils/getColumnComputedType.ts +0 -33
  158. package/src/components/InfiniteTable/utils/getColumnForGroupBy.tsx +0 -313
  159. package/src/components/InfiniteTable/utils/getComputedVisibleColumns.ts +0 -773
  160. package/src/components/InfiniteTable/utils/getContextMenuForColumn.tsx +0 -72
  161. package/src/components/InfiniteTable/utils/infiniteDOMUtils.ts +0 -189
  162. package/src/components/InfiniteTable/utils/mapToObject.ts +0 -20
  163. package/src/components/InfiniteTable/utils/moveXatY.ts +0 -25
  164. package/src/components/InfiniteTable/utils/objectValuesExcept.ts +0 -9
  165. package/src/components/InfiniteTable/utils/progressiveSpeedScroller.ts +0 -158
  166. package/src/components/InfiniteTable/utils/rafFn.ts +0 -13
  167. package/src/components/InfiniteTable/utils/toCSSVarName.ts +0 -1
  168. package/src/components/InfiniteTable/utils/toMap.ts +0 -24
  169. package/src/components/Menu/Menu.tsx +0 -750
  170. package/src/components/Menu/MenuCls.css.ts +0 -139
  171. package/src/components/Menu/MenuContext.ts +0 -10
  172. package/src/components/Menu/MenuContextValue.ts +0 -15
  173. package/src/components/Menu/MenuProps.ts +0 -98
  174. package/src/components/Menu/MenuState.ts +0 -56
  175. package/src/components/Menu/MenuTriangleContext.ts +0 -352
  176. package/src/components/Menu/childrenToRuntimeItems.tsx +0 -103
  177. package/src/components/Menu/getMenuState.tsx +0 -119
  178. package/src/components/Menu/index.tsx +0 -88
  179. package/src/components/Menu/propToIdentifyMenu.ts +0 -1
  180. package/src/components/RawList/AvoidReactDiff.tsx +0 -48
  181. package/src/components/RawList/MappedItems.ts +0 -143
  182. package/src/components/RawList/ReactVirtualRenderer.tsx +0 -307
  183. package/src/components/RawList/index.tsx +0 -42
  184. package/src/components/RawList/types.ts +0 -23
  185. package/src/components/RawList/useVirtualRenderer.ts +0 -36
  186. package/src/components/RenderHookComponent.tsx +0 -10
  187. package/src/components/ResizeObserver/index.tsx +0 -170
  188. package/src/components/VirtualBrain/MatrixBrain.ts +0 -1621
  189. package/src/components/VirtualBrain/ScrollListener.ts +0 -39
  190. package/src/components/VirtualBrain/index.ts +0 -664
  191. package/src/components/VirtualList/ColumnListWithExternalScrolling.tsx +0 -78
  192. package/src/components/VirtualList/InfiniteListRootClassName.ts +0 -1
  193. package/src/components/VirtualList/RowListWithExternalScrolling.tsx +0 -120
  194. package/src/components/VirtualList/SpacePlaceholder.tsx +0 -32
  195. package/src/components/VirtualList/VirtualList.css.ts +0 -29
  196. package/src/components/VirtualList/VirtualList.tsx +0 -141
  197. package/src/components/VirtualList/VirtualRowList.tsx +0 -34
  198. package/src/components/VirtualList/types.ts +0 -56
  199. package/src/components/VirtualList/useCorrectHeightForRowElements.ts +0 -14
  200. package/src/components/VirtualScrollContainer/VirtualScrollContainer.css.ts +0 -51
  201. package/src/components/VirtualScrollContainer/getScrollableClassName.ts +0 -30
  202. package/src/components/VirtualScrollContainer/index.tsx +0 -63
  203. package/src/components/flexbox/index.ts +0 -562
  204. package/src/components/hooks/useComponentState/index.tsx +0 -562
  205. package/src/components/hooks/useComponentState/types.ts +0 -32
  206. package/src/components/hooks/useEffectOnceWithProperUnmount.ts +0 -33
  207. package/src/components/hooks/useEffectWithChanges.ts +0 -56
  208. package/src/components/hooks/useImmutableRef.ts +0 -7
  209. package/src/components/hooks/useInterceptedMap.ts +0 -71
  210. package/src/components/hooks/useLatest.tsx +0 -8
  211. package/src/components/hooks/useLazyLatest.ts +0 -23
  212. package/src/components/hooks/useMemoShallowObjectMerge.ts +0 -16
  213. package/src/components/hooks/useMounted.ts +0 -18
  214. package/src/components/hooks/useOnMount.ts +0 -21
  215. package/src/components/hooks/useOnScroll.ts +0 -34
  216. package/src/components/hooks/useOnce.ts +0 -10
  217. package/src/components/hooks/useOverlay/index.tsx +0 -418
  218. package/src/components/hooks/usePrevious.ts +0 -8
  219. package/src/components/hooks/usePropertyOld.ts +0 -168
  220. package/src/components/hooks/useRerender.ts +0 -12
  221. package/src/components/hooks/xuseProperty.ts +0 -165
  222. package/src/components/types/CellPosition.ts +0 -4
  223. package/src/components/types/NonUndefined.ts +0 -1
  224. package/src/components/types/RemoveObject.ts +0 -22
  225. package/src/components/types/Renderable.ts +0 -3
  226. package/src/components/types/ScrollPosition.ts +0 -7
  227. package/src/components/types/Setter.ts +0 -2
  228. package/src/components/types/Size.ts +0 -6
  229. package/src/components/types/SubscriptionCallback.ts +0 -10
  230. package/src/components/types/VoidFn.ts +0 -1
  231. package/src/components/utils/buildSubscriptionCallback.tsx +0 -60
  232. package/src/components/utils/clamp.ts +0 -3
  233. package/src/components/utils/debounce.ts +0 -16
  234. package/src/components/utils/decamelize.ts +0 -11
  235. package/src/components/utils/discardCallsWithEqualArg.ts +0 -42
  236. package/src/components/utils/getRenderIndexes.ts +0 -33
  237. package/src/components/utils/getScrollbarWidth.ts +0 -31
  238. package/src/components/utils/getVirtualRenderCount.ts +0 -21
  239. package/src/components/utils/isControlled.ts +0 -12
  240. package/src/components/utils/isControlledValue.ts +0 -5
  241. package/src/index.tsx +0 -34
  242. package/src/test.tsx +0 -18
  243. package/src/utils/DeepMap/index.ts +0 -588
  244. package/src/utils/DeepMap/once.ts +0 -16
  245. package/src/utils/DeepMap/sortAscending.ts +0 -1
  246. package/src/utils/DeepMap/tsconfig.deepmap.json +0 -17
  247. package/src/utils/DeepMap/xpackage.json +0 -26
  248. package/src/utils/debug.ts +0 -40
  249. package/src/utils/deepClone.ts +0 -38
  250. package/src/utils/getGlobal.ts +0 -3
  251. package/src/utils/groupAndPivot/defaultToKey.ts +0 -3
  252. package/src/utils/groupAndPivot/getGroupKeysForDataItem.ts +0 -19
  253. package/src/utils/groupAndPivot/getPivotColumnsAndColumnGroups.ts +0 -361
  254. package/src/utils/groupAndPivot/index.ts +0 -1229
  255. package/src/utils/groupAndPivot/types.ts +0 -10
  256. package/src/utils/join.ts +0 -4
  257. package/src/utils/mathIntersection.ts +0 -21
  258. package/src/utils/minified.d.ts +0 -2
  259. package/src/utils/minified.js +0 -6
  260. package/src/utils/multisort/index.ts +0 -151
  261. package/src/utils/multisort/sortTypes.ts +0 -25
  262. package/src/utils/notes-status.md +0 -92
  263. package/src/utils/pageGeometry/ConvexPoly.ts +0 -34
  264. package/src/utils/pageGeometry/Point.ts +0 -51
  265. package/src/utils/pageGeometry/PolyWithPoints.ts +0 -52
  266. package/src/utils/pageGeometry/Rectangle.ts +0 -98
  267. package/src/utils/pageGeometry/Triangle.ts +0 -16
  268. package/src/utils/pageGeometry/alignment/index.ts +0 -221
  269. package/src/utils/pageGeometry/polyContainsPoint.ts +0 -182
  270. package/src/utils/pageGeometry/types.ts +0 -3
  271. package/src/utils/proxyFnCall.ts +0 -46
  272. package/src/utils/raf.ts +0 -13
  273. package/src/utils/selectParent.ts +0 -18
  274. package/src/utils/shallowEqualObjects.ts +0 -49
  275. package/src/utils/stripVar.ts +0 -3
  276. package/src/utils/toUpperFirst.ts +0 -5
  277. package/todo.md +0 -2
  278. package/tsconfig.build.json +0 -8
  279. package/tsconfig.dev.json +0 -7
  280. package/tsconfig.json +0 -30
  281. package/tsconfig.typebundler.json +0 -13
  282. package/typedoc.json +0 -4
@@ -1,562 +0,0 @@
1
- import * as React from 'react';
2
- import {
3
- useReducer,
4
- createContext,
5
- useMemo,
6
- useEffect,
7
- useState,
8
- useRef,
9
- useLayoutEffect,
10
- } from 'react';
11
-
12
- import { dbg } from '../../../utils/debug';
13
-
14
- import { proxyFn } from '../../../utils/proxyFnCall';
15
- import { toUpperFirst } from '../../../utils/toUpperFirst';
16
-
17
- import { isControlled } from '../../utils/isControlled';
18
- import { useEffectOnce } from '../useEffectOnceWithProperUnmount';
19
- import { useLatest } from '../useLatest';
20
- import { usePrevious } from '../usePrevious';
21
- import {
22
- ComponentInterceptedActions,
23
- ComponentMappedCallbacks,
24
- ComponentStateActions,
25
- ComponentStateContext,
26
- ComponentStateGeneratedActions,
27
- } from './types';
28
-
29
- export const notifyChange = (
30
- props: any,
31
- callbackPropName: string,
32
- values: any[],
33
- ) => {
34
- const callbackProp = props[callbackPropName] as Function;
35
-
36
- if (typeof callbackProp === 'function') {
37
- callbackProp(...values);
38
- }
39
- };
40
-
41
- const debug = dbg('rerender');
42
-
43
- let ComponentContext: any;
44
-
45
- export function getComponentStateContext<T>(): React.Context<T> {
46
- if (ComponentContext as React.Context<T>) {
47
- return ComponentContext;
48
- }
49
-
50
- return (ComponentContext = createContext<T>(null as any as T));
51
- }
52
-
53
- function getReducerGeneratedActions<T_STATE, T_PROPS>(
54
- dispatch: React.Dispatch<any>,
55
- getState: () => T_STATE,
56
- getProps: () => T_PROPS,
57
- propsToForward: ForwardPropsToStateFnResult<T_PROPS, T_STATE>,
58
- allowedControlledPropOverrides?: Record<keyof T_PROPS, boolean>,
59
- interceptedActions?: ComponentInterceptedActions<T_STATE>,
60
- mappedCallbacks?: ComponentMappedCallbacks<T_STATE>,
61
- ): ComponentStateGeneratedActions<T_STATE> {
62
- const state = getState();
63
- //@ts-ignore
64
- return Object.keys(state).reduce((actions, stateKey) => {
65
- const key = stateKey as any as keyof T_STATE;
66
-
67
- const setter = (value: T_STATE[typeof key]) => {
68
- const props = getProps();
69
- const state = getState();
70
- const currentValue = state[key];
71
- if (currentValue === value) {
72
- // #samevaluecheckfailswhennotflushed
73
- // early exit, as no change detected - this works if state updates are flushed, but could fail us when state updates are batched
74
- // as we could discard a valid update, since the last/previous value could have not been flushed yet
75
- // eg: in DataSource.useLoadData we set actions.loading = true, but this is not written to the state right away but is batched
76
- // so if on the same tick we do actions.loading = false, this will be discarded, as the state is still loading: false as the above/previous actions was batched and hasn't been applied
77
- //
78
- // so in order to avoid the above scenario, simply allow same value updates to be applied
79
- // return;
80
- // we skip this return, as starting with React 18 we have batched updates
81
- // so if we return we could be discarding a valid update, since the last/previous value could not have been flushed yet
82
- // so this current value could be the same as the old value, but different from the value that was not yet flushed
83
- // and therefore the current value to be set could be a valid new value
84
- }
85
-
86
- let notifyTheChange = true;
87
-
88
- if (interceptedActions && typeof interceptedActions[key] === 'function') {
89
- if (interceptedActions[key]!(value, { actions, state }) === false) {
90
- notifyTheChange = false;
91
- }
92
- }
93
-
94
- // it's important that we notify with the value that we receive
95
- // directly from the setter (see continuation below)
96
- if (notifyTheChange) {
97
- let callbackParams = [value];
98
- let callbackName = `on${toUpperFirst(stateKey)}Change` as string;
99
-
100
- if (mappedCallbacks && mappedCallbacks[key]) {
101
- const res = mappedCallbacks[key](value, state);
102
- callbackName = res.callbackName || callbackName;
103
- callbackParams = res.callbackParams;
104
- }
105
-
106
- notifyChange(props, callbackName, callbackParams);
107
- }
108
-
109
- //@ts-ignore
110
- const forwardFn = propsToForward[key];
111
-
112
- if (typeof forwardFn === 'function') {
113
- // and not with the modified value from the forwardFn
114
- value = forwardFn(value);
115
- }
116
-
117
- const allowControlled =
118
- !!allowedControlledPropOverrides?.[key as any as keyof T_PROPS];
119
-
120
- if (isControlled(stateKey as keyof T_PROPS, props) && !allowControlled) {
121
- return;
122
- }
123
-
124
- dispatch({
125
- payload: {
126
- updatedProps: null,
127
- mappedState: {
128
- [stateKey]: value,
129
- },
130
- },
131
- });
132
- };
133
-
134
- Object.defineProperty(actions, stateKey, {
135
- set: setter,
136
- });
137
-
138
- return actions;
139
- }, {} as ComponentStateGeneratedActions<T_STATE>);
140
- }
141
-
142
- export type ForwardPropsToStateFnResult<TYPE_PROPS, TYPE_RESULT> = {
143
- [propName in keyof TYPE_PROPS & keyof TYPE_RESULT]:
144
- | 1
145
- | ((value: TYPE_PROPS[propName]) => TYPE_RESULT[propName]);
146
- };
147
-
148
- function forwardProps<T_PROPS, T_RESULT>(
149
- propsToForward: Partial<ForwardPropsToStateFnResult<T_PROPS, T_RESULT>>,
150
- props: T_PROPS,
151
- ): T_RESULT {
152
- const mappedState = {} as T_RESULT;
153
- for (let k in propsToForward)
154
- if (propsToForward.hasOwnProperty(k)) {
155
- const forwardFn = propsToForward[k as keyof typeof propsToForward];
156
- let propValue = isControlled(k as keyof T_PROPS, props)
157
- ? props[k as keyof T_PROPS]
158
- : props[`default${toUpperFirst(k)}` as keyof T_PROPS];
159
-
160
- if (typeof forwardFn === 'function') {
161
- //@ts-ignore
162
- propValue = forwardFn(propValue);
163
- }
164
- //@ts-ignore
165
- mappedState[k as any as keyof T_RESULT] = propValue;
166
- }
167
-
168
- return mappedState;
169
- }
170
-
171
- type ComponentStateRootConfig<
172
- T_PROPS,
173
- COMPONENT_MAPPED_STATE,
174
- COMPONENT_SETUP_STATE = {},
175
- COMPONENT_DERIVED_STATE = {},
176
- T_ACTIONS = {},
177
- T_PARENT_STATE = {},
178
- > = {
179
- debugName?: string;
180
- initSetupState?: () => COMPONENT_SETUP_STATE;
181
-
182
- layoutEffect?: boolean;
183
-
184
- forwardProps?: (
185
- setupState: COMPONENT_SETUP_STATE,
186
- ) => ForwardPropsToStateFnResult<T_PROPS, COMPONENT_MAPPED_STATE>;
187
- allowedControlledPropOverrides?: Record<keyof T_PROPS, true>;
188
- interceptActions?: ComponentInterceptedActions<
189
- COMPONENT_MAPPED_STATE & COMPONENT_DERIVED_STATE & COMPONENT_SETUP_STATE
190
- >;
191
- mappedCallbacks?: ComponentMappedCallbacks<
192
- COMPONENT_MAPPED_STATE & COMPONENT_DERIVED_STATE & COMPONENT_SETUP_STATE
193
- >;
194
- onPropChange?: (
195
- params: {
196
- name: keyof T_PROPS;
197
- oldValue: any;
198
- newValue: any;
199
- },
200
- props: T_PROPS,
201
- actions: ComponentStateActions<
202
- COMPONENT_MAPPED_STATE & COMPONENT_DERIVED_STATE & COMPONENT_SETUP_STATE
203
- >,
204
- ) => void;
205
- mapPropsToState?: (params: {
206
- props: T_PROPS;
207
- state: COMPONENT_MAPPED_STATE &
208
- COMPONENT_SETUP_STATE &
209
- Partial<COMPONENT_DERIVED_STATE>;
210
- oldState:
211
- | null
212
- | (COMPONENT_MAPPED_STATE &
213
- COMPONENT_SETUP_STATE &
214
- Partial<COMPONENT_DERIVED_STATE>);
215
- parentState: T_PARENT_STATE | null;
216
- }) => COMPONENT_DERIVED_STATE;
217
- concludeReducer?: (params: {
218
- previousState: COMPONENT_MAPPED_STATE &
219
- COMPONENT_SETUP_STATE &
220
- COMPONENT_DERIVED_STATE;
221
- state: COMPONENT_MAPPED_STATE &
222
- COMPONENT_SETUP_STATE &
223
- COMPONENT_DERIVED_STATE;
224
- updatedProps: Partial<T_PROPS> | null;
225
- parentState: T_PARENT_STATE | null;
226
- }) => COMPONENT_MAPPED_STATE &
227
- COMPONENT_SETUP_STATE &
228
- COMPONENT_DERIVED_STATE;
229
- getReducerActions?: (dispatch: React.Dispatch<any>) => T_ACTIONS;
230
-
231
- getParentState?: () => T_PARENT_STATE;
232
-
233
- cleanup?: (
234
- state: COMPONENT_MAPPED_STATE &
235
- COMPONENT_SETUP_STATE &
236
- COMPONENT_DERIVED_STATE,
237
- ) => void;
238
-
239
- onControlledPropertyChange?: (
240
- name: string,
241
- newValue: any,
242
- oldValue: any,
243
- ) => void | ((value: any, oldValue: any) => any);
244
- };
245
-
246
- export function getComponentStateRoot<
247
- T_PROPS,
248
- COMPONENT_MAPPED_STATE extends object,
249
- COMPONENT_SETUP_STATE extends object = {},
250
- COMPONENT_DERIVED_STATE extends object = {},
251
- T_ACTIONS = {},
252
- T_PARENT_STATE = {},
253
- >(
254
- config: ComponentStateRootConfig<
255
- T_PROPS,
256
- COMPONENT_MAPPED_STATE,
257
- COMPONENT_SETUP_STATE,
258
- COMPONENT_DERIVED_STATE,
259
- T_ACTIONS,
260
- T_PARENT_STATE
261
- >,
262
- ) {
263
- /**
264
- * since config is passed outside the cmp, we can skip it inside useMemo deps list
265
- */
266
- return React.memo(function ComponentStateRoot(
267
- props: T_PROPS & { children: React.ReactNode },
268
- ) {
269
- const [initialSetupState] = useState<COMPONENT_SETUP_STATE>(() => {
270
- return config.initSetupState
271
- ? config.initSetupState()
272
- : ({} as COMPONENT_SETUP_STATE);
273
- });
274
- const propsToStateSetRef = useRef<Set<string>>(new Set());
275
- const propsToForward = useMemo<
276
- Partial<ForwardPropsToStateFnResult<T_PROPS, COMPONENT_MAPPED_STATE>>
277
- >(
278
- () => (config.forwardProps ? config.forwardProps(initialSetupState) : {}),
279
- [initialSetupState],
280
- );
281
-
282
- type COMPONENT_STATE = COMPONENT_MAPPED_STATE &
283
- COMPONENT_DERIVED_STATE &
284
- COMPONENT_SETUP_STATE;
285
-
286
- const parentState = config.getParentState?.() ?? null;
287
- const getParentState = useLatest(parentState);
288
-
289
- function initStateOnce() {
290
- // STEP 1: call setupState
291
-
292
- let mappedState = {} as COMPONENT_MAPPED_STATE;
293
-
294
- if (propsToForward) {
295
- mappedState = forwardProps<T_PROPS, COMPONENT_MAPPED_STATE>(
296
- propsToForward,
297
- props,
298
- );
299
- }
300
-
301
- const state = { ...initialSetupState, ...mappedState };
302
-
303
- if (config.mapPropsToState) {
304
- const { fn: mapPropsToState, propertyReads } = proxyFn(
305
- config.mapPropsToState,
306
- {
307
- getProxyTargetFromArgs: (initialArg) => initialArg.props,
308
- putProxyToArgs: (props: T_PROPS, initialArg) => {
309
- return [{ ...initialArg, props }];
310
- },
311
- },
312
- );
313
- const stateFromProps = mapPropsToState({
314
- props,
315
- state,
316
- oldState: null,
317
- parentState,
318
- // getState: getComponentState,
319
- });
320
-
321
- propsToStateSetRef.current = new Set([
322
- ...propsToStateSetRef.current,
323
- ...propertyReads,
324
- ]);
325
- return {
326
- ...state,
327
- ...stateFromProps,
328
- };
329
- }
330
-
331
- return state as COMPONENT_MAPPED_STATE &
332
- COMPONENT_DERIVED_STATE &
333
- COMPONENT_SETUP_STATE;
334
- }
335
- const [wholeState] = useState<COMPONENT_STATE>(initStateOnce);
336
-
337
- const getProps = useLatest(props);
338
-
339
- const theReducer: React.Reducer<COMPONENT_STATE, any> = (
340
- previousState: COMPONENT_STATE,
341
- action: any,
342
- ) => {
343
- if (action.type === 'REPLACE_STATE') {
344
- return action.payload;
345
- }
346
-
347
- const parentState = getParentState?.() ?? null;
348
- const mappedState: Partial<COMPONENT_MAPPED_STATE> | null =
349
- action.payload.mappedState;
350
- const updatedProps: Partial<T_PROPS> | null =
351
- action.payload.updatedPropsToState;
352
-
353
- const newState: COMPONENT_STATE = Object.assign({}, previousState);
354
-
355
- if (mappedState) {
356
- Object.assign(newState, mappedState);
357
- }
358
-
359
- if (config.mapPropsToState) {
360
- const { fn: mapPropsToState, propertyReads } = proxyFn(
361
- config.mapPropsToState,
362
- {
363
- getProxyTargetFromArgs: (initialArg) => initialArg.props,
364
- putProxyToArgs: (props: T_PROPS, initialArg) => {
365
- return [{ ...initialArg, props }];
366
- },
367
- },
368
- );
369
-
370
- const stateFromProps = mapPropsToState({
371
- props: getProps(),
372
- state: newState,
373
- oldState: previousState,
374
- parentState,
375
- // getState: getComponentState
376
- });
377
-
378
- propsToStateSetRef.current = new Set([
379
- ...propsToStateSetRef.current,
380
- ...propertyReads,
381
- ]);
382
-
383
- Object.assign(newState, stateFromProps);
384
- }
385
-
386
- const result = config.concludeReducer
387
- ? config.concludeReducer({
388
- previousState,
389
- state: newState,
390
- updatedProps,
391
- parentState,
392
- })
393
- : newState;
394
-
395
- return result;
396
- };
397
-
398
- const [state, dispatch] = useReducer<React.Reducer<COMPONENT_STATE, any>>(
399
- theReducer,
400
- wholeState,
401
- );
402
-
403
- const getComponentState = useLatest(state);
404
-
405
- type ACTIONS_TYPE = ComponentStateActions<COMPONENT_STATE>;
406
-
407
- const { allowedControlledPropOverrides } = config;
408
-
409
- const actions = useMemo(() => {
410
- const generatedActions = getReducerGeneratedActions<
411
- COMPONENT_STATE,
412
- T_PROPS
413
- >(
414
- dispatch,
415
- getComponentState,
416
- getProps,
417
- propsToForward as ForwardPropsToStateFnResult<T_PROPS, COMPONENT_STATE>,
418
- allowedControlledPropOverrides,
419
- config.interceptActions,
420
- config.mappedCallbacks,
421
- );
422
-
423
- return generatedActions;
424
- }, [
425
- dispatch,
426
- propsToForward,
427
- allowedControlledPropOverrides,
428
- ]) as ACTIONS_TYPE;
429
-
430
- const Context =
431
- getComponentStateContext<
432
- ComponentStateContext<COMPONENT_STATE, ACTIONS_TYPE>
433
- >();
434
-
435
- const contextValue = useMemo(
436
- () => ({
437
- componentState: state,
438
- componentActions: actions,
439
- getComponentState,
440
- }),
441
- [state, actions, getComponentState],
442
- );
443
-
444
- const prevProps = usePrevious(props);
445
-
446
- const effectFn = config.layoutEffect ? useLayoutEffect : useEffect;
447
- effectFn(() => {
448
- const currentProps = props;
449
- const newMappedState: Partial<COMPONENT_MAPPED_STATE> = {};
450
- let newMappedStateCount = 0;
451
-
452
- const updatedPropsToState: Partial<T_PROPS> = {};
453
- let updatedPropsToStateCount = 0;
454
-
455
- type UPDATED_PROPS = {
456
- [key in keyof T_PROPS]?: {
457
- newValue: T_PROPS[key];
458
- oldValue: T_PROPS[key];
459
- };
460
- };
461
- const rawUpdatedProps: UPDATED_PROPS = {};
462
-
463
- for (var k in props) {
464
- const key = k as string as keyof T_PROPS;
465
- const oldValue = prevProps[key];
466
- const newValue = currentProps[key];
467
-
468
- if (key === 'children') {
469
- continue;
470
- }
471
- if (oldValue === newValue) {
472
- continue;
473
- }
474
- rawUpdatedProps[key] = { newValue, oldValue };
475
-
476
- if (isControlled(key, props) || isControlled(key, prevProps)) {
477
- if (propsToForward.hasOwnProperty(k)) {
478
- let valueToSet = newValue;
479
- const forwardFn = propsToForward[k as keyof typeof propsToForward];
480
- if (typeof forwardFn === 'function') {
481
- //@ts-ignore
482
- valueToSet = forwardFn(newValue);
483
- }
484
- //@ts-ignore
485
- if (state[key] !== valueToSet) {
486
- //@ts-ignore
487
- newMappedState[key] = valueToSet;
488
- newMappedStateCount++;
489
- }
490
-
491
- // or even if there is not, but props from propsToStateSet have changed
492
- } else if (propsToStateSetRef.current.has(k)) {
493
- updatedPropsToState[key] = currentProps[key];
494
- updatedPropsToStateCount++;
495
- }
496
- }
497
- }
498
-
499
- if (updatedPropsToStateCount > 0 || newMappedStateCount > 0) {
500
- debug(
501
- 'Triggered by new values for the following props',
502
- ...[
503
- ...Object.keys(newMappedState ?? {}),
504
- ...Object.keys(updatedPropsToState ?? {}),
505
- ],
506
- );
507
- const action = {
508
- payload: {
509
- mappedState: newMappedStateCount ? newMappedState : null,
510
- updatedPropsToState: updatedPropsToStateCount
511
- ? updatedPropsToState
512
- : null,
513
- },
514
- };
515
-
516
- // const newState = theReducer(state, action);
517
-
518
- dispatch(action);
519
-
520
- if (config.onPropChange) {
521
- for (var prop in rawUpdatedProps)
522
- if (rawUpdatedProps.hasOwnProperty(prop)) {
523
- const { newValue, oldValue } = rawUpdatedProps[prop]!;
524
- config.onPropChange(
525
- { name: prop, newValue, oldValue },
526
- props,
527
- actions,
528
- );
529
- }
530
- }
531
- // config.onPropChange?.(
532
- // { name: key, oldValue, newValue },
533
-
534
- // actions as ACTIONS_TYPE,
535
- // );
536
- // dispatch({
537
- // type: 'REPLACE_STATE',
538
- // payload: newState,
539
- // });
540
- }
541
- });
542
-
543
- useEffectOnce(() => {
544
- return () => {
545
- config.cleanup?.(getComponentState());
546
- };
547
- });
548
-
549
- return (
550
- <Context.Provider value={contextValue}>{props.children}</Context.Provider>
551
- );
552
- });
553
- }
554
-
555
- export function useComponentState<COMPONENT_STATE>() {
556
- type ACTIONS_TYPE = ComponentStateActions<COMPONENT_STATE>;
557
- const Context =
558
- getComponentStateContext<
559
- ComponentStateContext<COMPONENT_STATE, ACTIONS_TYPE>
560
- >();
561
- return React.useContext(Context);
562
- }
@@ -1,32 +0,0 @@
1
- export type ComponentStateContext<T_STATE, T_ACTIONS> = {
2
- getComponentState: () => T_STATE;
3
- componentState: T_STATE;
4
- componentActions: T_ACTIONS;
5
- };
6
-
7
- export type ComponentStateGeneratedActions<T_STATE> = {
8
- [k in keyof T_STATE]: T_STATE[k] | React.SetStateAction<T_STATE[k]>;
9
- };
10
-
11
- export type ComponentInterceptedActions<T_STATE> = {
12
- [k in keyof T_STATE]?: (
13
- value: T_STATE[k],
14
- {
15
- actions,
16
- state,
17
- }: { actions: ComponentStateGeneratedActions<T_STATE>; state: T_STATE },
18
- ) => void | boolean;
19
- };
20
-
21
- export type ComponentMappedCallbacks<T_STATE> = {
22
- [k in keyof T_STATE]: (
23
- value: T_STATE[k],
24
- state: T_STATE,
25
- ) => {
26
- callbackName: string;
27
- callbackParams: any[];
28
- };
29
- };
30
-
31
- export type ComponentStateActions<T_STATE> =
32
- ComponentStateGeneratedActions<T_STATE>;
@@ -1,33 +0,0 @@
1
- import { EffectCallback, useCallback, useEffect, useRef } from 'react';
2
- import { useRerender } from './useRerender';
3
-
4
- export const useEffectOnce = (effectCallback: EffectCallback) => {
5
- const effectFunction = useCallback<EffectCallback>(effectCallback, []);
6
- const effectCalled = useRef(false);
7
- const componentRendered = useRef(false);
8
- const destroyFn = useRef<void | VoidFunction>();
9
- const [_, rerender] = useRerender();
10
-
11
- if (effectCalled.current) {
12
- componentRendered.current = true;
13
- }
14
-
15
- useEffect(() => {
16
- if (!effectCalled.current) {
17
- destroyFn.current = effectFunction;
18
- effectCalled.current = true;
19
- }
20
-
21
- rerender();
22
-
23
- return () => {
24
- if (!componentRendered.current) {
25
- return;
26
- }
27
-
28
- if (destroyFn.current) {
29
- destroyFn.current();
30
- }
31
- };
32
- }, []);
33
- };
@@ -1,56 +0,0 @@
1
- import { EffectCallback, useEffect, useRef } from 'react';
2
-
3
- export function useEffectWithChanges<T>(
4
- fn: (
5
- changes: Record<keyof T, any>,
6
- prevValues: Record<string, any>,
7
- ) => void | (() => void),
8
- deps: Record<keyof T, any>,
9
- ) {
10
- const prevRef = useRef({});
11
-
12
- const oldValuesRef = useRef<Record<string, any>>({});
13
- const oldValues: Record<string, any> = oldValuesRef.current;
14
-
15
- const changesRef = useRef<Record<keyof T, any>>({} as Record<keyof T, any>);
16
- const changes: Record<string, any> = changesRef.current;
17
-
18
- const useEffectDeps: any[] = [];
19
-
20
- for (const k in deps) {
21
- if (deps.hasOwnProperty(k)) {
22
- if (deps[k] !== (prevRef.current as any)[k]) {
23
- changes[k] = deps[k];
24
- oldValues[k] = (prevRef.current as any)[k];
25
- }
26
- useEffectDeps.push(deps[k]);
27
- }
28
- }
29
-
30
- prevRef.current = deps;
31
-
32
- useEffect(() => {
33
- const changes = changesRef.current;
34
-
35
- const result = fn(changes, oldValues);
36
-
37
- changesRef.current = {} as Record<keyof T, any>;
38
- oldValuesRef.current = {};
39
- return result;
40
- }, useEffectDeps);
41
- }
42
-
43
- export function useEffectWithObject(
44
- fn: EffectCallback,
45
- deps: Record<string, any>,
46
- ) {
47
- const useEffectDeps: any[] = [];
48
-
49
- for (const k in deps) {
50
- if (deps.hasOwnProperty(k)) {
51
- useEffectDeps.push(deps[k]);
52
- }
53
- }
54
-
55
- useEffect(fn, useEffectDeps);
56
- }
@@ -1,7 +0,0 @@
1
- import { useRef } from 'react';
2
-
3
- const useImmutableRef = <T>(value: T) => {
4
- return useRef<T>(value).current;
5
- };
6
-
7
- export default useImmutableRef;