@kne/table-page 0.1.19 → 0.1.23

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.
@@ -1157,7 +1157,7 @@ const TreeExpandContent = ({
1157
1157
 
1158
1158
  const _excluded$3 = ["className", "dataSource", "columns", "rowKey", "rowSelection", "valueIsEmpty", "emptyIsPlaceholder", "placeholder", "empty", "onRowSelect", "render", "renderMobile", "context", "sticky", "scrollTopInset", "stickyOffset", "getStickyContainer", "headerStyle", "pagination", "sortRender", "mobileSortToolbar", "name", "controllerOpen", "tableServerApis", "scroll", "summary", "size", "dataType", "parentKey", "childrenKey", "hasChildrenKey", "treeTitleKey", "onLoadChildren", "expandedKeys", "defaultExpandedKeys", "onExpandedKeysChange", "indentSize"],
1159
1159
  _excluded2$2 = ["y"],
1160
- _excluded3$1 = ["getContainer"];
1160
+ _excluded3$2 = ["getContainer"];
1161
1161
  const mapJustifyToAlign = justify => {
1162
1162
  if (justify === 'center') {
1163
1163
  return 'center';
@@ -1870,7 +1870,7 @@ const Table = p => {
1870
1870
  }
1871
1871
  const config = typeof sticky === 'object' ? Object.assign({}, sticky) : {};
1872
1872
  if (hasScrollY) {
1873
- const scrollSticky = _objectWithoutPropertiesLoose(config, _excluded3$1);
1873
+ const scrollSticky = _objectWithoutPropertiesLoose(config, _excluded3$2);
1874
1874
  return Object.assign({
1875
1875
  offsetHeader: 0
1876
1876
  }, scrollSticky);
@@ -2322,11 +2322,11 @@ const resolveToolbarButtonGroupProps = (buttonGroup, getPopupContainer) => {
2322
2322
  });
2323
2323
  };
2324
2324
 
2325
- var style = {"kne-responsive-boundary":"kne-table-page_u90lj","kne-responsive-scroll":"kne-table-page_bSj1H","table-toolbar-section":"kne-table-page_I21Xn","has-value-display":"kne-table-page_COnMY","table-toolbar":"kne-table-page_x6mv7","table-toolbar-value-display":"kne-table-page_vbwMg","table-toolbar-batch":"kne-table-page_k8-Ku","table-toolbar-batch-btn":"kne-table-page_gZtbT","table-toolbar-batch-clear-btn":"kne-table-page_o6Oml","table-toolbar-actions":"kne-table-page_t3-57","table-toolbar-button-group":"kne-table-page_2sdSC","table-toolbar-search":"kne-table-page_pB5en","table-page-tabs":"kne-table-page_UTy3R","is-mobile-render":"kne-table-page_J-AEV","is-mobile":"kne-table-page_n9eAS","table-toolbar-mobile-actions":"kne-table-page_I--A1","table-toolbar-mobile-filter-row":"kne-table-page_Qo5Zl","table-toolbar-filter":"kne-table-page_Jyfm7","has-mobile-search":"kne-table-page_14SJt","table-toolbar-search-row":"kne-table-page_zuGJv","has-mobile-search-only":"kne-table-page_Y5IR1","has-tabs":"kne-table-page_EImBh","table-toolbar-search-input":"kne-table-page_t60hK","table-toolbar-divider":"kne-table-page_z--2N","table-toolbar-filter-outer":"kne-table-page_PiWGM","table-toolbar-filter-inner":"kne-table-page_AX3aK"};
2325
+ var style = {"kne-responsive-boundary":"kne-table-page_u90lj","kne-responsive-scroll":"kne-table-page_bSj1H","table-toolbar-section":"kne-table-page_I21Xn","has-value-display":"kne-table-page_COnMY","table-toolbar":"kne-table-page_x6mv7","table-toolbar-value-display":"kne-table-page_vbwMg","table-toolbar-batch":"kne-table-page_k8-Ku","table-toolbar-batch-btn":"kne-table-page_gZtbT","table-toolbar-batch-clear-btn":"kne-table-page_o6Oml","table-toolbar-actions":"kne-table-page_t3-57","table-toolbar-button-group":"kne-table-page_2sdSC","table-toolbar-search":"kne-table-page_pB5en","table-toolbar-search-input":"kne-table-page_t60hK","table-page-tabs":"kne-table-page_UTy3R","is-mobile-render":"kne-table-page_J-AEV","is-mobile":"kne-table-page_n9eAS","table-toolbar-mobile-actions":"kne-table-page_I--A1","table-toolbar-mobile-filter-row":"kne-table-page_Qo5Zl","table-toolbar-filter":"kne-table-page_Jyfm7","has-mobile-search":"kne-table-page_14SJt","table-toolbar-search-row":"kne-table-page_zuGJv","has-mobile-search-only":"kne-table-page_Y5IR1","has-tabs":"kne-table-page_EImBh","table-toolbar-divider":"kne-table-page_z--2N","table-toolbar-filter-outer":"kne-table-page_PiWGM","table-toolbar-filter-inner":"kne-table-page_AX3aK"};
2326
2326
 
2327
2327
  const _excluded$1 = ["getPopupContainer"],
2328
- _excluded2$1 = ["list", "displayLine", "extraExpand", "className"],
2329
- _excluded3 = ["className", "style"];
2328
+ _excluded2$1 = ["list", "displayLine", "extraExpand", "className", "searchParamsValue", "mapFilterValue", "value", "defaultValue", "onChange"],
2329
+ _excluded3$1 = ["className", "style"];
2330
2330
  const TAB_ALL_KEY = '__all__';
2331
2331
  const getTabActiveKey = (filterValue, tabName) => {
2332
2332
  var _entry$value;
@@ -2539,7 +2539,7 @@ const TableToolbar = ({
2539
2539
  className: searchClassName,
2540
2540
  style: searchStyle
2541
2541
  } = _ref3,
2542
- searchRest = _objectWithoutPropertiesLoose(_ref3, _excluded3);
2542
+ searchRest = _objectWithoutPropertiesLoose(_ref3, _excluded3$1);
2543
2543
 
2544
2544
  // tab 已有选中态展示,已选筛选标签中不再重复显示 tab 的值
2545
2545
  const displayFilterValue = hasTab ? (filterValue || []).filter(item => (item == null ? void 0 : item.name) !== tab.name) : filterValue || [];
@@ -2557,12 +2557,15 @@ const TableToolbar = ({
2557
2557
  bordered: false,
2558
2558
  variant: "filled",
2559
2559
  size: isMobile ? 'middle' : 'small',
2560
- className: classnames(style['table-toolbar-search-input'], searchClassName),
2560
+ className: classnames(style['table-toolbar-search-input'], searchClassName)
2561
+ }, searchRest, {
2561
2562
  style: isMobile ? _extends({}, searchStyle, {
2562
2563
  width: '100%',
2563
2564
  maxWidth: '100%'
2564
- }) : searchStyle
2565
- }, searchRest)) : null;
2565
+ }) : _extends({}, searchStyle, {
2566
+ width: 180
2567
+ })
2568
+ })) : null;
2566
2569
  const buttonGroupNode = showButtonGroup ? /*#__PURE__*/jsx("div", {
2567
2570
  className: style['table-toolbar-button-group'],
2568
2571
  children: /*#__PURE__*/jsx(ButtonGroup, _extends({}, resolveToolbarButtonGroupProps(buttonGroup, getPopupContainer)))
@@ -2642,6 +2645,7 @@ const TableToolbar = ({
2642
2645
  [style['is-mobile-render']]: isMobileRender
2643
2646
  }),
2644
2647
  children: /*#__PURE__*/jsx(FilterValueDisplay, {
2648
+ hideLabel: true,
2645
2649
  value: displayFilterValue,
2646
2650
  onChange: handleValueDisplayChange,
2647
2651
  extraExpand: extraExpand,
@@ -2652,8 +2656,175 @@ const TableToolbar = ({
2652
2656
  });
2653
2657
  };
2654
2658
 
2659
+ /**
2660
+ * 与 @kne/react-filter useSearchParamsValue 内部解析保持一致(不从该包导出)。
2661
+ *
2662
+ * fields 项:`name` 为 URL key;`label` 为筛选项标题;可选 `labelKey` 为选中值展示文案的 URL key。
2663
+ *
2664
+ * @param {Object} [options]
2665
+ * @param {URLSearchParams} [options.searchParams]
2666
+ * @param {Array<{ name: string, label?: string, labelKey?: string }>} [options.fields]
2667
+ * @returns {{ items: Array, consumedKeys: string[] }}
2668
+ */
2669
+ const parseSearchParamsValue = ({
2670
+ searchParams,
2671
+ fields
2672
+ } = {}) => {
2673
+ const items = [];
2674
+ const consumedKeys = [];
2675
+ if (!searchParams || !Array.isArray(fields) || fields.length === 0) {
2676
+ return {
2677
+ items,
2678
+ consumedKeys
2679
+ };
2680
+ }
2681
+ fields.forEach(field => {
2682
+ const name = field == null ? void 0 : field.name;
2683
+ if (!name || !searchParams.has(name)) {
2684
+ return;
2685
+ }
2686
+ const raw = searchParams.get(name);
2687
+ if (raw == null || raw === '') {
2688
+ return;
2689
+ }
2690
+ consumedKeys.push(name);
2691
+ let valueText = raw;
2692
+ const labelKey = field.labelKey;
2693
+ if (labelKey && searchParams.has(labelKey)) {
2694
+ const fromLabelKey = searchParams.get(labelKey);
2695
+ if (fromLabelKey != null && fromLabelKey !== '') {
2696
+ valueText = fromLabelKey;
2697
+ consumedKeys.push(labelKey);
2698
+ }
2699
+ }
2700
+ const label = field.label != null ? field.label : name;
2701
+ items.push({
2702
+ name,
2703
+ label,
2704
+ value: {
2705
+ label: valueText,
2706
+ value: raw
2707
+ }
2708
+ });
2709
+ });
2710
+ return {
2711
+ items,
2712
+ consumedKeys
2713
+ };
2714
+ };
2715
+
2716
+ /**
2717
+ * 按 name 合并筛选数组;同名时 fromUrl 覆盖 base。
2718
+ * @param {Array} base
2719
+ * @param {Array} fromUrl
2720
+ * @returns {Array}
2721
+ */
2722
+ const mergeFilterByName = (base = [], fromUrl = []) => {
2723
+ if (!fromUrl.length) {
2724
+ return Array.isArray(base) ? [...base] : [];
2725
+ }
2726
+ const map = new Map();
2727
+ (base || []).forEach(item => {
2728
+ if (item != null && item.name) {
2729
+ map.set(item.name, item);
2730
+ }
2731
+ });
2732
+ fromUrl.forEach(item => {
2733
+ if (item != null && item.name) {
2734
+ map.set(item.name, item);
2735
+ }
2736
+ });
2737
+ return Array.from(map.values());
2738
+ };
2739
+
2740
+ /**
2741
+ * @param {URLSearchParams} searchParams
2742
+ * @param {string[]} consumedKeys
2743
+ * @returns {URLSearchParams|null}
2744
+ */
2745
+ const stripConsumedUrlParams = (searchParams, consumedKeys) => {
2746
+ if (!(consumedKeys != null && consumedKeys.length)) {
2747
+ return null;
2748
+ }
2749
+ const next = new URLSearchParams(searchParams);
2750
+ let changed = false;
2751
+ consumedKeys.forEach(key => {
2752
+ if (next.has(key)) {
2753
+ next.delete(key);
2754
+ changed = true;
2755
+ }
2756
+ });
2757
+ return changed ? next : null;
2758
+ };
2759
+ const parsePositiveInt = raw => {
2760
+ if (raw == null || raw === '') {
2761
+ return undefined;
2762
+ }
2763
+ const num = Number(raw);
2764
+ if (!Number.isFinite(num) || num <= 0) {
2765
+ return undefined;
2766
+ }
2767
+ return Math.floor(num);
2768
+ };
2769
+
2770
+ /**
2771
+ * 从 URL 读取分页状态(非法数字忽略)。
2772
+ * @param {URLSearchParams} [searchParams]
2773
+ * @param {{ currentName?: string, pageSizeName?: string }} [options]
2774
+ * @returns {{ current?: number, pageSize?: number }}
2775
+ */
2776
+ const parsePaginationSearchParams = (searchParams, {
2777
+ currentName: _currentName = 'currentPage',
2778
+ pageSizeName: _pageSizeName = 'perPage'
2779
+ } = {}) => {
2780
+ const result = {};
2781
+ if (!searchParams) {
2782
+ return result;
2783
+ }
2784
+ const current = parsePositiveInt(searchParams.get(_currentName));
2785
+ const pageSize = parsePositiveInt(searchParams.get(_pageSizeName));
2786
+ if (current != null) {
2787
+ result.current = current;
2788
+ }
2789
+ if (pageSize != null) {
2790
+ result.pageSize = pageSize;
2791
+ }
2792
+ return result;
2793
+ };
2794
+
2795
+ /**
2796
+ * 写入分页参数到 URL,保留其它 query。
2797
+ * @param {URLSearchParams} [searchParams]
2798
+ * @param {{ current?: number, pageSize?: number, currentName?: string, pageSizeName?: string }} options
2799
+ * @returns {URLSearchParams}
2800
+ */
2801
+ const patchPaginationSearchParams = (searchParams, {
2802
+ current,
2803
+ pageSize,
2804
+ currentName: _currentName2 = 'currentPage',
2805
+ pageSizeName: _pageSizeName2 = 'perPage'
2806
+ } = {}) => {
2807
+ const next = new URLSearchParams(searchParams);
2808
+ if (current != null) {
2809
+ next.set(_currentName2, String(current));
2810
+ }
2811
+ if (pageSize != null) {
2812
+ next.set(_pageSizeName2, String(pageSize));
2813
+ }
2814
+ return next;
2815
+ };
2816
+
2817
+ /**
2818
+ * @param {object} [pagination]
2819
+ * @returns {boolean}
2820
+ */
2821
+ const isPaginationSearchParamsEnabled = pagination => {
2822
+ return !!(pagination != null && pagination.searchParams && typeof pagination.setSearchParams === 'function');
2823
+ };
2824
+
2655
2825
  const _excluded = ["data", "refresh", "reload", "requestParams", "fetchProps", "isComplete", "loadMore", "send", "dataFormat", "className", "columns", "getColumns", "pagination", "columnRenderProps", "summary", "sticky", "scrollTopInset", "stickyOffset", "renderType", "horizontalScroller", "getScrollContainer", "filter", "search", "tab", "tabProps", "batchActions", "buttonGroup", "selectedRows", "rowSelection", "renderMobile", "renderCard", "forceCard", "mobileSortToolbar"],
2656
- _excluded2 = ["pagination", "horizontalScroller", "getScrollContainer"];
2826
+ _excluded2 = ["pagination", "horizontalScroller", "getScrollContainer", "filter"],
2827
+ _excluded3 = ["searchParamsValue"];
2657
2828
  const defaultMergeList = (data, newData) => {
2658
2829
  return Object.assign({}, newData, {
2659
2830
  pageData: [...((data == null ? void 0 : data.pageData) || []), ...((newData == null ? void 0 : newData.pageData) || [])]
@@ -2861,8 +3032,24 @@ const TablePageInnerContent = withLocale(_ref => {
2861
3032
  preferToolbar: hasToolbar
2862
3033
  });
2863
3034
  });
3035
+ const syncPaginationToUrl = useRefCallback((page, size) => {
3036
+ if (!isPaginationSearchParamsEnabled(pagination)) {
3037
+ return;
3038
+ }
3039
+ const nextParams = patchPaginationSearchParams(pagination.searchParams, {
3040
+ current: page,
3041
+ pageSize: size,
3042
+ currentName: pagination.currentName,
3043
+ pageSizeName: pagination.pageSizeName
3044
+ });
3045
+ pagination.setSearchParams(nextParams, {
3046
+ replace: true
3047
+ });
3048
+ });
2864
3049
  const handleFilterChange = useRefCallback(value => {
2865
3050
  setFilterValue(value);
3051
+ const currentSize = Number(get(requestParams, [pagination.paramsType, pagination.pageSizeName], pagination.pageSize)) || pagination.pageSize || 20;
3052
+ syncPaginationToUrl(1, currentSize);
2866
3053
  reload({
2867
3054
  [pagination.paramsType]: buildRequestParamsWithFilter(value, {
2868
3055
  [pagination.currentName]: 1
@@ -2878,17 +3065,19 @@ const TablePageInnerContent = withLocale(_ref => {
2878
3065
  }, [isComplete, data, scrollTable]);
2879
3066
  const handlePaginationChange = useRefCallback((page, size) => {
2880
3067
  pendingScrollRef.current = true;
3068
+ const nextSize = Number(size);
3069
+ const currentPage = get(requestParams, [pagination.paramsType, pagination.currentName], 1);
3070
+ const currentSize = Number(get(requestParams, [pagination.paramsType, pagination.pageSizeName], pagination.pageSize)) || pagination.pageSize || 20;
2881
3071
  if (typeof pagination.onChange === 'function') {
3072
+ syncPaginationToUrl(page, nextSize || currentSize);
2882
3073
  pagination.onChange(page, size);
2883
3074
  return;
2884
3075
  }
2885
- const nextSize = Number(size);
2886
- const currentPage = get(requestParams, [pagination.paramsType, pagination.currentName], 1);
2887
- const currentSize = Number(get(requestParams, [pagination.paramsType, pagination.pageSizeName], pagination.pageSize)) || pagination.pageSize || 20;
2888
3076
  if (nextSize !== currentSize) {
2889
3077
  pagination.onShowSizeChange && pagination.onShowSizeChange(page, nextSize);
2890
3078
  }
2891
3079
  if (page !== currentPage || nextSize !== currentSize) {
3080
+ syncPaginationToUrl(page, nextSize);
2892
3081
  (pagination.requestType === 'refresh' ? refresh : reload)({
2893
3082
  [pagination.paramsType]: buildRequestParamsWithFilter(filterValue, {
2894
3083
  [pagination.currentName]: page,
@@ -3102,7 +3291,8 @@ const TablePage = /*#__PURE__*/forwardRef((_ref2, ref) => {
3102
3291
  let {
3103
3292
  pagination,
3104
3293
  horizontalScroller = true,
3105
- getScrollContainer
3294
+ getScrollContainer,
3295
+ filter: filterProp
3106
3296
  } = _ref2,
3107
3297
  props = _objectWithoutPropertiesLoose(_ref2, _excluded2);
3108
3298
  pagination = Object.assign({}, {
@@ -3118,32 +3308,88 @@ const TablePage = /*#__PURE__*/forwardRef((_ref2, ref) => {
3118
3308
  }, pagination);
3119
3309
  const pageSizeKey = `${(props.name || 'common').toUpperCase()}_TABLE_PAGE_SIZE`;
3120
3310
  const cachePageSize = pagination.cachePageSize !== false;
3311
+ const paginationUrlEnabled = isPaginationSearchParamsEnabled(pagination);
3312
+ const [urlPagination] = useState(() => paginationUrlEnabled ? parsePaginationSearchParams(pagination.searchParams, {
3313
+ currentName: pagination.currentName,
3314
+ pageSizeName: pagination.pageSizeName
3315
+ }) : {});
3121
3316
  const [pageSize, setPageSize] = useState(() => {
3122
- var _ref3;
3123
- return (_ref3 = cachePageSize ? readPageSize(pageSizeKey) : null) != null ? _ref3 : pagination.pageSize;
3317
+ var _ref3, _urlPagination$pageSi;
3318
+ return (_ref3 = (_urlPagination$pageSi = urlPagination.pageSize) != null ? _urlPagination$pageSi : cachePageSize ? readPageSize(pageSizeKey) : null) != null ? _ref3 : pagination.pageSize;
3319
+ });
3320
+ const [initialCurrent] = useState(() => {
3321
+ if (urlPagination.current != null) {
3322
+ return urlPagination.current;
3323
+ }
3324
+ const fromProps = Number(get(props[pagination.paramsType], pagination.currentName));
3325
+ return Number.isFinite(fromProps) && fromProps > 0 ? fromProps : 1;
3124
3326
  });
3327
+ const [searchParamsSnapshot] = useState(() => parseSearchParamsValue(filterProp == null ? void 0 : filterProp.searchParamsValue));
3328
+ const {
3329
+ items: fromUrl,
3330
+ consumedKeys
3331
+ } = searchParamsSnapshot;
3332
+ const filter = useMemo(() => {
3333
+ if (!filterProp) {
3334
+ return filterProp;
3335
+ }
3336
+ const rest = _objectWithoutPropertiesLoose(filterProp, _excluded3);
3337
+ if ('value' in filterProp) {
3338
+ return rest;
3339
+ }
3340
+ return Object.assign({}, rest, {
3341
+ defaultValue: mergeFilterByName(filterProp.defaultValue || [], fromUrl)
3342
+ });
3343
+ }, [filterProp, fromUrl]);
3125
3344
  const params = props[pagination.paramsType];
3126
3345
  const filterDefaultParams = useMemo(() => {
3127
- const filter = props.filter;
3128
- if (!filter) {
3346
+ var _ref4, _ref5;
3347
+ if (!filterProp && !filter) {
3129
3348
  return {};
3130
3349
  }
3131
- const initialValue = resolveInitialFilterValue(filter);
3132
- const mapFilterValue = filter.mapFilterValue || getFilterValue;
3133
- // 有 mapFilterValue 时即使 initialValue 为空也要跑(注入 namespace 等)
3134
- if (!initialValue.length && !filter.mapFilterValue) {
3350
+ const mapFilterValue = ((_ref4 = filter || filterProp) == null ? void 0 : _ref4.mapFilterValue) || getFilterValue;
3351
+ let initialValue;
3352
+ if (filterProp && 'value' in filterProp) {
3353
+ // 受控:不改 value;首包用 value ∪ fromUrl 作请求种子
3354
+ initialValue = mergeFilterByName(filterProp.value || [], fromUrl);
3355
+ } else {
3356
+ initialValue = resolveInitialFilterValue(filter);
3357
+ }
3358
+ if (!initialValue.length && !((_ref5 = filter || filterProp) != null && _ref5.mapFilterValue)) {
3135
3359
  return {};
3136
3360
  }
3137
3361
  return mapFilterValue(initialValue);
3138
- }, [props.filter]);
3362
+ }, [filter, filterProp, fromUrl]);
3363
+ const urlStrippedRef = useRef(false);
3364
+ useEffect(() => {
3365
+ var _filterProp$searchPar, _filterProp$searchPar2;
3366
+ if (urlStrippedRef.current) {
3367
+ return;
3368
+ }
3369
+ urlStrippedRef.current = true;
3370
+ const setSearchParams = filterProp == null || (_filterProp$searchPar = filterProp.searchParamsValue) == null ? void 0 : _filterProp$searchPar.setSearchParams;
3371
+ const searchParams = filterProp == null || (_filterProp$searchPar2 = filterProp.searchParamsValue) == null ? void 0 : _filterProp$searchPar2.searchParams;
3372
+ if (typeof setSearchParams !== 'function') {
3373
+ return;
3374
+ }
3375
+ const nextParams = stripConsumedUrlParams(searchParams, consumedKeys);
3376
+ if (nextParams) {
3377
+ setSearchParams(nextParams, {
3378
+ replace: true
3379
+ });
3380
+ }
3381
+ }, [filterProp, consumedKeys]);
3139
3382
  const fetchParams = useMemo(() => {
3140
3383
  return {
3141
- [pagination.paramsType]: Object.assign({}, params, filterDefaultParams, {
3384
+ [pagination.paramsType]: Object.assign({}, params, filterDefaultParams, _extends({}, paginationUrlEnabled ? {
3385
+ [pagination.currentName]: initialCurrent
3386
+ } : null, {
3142
3387
  [pagination.pageSizeName]: pageSize
3143
- })
3388
+ }))
3144
3389
  };
3145
- }, [params, pagination.pageSizeName, pagination.paramsType, pageSize, filterDefaultParams]);
3390
+ }, [params, pagination.pageSizeName, pagination.paramsType, pagination.currentName, pageSize, filterDefaultParams, paginationUrlEnabled, initialCurrent]);
3146
3391
  return /*#__PURE__*/jsx(TablePageInner, _extends({}, props, fetchParams, {
3392
+ filter: filter,
3147
3393
  horizontalScroller: horizontalScroller,
3148
3394
  getScrollContainer: getScrollContainer,
3149
3395
  pagination: Object.assign({}, pagination, {