@synerise/ds-table 0.47.5 → 0.47.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [0.47.6](https://github.com/synerise/synerise-design/compare/@synerise/ds-table@0.47.5...@synerise/ds-table@0.47.6) (2024-01-18)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **table:** fix selection handler for filtered data ([8f56d41](https://github.com/synerise/synerise-design/commit/8f56d41465f833cf532c8bd1dc432889eb0bb17a))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [0.47.5](https://github.com/synerise/synerise-design/compare/@synerise/ds-table@0.47.4...@synerise/ds-table@0.47.5) (2024-01-17)
7
18
 
8
19
  **Note:** Version bump only for package @synerise/ds-table
package/README.md CHANGED
@@ -20,6 +20,7 @@ Table UI Component
20
20
  | columns | Columns of table | [ColumnProps](https://git.io/vMMXC)\[] | - |
21
21
  | components | Override default table elements | [TableComponents](https://git.io/fANxz) | - |
22
22
  | dataSource | Data record array to be displayed | any / [] | - |
23
+ | dataSourceFull | If `dataSource` prop is a subset of a larger array (via search component for example), but you want selection row to operate on the full data array (selection onChange handler will return keys that are not in the current subset but have been selected prior to searching) | same as `dataSource` | - |
23
24
  | defaultExpandAllRows | Expand all rows initially | boolean | `false` |
24
25
  | defaultExpandedRowKeys | Initial expanded row keys | string / [] | - |
25
26
  | disableColumnNamesLineBreak | Disable line break when rendering column names | boolean | false |
@@ -57,7 +57,7 @@ function GroupTableBody(_ref) {
57
57
  indeterminate: selectedRowsNumber > 0 && selectedRowsNumber < allRowKeys.length,
58
58
  onChange: function onChange(event) {
59
59
  if (event.target.checked) {
60
- var selectedKeys = [].concat(new Set([].concat(selection == null ? void 0 : selection.selectedRowKeys, allRowKeys)));
60
+ var selectedKeys = Array.from(new Set([].concat(selection == null ? void 0 : selection.selectedRowKeys, allRowKeys)));
61
61
  selection.onChange(selectedKeys, allItems.filter(function (item) {
62
62
  return selectedKeys.indexOf(getRowKey(item)) >= 0;
63
63
  }));
@@ -87,6 +87,7 @@ export function RowSelectionColumn(_ref) {
87
87
  mouseLeaveDelay: 0
88
88
  }, /*#__PURE__*/React.createElement(Button.Checkbox, {
89
89
  key: "checkbox-" + recordKey,
90
+ "data-testid": "ds-table-selection-button",
90
91
  checked: isChecked,
91
92
  disabled: !isChecked && Boolean(limit !== undefined && limit <= selectedRowKeys.length),
92
93
  indeterminate: isIndeterminate,
package/dist/Table.js CHANGED
@@ -34,6 +34,7 @@ function DSTable(props) {
34
34
  itemsMenu = props.itemsMenu,
35
35
  cellSize = props.cellSize,
36
36
  dataSource = props.dataSource,
37
+ dataSourceFull = props.dataSourceFull,
37
38
  roundedHeader = props.roundedHeader,
38
39
  filters = props.filters,
39
40
  searchComponent = props.searchComponent,
@@ -72,6 +73,7 @@ function DSTable(props) {
72
73
  itemsMenu: itemsMenu,
73
74
  selection: selection,
74
75
  dataSource: data,
76
+ dataSourceFull: dataSourceFull,
75
77
  searchComponent: searchComponent,
76
78
  filterComponent: filterComponent,
77
79
  headerButton: headerButton,
@@ -80,7 +82,7 @@ function DSTable(props) {
80
82
  renderSelectionTitle: renderSelectionTitle,
81
83
  hideTitlePart: hideTitlePart
82
84
  });
83
- }, [selection, grouped, dataSource, renderSelectionTitle, hideTitleBar, headerWithBorderTop, title, filters, itemsMenu, searchComponent, filterComponent, headerButton, rowKey, tableLocale, hideTitlePart]);
85
+ }, [selection, grouped, dataSource, hideTitleBar, headerWithBorderTop, title, filters, itemsMenu, dataSourceFull, searchComponent, filterComponent, headerButton, rowKey, tableLocale, renderSelectionTitle, hideTitlePart]);
84
86
  var footerPagination = React.useMemo(function () {
85
87
  return _objectSpread({
86
88
  showTotal: function showTotal(total, range) {
@@ -115,5 +115,6 @@ export interface DSTableProps<T extends any & GroupType<T>> extends AntTableProp
115
115
  hideTitlePart?: boolean;
116
116
  disableColumnNamesLineBreak?: boolean;
117
117
  onSort?: OnSortFn;
118
+ dataSourceFull?: T[];
118
119
  }
119
120
  export {};
@@ -20,6 +20,7 @@ var TableHeader = function TableHeader(_ref) {
20
20
  itemsMenu = _ref.itemsMenu,
21
21
  selection = _ref.selection,
22
22
  dataSource = _ref.dataSource,
23
+ dataSourceFull = _ref.dataSourceFull,
23
24
  rowKey = _ref.rowKey,
24
25
  withBorderTop = _ref.withBorderTop,
25
26
  headerButton = _ref.headerButton,
@@ -55,6 +56,7 @@ var TableHeader = function TableHeader(_ref) {
55
56
  }, selection && /*#__PURE__*/React.createElement(TableSelection, {
56
57
  rowKey: rowKey,
57
58
  dataSource: dataSource,
59
+ dataSourceFull: dataSourceFull,
58
60
  selection: selection,
59
61
  locale: locale
60
62
  }), renderSelectionTitle ? renderSelectionTitle(selection, filters) : /*#__PURE__*/React.createElement(S.TitleContainer, null, /*#__PURE__*/React.createElement(S.TitlePart, null, /*#__PURE__*/React.createElement("strong", null, formatValue(selectedRows)), " ", /*#__PURE__*/React.createElement("span", null, locale.selected))), itemsMenu) : /*#__PURE__*/React.createElement(S.Left, {
@@ -62,6 +64,7 @@ var TableHeader = function TableHeader(_ref) {
62
64
  }, selection && /*#__PURE__*/React.createElement(TableSelection, {
63
65
  rowKey: rowKey,
64
66
  dataSource: dataSource,
67
+ dataSourceFull: dataSourceFull,
65
68
  selection: selection,
66
69
  locale: locale
67
70
  }), /*#__PURE__*/React.createElement(S.TitleContainer, null, title && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(S.TitlePartEllipsis, null, isTitleTruncated ? /*#__PURE__*/React.createElement(Tooltip, {
@@ -74,7 +77,7 @@ var TableHeader = function TableHeader(_ref) {
74
77
  }, title)) : /*#__PURE__*/React.createElement("strong", {
75
78
  ref: titleRef
76
79
  }, title)), !hideTitlePart && /*#__PURE__*/React.createElement(S.TitleSeparator, null)), !hideTitlePart && /*#__PURE__*/React.createElement(S.TitlePart, null, /*#__PURE__*/React.createElement("strong", null, formatValue(dataSource.length)), " ", /*#__PURE__*/React.createElement("span", null, locale.pagination.items))));
77
- }, [selection, dataSource, itemsMenu, locale, selectedRows, rowKey, renderSelectionTitle, filters, formatValue, title, isTitleTruncated, hideTitlePart]);
80
+ }, [selection, dataSource, itemsMenu, locale, selectedRows, rowKey, dataSourceFull, renderSelectionTitle, filters, formatValue, title, isTitleTruncated, hideTitlePart]);
78
81
  return /*#__PURE__*/React.createElement(S.Header, {
79
82
  withBorderTop: withBorderTop
80
83
  }, renderLeftSide, /*#__PURE__*/React.createElement(S.Right, null, headerButton, filters == null ? void 0 : filters.map(function (filter) {
@@ -9,6 +9,7 @@ export interface Props<T extends {
9
9
  itemsMenu: React.ReactNode;
10
10
  selection?: RowSelection<T>;
11
11
  dataSource: T[];
12
+ dataSourceFull?: T[];
12
13
  searchComponent?: React.ReactNode;
13
14
  filterComponent?: React.ReactNode;
14
15
  rowKey?: Function | string;
@@ -3,5 +3,5 @@ import { Props } from './TableSelection.types';
3
3
  declare function TableSelection<T extends {
4
4
  key: React.ReactText;
5
5
  children?: T[];
6
- }>({ dataSource, selection, rowKey, locale, }: Props<T>): React.ReactElement | null;
6
+ }>({ dataSource, dataSourceFull, selection, rowKey, locale, }: Props<T>): React.ReactElement | null;
7
7
  export default TableSelection;
@@ -10,6 +10,7 @@ import { useRowKey } from '../hooks/useRowKey'; // @ts-ignore
10
10
 
11
11
  function TableSelection(_ref) {
12
12
  var dataSource = _ref.dataSource,
13
+ dataSourceFull = _ref.dataSourceFull,
13
14
  selection = _ref.selection,
14
15
  rowKey = _ref.rowKey,
15
16
  locale = _ref.locale;
@@ -17,31 +18,85 @@ function TableSelection(_ref) {
17
18
  var _useRowKey = useRowKey(rowKey),
18
19
  getRowKey = _useRowKey.getRowKey;
19
20
 
21
+ var allData = dataSourceFull || dataSource;
22
+ var isShowingSubset = dataSourceFull && dataSourceFull.length !== dataSource.length;
23
+ var getRowsForKeys = React.useCallback(function (keys) {
24
+ if (selection) {
25
+ var rows = [];
26
+ allData.forEach(function (record) {
27
+ if (Array.isArray(record.children)) {
28
+ record.children.forEach(function (child) {
29
+ var key = getRowKey(child);
30
+
31
+ if (record.children && key && keys.includes(key)) {
32
+ rows = [].concat(rows, [record]);
33
+ }
34
+ });
35
+ }
36
+
37
+ if (!Array.isArray(record.children) || selection.independentSelectionExpandedRows) {
38
+ var key = getRowKey(record);
39
+
40
+ if (key && keys.includes(key)) {
41
+ rows = [].concat(rows, [record]);
42
+ }
43
+ }
44
+ });
45
+ return rows;
46
+ }
47
+
48
+ return [];
49
+ }, [allData, getRowKey, selection]);
20
50
  var selectAll = React.useCallback(function () {
21
51
  if (dataSource && selection) {
22
- var keys = [];
23
- var rows = [];
52
+ var selectedRowKeys = selection.selectedRowKeys;
53
+ var keys = isShowingSubset ? [].concat(selectedRowKeys) : [];
24
54
  dataSource.forEach(function (record) {
25
55
  if (Array.isArray(record.children)) {
26
56
  keys = [].concat(keys, record.children.reduce(function (acc, child) {
27
57
  var key = getRowKey(child);
28
58
  return key ? [].concat(acc, [key]) : acc;
29
59
  }, []));
30
- rows = [].concat(rows, record.children);
31
60
  }
32
61
 
33
62
  if (!Array.isArray(record.children) || selection.independentSelectionExpandedRows) {
34
63
  var key = getRowKey(record);
35
64
  keys = key !== undefined ? [].concat(keys, [key]) : [].concat(keys);
36
- rows = [].concat(rows, [record]);
37
65
  }
38
66
  });
39
- selection.onChange(keys, rows);
67
+ var uniqueKeys = Array.from(new Set(keys));
68
+ var rows = getRowsForKeys(uniqueKeys);
69
+ selection.onChange(uniqueKeys, rows);
40
70
  }
41
- }, [dataSource, selection, getRowKey]);
71
+ }, [dataSource, selection, isShowingSubset, getRowsForKeys, getRowKey]);
42
72
  var unselectAll = React.useCallback(function () {
43
- if (selection) selection.onChange([], []);
44
- }, [selection]);
73
+ if (selection) {
74
+ if (isShowingSubset) {
75
+ var selectedRowKeys = selection.selectedRowKeys;
76
+ var keysToUnselect = [];
77
+ dataSource.forEach(function (record) {
78
+ if (Array.isArray(record.children)) {
79
+ keysToUnselect = [].concat(keysToUnselect, record.children.reduce(function (acc, child) {
80
+ var key = getRowKey(child);
81
+ return key ? [].concat(acc, [key]) : acc;
82
+ }, []));
83
+ }
84
+
85
+ if (!Array.isArray(record.children) || selection.independentSelectionExpandedRows) {
86
+ var key = getRowKey(record);
87
+ keysToUnselect = key !== undefined ? [].concat(keysToUnselect, [key]) : [].concat(keysToUnselect);
88
+ }
89
+ });
90
+ var keysLeft = selectedRowKeys.filter(function (key) {
91
+ return !keysToUnselect.includes(key);
92
+ });
93
+ var rows = getRowsForKeys(keysLeft);
94
+ selection.onChange(keysLeft, rows);
95
+ } else {
96
+ selection.onChange([], []);
97
+ }
98
+ }
99
+ }, [dataSource, getRowKey, getRowsForKeys, isShowingSubset, selection]);
45
100
  var getSelectableChildren = React.useCallback(function (children) {
46
101
  return children ? children.filter(function (child) {
47
102
  return getRowKey(child) !== undefined;
@@ -49,48 +104,76 @@ function TableSelection(_ref) {
49
104
  }, [getRowKey]);
50
105
  var selectInvert = React.useCallback(function () {
51
106
  if (dataSource && selection) {
52
- var selected = [];
107
+ var selectedRowKeys = selection.selectedRowKeys;
108
+ var keys = isShowingSubset ? [].concat(selectedRowKeys) : [];
53
109
  dataSource.forEach(function (record) {
54
110
  var hasChildren = Array.isArray(record.children);
55
111
  var selectableChildren = hasChildren ? getSelectableChildren(record.children) : false;
56
112
 
57
113
  if (selectableChildren) {
58
114
  selectableChildren.forEach(function (child) {
59
- if ((selection == null ? void 0 : selection.selectedRowKeys.indexOf(getRowKey(child))) < 0) {
60
- selected = [].concat(selected, [child]);
115
+ var key = getRowKey(child);
116
+
117
+ if (selectedRowKeys.includes(key)) {
118
+ if (isShowingSubset) keys.splice(keys.indexOf(key), 1);
119
+ } else {
120
+ keys = [].concat(keys, [key]);
61
121
  }
62
122
  });
63
123
  }
64
124
 
65
- if ((selection == null ? void 0 : selection.selectedRowKeys.indexOf(getRowKey(record))) < 0 && (!selectableChildren || selection.independentSelectionExpandedRows)) {
66
- selected = [].concat(selected, [record]);
125
+ if (!selectableChildren || selection.independentSelectionExpandedRows) {
126
+ var key = getRowKey(record);
127
+
128
+ if (selectedRowKeys.includes(key)) {
129
+ if (isShowingSubset) keys.splice(keys.indexOf(key), 1);
130
+ } else {
131
+ keys = [].concat(keys, [key]);
132
+ }
67
133
  }
68
134
  });
69
- selection.onChange(selected.map(function (record) {
70
- return getRowKey(record);
71
- }), selected);
135
+ var rows = getRowsForKeys(keys);
136
+ selection.onChange(keys, rows);
72
137
  }
73
- }, [dataSource, selection, getRowKey, getSelectableChildren]);
138
+ }, [dataSource, selection, isShowingSubset, getRowsForKeys, getSelectableChildren, getRowKey]);
74
139
  var isEmpty = React.useMemo(function () {
75
140
  return dataSource.length === 0;
76
141
  }, [dataSource]);
77
142
  var allSelected = React.useMemo(function () {
78
- if (isEmpty) return false;
79
- var allRecords = dataSource.reduce(function (count, record) {
80
- if (selection != null && selection.independentSelectionExpandedRows) {
143
+ if (isEmpty || !selection) return false;
144
+ var selectedRowKeys = selection.selectedRowKeys,
145
+ independentSelectionExpandedRows = selection.independentSelectionExpandedRows;
146
+ var allRecordsCount = dataSource.reduce(function (count, record) {
147
+ if (independentSelectionExpandedRows) {
81
148
  return Array.isArray(record.children) ? count + getSelectableChildren(record.children).length + 1 : count + 1;
82
149
  }
83
150
 
84
151
  return Array.isArray(record.children) ? count + getSelectableChildren(record.children).length : count + 1;
85
152
  }, 0);
86
- return (selection == null ? void 0 : selection.selectedRowKeys) && allRecords === selection.selectedRowKeys.length;
87
- }, [isEmpty, dataSource, selection, getSelectableChildren]);
153
+ var selectedKeysInDataSourceCount = isShowingSubset ? dataSource.reduce(function (count, record) {
154
+ if (Array.isArray(record.children)) {
155
+ return record.children.reduce(function (childCount, child) {
156
+ var key = getRowKey(child);
157
+ return selectedRowKeys.includes(key) ? childCount + 1 : childCount;
158
+ }, 0);
159
+ }
160
+
161
+ if (!Array.isArray(record.children) || selection.independentSelectionExpandedRows) {
162
+ var key = getRowKey(record);
163
+ return selectedRowKeys.includes(key) ? count + 1 : count;
164
+ }
165
+
166
+ return count;
167
+ }, 0) : selectedRowKeys.length;
168
+ return allRecordsCount === selectedKeysInDataSourceCount;
169
+ }, [isEmpty, selection, dataSource, isShowingSubset, getSelectableChildren, getRowKey]);
88
170
  return selection != null && selection.selectedRowKeys ? /*#__PURE__*/React.createElement(S.Selection, {
89
171
  "data-popup-container": true
90
172
  }, /*#__PURE__*/React.createElement(Tooltip, {
91
173
  title: locale == null ? void 0 : locale.selectAllTooltip
92
174
  }, /*#__PURE__*/React.createElement(Button.Checkbox, {
93
175
  disabled: isEmpty,
176
+ "data-testid": "ds-table-batch-selection-button",
94
177
  checked: allSelected,
95
178
  onChange: function onChange(isChecked) {
96
179
  if (isChecked) {
@@ -5,6 +5,7 @@ export interface Props<T extends {
5
5
  }> {
6
6
  selection?: RowSelection<T>;
7
7
  dataSource: T[];
8
+ dataSourceFull?: T[];
8
9
  locale?: Locale;
9
10
  rowKey?: Function | string;
10
11
  }
@@ -81,6 +81,7 @@ function VirtualTable(props) {
81
81
  initialWidth = _props$initialWidth === void 0 ? 0 : _props$initialWidth,
82
82
  _props$dataSource = props.dataSource,
83
83
  dataSource = _props$dataSource === void 0 ? [] : _props$dataSource,
84
+ dataSourceFull = props.dataSourceFull,
84
85
  expandable = props.expandable,
85
86
  locale = props.locale,
86
87
  onListRefChange = props.onListRefChange;
@@ -103,6 +104,8 @@ function VirtualTable(props) {
103
104
  var _useRowKey = useRowKey(rowKey),
104
105
  getRowKey = _useRowKey.getRowKey;
105
106
 
107
+ var allData = dataSourceFull || dataSource;
108
+
106
109
  var propsForRowStar = _objectSpread({}, props, {
107
110
  rowStar: _objectSpread({}, rowStar, {
108
111
  onClick: function onClick(e) {
@@ -125,7 +128,7 @@ function VirtualTable(props) {
125
128
  var _ref2 = selection,
126
129
  selectedRowKeys = _ref2.selectedRowKeys;
127
130
  var selectedRows = [];
128
- dataSource && dataSource.forEach(function (row) {
131
+ allData.forEach(function (row) {
129
132
  var key = getRowKey(row);
130
133
 
131
134
  if (key && selectedRowKeys.indexOf(key) >= 0) {
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
- import * as React from 'react';
1
+ import { RefObject } from 'react';
2
2
  import { FixedSizeList as List } from 'react-window';
3
3
  import { InfiniteScrollProps } from '../InfiniteScroll/constants';
4
4
  import { DSTableProps } from '../Table.types';
@@ -10,6 +10,6 @@ export interface Props<T> extends DSTableProps<T> {
10
10
  x?: number;
11
11
  y: number;
12
12
  };
13
- onListRefChange?: (ref: React.RefObject<List>) => void;
13
+ onListRefChange?: (ref: RefObject<List>) => void;
14
14
  onRowClick?: (row: T) => void;
15
15
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@synerise/ds-table",
3
- "version": "0.47.5",
3
+ "version": "0.47.6",
4
4
  "description": "Table UI Component for the Synerise Design System",
5
5
  "license": "ISC",
6
6
  "repository": "synerise/synerise-design",
@@ -79,5 +79,5 @@
79
79
  "devDependencies": {
80
80
  "@testing-library/react": "10.0.1"
81
81
  },
82
- "gitHead": "16918244eff3f4e37db24ae3d90687d4e6a233a4"
82
+ "gitHead": "5c8c548309589dafd3b1addec290873c4e55a882"
83
83
  }