@propellerads/table 4.5.0 → 4.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@propellerads/table",
3
- "version": "4.5.0",
3
+ "version": "4.6.0",
4
4
  "repository": "https://github.com/propellerads/ui-components",
5
5
  "author": "i.pasyuk@propellerads.net",
6
6
  "license": "MIT",
@@ -41,5 +41,5 @@
41
41
  "@types/react-table": "^7.7.12",
42
42
  "react-table": "^7.8.0"
43
43
  },
44
- "gitHead": "e62c80448d64abc7d60fb748c8819985d3802b02"
44
+ "gitHead": "8ef053e09af9c826817843883c68274027b3f72d"
45
45
  }
package/src/index.tsx CHANGED
@@ -29,7 +29,6 @@ import {
29
29
  StandardCell,
30
30
  ColumnWithSort,
31
31
  TableHooksInstanceProps,
32
- SelectableRowInstanceProps,
33
32
  TableOptionsProps,
34
33
  SelectableRow,
35
34
  DefaultObject,
@@ -63,6 +62,10 @@ import {
63
62
  EmptyStateCell,
64
63
  } from './style';
65
64
 
65
+ const isEnableRowSelectDefault: () => boolean = () => true;
66
+ const selectColumnPropsDefault = {};
67
+ const getRowPrePropsDefault: () => DefaultObject = () => ({});
68
+
66
69
  export const defaultProps = {
67
70
  hasDefaultPagination: false,
68
71
  isLoading: false,
@@ -74,7 +77,9 @@ export const defaultProps = {
74
77
  showLoadingState: false,
75
78
  initialState: {},
76
79
  LoadingCellComponent: EmptyStateCell,
77
- getRowPreProps: () => ({}),
80
+ getRowPreProps: getRowPrePropsDefault,
81
+ isEnableRowSelect: isEnableRowSelectDefault,
82
+ selectColumnProps: selectColumnPropsDefault,
78
83
  noDataMessage: '',
79
84
  };
80
85
 
@@ -143,6 +148,8 @@ const Table: FunctionComponent<TableProps & DefaultProps> = (props) => {
143
148
  showLoadingState,
144
149
  noDataMessage,
145
150
  rowSubComponent,
151
+ isEnableRowSelect,
152
+ selectColumnProps,
146
153
  } = props;
147
154
 
148
155
  const memoColumns = useMemo(() => columns, [columns]);
@@ -203,17 +210,21 @@ const Table: FunctionComponent<TableProps & DefaultProps> = (props) => {
203
210
  const newColumn = {
204
211
  id: 'selection',
205
212
  disableSortBy: true,
206
- Header: (instance: SelectableRowInstanceProps) => {
213
+ Header: (instance: TableHooksInstanceProps & { toggleAllPageRowsSelected: () => void}) => {
207
214
  const {
208
- getToggleAllRowsSelectedProps,
209
- toggleAllRowsSelected,
215
+ getToggleAllPageRowsSelectedProps,
216
+ toggleAllPageRowsSelected,
217
+ page,
210
218
  } = instance;
211
219
 
220
+ const isDisabledAllRows = page.map(({original}) => original).filter(isEnableRowSelect).length === 0;
221
+
212
222
  return (
213
223
  <Checkbox
214
224
  elementId="all"
215
- onChange={toggleAllRowsSelected}
216
- isChecked={getToggleAllRowsSelectedProps().checked}
225
+ onChange={toggleAllPageRowsSelected}
226
+ isChecked={getToggleAllPageRowsSelectedProps().checked}
227
+ isDisabled={isDisabledAllRows}
217
228
  />
218
229
  );
219
230
  },
@@ -226,15 +237,18 @@ const Table: FunctionComponent<TableProps & DefaultProps> = (props) => {
226
237
  } = row;
227
238
 
228
239
  const elementId = Number.isInteger(original.id) ? original.id : id;
240
+ const isEnabled = isEnableRowSelect(row.original);
229
241
 
230
242
  return (
231
243
  <Checkbox
232
244
  elementId={elementId}
233
245
  onChange={toggleRowSelected}
234
- isChecked={getToggleRowSelectedProps().checked}
246
+ isDisabled={!isEnabled}
247
+ isChecked={isEnabled && getToggleRowSelectedProps().checked}
235
248
  />
236
249
  );
237
250
  },
251
+ ...selectColumnProps,
238
252
  };
239
253
 
240
254
  hooks.visibleColumns.push((tableColumns: Array<StandardColumn>) => [
@@ -294,7 +308,7 @@ const Table: FunctionComponent<TableProps & DefaultProps> = (props) => {
294
308
  useEffect(() => {
295
309
  if (hasSelectedRowsAbility) {
296
310
  onSelectRowsChange?.(
297
- selectedFlatRows?.map((row: StandardRow) => row.original),
311
+ selectedFlatRows?.map((row: StandardRow) => row.original).filter(isEnableRowSelect),
298
312
  );
299
313
  }
300
314
  }, [selectedRowIds]);
@@ -455,8 +469,8 @@ const Table: FunctionComponent<TableProps & DefaultProps> = (props) => {
455
469
 
456
470
  if (isDelimiterTd) {
457
471
  return (
458
- <>
459
- <TRGroup key={`group_${row.index}`}>
472
+ <React.Fragment key={`group_${row.index}`}>
473
+ <TRGroup>
460
474
  <TR {...row.getRowProps(getTableRowProps(getRowProps))}>
461
475
  <TD
462
476
  colSpan={visibleColumns.length}
@@ -469,13 +483,13 @@ const Table: FunctionComponent<TableProps & DefaultProps> = (props) => {
469
483
  </TR>
470
484
  </TRGroup>
471
485
  {row?.isExpanded && rowSubComponent && rowSubComponent(row)}
472
- </>
486
+ </React.Fragment>
473
487
  );
474
488
  }
475
489
 
476
490
  return (
477
- <>
478
- <TRGroup key={`group_${row.index}`}>
491
+ <React.Fragment key={`group_${row.index}`}>
492
+ <TRGroup>
479
493
  <TR {...row.getRowProps(getTableRowProps(getRowProps))}>
480
494
  {row.cells.map((cell: StandardCell) => (
481
495
  <TD
@@ -489,7 +503,7 @@ const Table: FunctionComponent<TableProps & DefaultProps> = (props) => {
489
503
  </TR>
490
504
  </TRGroup>
491
505
  {row?.isExpanded && rowSubComponent && rowSubComponent(row)}
492
- </>
506
+ </React.Fragment>
493
507
  );
494
508
  })}
495
509
  </TBody>
package/src/types.tsx CHANGED
@@ -85,7 +85,8 @@ export type TableProps = {
85
85
  showLoadingState?: boolean,
86
86
  LoadingCellComponent?: FunctionComponent<DefaultObject>,
87
87
  noDataMessage?: string | ReactElement
88
- rowSubComponent?: (row: StandardRow) => ReactElement
88
+ rowSubComponent?: (row: StandardRow) => ReactElement,
89
+ isEnableRowSelect?: (original: DefaultObject) => boolean,
89
90
  }
90
91
 
91
92
  export type TableOptions = {