@infinite-table/infinite-react 0.4.13 → 0.5.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.
- package/.vscode/settings.json +3 -0
- package/API.md +0 -0
- package/build/buildPackageJson.js +43 -0
- package/build/scripts/test.js +37 -0
- package/bundler.config.js +25 -0
- package/globals.d.ts +7 -0
- package/package.json +102 -3
- package/rollup.config.js +15 -0
- package/run-build-deepmap.js +47 -0
- package/run-build.js +45 -0
- package/run-dts-concat.js +21 -0
- package/src/components/CSSVariableWatch.tsx +82 -0
- package/src/components/DataSource/BooleanCollectionState.ts +214 -0
- package/src/components/DataSource/BooleanDeepCollectionState.ts +208 -0
- package/src/components/DataSource/DataSourceContext.ts +20 -0
- package/src/components/DataSource/GroupRowsState.ts +74 -0
- package/src/components/DataSource/Indexer.ts +27 -0
- package/src/components/DataSource/RowSelectionState.ts +763 -0
- package/src/components/DataSource/defaultFilterTypes.ts +64 -0
- package/src/components/DataSource/index.tsx +108 -0
- package/src/components/DataSource/privateHooks/getChangeDetect.ts +7 -0
- package/src/components/DataSource/privateHooks/useLoadData.ts +783 -0
- package/src/components/DataSource/publicHooks/useDataSource.ts +19 -0
- package/src/components/DataSource/publicHooks/useDataSourceActions.ts +14 -0
- package/src/components/DataSource/state/getInitialState.ts +494 -0
- package/src/components/DataSource/state/normalizeSortInfo.ts +35 -0
- package/src/components/DataSource/state/reducer.ts +466 -0
- package/src/components/DataSource/state/rowInfoStatus.ts +27 -0
- package/src/components/DataSource/types.ts +548 -0
- package/src/components/HeadlessTable/HeadlessTableWithPinnedContainers.tsx +142 -0
- package/src/components/HeadlessTable/MappedCells.ts +251 -0
- package/src/components/HeadlessTable/RawTable.tsx +98 -0
- package/src/components/HeadlessTable/ReactHeadlessTableRenderer.tsx +1516 -0
- package/src/components/HeadlessTable/index.tsx +234 -0
- package/src/components/InfiniteTable/InfiniteCls.css.ts +164 -0
- package/src/components/InfiniteTable/InfiniteTableContext.ts +16 -0
- package/src/components/InfiniteTable/api/getImperativeApi.ts +391 -0
- package/src/components/InfiniteTable/api/getSelectionApi.ts +364 -0
- package/src/components/InfiniteTable/api/type.ts +11 -0
- package/src/components/InfiniteTable/components/ActiveCellIndicator.css.ts +51 -0
- package/src/components/InfiniteTable/components/ActiveCellIndicator.tsx +116 -0
- package/src/components/InfiniteTable/components/ActiveRowIndicator.css.ts +56 -0
- package/src/components/InfiniteTable/components/ActiveRowIndicator.tsx +94 -0
- package/src/components/InfiniteTable/components/CheckBox.css.ts +13 -0
- package/src/components/InfiniteTable/components/CheckBox.tsx +96 -0
- package/src/components/InfiniteTable/components/FilterEditors.tsx +52 -0
- package/src/components/InfiniteTable/components/InfiniteTableBody/InfiniteTableBody.tsx +27 -0
- package/src/components/InfiniteTable/components/InfiniteTableBody/index.tsx +1 -0
- package/src/components/InfiniteTable/components/InfiniteTableFooter/InfiniteTableFooter.tsx +31 -0
- package/src/components/InfiniteTable/components/InfiniteTableFooter/index.tsx +1 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableColumnHeaderFilter.tsx +102 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeader.tsx +163 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderCell.tsx +574 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderGroup.tsx +79 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderTypes.tsx +25 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderWrapper.tsx +185 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/GroupResizeHandle.tsx +159 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/ResizeHandle.css.ts +124 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/columnResizer.ts +166 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/index.tsx +146 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/buildColumnAndGroupTree.ts +240 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/header.css.ts +358 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/index.tsx +1 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/useColumnGroupResizeHandle.tsx +114 -0
- package/src/components/InfiniteTable/components/InfiniteTableHeader/useColumnResizeHandle.tsx +146 -0
- package/src/components/InfiniteTable/components/InfiniteTableLicenseFooter/footer.css.ts +7 -0
- package/src/components/InfiniteTable/components/InfiniteTableLicenseFooter/index.tsx +110 -0
- package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableCell.tsx +120 -0
- package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableCellTypes.ts +74 -0
- package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableColumnCell.tsx +457 -0
- package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRow.tsx +107 -0
- package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRowClassName.ts +6 -0
- package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRowTypes.ts +32 -0
- package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRowUnvirtualized.tsx +75 -0
- package/src/components/InfiniteTable/components/InfiniteTableRow/columnRendering.tsx +263 -0
- package/src/components/InfiniteTable/components/InfiniteTableRow/index.tsx +1 -0
- package/src/components/InfiniteTable/components/InfiniteTableRow/row.css.ts +74 -0
- package/src/components/InfiniteTable/components/InfiniteTableRow/useRowDOMProps.ts +199 -0
- package/src/components/InfiniteTable/components/LoadMask.css.ts +34 -0
- package/src/components/InfiniteTable/components/LoadMask.tsx +27 -0
- package/src/components/InfiniteTable/components/ScrollbarPlaceholder.tsx +56 -0
- package/src/components/InfiniteTable/components/cell.css.ts +192 -0
- package/src/components/InfiniteTable/components/icons/ArrowDown.tsx +14 -0
- package/src/components/InfiniteTable/components/icons/ArrowUp.tsx +14 -0
- package/src/components/InfiniteTable/components/icons/ExpanderIcon.css.ts +31 -0
- package/src/components/InfiniteTable/components/icons/ExpanderIcon.tsx +63 -0
- package/src/components/InfiniteTable/components/icons/Icon.tsx +18 -0
- package/src/components/InfiniteTable/components/icons/InfiniteTableIconClassName.ts +1 -0
- package/src/components/InfiniteTable/components/icons/LoadingIcon.css.ts +8 -0
- package/src/components/InfiniteTable/components/icons/LoadingIcon.tsx +45 -0
- package/src/components/InfiniteTable/components/icons/MenuIcon.tsx +50 -0
- package/src/components/InfiniteTable/components/icons/SortIcon.tsx +113 -0
- package/src/components/InfiniteTable/eventHandlers/eventHandlerTypes.ts +57 -0
- package/src/components/InfiniteTable/eventHandlers/index.ts +95 -0
- package/src/components/InfiniteTable/eventHandlers/keyboardNavigation.ts +232 -0
- package/src/components/InfiniteTable/eventHandlers/keyboardSelection.ts +110 -0
- package/src/components/InfiniteTable/eventHandlers/onCellClick.ts +94 -0
- package/src/components/InfiniteTable/eventHandlers/onKeyDown.ts +25 -0
- package/src/components/InfiniteTable/hooks/adjustColumnOrderForAllColumns.ts +70 -0
- package/src/components/InfiniteTable/hooks/reorderColumnsOnDrag.ts +476 -0
- package/src/components/InfiniteTable/hooks/useAutoSizeColumns.ts +206 -0
- package/src/components/InfiniteTable/hooks/useCellClassName.ts +80 -0
- package/src/components/InfiniteTable/hooks/useCellRendering.tsx +217 -0
- package/src/components/InfiniteTable/hooks/useColumnGroups.ts +71 -0
- package/src/components/InfiniteTable/hooks/useColumnMenu.ts +72 -0
- package/src/components/InfiniteTable/hooks/useColumnPointerEvents.ts +258 -0
- package/src/components/InfiniteTable/hooks/useColumnRowspan.ts +43 -0
- package/src/components/InfiniteTable/hooks/useColumnSizeFn.ts +24 -0
- package/src/components/InfiniteTable/hooks/useColumnsWhen.ts +582 -0
- package/src/components/InfiniteTable/hooks/useComputed.ts +186 -0
- package/src/components/InfiniteTable/hooks/useComputedVisibleColumns.ts +219 -0
- package/src/components/InfiniteTable/hooks/useDOMProps.ts +306 -0
- package/src/components/InfiniteTable/hooks/useEffectWithRaf.ts +47 -0
- package/src/components/InfiniteTable/hooks/useInfiniteTable.ts +10 -0
- package/src/components/InfiniteTable/hooks/useInfiniteTableState.ts +11 -0
- package/src/components/InfiniteTable/hooks/useInternalProps.ts +8 -0
- package/src/components/InfiniteTable/hooks/useLicense/LicenseDetails.ts +13 -0
- package/src/components/InfiniteTable/hooks/useLicense/crc32.ts +56 -0
- package/src/components/InfiniteTable/hooks/useLicense/decode.ts +147 -0
- package/src/components/InfiniteTable/hooks/useLicense/useLicense.ts +39 -0
- package/src/components/InfiniteTable/hooks/useRerenderOnKeyChange.ts +21 -0
- package/src/components/InfiniteTable/hooks/useRowHover.ts +18 -0
- package/src/components/InfiniteTable/hooks/useScrollToActiveCell.ts +48 -0
- package/src/components/InfiniteTable/hooks/useScrollToActiveRow.ts +44 -0
- package/src/components/InfiniteTable/hooks/useScrollbars.ts +51 -0
- package/src/components/InfiniteTable/hooks/useToggleGroupRow.ts +56 -0
- package/src/components/InfiniteTable/hooks/useUnpinnedRendering.tsx +165 -0
- package/src/components/InfiniteTable/hooks/useYourBrain.ts +55 -0
- package/src/components/InfiniteTable/index.tsx +352 -0
- package/src/components/InfiniteTable/internalProps.ts +4 -0
- package/src/components/InfiniteTable/state/computeColumnGroupsDepths.ts +29 -0
- package/src/components/InfiniteTable/state/getColumnVisibilityForHideEmptyGroupColumns.ts +127 -0
- package/src/components/InfiniteTable/state/getInitialState.ts +375 -0
- package/src/components/InfiniteTable/theme.css.ts +556 -0
- package/src/components/InfiniteTable/types/InfiniteTableAction.ts +6 -0
- package/src/components/InfiniteTable/types/InfiniteTableActionType.ts +12 -0
- package/src/components/InfiniteTable/types/InfiniteTableColumn.ts +478 -0
- package/src/components/InfiniteTable/types/InfiniteTableComputedValues.ts +43 -0
- package/src/components/InfiniteTable/types/InfiniteTableContextValue.ts +12 -0
- package/src/components/InfiniteTable/types/InfiniteTableInternalProps.ts +3 -0
- package/src/components/InfiniteTable/types/InfiniteTableProps.ts +511 -0
- package/src/components/InfiniteTable/types/InfiniteTableState.ts +195 -0
- package/src/components/InfiniteTable/types/Utility.ts +35 -0
- package/src/components/InfiniteTable/types/index.ts +96 -0
- package/src/components/InfiniteTable/utilities.css.ts +179 -0
- package/src/components/InfiniteTable/utils/MultiRowSelector.ts +111 -0
- package/src/components/InfiniteTable/utils/RenderHookComponentForInfinite.tsx +52 -0
- package/src/components/InfiniteTable/utils/adjustColumnOrderForPinning.ts +33 -0
- package/src/components/InfiniteTable/utils/assignFiltered.ts +29 -0
- package/src/components/InfiniteTable/utils/defaultGetColumContextMenuItems.tsx +108 -0
- package/src/components/InfiniteTable/utils/getColumnComputedType.ts +33 -0
- package/src/components/InfiniteTable/utils/getColumnForGroupBy.tsx +313 -0
- package/src/components/InfiniteTable/utils/getComputedVisibleColumns.ts +773 -0
- package/src/components/InfiniteTable/utils/getContextMenuForColumn.tsx +72 -0
- package/src/components/InfiniteTable/utils/infiniteDOMUtils.ts +189 -0
- package/src/components/InfiniteTable/utils/mapToObject.ts +20 -0
- package/src/components/InfiniteTable/utils/moveXatY.ts +25 -0
- package/src/components/InfiniteTable/utils/objectValuesExcept.ts +9 -0
- package/src/components/InfiniteTable/utils/progressiveSpeedScroller.ts +158 -0
- package/src/components/InfiniteTable/utils/rafFn.ts +13 -0
- package/src/components/InfiniteTable/utils/toCSSVarName.ts +1 -0
- package/src/components/InfiniteTable/utils/toMap.ts +24 -0
- package/src/components/Menu/Menu.tsx +750 -0
- package/src/components/Menu/MenuCls.css.ts +139 -0
- package/src/components/Menu/MenuContext.ts +10 -0
- package/src/components/Menu/MenuContextValue.ts +15 -0
- package/src/components/Menu/MenuProps.ts +98 -0
- package/src/components/Menu/MenuState.ts +56 -0
- package/src/components/Menu/MenuTriangleContext.ts +352 -0
- package/src/components/Menu/childrenToRuntimeItems.tsx +103 -0
- package/src/components/Menu/getMenuState.tsx +119 -0
- package/src/components/Menu/index.tsx +88 -0
- package/src/components/Menu/propToIdentifyMenu.ts +1 -0
- package/src/components/RawList/AvoidReactDiff.tsx +48 -0
- package/src/components/RawList/MappedItems.ts +143 -0
- package/src/components/RawList/ReactVirtualRenderer.tsx +307 -0
- package/src/components/RawList/index.tsx +42 -0
- package/src/components/RawList/types.ts +23 -0
- package/src/components/RawList/useVirtualRenderer.ts +36 -0
- package/src/components/RenderHookComponent.tsx +10 -0
- package/src/components/ResizeObserver/index.tsx +170 -0
- package/src/components/VirtualBrain/MatrixBrain.ts +1621 -0
- package/src/components/VirtualBrain/ScrollListener.ts +39 -0
- package/src/components/VirtualBrain/index.ts +664 -0
- package/src/components/VirtualList/ColumnListWithExternalScrolling.tsx +78 -0
- package/src/components/VirtualList/InfiniteListRootClassName.ts +1 -0
- package/src/components/VirtualList/RowListWithExternalScrolling.tsx +120 -0
- package/src/components/VirtualList/SpacePlaceholder.tsx +32 -0
- package/src/components/VirtualList/VirtualList.css.ts +29 -0
- package/src/components/VirtualList/VirtualList.tsx +141 -0
- package/src/components/VirtualList/VirtualRowList.tsx +34 -0
- package/src/components/VirtualList/types.ts +56 -0
- package/src/components/VirtualList/useCorrectHeightForRowElements.ts +14 -0
- package/src/components/VirtualScrollContainer/VirtualScrollContainer.css.ts +51 -0
- package/src/components/VirtualScrollContainer/getScrollableClassName.ts +30 -0
- package/src/components/VirtualScrollContainer/index.tsx +63 -0
- package/src/components/flexbox/index.ts +562 -0
- package/src/components/hooks/useComponentState/index.tsx +562 -0
- package/src/components/hooks/useComponentState/types.ts +32 -0
- package/src/components/hooks/useEffectOnceWithProperUnmount.ts +33 -0
- package/src/components/hooks/useEffectWithChanges.ts +56 -0
- package/src/components/hooks/useImmutableRef.ts +7 -0
- package/src/components/hooks/useInterceptedMap.ts +71 -0
- package/src/components/hooks/useLatest.tsx +8 -0
- package/src/components/hooks/useLazyLatest.ts +23 -0
- package/src/components/hooks/useMemoShallowObjectMerge.ts +16 -0
- package/src/components/hooks/useMounted.ts +18 -0
- package/src/components/hooks/useOnMount.ts +21 -0
- package/src/components/hooks/useOnScroll.ts +34 -0
- package/src/components/hooks/useOnce.ts +10 -0
- package/src/components/hooks/useOverlay/index.tsx +418 -0
- package/src/components/hooks/usePrevious.ts +8 -0
- package/src/components/hooks/usePropertyOld.ts +168 -0
- package/src/components/hooks/useRerender.ts +12 -0
- package/src/components/hooks/xuseProperty.ts +165 -0
- package/src/components/types/CellPosition.ts +4 -0
- package/src/components/types/NonUndefined.ts +1 -0
- package/src/components/types/RemoveObject.ts +22 -0
- package/src/components/types/Renderable.ts +3 -0
- package/src/components/types/ScrollPosition.ts +7 -0
- package/src/components/types/Setter.ts +2 -0
- package/src/components/types/Size.ts +6 -0
- package/src/components/types/SubscriptionCallback.ts +10 -0
- package/src/components/types/VoidFn.ts +1 -0
- package/src/components/utils/buildSubscriptionCallback.tsx +60 -0
- package/src/components/utils/clamp.ts +3 -0
- package/src/components/utils/debounce.ts +16 -0
- package/src/components/utils/decamelize.ts +11 -0
- package/src/components/utils/discardCallsWithEqualArg.ts +42 -0
- package/src/components/utils/getRenderIndexes.ts +33 -0
- package/src/components/utils/getScrollbarWidth.ts +31 -0
- package/src/components/utils/getVirtualRenderCount.ts +21 -0
- package/src/components/utils/isControlled.ts +12 -0
- package/src/components/utils/isControlledValue.ts +5 -0
- package/src/index.tsx +34 -0
- package/src/test.tsx +18 -0
- package/src/utils/DeepMap/index.ts +588 -0
- package/src/utils/DeepMap/once.ts +16 -0
- package/src/utils/DeepMap/sortAscending.ts +1 -0
- package/src/utils/DeepMap/tsconfig.deepmap.json +17 -0
- package/src/utils/DeepMap/xpackage.json +26 -0
- package/src/utils/debug.ts +40 -0
- package/src/utils/deepClone.ts +38 -0
- package/src/utils/getGlobal.ts +3 -0
- package/src/utils/groupAndPivot/defaultToKey.ts +3 -0
- package/src/utils/groupAndPivot/getGroupKeysForDataItem.ts +19 -0
- package/src/utils/groupAndPivot/getPivotColumnsAndColumnGroups.ts +361 -0
- package/src/utils/groupAndPivot/index.ts +1229 -0
- package/src/utils/groupAndPivot/types.ts +10 -0
- package/src/utils/join.ts +4 -0
- package/src/utils/mathIntersection.ts +21 -0
- package/src/utils/minified.d.ts +2 -0
- package/src/utils/minified.js +6 -0
- package/src/utils/multisort/index.ts +151 -0
- package/src/utils/multisort/sortTypes.ts +25 -0
- package/src/utils/notes-status.md +92 -0
- package/src/utils/pageGeometry/ConvexPoly.ts +34 -0
- package/src/utils/pageGeometry/Point.ts +51 -0
- package/src/utils/pageGeometry/PolyWithPoints.ts +52 -0
- package/src/utils/pageGeometry/Rectangle.ts +98 -0
- package/src/utils/pageGeometry/Triangle.ts +16 -0
- package/src/utils/pageGeometry/alignment/index.ts +221 -0
- package/src/utils/pageGeometry/polyContainsPoint.ts +182 -0
- package/src/utils/pageGeometry/types.ts +3 -0
- package/src/utils/proxyFnCall.ts +46 -0
- package/src/utils/raf.ts +13 -0
- package/src/utils/selectParent.ts +18 -0
- package/src/utils/shallowEqualObjects.ts +49 -0
- package/src/utils/stripVar.ts +3 -0
- package/src/utils/toUpperFirst.ts +5 -0
- package/todo.md +2 -0
- package/tsconfig.build.json +8 -0
- package/tsconfig.dev.json +7 -0
- package/tsconfig.json +30 -0
- package/tsconfig.typebundler.json +13 -0
- package/typedoc.json +4 -0
- package/README.md +0 -104
- package/index.css +0 -857
- package/index.d.ts +0 -2111
- package/index.esm.css +0 -857
- package/index.esm.js +0 -13695
- package/index.js +0 -13565
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useState,
|
|
3
|
+
useRef,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useEffect,
|
|
6
|
+
useCallback,
|
|
7
|
+
} from 'react';
|
|
8
|
+
import { toUpperFirst } from '../../utils/toUpperFirst';
|
|
9
|
+
import { AllPropertiesOrNone } from '../InfiniteTable/types/Utility';
|
|
10
|
+
import { Setter } from '../types/Setter';
|
|
11
|
+
import { isControlled } from '../utils/isControlled';
|
|
12
|
+
import { isControlledValue } from '../utils/isControlledValue';
|
|
13
|
+
import { useLatest } from './useLatest';
|
|
14
|
+
|
|
15
|
+
import { usePrevious } from './usePrevious';
|
|
16
|
+
|
|
17
|
+
const DEFAULT_CONFIG = {
|
|
18
|
+
controlledToState: true,
|
|
19
|
+
defaultValue: undefined,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function useProperty<V extends keyof T_PROPS, T_PROPS, NORMALIZED>(
|
|
23
|
+
propName: V,
|
|
24
|
+
props: T_PROPS,
|
|
25
|
+
config: AllPropertiesOrNone<{
|
|
26
|
+
fromState?: () => NORMALIZED;
|
|
27
|
+
setState?: (v: NORMALIZED) => void;
|
|
28
|
+
}> & {
|
|
29
|
+
defaultValue?: T_PROPS[V];
|
|
30
|
+
|
|
31
|
+
normalize?: (v?: NORMALIZED | T_PROPS[V]) => NORMALIZED;
|
|
32
|
+
onControlledChange?: (n: NORMALIZED, v: NORMALIZED | T_PROPS[V]) => void;
|
|
33
|
+
controlledToState?: boolean;
|
|
34
|
+
} = {
|
|
35
|
+
normalize: (v?: NORMALIZED | T_PROPS[V]): NORMALIZED => {
|
|
36
|
+
return v as any as NORMALIZED;
|
|
37
|
+
},
|
|
38
|
+
controlledToState: DEFAULT_CONFIG.controlledToState,
|
|
39
|
+
},
|
|
40
|
+
): [NORMALIZED, Setter<NORMALIZED | T_PROPS[V]>] {
|
|
41
|
+
config = config ?? DEFAULT_CONFIG;
|
|
42
|
+
const propsRef = useRef<T_PROPS>(props);
|
|
43
|
+
|
|
44
|
+
const getConfig = useLatest(config);
|
|
45
|
+
|
|
46
|
+
const getNormalized = (v?: NORMALIZED | T_PROPS[V]): NORMALIZED => {
|
|
47
|
+
const fn =
|
|
48
|
+
getConfig().normalize ??
|
|
49
|
+
((v?: NORMALIZED | T_PROPS[V]): NORMALIZED => {
|
|
50
|
+
return v as any as NORMALIZED;
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return fn(v);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const controlledToState =
|
|
57
|
+
config.controlledToState ?? DEFAULT_CONFIG.controlledToState;
|
|
58
|
+
|
|
59
|
+
const upperPropName = toUpperFirst(propName as string);
|
|
60
|
+
const defaultPropName = `default${upperPropName}` as V;
|
|
61
|
+
|
|
62
|
+
const defaultValue =
|
|
63
|
+
props[defaultPropName] !== undefined
|
|
64
|
+
? props[defaultPropName]
|
|
65
|
+
: config.defaultValue;
|
|
66
|
+
|
|
67
|
+
let [stateValue, setStateValue] = useState<NORMALIZED>(() => {
|
|
68
|
+
let val = getNormalized(defaultValue);
|
|
69
|
+
const config = getConfig();
|
|
70
|
+
|
|
71
|
+
if (val === undefined && config && config.fromState) {
|
|
72
|
+
val = config.fromState();
|
|
73
|
+
}
|
|
74
|
+
return val;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
if (config && config.fromState) {
|
|
78
|
+
stateValue = config.fromState();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const propValue = props[propName];
|
|
82
|
+
|
|
83
|
+
const controlled: boolean = isControlled(propName, props);
|
|
84
|
+
const storeInState = !controlled || controlledToState;
|
|
85
|
+
|
|
86
|
+
const value: NORMALIZED = !controlled ? stateValue : getNormalized(propValue);
|
|
87
|
+
|
|
88
|
+
const setState = useCallback(
|
|
89
|
+
(
|
|
90
|
+
value: NORMALIZED | T_PROPS[V],
|
|
91
|
+
beforeSetState?: (
|
|
92
|
+
normalizedValue: NORMALIZED,
|
|
93
|
+
value: NORMALIZED | T_PROPS[V],
|
|
94
|
+
) => void,
|
|
95
|
+
) => {
|
|
96
|
+
const config = getConfig();
|
|
97
|
+
const normalizedValue: NORMALIZED = getNormalized(value);
|
|
98
|
+
|
|
99
|
+
if (beforeSetState) {
|
|
100
|
+
beforeSetState(normalizedValue, value);
|
|
101
|
+
}
|
|
102
|
+
if (config && config.setState) {
|
|
103
|
+
config.setState(normalizedValue);
|
|
104
|
+
} else {
|
|
105
|
+
setStateValue(normalizedValue);
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
[setStateValue],
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const setValue = useCallback(
|
|
112
|
+
(
|
|
113
|
+
val:
|
|
114
|
+
| (NORMALIZED | T_PROPS[V])
|
|
115
|
+
| ((prevVal: NORMALIZED | T_PROPS[V]) => NORMALIZED | T_PROPS[V]),
|
|
116
|
+
): void => {
|
|
117
|
+
// const latestProps: T_PROPS = propsRef.current;
|
|
118
|
+
const latestValue: NORMALIZED = valueRef.current;
|
|
119
|
+
// const currentProps: T_PROPS = latestProps;
|
|
120
|
+
|
|
121
|
+
const newValue: T_PROPS[V] | NORMALIZED =
|
|
122
|
+
val instanceof Function ? val(latestValue) : val;
|
|
123
|
+
|
|
124
|
+
if (storeInState) {
|
|
125
|
+
setState(newValue);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// const callbackPropName = `on${upperPropName}Change` as string;
|
|
129
|
+
// const callbackProp = (currentProps as any)[callbackPropName] as Function;
|
|
130
|
+
|
|
131
|
+
// if (typeof callbackProp === 'function') {
|
|
132
|
+
// callbackProp(newValue);
|
|
133
|
+
// }
|
|
134
|
+
},
|
|
135
|
+
[setState, storeInState],
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
const valueRef = useRef<NORMALIZED>(value);
|
|
139
|
+
|
|
140
|
+
useLayoutEffect(() => {
|
|
141
|
+
propsRef.current = props;
|
|
142
|
+
valueRef.current = value;
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
const latestProps = propsRef.current;
|
|
147
|
+
const propValue = latestProps[propName];
|
|
148
|
+
if (isControlledValue(propValue) && config?.controlledToState) {
|
|
149
|
+
setState(propValue, config.onControlledChange);
|
|
150
|
+
}
|
|
151
|
+
}, [propValue, setState]);
|
|
152
|
+
|
|
153
|
+
const prevStateValue = usePrevious(stateValue);
|
|
154
|
+
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
const currentProps = propsRef.current;
|
|
157
|
+
const callbackPropName = `on${upperPropName}Change` as string;
|
|
158
|
+
const callbackProp = (currentProps as any)[callbackPropName] as Function;
|
|
159
|
+
|
|
160
|
+
if (typeof callbackProp === 'function' && stateValue !== prevStateValue) {
|
|
161
|
+
callbackProp(stateValue);
|
|
162
|
+
}
|
|
163
|
+
}, [stateValue, prevStateValue]);
|
|
164
|
+
|
|
165
|
+
return [value, setValue];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export default useProperty;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
export const useRerender = (): [number, () => void] => {
|
|
4
|
+
const [state, setState] = useState(0);
|
|
5
|
+
|
|
6
|
+
return [
|
|
7
|
+
state,
|
|
8
|
+
useCallback(() => {
|
|
9
|
+
setState((state) => state + 1);
|
|
10
|
+
}, [setState]),
|
|
11
|
+
];
|
|
12
|
+
};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/*eslint-disable */
|
|
2
|
+
import { useEffect, useCallback, useRef } from 'react';
|
|
3
|
+
|
|
4
|
+
import { toUpperFirst } from '../../utils/toUpperFirst';
|
|
5
|
+
// import { AllPropertiesOrNone } from '../InfiniteTable/types/Utility';
|
|
6
|
+
import { Setter } from '../types/Setter';
|
|
7
|
+
import { isControlled } from '../utils/isControlled';
|
|
8
|
+
import { isControlledValue } from '../utils/isControlledValue';
|
|
9
|
+
import { useLatest } from './useLatest';
|
|
10
|
+
import { usePrevious } from './usePrevious';
|
|
11
|
+
|
|
12
|
+
export const notifyChange = (props: any, propName: string, newValue: any) => {
|
|
13
|
+
const upperPropName = toUpperFirst(propName);
|
|
14
|
+
const callbackPropName = `on${upperPropName}Change` as string;
|
|
15
|
+
const callbackProp = props[callbackPropName] as Function;
|
|
16
|
+
|
|
17
|
+
if (typeof callbackProp === 'function') {
|
|
18
|
+
callbackProp(newValue);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function useProperty<V extends keyof T_PROPS, T_PROPS, NORMALIZED>(
|
|
23
|
+
propName: V,
|
|
24
|
+
props: T_PROPS,
|
|
25
|
+
config: {
|
|
26
|
+
fromState: (defaultValue?: NORMALIZED) => NORMALIZED;
|
|
27
|
+
setState: (v: NORMALIZED) => void;
|
|
28
|
+
} & {
|
|
29
|
+
defaultValue?: T_PROPS[V];
|
|
30
|
+
|
|
31
|
+
normalize?: (v?: NORMALIZED | T_PROPS[V]) => NORMALIZED;
|
|
32
|
+
onControlledChange?: (n: NORMALIZED, v: NORMALIZED | T_PROPS[V]) => void;
|
|
33
|
+
},
|
|
34
|
+
): [NORMALIZED, Setter<NORMALIZED | T_PROPS[V]>] {
|
|
35
|
+
const getConfig = useLatest(config);
|
|
36
|
+
const getProps = useLatest(props);
|
|
37
|
+
|
|
38
|
+
// eslint-disable-next-line
|
|
39
|
+
if (__DEV__) {
|
|
40
|
+
const prevName = usePrevious(propName);
|
|
41
|
+
if (prevName !== propName) {
|
|
42
|
+
throw `useProperty hook: Cannot change managed property! Changed from "${prevName}" to "${propName}".`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const getNormalized = (v?: NORMALIZED | T_PROPS[V]): NORMALIZED => {
|
|
47
|
+
const fn =
|
|
48
|
+
getConfig().normalize ??
|
|
49
|
+
((v?: NORMALIZED | T_PROPS[V]): NORMALIZED => {
|
|
50
|
+
return v as any as NORMALIZED;
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return fn(v);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const stateValue = config.fromState();
|
|
57
|
+
const propValue = props[propName];
|
|
58
|
+
const controlled: boolean = isControlled(propName, props);
|
|
59
|
+
|
|
60
|
+
const value: NORMALIZED = !controlled ? stateValue : getNormalized(propValue);
|
|
61
|
+
|
|
62
|
+
const getValue = useLatest(value);
|
|
63
|
+
|
|
64
|
+
const notify = (newValue: any) => {
|
|
65
|
+
notifyChange(getProps(), propName as string, newValue);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const setValue = useCallback(
|
|
69
|
+
(
|
|
70
|
+
val:
|
|
71
|
+
| (NORMALIZED | T_PROPS[V])
|
|
72
|
+
| ((prevVal: NORMALIZED | T_PROPS[V]) => NORMALIZED | T_PROPS[V]),
|
|
73
|
+
): void => {
|
|
74
|
+
const latestValue: NORMALIZED = getValue();
|
|
75
|
+
const config = getConfig();
|
|
76
|
+
|
|
77
|
+
let newValue: T_PROPS[V] | NORMALIZED =
|
|
78
|
+
val instanceof Function ? val(latestValue) : val;
|
|
79
|
+
|
|
80
|
+
newValue = getNormalized(newValue);
|
|
81
|
+
|
|
82
|
+
if (isControlled(propName, getProps())) {
|
|
83
|
+
// internalCacheRef.current = newValue; // this line is to avoid another notification later when state changes
|
|
84
|
+
// config.setState(newValue);
|
|
85
|
+
// CASE 2
|
|
86
|
+
notify(newValue);
|
|
87
|
+
} else {
|
|
88
|
+
// CASE 3
|
|
89
|
+
skipNotifyRef.current = newValue; // this line is to avoid another notification later when state changes
|
|
90
|
+
config.setState(newValue);
|
|
91
|
+
notify(newValue);
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
[],
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
/***
|
|
98
|
+
CONTROLLED:
|
|
99
|
+
1 - controlled change = config.setState + SKIP notify!!!
|
|
100
|
+
2 - controlled prop + setValue = DO notify + step 1
|
|
101
|
+
3 - controlled prop is same + internal state change (from config.fromState) = DO notify
|
|
102
|
+
|
|
103
|
+
UNCONTROLLED:
|
|
104
|
+
- uncontrolled is same + config.fromState() value changes = DO notify
|
|
105
|
+
- uncontrolled + setValue = config.setState + DO NOTIFY
|
|
106
|
+
|
|
107
|
+
SUMMARY:
|
|
108
|
+
CASE 1 - controlled change = config.setState + SKIP notify
|
|
109
|
+
CASE 2 - controlled prop + setValue = DO notify + step 1
|
|
110
|
+
CASE 3 - uncontrolled prop + setValue = config.setState + DO notify = config.setState + step 4
|
|
111
|
+
CASE 4 - controlled/uncontrolled prop is same + config.fromState changes = DO NOTIFY
|
|
112
|
+
|
|
113
|
+
ALSO, there is another case we need to be aware of, but maybe we don't have to handle it here:
|
|
114
|
+
|
|
115
|
+
controlled prop that doesn't update, but we call actions.setColumnOrder([...]), which should theoretically modify the internal state
|
|
116
|
+
but actually it shouldn't, as it should be aware that it's a controlled prop, so should just notify
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
const initialRenderRef = useRef(true);
|
|
120
|
+
const skipNotifyRef = useRef<NORMALIZED>();
|
|
121
|
+
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
// CASE 1
|
|
124
|
+
// const initialRender = initialRenderRef.current;
|
|
125
|
+
// if (initialRender) {
|
|
126
|
+
// return;
|
|
127
|
+
// }
|
|
128
|
+
|
|
129
|
+
const props = getProps();
|
|
130
|
+
const propValue = props[propName];
|
|
131
|
+
const isControlled = isControlledValue(propValue);
|
|
132
|
+
if (isControlled) {
|
|
133
|
+
const normalizedValue = getNormalized(propValue);
|
|
134
|
+
skipNotifyRef.current = normalizedValue;
|
|
135
|
+
getConfig().setState(normalizedValue);
|
|
136
|
+
}
|
|
137
|
+
}, [propValue]);
|
|
138
|
+
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
const initialRender = initialRenderRef.current;
|
|
141
|
+
if (initialRender) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (stateValue === skipNotifyRef.current) {
|
|
145
|
+
// we had a change triggered by controlled change so we skip notify
|
|
146
|
+
// part of CASE 1
|
|
147
|
+
skipNotifyRef.current = undefined;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
skipNotifyRef.current = undefined;
|
|
151
|
+
// CASE 4
|
|
152
|
+
notify(stateValue);
|
|
153
|
+
}, [stateValue]);
|
|
154
|
+
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
initialRenderRef.current = false;
|
|
157
|
+
}, []);
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* WE NEED TO ALWAYS RETURN the value from STATE!!!
|
|
161
|
+
*/
|
|
162
|
+
return [stateValue, setValue];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export { useProperty };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type NonUndefined<T> = T extends undefined ? never : T;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
|
|
3
|
+
We have this utility - it's used in Menu Renderable declaration
|
|
4
|
+
because:
|
|
5
|
+
|
|
6
|
+
type Renderable = React.ReactNode;
|
|
7
|
+
|
|
8
|
+
const x: Renderable = <div />;
|
|
9
|
+
const y: Renderable = {};
|
|
10
|
+
|
|
11
|
+
we don't want to allow the above `y` as a valid declaration
|
|
12
|
+
|
|
13
|
+
it breaks some types in the Menu component as well
|
|
14
|
+
|
|
15
|
+
*/
|
|
16
|
+
export type RemoveObject<T> = T extends null | undefined
|
|
17
|
+
? T
|
|
18
|
+
: T extends unknown
|
|
19
|
+
? keyof T extends never
|
|
20
|
+
? never
|
|
21
|
+
: T
|
|
22
|
+
: never;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { VoidFn } from './VoidFn';
|
|
2
|
+
|
|
3
|
+
export type SubscriptionCallbackOnChangeFn<T> = (node: T | null) => void;
|
|
4
|
+
|
|
5
|
+
export interface SubscriptionCallback<T> {
|
|
6
|
+
(node: T, callback?: () => void): void;
|
|
7
|
+
get: () => T | null;
|
|
8
|
+
destroy: VoidFn;
|
|
9
|
+
onChange: (fn: SubscriptionCallbackOnChangeFn<T>) => VoidFn;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type VoidFn = () => void;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SubscriptionCallbackOnChangeFn,
|
|
3
|
+
SubscriptionCallback,
|
|
4
|
+
} from '../types/SubscriptionCallback';
|
|
5
|
+
|
|
6
|
+
export function buildSubscriptionCallback<T>(
|
|
7
|
+
withRaf = false,
|
|
8
|
+
): SubscriptionCallback<T> {
|
|
9
|
+
let lastCallValue: T | null = null;
|
|
10
|
+
let fns: SubscriptionCallbackOnChangeFn<T>[] = [];
|
|
11
|
+
|
|
12
|
+
let rafId: number | null = null;
|
|
13
|
+
|
|
14
|
+
const updater = (items: T, callback?: (results: any[]) => void) => {
|
|
15
|
+
const results: any[] = [];
|
|
16
|
+
if (withRaf) {
|
|
17
|
+
if (rafId != null) {
|
|
18
|
+
cancelAnimationFrame(rafId);
|
|
19
|
+
rafId = null;
|
|
20
|
+
}
|
|
21
|
+
requestAnimationFrame(() => {
|
|
22
|
+
lastCallValue = items;
|
|
23
|
+
rafId = null;
|
|
24
|
+
|
|
25
|
+
// change happens here
|
|
26
|
+
for (let i = 0, len = fns.length; i < len; i++) {
|
|
27
|
+
results.push(fns[i](items));
|
|
28
|
+
}
|
|
29
|
+
callback?.(results);
|
|
30
|
+
});
|
|
31
|
+
} else {
|
|
32
|
+
lastCallValue = items;
|
|
33
|
+
|
|
34
|
+
// change happens here
|
|
35
|
+
for (let i = 0, len = fns.length; i < len; i++) {
|
|
36
|
+
results.push(fns[i](items));
|
|
37
|
+
}
|
|
38
|
+
callback?.(results);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
updater.get = () => lastCallValue;
|
|
43
|
+
|
|
44
|
+
// this attaches a new listener to changes
|
|
45
|
+
updater.onChange = (fn: SubscriptionCallbackOnChangeFn<T>) => {
|
|
46
|
+
fns.push(fn);
|
|
47
|
+
return () => {
|
|
48
|
+
fns = fns.filter((f) => f !== fn);
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
updater.destroy = () => {
|
|
53
|
+
updater(null as any as T);
|
|
54
|
+
fns.length = 0;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
updater.getListenersCount = () => fns.length;
|
|
58
|
+
|
|
59
|
+
return updater;
|
|
60
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function debounce(fn: Function, { wait }: { wait: number }) {
|
|
2
|
+
let timeout: number | null = null;
|
|
3
|
+
|
|
4
|
+
return function (...args: any[]) {
|
|
5
|
+
//@ts-ignore
|
|
6
|
+
const context = this;
|
|
7
|
+
|
|
8
|
+
if (timeout !== null) {
|
|
9
|
+
clearTimeout(timeout);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
timeout = setTimeout(() => {
|
|
13
|
+
fn.apply(context, args);
|
|
14
|
+
}, wait) as any as number;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function decamelize(str: string, options?: { separator: string }) {
|
|
2
|
+
const { separator } = options ?? { separator: '-' };
|
|
3
|
+
|
|
4
|
+
return str
|
|
5
|
+
.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
|
|
6
|
+
.replace(
|
|
7
|
+
new RegExp('(' + separator + '[A-Z])([A-Z])', 'g'),
|
|
8
|
+
'$1' + separator + '$2',
|
|
9
|
+
)
|
|
10
|
+
.toLowerCase();
|
|
11
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { shallowEqualObjects } from '../../utils/shallowEqualObjects';
|
|
2
|
+
|
|
3
|
+
const map = new WeakMap<Function, { lastCallTime: number; lastCallArg: any }>();
|
|
4
|
+
|
|
5
|
+
const getCompareDefault = (a: any) => a;
|
|
6
|
+
|
|
7
|
+
export function discardCallsWithEqualArg<T = any>(
|
|
8
|
+
fn: (arg: T) => void,
|
|
9
|
+
timeframe = 50,
|
|
10
|
+
getCompareObject?: (arg: T) => any,
|
|
11
|
+
): (arg: T) => void {
|
|
12
|
+
map.set(fn, {
|
|
13
|
+
lastCallTime: 0,
|
|
14
|
+
lastCallArg: null,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const getCompare = getCompareObject ?? getCompareDefault;
|
|
18
|
+
|
|
19
|
+
return (arg: T) => {
|
|
20
|
+
const { lastCallTime, lastCallArg } = map.get(fn) || {
|
|
21
|
+
lastCallTime: 0,
|
|
22
|
+
lastCallArg: null,
|
|
23
|
+
};
|
|
24
|
+
const now = Date.now();
|
|
25
|
+
const diff = now - lastCallTime;
|
|
26
|
+
const objectsEqual = shallowEqualObjects(
|
|
27
|
+
getCompare(lastCallArg),
|
|
28
|
+
getCompare(arg),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
if (diff < timeframe && objectsEqual) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
map.set(fn, {
|
|
36
|
+
lastCallTime: now,
|
|
37
|
+
lastCallArg: arg,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
fn(arg);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export const getRenderIndexes = (info: {
|
|
2
|
+
count: number;
|
|
3
|
+
renderStartIndex: number;
|
|
4
|
+
renderCount: number;
|
|
5
|
+
}) => {
|
|
6
|
+
let { count, renderStartIndex, renderCount } = info;
|
|
7
|
+
|
|
8
|
+
renderCount = Math.min(renderCount, count);
|
|
9
|
+
|
|
10
|
+
const renderEndIndex = Math.max(
|
|
11
|
+
-1,
|
|
12
|
+
Math.min(renderStartIndex + renderCount - 1, count - 1),
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
if (renderStartIndex >= renderEndIndex) {
|
|
16
|
+
if (renderEndIndex + 1 - renderCount >= 0) {
|
|
17
|
+
renderStartIndex = renderEndIndex + 1 - renderCount;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (renderEndIndex < 0 || renderStartIndex < 0) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const result = {
|
|
26
|
+
renderStartIndex,
|
|
27
|
+
renderEndIndex,
|
|
28
|
+
renderCount,
|
|
29
|
+
count,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { getGlobal } from '../../utils/getGlobal';
|
|
2
|
+
|
|
3
|
+
let scrollbarWidth: number;
|
|
4
|
+
|
|
5
|
+
export function getScrollbarWidth(): number {
|
|
6
|
+
if (scrollbarWidth != null) {
|
|
7
|
+
return scrollbarWidth;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const doc = getGlobal().document ?? null;
|
|
11
|
+
|
|
12
|
+
if (!doc) {
|
|
13
|
+
return 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const outer = doc.createElement('div');
|
|
17
|
+
outer.style.visibility = 'hidden';
|
|
18
|
+
outer.style.overflow = 'scroll';
|
|
19
|
+
doc.body.appendChild(outer);
|
|
20
|
+
|
|
21
|
+
// Creating inner element and placing it in the container
|
|
22
|
+
const inner = doc.createElement('div');
|
|
23
|
+
outer.appendChild(inner);
|
|
24
|
+
|
|
25
|
+
// Calculating difference between container's full width and the child width
|
|
26
|
+
scrollbarWidth = outer.offsetWidth - inner.offsetWidth;
|
|
27
|
+
|
|
28
|
+
outer.parentNode?.removeChild(outer);
|
|
29
|
+
|
|
30
|
+
return scrollbarWidth;
|
|
31
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type Options = {
|
|
2
|
+
size: { width: number; height: number };
|
|
3
|
+
mainAxis: 'horizontal' | 'vertical';
|
|
4
|
+
itemMainAxisSize: number | ((itemIndex: number) => number);
|
|
5
|
+
itemMainAxisMinSize: number;
|
|
6
|
+
};
|
|
7
|
+
export const getVirtualRenderCount = (options: Options) => {
|
|
8
|
+
const { size, mainAxis, itemMainAxisSize, itemMainAxisMinSize } = options;
|
|
9
|
+
|
|
10
|
+
const renderSize = Math.round(
|
|
11
|
+
mainAxis === 'vertical' ? size.height : size.width,
|
|
12
|
+
);
|
|
13
|
+
let itemSize =
|
|
14
|
+
typeof itemMainAxisSize !== 'function'
|
|
15
|
+
? itemMainAxisSize
|
|
16
|
+
: itemMainAxisMinSize;
|
|
17
|
+
|
|
18
|
+
const renderCount = Math.ceil(renderSize / itemSize) + 1;
|
|
19
|
+
|
|
20
|
+
return renderCount;
|
|
21
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { isControlledValue } from './isControlledValue';
|
|
2
|
+
|
|
3
|
+
export function isControlled<V extends keyof T, T>(
|
|
4
|
+
propName: V,
|
|
5
|
+
props: T,
|
|
6
|
+
): boolean {
|
|
7
|
+
const propValue = props[propName];
|
|
8
|
+
|
|
9
|
+
const controlled: boolean = isControlledValue(propValue);
|
|
10
|
+
|
|
11
|
+
return controlled;
|
|
12
|
+
}
|