@kne/table-page 0.1.4 → 0.1.6

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.
@@ -18,7 +18,6 @@ import findLastIndex from 'lodash/findLastIndex';
18
18
  import isEqual from 'lodash/isEqual';
19
19
  import { useDebouncedCallback } from 'use-debounce';
20
20
  import { ReactSortable } from 'react-sortablejs';
21
- import set from 'lodash/set';
22
21
  import cloneDeep from 'lodash/cloneDeep';
23
22
  import { createWithIntlProvider, useIntl } from '@kne/react-intl';
24
23
  import filterZhCN from '@kne/react-filter/dist/locale/zh-CN';
@@ -1336,6 +1335,20 @@ const useSort = (props = {}) => {
1336
1335
  };
1337
1336
  useSort.sortDataSource = sortDataSource;
1338
1337
 
1338
+ const getColumnConfig = (config, columnName, field, defaultValue) => {
1339
+ const columnConfig = config == null ? void 0 : config[columnName];
1340
+ if (columnConfig != null && columnConfig[field] !== undefined) {
1341
+ return columnConfig[field];
1342
+ }
1343
+ const legacyValue = get(config, `${columnName}.${field}`);
1344
+ return legacyValue === undefined ? defaultValue : legacyValue;
1345
+ };
1346
+ const setColumnConfig = (config, columnName, updates) => {
1347
+ return Object.assign({}, config, {
1348
+ [columnName]: Object.assign({}, config == null ? void 0 : config[columnName], updates)
1349
+ });
1350
+ };
1351
+
1339
1352
  var configStyle = {"cell-resize-bar":"kne-table-page_PNaAP","table-changer-setting":"kne-table-page_B0U-O","is-active":"kne-table-page_vHrQo","table-config-header":"kne-table-page_Zp8MN","table-config-header-title":"kne-table-page_Pc6jR","columns-control-content":"kne-table-page_saJmr","columns-control-content-title":"kne-table-page_YmHni","columns-control-content-scroller":"kne-table-page_rgqWB","columns-control-sortable-list":"kne-table-page_JRcBW","columns-control-content-footer":"kne-table-page_nLwW1","columns-control-content-input":"kne-table-page_9qyJs","columns-control-content-list":"kne-table-page_Sslzr","columns-control-content-item":"kne-table-page_7CamD","is-drag":"kne-table-page_X18rq","columns-control-content-item-icon":"kne-table-page_Eab-e","columns-control-overlay":"kne-table-page_eT4HV","header-col":"kne-table-page_bjDly","header-cell-inner":"kne-table-page_9FKgD"};
1340
1353
 
1341
1354
  const locale$1 = {
@@ -1408,9 +1421,9 @@ const ColumnsControlContent = withLocale(({
1408
1421
  if (item.fixed === true || item.fixed === 'left') {
1409
1422
  return 'leftFixedColumns';
1410
1423
  }
1411
- return get(config, `${item.name}.visible`) !== true && item.hidden || get(config, `${item.name}.visible`) === false ? 'hiddenColumns' : 'visibleColumns';
1424
+ return getColumnConfig(config, item.name, 'visible') !== true && item.hidden || getColumnConfig(config, item.name, 'visible') === false ? 'hiddenColumns' : 'visibleColumns';
1412
1425
  })), (result, value, key) => {
1413
- result[key] = value.sort((a, b) => get(config, `${a.name}.rank`, 0) - get(config, `${b.name}.rank`, 0));
1426
+ result[key] = value.sort((a, b) => getColumnConfig(config, a.name, 'rank', 0) - getColumnConfig(config, b.name, 'rank', 0));
1414
1427
  }, {});
1415
1428
  }, [columns, config]);
