@atlaskit/table 0.1.0 → 0.1.2

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.
Files changed (67) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/constellation/index/examples.mdx +1 -1
  3. package/constellation/index/props.mdx +6 -21
  4. package/dist/cjs/body.js +18 -25
  5. package/dist/cjs/hooks/selection-provider.js +2 -11
  6. package/dist/cjs/hooks/use-selectable.js +15 -26
  7. package/dist/cjs/hooks/use-sorting.js +10 -1
  8. package/dist/cjs/hooks/use-table.js +4 -1
  9. package/dist/cjs/row.js +14 -12
  10. package/dist/cjs/selectable-cell.js +8 -5
  11. package/dist/cjs/sortable-column.js +15 -39
  12. package/dist/cjs/table.js +15 -8
  13. package/dist/cjs/thead.js +6 -5
  14. package/dist/cjs/ui/base-cell.js +5 -1
  15. package/dist/cjs/ui/index.js +9 -1
  16. package/dist/cjs/ui/sort-icon.js +56 -0
  17. package/dist/cjs/ui/th.js +3 -1
  18. package/dist/cjs/version.json +1 -1
  19. package/dist/es2019/body.js +14 -25
  20. package/dist/es2019/hooks/selection-provider.js +2 -9
  21. package/dist/es2019/hooks/use-selectable.js +2 -8
  22. package/dist/es2019/hooks/use-sorting.js +10 -1
  23. package/dist/es2019/hooks/use-table.js +2 -1
  24. package/dist/es2019/row.js +16 -12
  25. package/dist/es2019/selectable-cell.js +7 -5
  26. package/dist/es2019/sortable-column.js +8 -31
  27. package/dist/es2019/table.js +11 -7
  28. package/dist/es2019/thead.js +5 -3
  29. package/dist/es2019/ui/base-cell.js +5 -1
  30. package/dist/es2019/ui/index.js +2 -1
  31. package/dist/es2019/ui/sort-icon.js +42 -0
  32. package/dist/es2019/ui/th.js +3 -1
  33. package/dist/es2019/version.json +1 -1
  34. package/dist/esm/body.js +18 -27
  35. package/dist/esm/hooks/selection-provider.js +2 -9
  36. package/dist/esm/hooks/use-selectable.js +13 -25
  37. package/dist/esm/hooks/use-sorting.js +10 -1
  38. package/dist/esm/hooks/use-table.js +4 -1
  39. package/dist/esm/row.js +14 -12
  40. package/dist/esm/selectable-cell.js +9 -6
  41. package/dist/esm/sortable-column.js +10 -33
  42. package/dist/esm/table.js +11 -5
  43. package/dist/esm/thead.js +5 -4
  44. package/dist/esm/ui/base-cell.js +5 -1
  45. package/dist/esm/ui/index.js +2 -1
  46. package/dist/esm/ui/sort-icon.js +41 -0
  47. package/dist/esm/ui/th.js +3 -1
  48. package/dist/esm/version.json +1 -1
  49. package/dist/types/body.d.ts +1 -1
  50. package/dist/types/hooks/selection-provider.d.ts +11 -14
  51. package/dist/types/hooks/use-selectable.d.ts +31 -7
  52. package/dist/types/hooks/use-sorting.d.ts +1 -0
  53. package/dist/types/hooks/use-table.d.ts +1 -0
  54. package/dist/types/row.d.ts +4 -0
  55. package/dist/types/sortable-column.d.ts +4 -0
  56. package/dist/types/table.d.ts +4 -0
  57. package/dist/types/ui/base-cell.d.ts +44 -0
  58. package/dist/types/ui/index.d.ts +1 -0
  59. package/dist/types/ui/sort-icon.d.ts +10 -0
  60. package/dist/types/ui/table.d.ts +5 -1
  61. package/dist/types/ui/tr.d.ts +10 -0
  62. package/extract-react-types/cell.tsx +3 -0
  63. package/extract-react-types/row.tsx +8 -0
  64. package/extract-react-types/table.tsx +8 -0
  65. package/package.json +6 -4
  66. package/report.api.md +1 -1
  67. package/tmp/api-report-tmp.d.ts +1 -1
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/table",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "sideEffects": false
5
5
  }
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable @repo/internal/react/no-clone-element */
2
2
 
