@cloud-ru/ds-table 2.2.3 → 2.2.4-preview-01f86b34.0
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/dist/cjs/components/Table/hooks/useColumnSettings/useColumnSettings.js +8 -8
- package/dist/esm/components/Table/hooks/useColumnSettings/useColumnSettings.js +10 -10
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +20 -20
- package/src/components/Table/hooks/useColumnSettings/useColumnSettings.ts +12 -11
|
@@ -11,23 +11,23 @@ function useColumnSettings({ columnDefinitions, columnsSettings, pinnedGroups, s
|
|
|
11
11
|
const localStorageKey = `${savedState?.id}_columnSettings`;
|
|
12
12
|
const configurableColumns = (0, react_1.useMemo)(() => columnDefinitions.filter(utils_3.isFilterableColumn), [columnDefinitions]);
|
|
13
13
|
const [enabledColumns, setEnabledColumns] = (0, react_1.useState)(() => (0, utils_3.getEnabledColumnsInitialState)(configurableColumns, savedState, localStorageKey));
|
|
14
|
+
const knownColumnIdsRef = (0, react_1.useRef)(configurableColumns.map(utils_2.getColumnIdentifier));
|
|
14
15
|
(0, react_1.useEffect)(() => {
|
|
16
|
+
const currentIds = configurableColumns.map(utils_2.getColumnIdentifier);
|
|
17
|
+
const knownIds = knownColumnIdsRef.current;
|
|
18
|
+
knownColumnIdsRef.current = currentIds;
|
|
19
|
+
const newColumns = configurableColumns.filter(col => !knownIds.includes((0, utils_2.getColumnIdentifier)(col)));
|
|
20
|
+
const addedIds = newColumns.length ? (0, utils_3.getEnabledColumnsInitialState)(newColumns, savedState, localStorageKey) : [];
|
|
15
21
|
setEnabledColumns(prev => {
|
|
16
|
-
const currentIds = configurableColumns.map(utils_2.getColumnIdentifier);
|
|
17
22
|
const kept = prev.filter(id => currentIds.includes(id));
|
|
18
|
-
const added =
|
|
19
|
-
.filter(id => !kept.includes(id))
|
|
20
|
-
.filter(id => {
|
|
21
|
-
const colDef = configurableColumns.find(col => (0, utils_2.getColumnIdentifier)(col) === id);
|
|
22
|
-
return colDef ? (0, utils_3.isColumnEnabledInitially)(colDef) : false;
|
|
23
|
-
});
|
|
23
|
+
const added = addedIds.filter(id => !kept.includes(id));
|
|
24
24
|
const reconciled = [...kept, ...added];
|
|
25
25
|
if (reconciled.length === prev.length && reconciled.every((id, i) => id === prev[i])) {
|
|
26
26
|
return prev;
|
|
27
27
|
}
|
|
28
28
|
return reconciled;
|
|
29
29
|
});
|
|
30
|
-
}, [configurableColumns]);
|
|
30
|
+
}, [configurableColumns, localStorageKey, savedState]);
|
|
31
31
|
const setEnabledColumnsOuter = (0, react_1.useCallback)((value) => {
|
|
32
32
|
if (savedState?.columnSettings && (0, utils_1.isBrowser)()) {
|
|
33
33
|
localStorage.setItem(localStorageKey, JSON.stringify(value));
|
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
import { isBrowser } from '@cloud-ru/ds-utils';
|
|
2
|
-
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
3
|
import { tableLocale } from '../../../../locale/index.js';
|
|
4
4
|
import { getColumnIdentifier, getTableColumnsDefinitions } from '../../utils/index.js';
|
|
5
|
-
import { getEnabledColumnsInitialState,
|
|
5
|
+
import { getEnabledColumnsInitialState, isFilterableColumn, prepareColumnsSettings, prepareReorderableColumnsSettings, } from './utils/index.js';
|
|
6
6
|
export function useColumnSettings({ columnDefinitions, columnsSettings, pinnedGroups, savedState, rowSelection, enableSelectPinned, expanding, masterSelection = {}, }) {
|
|
7
7
|
const { t } = tableLocale.useTranslations();
|
|
8
8
|
const localStorageKey = `${savedState?.id}_columnSettings`;
|
|
9
9
|
const configurableColumns = useMemo(() => columnDefinitions.filter(isFilterableColumn), [columnDefinitions]);
|
|
10
10
|
const [enabledColumns, setEnabledColumns] = useState(() => getEnabledColumnsInitialState(configurableColumns, savedState, localStorageKey));
|
|
11
|
+
const knownColumnIdsRef = useRef(configurableColumns.map(getColumnIdentifier));
|
|
11
12
|
useEffect(() => {
|
|
13
|
+
const currentIds = configurableColumns.map(getColumnIdentifier);
|
|
14
|
+
const knownIds = knownColumnIdsRef.current;
|
|
15
|
+
knownColumnIdsRef.current = currentIds;
|
|
16
|
+
const newColumns = configurableColumns.filter(col => !knownIds.includes(getColumnIdentifier(col)));
|
|
17
|
+
const addedIds = newColumns.length ? getEnabledColumnsInitialState(newColumns, savedState, localStorageKey) : [];
|
|
12
18
|
setEnabledColumns(prev => {
|
|
13
|
-
const currentIds = configurableColumns.map(getColumnIdentifier);
|
|
14
19
|
const kept = prev.filter(id => currentIds.includes(id));
|
|
15
|
-
const added =
|
|
16
|
-
.filter(id => !kept.includes(id))
|
|
17
|
-
.filter(id => {
|
|
18
|
-
const colDef = configurableColumns.find(col => getColumnIdentifier(col) === id);
|
|
19
|
-
return colDef ? isColumnEnabledInitially(colDef) : false;
|
|
20
|
-
});
|
|
20
|
+
const added = addedIds.filter(id => !kept.includes(id));
|
|
21
21
|
const reconciled = [...kept, ...added];
|
|
22
22
|
if (reconciled.length === prev.length && reconciled.every((id, i) => id === prev[i])) {
|
|
23
23
|
return prev;
|
|
24
24
|
}
|
|
25
25
|
return reconciled;
|
|
26
26
|
});
|
|
27
|
-
}, [configurableColumns]);
|
|
27
|
+
}, [configurableColumns, localStorageKey, savedState]);
|
|
28
28
|
const setEnabledColumnsOuter = useCallback((value) => {
|
|
29
29
|
if (savedState?.columnSettings && isBrowser()) {
|
|
30
30
|
localStorage.setItem(localStorageKey, JSON.stringify(value));
|