1416
1429
  const sortableVisibleColumns = useMemo(() => {
@@ -1422,10 +1435,14 @@ const ColumnsControlContent = withLocale(({
1422
1435
  const newConfig = cloneDeep(config);
1423
1436
  const columnsList = [].concat(columnsState.leftFixedColumns, columnsState.visibleColumns, columnsState.rightFixedColumns);
1424
1437
  (columnsState.hiddenColumns || []).forEach(col => {
1425
- set(newConfig, `${col.name}.visible`, false);
1438
+ Object.assign(newConfig, setColumnConfig(newConfig, col.name, {
1439
+ visible: false
1440
+ }));
1426
1441
  });
1427
1442
  columnsList.forEach((col, index) => {
1428
- set(newConfig, `${col.name}.rank`, index + 1);
1443
+ Object.assign(newConfig, setColumnConfig(newConfig, col.name, {
1444
+ rank: index + 1
1445
+ }));
1429
1446
  });
1430
1447
  onChange(newConfig);
1431
1448
  };
@@ -1768,8 +1785,8 @@ const useTableConfig = ({
1768
1785
  }
1769
1786
  }, [name, _controllerOpen, _tableServerApis]);
1770
1787
  const visibleColumns = useMemo(() => {
1771
- return columns.filter(col => !(get(config, `${col.name}.visible`) === false || get(config, `${col.name}.visible`) !== true && col.hidden === true)).sort((a, b) => {
1772
- const computedIndex = item => get(config, `${item.name}.rank`, 0) + (item.fixed === 'left' || item.fixed === true ? -10000 : 0) + (item.fixed === 'right' ? 10000 : 0);
1788
+ return columns.filter(col => !(getColumnConfig(config, col.name, 'visible') === false || getColumnConfig(config, col.name, 'visible') !== true && col.hidden === true)).sort((a, b) => {
1789
+ const computedIndex = item => getColumnConfig(config, item.name, 'rank', 0) + (item.fixed === 'left' || item.fixed === true ? -10000 : 0) + (item.fixed === 'right' ? 10000 : 0);
1773
1790
  return computedIndex(a) - computedIndex(b);
1774
1791
  });
1775
1792
  }, [columns, config]);
@@ -1783,7 +1800,7 @@ const useTableConfig = ({
1783
1800
  const {
1784
1801
  width
1785
1802
  } = getColumnSize(col);
1786
- const contentWidth = get(config, `${col.name}.width`) || width;
1803
+ const contentWidth = getColumnConfig(config, col.name, 'width') || width;
1787
1804
  return sum + withConfigColumnExtra(contentWidth, index, columnCount, _controllerOpen);
1788
1805
  }, 0);
1789
1806
  }, [visibleColumns, config, columnCount, _controllerOpen]);
@@ -1806,7 +1823,7 @@ const useTableConfig = ({
1806
1823
  min,
1807
1824
  max
1808
1825
  } = getColumnSize(column);
1809
- const currentWidth = Math.min(Math.max(get(config, `${column.name}.width`) || width, min), max);
1826
+ const currentWidth = Math.min(Math.max(getColumnConfig(config, column.name, 'width') || width, min), max);
1810
1827
  return {
1811
1828
  currentWidth,
1812
1829
  min,
@@ -3080,29 +3097,47 @@ TableView.useSelectedRow = useSelectedRow;
3080
3097
  TableView.useSort = useSort;
3081
3098
  TableView.sortDataSource = useSort.sortDataSource;
3082
3099
 
3083
- var style$1 = {"table-page":"kne-table-page_ut9rJ","table-content":"kne-table-page_dxN6N","table-with-toolbar":"kne-table-page_sf06r","has-summary":"kne-table-page_IRdzb","table-in-toolbar":"kne-table-page_sawIT","pagination":"kne-table-page_DSTEf","total_text":"kne-table-page_pJhKr","floating-scrollbar":"kne-table-page_iu8E1","floating-scrollbar-thumb":"kne-table-page_RjZsu","is-moving":"kne-table-page_y-4Ou"};
3100
+ var style$1 = {"table-page":"kne-table-page_ut9rJ","table-content":"kne-table-page_dxN6N","table-with-toolbar":"kne-table-page_sf06r","has-summary":"kne-table-page_IRdzb","table-in-toolbar":"kne-table-page_sawIT","pagination":"kne-table-page_DSTEf","total_text":"kne-table-page_pJhKr","floating-scrollbar":"kne-table-page_iu8E1","floating-scrollbar-embedded":"kne-table-page_PIv-C","floating-scrollbar-thumb":"kne-table-page_RjZsu","is-moving":"kne-table-page_y-4Ou"};
3084
3101
 
3085
3102
  const BAR_HEIGHT = 15;
3086
3103
  const THUMB_MARGIN = 2;
3087
3104
  const computeBarMetrics = (scrollEl, viewportState, getPortalContainer) => {
3088
3105
  const rect = scrollEl.getBoundingClientRect();
3089
3106
  const portalContainer = typeof getPortalContainer === 'function' ? getPortalContainer() : null;
3090
- const useContainerScroll = !isDocumentScrollContainer(portalContainer);
3091
- const viewport = getViewportRect();
3092
- const anchorRect = useContainerScroll ? portalContainer.getBoundingClientRect() : viewport;
3107
+ const useEmbeddedPlacement = !isDocumentScrollContainer(portalContainer);
3093
3108
  const trackWidth = rect.width;
3094
3109
  const thumbWidth = Math.max(trackWidth * scrollEl.clientWidth / scrollEl.scrollWidth - THUMB_MARGIN * 2, 24);
3095
3110
  const maxThumbOffset = trackWidth - thumbWidth - THUMB_MARGIN * 2;
3096
3111
  const scrollRatio = scrollEl.scrollWidth > scrollEl.clientWidth ? scrollEl.scrollLeft / (scrollEl.scrollWidth - scrollEl.clientWidth) : 0;
3112
+ const visible = shouldShowFloatingScrollbar(scrollEl, viewportState, getPortalContainer);
3113
+ if (useEmbeddedPlacement) {
3114
+ const containerRect = portalContainer.getBoundingClientRect();
3115
+ return {
3116
+ placement: 'embedded',
3117
+ offsetLeft: Math.round(rect.left - containerRect.left),
3118
+ width: Math.round(trackWidth),
3119
+ thumbWidth,
3120
+ thumbLeft: THUMB_MARGIN + maxThumbOffset * scrollRatio,
3121
+ visible
3122
+ };
3123
+ }
3124
+ const viewport = getViewportRect();
3097
3125
  return {
3098
- left: rect.left,
3099
- width: trackWidth,
3100
- bottom: window.innerHeight - anchorRect.bottom,
3126
+ placement: 'fixed',
3127
+ left: Math.round(rect.left),
3128
+ width: Math.round(trackWidth),
3129
+ bottom: window.innerHeight - viewport.bottom,
3101
3130
  thumbWidth,
3102
3131
  thumbLeft: THUMB_MARGIN + maxThumbOffset * scrollRatio,
3103
- visible: shouldShowFloatingScrollbar(scrollEl, viewportState, getPortalContainer)
3132
+ visible
3104
3133
  };
3105
3134
  };
3135
+ const metricsEqual = (prev, next) => {
3136
+ if (!prev || !next) {
3137
+ return prev === next;
3138
+ }
3139
+ return prev.placement === next.placement && prev.visible === next.visible && prev.width === next.width && prev.thumbWidth === next.thumbWidth && prev.thumbLeft === next.thumbLeft && (prev.placement === 'embedded' ? prev.offsetLeft === next.offsetLeft : prev.left === next.left && prev.bottom === next.bottom);
3140
+ };
3106
3141
  const FloatingScrollBar = ({
3107
3142
  metrics,
3108
3143
  onThumbDrag,
@@ -3137,10 +3172,18 @@ const FloatingScrollBar = ({
3137
3172
  if (!(metrics != null && metrics.visible)) {
3138
3173
  return null;
3139
3174
  }
3140
- const portalTarget = document.body;
3175
+ const portalContainer = typeof getPortalContainer === 'function' ? getPortalContainer() : null;
3176
+ const useEmbeddedPlacement = metrics.placement === 'embedded';
3177
+ const portalTarget = useEmbeddedPlacement && portalContainer ? portalContainer : document.body;
3141
3178
  return /*#__PURE__*/createPortal(/*#__PURE__*/jsx("div", {
3142
- className: classnames(style$1['floating-scrollbar'], 'table-page-floating-scrollbar'),
3143
- style: {
3179
+ className: classnames(style$1['floating-scrollbar'], 'table-page-floating-scrollbar', {
3180
+ [style$1['floating-scrollbar-embedded']]: useEmbeddedPlacement
3181
+ }),
3182
+ style: useEmbeddedPlacement ? {
3183
+ marginLeft: metrics.offsetLeft,
3184
+ width: metrics.width,
3185
+ height: BAR_HEIGHT
3186
+ } : {
3144
3187
  left: metrics.left,
3145
3188
  width: metrics.width,
3146
3189
  height: BAR_HEIGHT,
@@ -3172,6 +3215,7 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3172
3215
  const containerRef = useRef(null);
3173
3216
  const scrollElRef = useRef(null);
3174
3217
  const viewportStateRef = useRef(null);
3218
+ const metricsRef = useRef(null);
3175
3219
  const setContainerRef = useRefCallback(node => {
3176
3220
  containerRef.current = node;
3177
3221
  if (typeof forwardedRef === 'function') {
@@ -3183,10 +3227,16 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3183
3227
  const updateMetrics = useRefCallback(() => {
3184
3228
  const scrollEl = scrollElRef.current;
3185
3229
  if (!scrollEl) {
3230
+ metricsRef.current = null;
3186
3231
  setMetrics(null);
3187
3232
  return;
3188
3233
  }
3189
- setMetrics(computeBarMetrics(scrollEl, viewportStateRef.current, getPortalContainer));
3234
+ const nextMetrics = computeBarMetrics(scrollEl, viewportStateRef.current, getPortalContainer);
3235
+ if (metricsEqual(metricsRef.current, nextMetrics)) {
3236
+ return;
3237
+ }
3238
+ metricsRef.current = nextMetrics;
3239
+ setMetrics(nextMetrics);
3190
3240
  });
3191
3241
  const handleThumbDrag = useRefCallback(deltaX => {
3192
3242
  const scrollEl = scrollElRef.current;
@@ -3207,6 +3257,7 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3207
3257
  if (!_enabled) {
3208
3258
  scrollElRef.current = null;
3209
3259
  viewportStateRef.current = null;
3260
+ metricsRef.current = null;
3210
3261
  setMetrics(null);
3211
3262
  return undefined;
3212
3263
  }
@@ -3217,6 +3268,8 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3217
3268
  let scrollEl = null;
3218
3269
  let unobserveViewport = null;
3219
3270
  let contentResizeObserver = null;
3271
+ const portalContainer = typeof getPortalContainer === 'function' ? getPortalContainer() : null;
3272
+ const useEmbeddedPlacement = !isDocumentScrollContainer(portalContainer);
3220
3273
  const detachScrollEl = () => {
3221
3274
  var _contentResizeObserve;
3222
3275
  unobserveViewport == null || unobserveViewport();
@@ -3238,10 +3291,12 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3238
3291
  detachScrollEl();
3239
3292
  scrollEl = nextScrollEl;
3240
3293
  scrollElRef.current = scrollEl;
3241
- unobserveViewport = observeViewportIntersection(scrollEl, state => {
3242
- viewportStateRef.current = state;
3243
- updateMetrics();
3244
- });
3294
+ if (!useEmbeddedPlacement) {
3295
+ unobserveViewport = observeViewportIntersection(scrollEl, state => {
3296
+ viewportStateRef.current = state;
3297
+ updateMetrics();
3298
+ });
3299
+ }
3245
3300
  scrollEl.addEventListener('scroll', updateMetrics, {
3246
3301
  passive: true
3247
3302
  });
@@ -3258,16 +3313,38 @@ const HorizontalScroller = /*#__PURE__*/forwardRef(({
3258
3313
  const containerResizeObserver = new ResizeObserver(onLayoutChange);
3259
3314
  containerResizeObserver.observe(root);
3260
3315
  onLayoutChange();
3261
- const portalContainer = typeof getPortalContainer === 'function' ? getPortalContainer() : null;
3262
- if (!isDocumentScrollContainer(portalContainer)) {
3316
+ const onWindowChange = () => {
3317
+ if (!useEmbeddedPlacement) {
3318
+ updateMetrics();
3319
+ }
3320
+ };
3321
+ if (!useEmbeddedPlacement) {
3322
+ var _window$visualViewpor, _window$visualViewpor2;
3323
+ window.addEventListener('scroll', onWindowChange, true);
3324
+ window.addEventListener('resize', onWindowChange);
3325
+ (_window$visualViewpor = window.visualViewport) == null || _window$visualViewpor.addEventListener('resize', onWindowChange);
3326
+ (_window$visualViewpor2 = window.visualViewport) == null || _window$visualViewpor2.addEventListener('scroll', onWindowChange);
3327
+ }
3328
+ if (useEmbeddedPlacement && portalContainer) {
3263
3329
  portalContainer.addEventListener('scroll', updateMetrics, {
3264
3330
  passive: true
3265
3331
  });
3332
+ window.addEventListener('resize', updateMetrics);
3266
3333
  }
3267
3334
  return () => {
3268
3335
  detachScrollEl();
3269
3336
  containerResizeObserver.disconnect();
3270
- portalContainer == null || portalContainer.removeEventListener('scroll', updateMetrics);
3337
+ if (!useEmbeddedPlacement) {
3338
+ var _window$visualViewpor3, _window$visualViewpor4;
3339
+ window.removeEventListener('scroll', onWindowChange, true);
3340
+ window.removeEventListener('resize', onWindowChange);
3341
+ (_window$visualViewpor3 = window.visualViewport) == null || _window$visualViewpor3.removeEventListener('resize', onWindowChange);
3342
+ (_window$visualViewpor4 = window.visualViewport) == null || _window$visualViewpor4.removeEventListener('scroll', onWindowChange);
3343
+ }
3344
+ if (useEmbeddedPlacement && portalContainer) {
3345
+ portalContainer.removeEventListener('scroll', updateMetrics);
3346
+ window.removeEventListener('resize', updateMetrics);
3347
+ }
3271
3348
  };
3272
3349
  }, [_enabled, updateMetrics, getPortalContainer]);
3273
3350
  return /*#__PURE__*/jsxs(Fragment, {