3
3
  /** @jsx jsx */
4
- import { Children, useEffect } from 'react';
4
+ import { Children, useEffect, useMemo } from 'react';
5
5
  import { jsx } from '@emotion/react';
6
6
  import { useSelection } from './hooks/selection-provider';
7
7
  import { RowProvider } from './hooks/use-row-id';
@@ -17,40 +17,29 @@ function TBody({
17
17
  children
18
18
  }) {
19
19
  const {
20
- sortKey,
21
- sortDirection
20
+ sortFn
22
21
  } = useTable();
23
22
  const [_state, {
24
23
  removeAll,
25
24
  setMax
26
- }] = useSelection(); // TODO this seems like something the user should control
25
+ }] = useSelection(); // TODO: this seems like something the user should control or opt into.
27
26
 
28
27
  useEffect(() => {
29
- if (removeAll) {
30
- removeAll();
31
- } // eslint-disable-next-line react-hooks/exhaustive-deps
32
-
33
- }, [rows]); // Set data length (via setMax) whenever data changes
28
+ removeAll === null || removeAll === void 0 ? void 0 : removeAll(); // eslint-disable-next-line react-hooks/exhaustive-deps -- When the rows change, we [currently] want to call removeAll.
29
+ }, [rows]);
30
+ const childrenCount = Children.count(children);
31
+ const rowsLength = rows === null || rows === void 0 ? void 0 : rows.length; // Set data length (via setMax) whenever data changes
34
32
 
