@bigbinary/neeto-atoms 1.0.121 → 1.0.123

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.
@@ -1,6 +1,6 @@
1
1
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
2
  import * as React from 'react';
3
- import { useState, useCallback, useEffect, useMemo, useRef, useLayoutEffect } from 'react';
3
+ import { useMemo, useState, useCallback, useEffect, useRef, useLayoutEffect } from 'react';
4
4
  import { TableRow, TableCell, Table, TableHeader, TableHead, TableBody } from './primitives/Table.js';
5
5
  import { c as cn } from './utils-BJnb9o5c.js';
6
6
  import { h as hyphenize } from './hyphenize-TRhMI0tl.js';
@@ -3161,28 +3161,26 @@ const STORAGE_KEY_PREFIX = "NEETOUI";
3161
3161
  const STORAGE_KEY_SUFFIX = "FIXED_COLUMNS";
3162
3162
  const COLUMN_SIZING_STORAGE_KEY_SUFFIX = "COLUMN_SIZING";
3163
3163
 
3164
- const getSearchParams = () => new URLSearchParams(window.location.search);
3165
- const readSortFromURL = () => {
3166
- const params = getSearchParams();
3164
+ const readSortFromURL = (search) => {
3165
+ const params = new URLSearchParams(search);
3167
3166
  const sortBy = params.get(URL_SORT_PARAMS.SORT_BY);
3168
3167
  const orderBy = params.get(URL_SORT_PARAMS.ORDER_BY);
3169
- if (!sortBy) return [];
3170
- return [{ id: sortBy, desc: orderBy === SORT_DIRECTIONS.DESC }];
3168
+ return sortBy ? [{ id: sortBy, desc: orderBy === SORT_DIRECTIONS.DESC }] : [];
3171
3169
  };
3172
- const writeSortToURL = (sorting) => {
3173
- const url = new URL(window.location.href);
3170
+ const buildSortQuery = (search, sorting) => {
3171
+ const searchParams = new URLSearchParams(search);
3174
3172
  if (sorting.length === 0) {
3175
- url.searchParams.delete(URL_SORT_PARAMS.SORT_BY);
3176
- url.searchParams.delete(URL_SORT_PARAMS.ORDER_BY);
3173
+ searchParams.delete(URL_SORT_PARAMS.SORT_BY);
3174
+ searchParams.delete(URL_SORT_PARAMS.ORDER_BY);
3177
3175
  } else {
3178
3176
  const { id, desc } = sorting[0];
3179
- url.searchParams.set(URL_SORT_PARAMS.SORT_BY, id);
3180
- url.searchParams.set(
3177
+ searchParams.set(URL_SORT_PARAMS.SORT_BY, id);
3178
+ searchParams.set(
3181
3179
  URL_SORT_PARAMS.ORDER_BY,
3182
3180
  desc ? SORT_DIRECTIONS.DESC : SORT_DIRECTIONS.ASC
3183
3181
  );
3184
3182
  }
3185
- window.history.pushState({}, "", url.toString());
3183
+ return searchParams.toString();
3186
3184
  };
3187
3185
  const keysToRowSelection = (keys) => Object.fromEntries(keys.map((key) => [key, true]));
3188
3186
  const rowSelectionToKeys = (selection) => Object.keys(selection).filter((key) => selection[key]);
@@ -3282,17 +3280,22 @@ const useTableSort = ({
3282
3280
  sorting: controlledSorting,
3283
3281
  onSortingChange: controlledOnSortingChange,
3284
3282
  onSort,
3285
- enableURLSort = true
3283
+ enableURLSort
3286
3284
  }) => {
3287
3285
  const isControlled = controlledSorting !== void 0;
3288
- const [internalSorting, setInternalSorting] = useState(
3289
- () => enableURLSort ? readSortFromURL() : []
3290
- );
3291
- const sorting = isControlled ? controlledSorting : internalSorting;
3286
+ const isURLSortEnabled = enableURLSort ?? (!isControlled && controlledOnSortingChange === void 0 && !onSort);
3287
+ const history = useHistory();
3288
+ const location = useLocation();
3289
+ const sortingFromURL = useMemo(() => {
3290
+ if (!isURLSortEnabled) return [];
3291
+ return readSortFromURL(location.search);
3292
+ }, [location.search, isURLSortEnabled]);
3293
+ const [internalSorting, setInternalSorting] = useState([]);
3294
+ const sorting = isControlled ? controlledSorting : isURLSortEnabled ? sortingFromURL : internalSorting;
3292
3295
  const onSortingChange = useCallback(
3293
3296
  (updaterOrValue) => {
3294
3297
  const newSorting = typeof updaterOrValue === "function" ? updaterOrValue(sorting) : updaterOrValue;
3295
- if (!isControlled) {
3298
+ if (!isControlled && !isURLSortEnabled) {
3296
3299
  setInternalSorting(newSorting);
3297
3300
  }
3298
3301
  controlledOnSortingChange?.(updaterOrValue);
@@ -3304,18 +3307,21 @@ const useTableSort = ({
3304
3307
  } : null
3305
3308
  );
3306
3309
  }
3307
- if (enableURLSort) {
3308
- writeSortToURL(newSorting);
3310
+ if (isURLSortEnabled) {
3311
+ history.push({
3312
+ search: buildSortQuery(history.location.search, newSorting)
3313
+ });
3309
3314
  }
3310
3315
  },
3311
- [sorting, isControlled, controlledOnSortingChange, onSort, enableURLSort]
3316
+ [
3317
+ sorting,
3318
+ isControlled,
3319
+ controlledOnSortingChange,
3320
+ onSort,
3321
+ isURLSortEnabled,
3322
+ history
3323
+ ]
3312
3324
  );
3313
- useEffect(() => {
3314
- if (enableURLSort && !isControlled) {
3315
- const urlSorting = readSortFromURL();
3316
- setInternalSorting(urlSorting);
3317
- }
3318
- }, [enableURLSort, isControlled]);
3319
3325
  return { sorting, onSortingChange };
3320
3326
  };
3321
3327
 
@@ -4150,7 +4156,7 @@ const DataTable = ({
4150
4156
  onSortingChange: onSortingChangeProp,
4151
4157
  onSort: onSortProp,
4152
4158
  enableSorting = false,
4153
- enableURLSort = true,
4159
+ enableURLSort,
4154
4160
  totalCount,
4155
4161
  pageSize = DEFAULT_PAGE_SIZE,
4156
4162
  currentPage: currentPageProp,
@@ -4600,4 +4606,4 @@ const DataTable = ({
4600
4606
  };
4601
4607
 
4602
4608
  export { DataTable as D, useColumnPinning as a, useColumnVisibility as b, useTablePagination as c, useTableSelection as d, useTableSort as e, useColumnOrdering as u };
4603
- //# sourceMappingURL=DataTable-BNhOcmQ8.js.map
4609
+ //# sourceMappingURL=DataTable-DI4qPCmM.js.map