35
33
  useEffect(() => {
36
- const numRows = rows !== undefined ? rows.length : Children.count(children);
37
- setMax && setMax(numRows); // eslint-disable-next-line react-hooks/exhaustive-deps
38
- }, [rows === null || rows === void 0 ? void 0 : rows.length, Children.count(children)]);
39
-
40
- const sortFn = (rowA, rowB) => {
41
- if (sortKey === 'unset') {
42
- return 0;
43
- }
44
-
45
- const ascendingComparator = rowA[sortKey] < rowB[sortKey] ? -1 : 1;
46
- return sortDirection === 'ascending' ? ascendingComparator : -ascendingComparator;
47
- };
48
-
34
+ const numRows = rowsLength !== null && rowsLength !== void 0 ? rowsLength : childrenCount;
35
+ setMax === null || setMax === void 0 ? void 0 : setMax(numRows);
36
+ }, [rowsLength, childrenCount, setMax]);
37
+ const sortedRows = useMemo(() => rows === null || rows === void 0 ? void 0 : rows.map((row, idx) => ({ ...row,
38
+ idx
39
+ })).sort(sortFn), [rows, sortFn]);
49
40
  return jsx(TableBodyProvider, {
50
41
  value: true
51
- }, jsx(Primitives.TBody, null, typeof children === 'function' && rows ? rows.map((row, idx) => ({ ...row,
52
- idx
53
- })).sort(sortFn).map(({
42
+ }, jsx(Primitives.TBody, null, typeof children === 'function' && sortedRows ? sortedRows.map(({
54
43
  idx,
55
44
  ...row
56
45
  }) => jsx(RowProvider, {
@@ -1,13 +1,6 @@
1
1
  import React, { createContext, useContext } from 'react';
2
- import useSelectionReducer from './use-selectable';
3
- const SelectionContext = /*#__PURE__*/createContext([{
4
- checked: [],
5
- allChecked: false,
6
- anyChecked: false,
7
- maxChecked: 0,
8
- selectionStart: -1,
9
- previousSelection: []
10
- }, {}]);
2
+ import useSelectionReducer, { defaultSelectableState } from './use-selectable';
3
+ const SelectionContext = /*#__PURE__*/createContext([defaultSelectableState, {}]);
11
4
  /**
12
5
  * __Selection provider__
13
6
  *
@@ -1,5 +1,5 @@
1
1
  import { useCallback, useReducer } from 'react';
2
- const defaultState = {
2
+ export const defaultSelectableState = {
3
3
  checked: [],
4
4
  allChecked: false,
5
5
  anyChecked: false,
@@ -112,14 +112,8 @@ function reducer({
112
112
  }
113
113
  }
114
114
 
115
- const initialiseState = () => {
116
- return { ...defaultState,
117
- checked: []
118
- };
119
- };
120
-
121
115
  function useSelectable() {
122
- const [state, dispatch] = useReducer(reducer, initialiseState());
116
+ const [state, dispatch] = useReducer(reducer, defaultSelectableState);
123
117
  const toggleSelection = useCallback((id, shiftHeld) => {
124
118
  dispatch({
125
119
  type: 'toggle_selection',
@@ -29,9 +29,18 @@ export const useSorting = sortKey => {
29
29
  return localSortKey;
30
30
  });
31
31
  }, [toggleSortDirection]);
32
+ const sortFn = useCallback((rowA, rowB) => {
33
+ if (localSortKey === 'unset') {
34
+ return 0;
35
+ }
36
+
37
+ const ascendingComparator = rowA[localSortKey] < rowB[localSortKey] ? -1 : 1;
38
+ return localSortDirection === 'ascending' ? ascendingComparator : -ascendingComparator;
39
+ }, [localSortDirection, localSortKey]);
32
40
  return {
33
41
  sortKey: localSortKey,
34
42
  sortDirection: localSortDirection,
35
- setSortState
43
+ setSortState,
44
+ sortFn
36
45
  };
37
46
  };
@@ -5,7 +5,8 @@ function generateContext() {
5
5
  return /*#__PURE__*/createContext({
6
6
  isSelectable: false,
7
7
  sortKey: 'unset',
8
- setSortState: __noop
8
+ setSortState: __noop,
9
+ sortFn: () => 0
9
10
  });
10
11
  }
11
12
 
@@ -1,5 +1,5 @@
1
1
  /** @jsx jsx */
2
- import { memo } from 'react';
2
+ import { memo, useMemo } from 'react';
3
3
  import { jsx } from '@emotion/react';
4
4
  import { useSelection } from './hooks/selection-provider';
5
5
  import { useRowId } from './hooks/use-row-id';
@@ -22,20 +22,24 @@ const Row = /*#__PURE__*/memo(({
22
22
  // To ensure valid nesting
23
23
  useTableBody(); // to access table state
24
24
 
25
- const table = useTable();
26
- const [selectionState] = useSelection();
25
+ const {
26
+ isSelectable
27
+ } = useTable();
28
+ const [{
29
+ allChecked,
30
+ checked
31
+ }] = useSelection();
27
32
  const rowId = useRowId();
33
+ const isSelected = useMemo(() => {
34
+ if (!isSelectable) {
35
+ return undefined;
36
+ }
28
37
 
29
- if (!table.isSelectable) {
30
- return jsx(Primitives.TR, {
31
- testId: testId
32
- }, children);
33
- }
34
-
35
- const isChecked = selectionState.allChecked || selectionState.checked.includes(rowId);
38
+ return allChecked || checked.includes(rowId);
39
+ }, [allChecked, checked, isSelectable, rowId]);
36
40
  return jsx(Primitives.TR, {
37
- isSelected: !!isChecked,
41
+ isSelected: isSelected,
38
42
  testId: testId
39
- }, jsx(SelectableCell, null), children);
43
+ }, isSelectable && jsx(SelectableCell, null), children);
40
44
  });
41
45
  export default Row;
@@ -1,5 +1,5 @@
1
1
  /** @jsx jsx */
2
- import { memo, useCallback } from 'react';
2
+ import { memo, useCallback, useMemo } from 'react';
3
3
  import { jsx } from '@emotion/react';
4
4
  import Checkbox from '@atlaskit/checkbox';
5
5
  import { useSelection } from './hooks/selection-provider';
@@ -7,13 +7,15 @@ import { useRowId } from './hooks/use-row-id';
7
7
  import * as Primitives from './ui';
8
8
 
9
9
  const SelectableCell = () => {
10
- const [state, {
10
+ const [{
11
+ allChecked,
12
+ checked
13
+ }, {
11
14
  toggleSelection
12
15
  }] = useSelection();
13
16
  const idx = useRowId();
14
- const isChecked = state.allChecked || state.checked.includes(idx);
15
- const onChange = useCallback(e => toggleSelection(idx, e.nativeEvent.shiftKey), // eslint-disable-next-line react-hooks/exhaustive-deps
16
- [idx]);
17
+ const isChecked = useMemo(() => allChecked || checked.includes(idx), [allChecked, checked, idx]);
18
+ const onChange = useCallback(e => toggleSelection === null || toggleSelection === void 0 ? void 0 : toggleSelection(idx, e.nativeEvent.shiftKey), [idx, toggleSelection]);
17
19
  return jsx(Primitives.SelectableCell, {
18
20
  as: "td"
19
21
  }, jsx(Checkbox, {
@@ -3,13 +3,12 @@ import _extends from "@babel/runtime/helpers/extends";
3
3
  /* eslint-disable no-unused-vars */
4
4
 
5
5
  /** @jsx jsx */
6
+ import { useCallback } from 'react';
6
7
  import { css, jsx } from '@emotion/react';
7
8
  import Button from '@atlaskit/button';
8
- import ArrowDownIcon from '@atlaskit/icon/glyph/arrow-down';
9
- import ArrowUpIcon from '@atlaskit/icon/glyph/arrow-up';
10
9
  import Tooltip from '@atlaskit/tooltip';
11
10
  import { useTable } from './hooks/use-table';
12
- import { TH } from './ui';
11
+ import { SortIcon, TH } from './ui';
13
12
 
14
13
  /**
15
14
  * TODO these need to be i18n supported
@@ -63,31 +62,8 @@ const SortableColumn = ({
63
62
  return sortingMessages.unsorted.string;
64
63
  };
65
64
 
66
- const getSortIcon = () => {
67
- if (sortKey === name) {
68
- switch (sortDirection) {
69
- case undefined:
70
- return undefined;
71
-
72
- case 'ascending':
73
- return jsx(ArrowUpIcon, {
74
- size: "small",
75
- label: "",
76
- primaryColor: "inherit"
77
- });
78
-
79
- case 'descending':
80
- return jsx(ArrowDownIcon, {
81
- size: "small",
82
- label: "",
83
- primaryColor: "inherit"
84
- });
85
- }
86
- }
87
-
88
- return undefined;
89
- };
90
-
65
+ const updateSortState = useCallback( // @ts-expect-error: TODO: Our `name` typing is off; refer to `SortKey`
66
+ () => setSortState(name), [setSortState, name]);
91
67
  return (// eslint-disable-next-line @repo/internal/react/no-unsafe-spread-props
92
68
  jsx(TH, _extends({
93
69
  testId: testId
@@ -97,10 +73,11 @@ const SortableColumn = ({
97
73
  }, tooltipProps => jsx(Button, _extends({
98
74
  spacing: "compact",
99
75
  appearance: "subtle",
100
- iconAfter: getSortIcon()
76
+ iconAfter: jsx(SortIcon, {
77
+ name: name
78
+ })
101
79
  }, tooltipProps, {
102
- // @ts-expect-error
103
- onClick: () => setSortState(name),
80
+ onClick: updateSortState,
104
81
  css: overrideStyles
105
82
  }), children)))
106
83
  );
@@ -1,4 +1,5 @@
1
1
  /** @jsx jsx */
2
+ import { useMemo } from 'react';
2
3
  import { jsx } from '@emotion/react';
3
4
  import SelectionProvider from './hooks/selection-provider';
4
5
  import { useSorting } from './hooks/use-sorting';
@@ -21,15 +22,18 @@ function Table({
21
22
  const {
22
23
  sortKey: localSortKey,
23
24
  sortDirection,
24
- setSortState
25
+ setSortState,
26
+ sortFn
25
27
  } = useSorting(sortKey);
28
+ const tableProviderState = useMemo(() => ({
29
+ isSelectable,
30
+ sortKey: localSortKey,
31
+ sortDirection: sortDirection,
32
+ setSortState,
33
+ sortFn
34
+ }), [isSelectable, localSortKey, setSortState, sortDirection, sortFn]);
26
35
  return jsx(TableProvider, {
27
- state: {
28
- isSelectable,
29
- sortKey: localSortKey,
30
- sortDirection: sortDirection,
31
- setSortState
32
- }
36
+ state: tableProviderState
33
37
  }, jsx(Primitives.Table, {
34
38
  testId: testId
35
39
  }, isSelectable ? jsx(SelectionProvider, null, children) : children));
@@ -11,19 +11,21 @@ const THead = ({
11
11
  actions,
12
12
  children
13
13
  }) => {
14
- const table = useTable();
14
+ const {
15
+ isSelectable
16
+ } = useTable();
15
17
  const [state, {
16
18
  setAll,
17
19
  removeAll
18
20
  }] = useSelection();
19
- const isChecked = state.allChecked || state.anyChecked;
20
21
 
21
- if (!table.isSelectable) {
22
+ if (!isSelectable) {
22
23
  return jsx(Primitives.THead, null, jsx(Primitives.TR, {
23
24
  isBodyRow: false
24
25
  }, children));
25
26
  }
26
27
 
28
+ const isChecked = state.allChecked || state.anyChecked;
27
29
  return jsx(Primitives.THead, null, jsx(Primitives.TR, {
28
30
  isBodyRow: false
29
31
  }, jsx(Primitives.SelectableCell, {
@@ -37,6 +37,7 @@ export const BaseCell = /*#__PURE__*/forwardRef(({
37
37
  paddingInline = 'scale.200',
38
38
  backgroundColor,
39
39
  scope,
40
+ width,
40
41
  className
41
42
  }, ref) => jsx(Box, {
42
43
  css: baseResetStyles,
@@ -47,7 +48,10 @@ export const BaseCell = /*#__PURE__*/forwardRef(({
47
48
  paddingInline: paddingInline,
48
49
  as: as,
49
50
  testId: testId,
50
- className: className
51
+ className: className,
52
+ UNSAFE_style: width ? {
53
+ width
54
+ } : undefined
51
55
  }, jsx(Inline, {
52
56
  justifyContent: alignMap[align],
53
57
  gap: "scale.0"
@@ -8,4 +8,5 @@ export { TD } from './td';
8
8
  export { TH } from './th';
9
9
  export { SelectableCell } from './selectable-cell';
10
10
  export { THead } from './thead';
11
- export { BulkActionOverlay } from './bulk-action-overlay';
11
+ export { BulkActionOverlay } from './bulk-action-overlay';
12
+ export { SortIcon } from './sort-icon';
@@ -0,0 +1,42 @@
1
+ /* eslint-disable no-unused-vars */
2
+
3
+ /** @jsx jsx */
4
+ import { memo } from 'react';
5
+ import { jsx } from '@emotion/react';
6
+ import ArrowDownIcon from '@atlaskit/icon/glyph/arrow-down';
7
+ import ArrowUpIcon from '@atlaskit/icon/glyph/arrow-up';
8
+ import { useTable } from '../hooks/use-table';
9
+ /**
10
+ * __SortIcon__
11
+ *
12
+ * SortIcon is used to display the sort state in our SortableColumn.
13
+ */
14
+
15
+ export const SortIcon = /*#__PURE__*/memo(({
16
+ name
17
+ }) => {
18
+ const {
19
+ sortKey,
20
+ sortDirection
21
+ } = useTable();
22
+
23
+ if (sortKey === name) {
24
+ switch (sortDirection) {
25
+ case 'ascending':
26
+ return jsx(ArrowUpIcon, {
27
+ size: "small",
28
+ label: "",
29
+ primaryColor: "inherit"
30
+ });
31
+
32
+ case 'descending':
33
+ return jsx(ArrowDownIcon, {
34
+ size: "small",
35
+ label: "",
36
+ primaryColor: "inherit"
37
+ });
38
+ }
39
+ }
40
+
41
+ return null;
42
+ });
@@ -13,7 +13,8 @@ export const TH = ({
13
13
  testId,
14
14
  align,
15
15
  scope,
16
- backgroundColor
16
+ backgroundColor,
17
+ width
17
18
  }) =>
18
19
  /*#__PURE__*/
19
20
  // eslint-disable-next-line @repo/internal/react/no-unsafe-spread-props
@@ -22,5 +23,6 @@ React.createElement(BaseCell, {
22
23
  testId: testId,
23
24
  align: align,
24
25
  scope: scope,
26
+ width: width,
25
27
  backgroundColor: backgroundColor
26
28
  }, children);
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/table",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "sideEffects": false
5
5
  }
package/dist/esm/body.js CHANGED
@@ -10,7 +10,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
10
10
  /* eslint-disable @repo/internal/react/no-clone-element */
11
11
 
12
12
  /** @jsx jsx */
13
- import { Children, useEffect } from 'react';
13
+ import { Children, useEffect, useMemo } from 'react';
14
14
  import { jsx } from '@emotion/react';
15
15
  import { useSelection } from './hooks/selection-provider';
16
16
  import { RowProvider } from './hooks/use-row-id';
@@ -26,45 +26,36 @@ function TBody(_ref) {
26
26
  children = _ref.children;
27
27
 
28
28
  var _useTable = useTable(),
29
- sortKey = _useTable.sortKey,
30
- sortDirection = _useTable.sortDirection;
29
+ sortFn = _useTable.sortFn;
31
30
 
32
31
  var _useSelection = useSelection(),
33
32
  _useSelection2 = _slicedToArray(_useSelection, 2),
34
33
  _state = _useSelection2[0],
35
34
  _useSelection2$ = _useSelection2[1],
36
35
  removeAll = _useSelection2$.removeAll,
37
- setMax = _useSelection2$.setMax; // TODO this seems like something the user should control
36
+ setMax = _useSelection2$.setMax; // TODO: this seems like something the user should control or opt into.
38
37
 
39
38
 
40
39
  useEffect(function () {
41
- if (removeAll) {
42
- removeAll();
43
- } // eslint-disable-next-line react-hooks/exhaustive-deps
44
-
45
- }, [rows]); // Set data length (via setMax) whenever data changes
40
+ removeAll === null || removeAll === void 0 ? void 0 : removeAll(); // eslint-disable-next-line react-hooks/exhaustive-deps -- When the rows change, we [currently] want to call removeAll.
41
+ }, [rows]);
42
+ var childrenCount = Children.count(children);
43
+ var rowsLength = rows === null || rows === void 0 ? void 0 : rows.length; // Set data length (via setMax) whenever data changes
46
44
 
47
45
  useEffect(function () {
48
- var numRows = rows !== undefined ? rows.length : Children.count(children);
49
- setMax && setMax(numRows); // eslint-disable-next-line react-hooks/exhaustive-deps
50
- }, [rows === null || rows === void 0 ? void 0 : rows.length, Children.count(children)]);
51
-
52
- var sortFn = function sortFn(rowA, rowB) {
53
- if (sortKey === 'unset') {
54
- return 0;
55
- }
56
-
57
- var ascendingComparator = rowA[sortKey] < rowB[sortKey] ? -1 : 1;
58
- return sortDirection === 'ascending' ? ascendingComparator : -ascendingComparator;
59
- };
60
-
46
+ var numRows = rowsLength !== null && rowsLength !== void 0 ? rowsLength : childrenCount;
47
+ setMax === null || setMax === void 0 ? void 0 : setMax(numRows);
48
+ }, [rowsLength, childrenCount, setMax]);
49
+ var sortedRows = useMemo(function () {
50
+ return rows === null || rows === void 0 ? void 0 : rows.map(function (row, idx) {
51
+ return _objectSpread(_objectSpread({}, row), {}, {
52
+ idx: idx
53
+ });
54
+ }).sort(sortFn);
55
+ }, [rows, sortFn]);
61
56
  return jsx(TableBodyProvider, {
62
57
  value: true
63
- }, jsx(Primitives.TBody, null, typeof children === 'function' && rows ? rows.map(function (row, idx) {
64
- return _objectSpread(_objectSpread({}, row), {}, {
65
- idx: idx
66
- });
67
- }).sort(sortFn).map(function (_ref2) {
58
+ }, jsx(Primitives.TBody, null, typeof children === 'function' && sortedRows ? sortedRows.map(function (_ref2) {
68
59
  var idx = _ref2.idx,
69
60
  row = _objectWithoutProperties(_ref2, _excluded);
70
61
 
@@ -1,13 +1,6 @@
1
1
  import React, { createContext, useContext } from 'react';
2
- import useSelectionReducer from './use-selectable';
3
- var SelectionContext = /*#__PURE__*/createContext([{
4
- checked: [],
5
- allChecked: false,
6
- anyChecked: false,
7
- maxChecked: 0,
8
- selectionStart: -1,
9
- previousSelection: []
10
- }, {}]);
2
+ import useSelectionReducer, { defaultSelectableState } from './use-selectable';
3
+ var SelectionContext = /*#__PURE__*/createContext([defaultSelectableState, {}]);
11
4
  /**
12
5
  * __Selection provider__
13
6
  *
@@ -1,12 +1,6 @@
1
1
  import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
-
4
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
5
-
6
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
7
-
8
2
  import { useCallback, useReducer } from 'react';
9
- var defaultState = {
3
+ export var defaultSelectableState = {
10
4
  checked: [],
11
5
  allChecked: false,
12
6
  anyChecked: false,
@@ -45,15 +39,15 @@ function reducer(_ref, action) {
45
39
  case 'toggle_selection':
46
40
  {
47
41
  var _action$value = action.value,
48
- id = _action$value.id,
49
- shiftHeld = _action$value.shiftHeld;
42
+ _id = _action$value.id,
43
+ _shiftHeld = _action$value.shiftHeld;
50
44
  var updated = checked.slice();
51
45
  var newSelectionStart = selectionStart;
52
46
  var newPreviousSelection = previousSelection.slice();
53
47
 
54
- if (shiftHeld) {
48
+ if (_shiftHeld) {
55
49
  if (checked.length > 0) {
56
- var newIds = arrayFromRange(selectionStart, id); // create an array of the new ids
50
+ var newIds = arrayFromRange(selectionStart, _id); // create an array of the new ids
57
51
  // Uncheck ids from the previous selection.
58
52
  // This is done to maintain consistency with Shift-select behaviour elsewhere (e.g. macOS)
59
53
  // TODO: Code could be improved to only remove ids that are not included in the new range, avoiding needing to re-add them below
@@ -68,15 +62,15 @@ function reducer(_ref, action) {
68
62
  newPreviousSelection = newIds; // Maintain an array of the previous selection to allow for consistent Shift-select behaviour
69
63
  }
70
64
  } else {
71
- var checkedIndex = checked.indexOf(id); // is index already checked
65
+ var checkedIndex = checked.indexOf(_id); // is index already checked
72
66
 
73
67
  if (checkedIndex !== -1) {
74
68
  updated.splice(checkedIndex, 1); // if index is already checked, uncheck
75
69
  } else {
76
- updated.push(id); // if index is not checked, check
70
+ updated.push(_id); // if index is not checked, check
77
71
  }
78
72
 
79
- newSelectionStart = id; // Reset selection start id to this non-shift-selected id
73
+ newSelectionStart = _id; // Reset selection start id to this non-shift-selected id
80
74
 
81
75
  newPreviousSelection = []; // Reset previous selection as it is no longer relevant once a new non-shift-selected id is added
82
76
  }
@@ -86,7 +80,7 @@ function reducer(_ref, action) {
86
80
  return {
87
81
  checked: updated,
88
82
  allChecked: updated.length === maxChecked,
89
- anyChecked: _anyChecked || Boolean(updated[id]),
83
+ anyChecked: _anyChecked || Boolean(updated[_id]),
90
84
  maxChecked: maxChecked,
91
85
  selectionStart: newSelectionStart,
92
86
  previousSelection: newPreviousSelection
@@ -105,10 +99,10 @@ function reducer(_ref, action) {
105
99
 
106
100
  case 'set_max':
107
101
  {
108
- var max = action.value;
102
+ var _max = action.value;
109
103
  return {
110
- maxChecked: max,
111
- allChecked: checked.length === max,
104
+ maxChecked: _max,
105
+ allChecked: checked.length === _max,
112
106
  anyChecked: anyChecked,
113
107
  checked: checked,
114
108
  selectionStart: selectionStart,
@@ -121,14 +115,8 @@ function reducer(_ref, action) {
121
115
  }
122
116
  }
123
117
 
124
- var initialiseState = function initialiseState() {
125
- return _objectSpread(_objectSpread({}, defaultState), {}, {
126
- checked: []
127
- });
128
- };
129
-
130
118
  function useSelectable() {
131
- var _useReducer = useReducer(reducer, initialiseState()),
119
+ var _useReducer = useReducer(reducer, defaultSelectableState),
132
120
  _useReducer2 = _slicedToArray(_useReducer, 2),
133
121
  state = _useReducer2[0],
134
122
  dispatch = _useReducer2[1];
@@ -38,9 +38,18 @@ export var useSorting = function useSorting(sortKey) {
38
38
  return localSortKey;
39
39
  });
40
40
  }, [toggleSortDirection]);
41
+ var sortFn = useCallback(function (rowA, rowB) {
42
+ if (localSortKey === 'unset') {
43
+ return 0;
44
+ }
45
+
46
+ var ascendingComparator = rowA[localSortKey] < rowB[localSortKey] ? -1 : 1;
47
+ return localSortDirection === 'ascending' ? ascendingComparator : -ascendingComparator;
48
+ }, [localSortDirection, localSortKey]);
41
49
  return {
42
50
  sortKey: localSortKey,
43
51
  sortDirection: localSortDirection,
44
- setSortState: setSortState
52
+ setSortState: setSortState,
53
+ sortFn: sortFn
45
54
  };
46
55
  };
@@ -5,7 +5,10 @@ function generateContext() {
5
5
  return /*#__PURE__*/createContext({
6
6
  isSelectable: false,
7
7
  sortKey: 'unset',
8
- setSortState: __noop
8
+ setSortState: __noop,
9
+ sortFn: function sortFn() {
10
+ return 0;
11
+ }
9
12
  });
10
13
  }
11
14