@atlaskit/table 0.1.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/CHANGELOG.md +7 -0
- package/LICENSE.md +13 -0
- package/README.md +83 -0
- package/constellation/index/examples.mdx +19 -0
- package/constellation/index/props.mdx +32 -0
- package/dist/cjs/body.js +105 -0
- package/dist/cjs/head-cell.js +42 -0
- package/dist/cjs/hooks/selection-provider.js +52 -0
- package/dist/cjs/hooks/use-row-id.js +26 -0
- package/dist/cjs/hooks/use-selectable.js +183 -0
- package/dist/cjs/hooks/use-sorting.js +59 -0
- package/dist/cjs/hooks/use-table-body.js +28 -0
- package/dist/cjs/hooks/use-table.js +56 -0
- package/dist/cjs/index.js +63 -0
- package/dist/cjs/row.js +70 -0
- package/dist/cjs/selectable-cell.js +53 -0
- package/dist/cjs/sortable-column.js +129 -0
- package/dist/cjs/table.js +60 -0
- package/dist/cjs/thead.js +73 -0
- package/dist/cjs/ui/base-cell.js +72 -0
- package/dist/cjs/ui/bulk-action-overlay.js +39 -0
- package/dist/cjs/ui/index.js +69 -0
- package/dist/cjs/ui/selectable-cell.js +39 -0
- package/dist/cjs/ui/table.js +31 -0
- package/dist/cjs/ui/tbody.js +38 -0
- package/dist/cjs/ui/td.js +38 -0
- package/dist/cjs/ui/th.js +40 -0
- package/dist/cjs/ui/thead.js +40 -0
- package/dist/cjs/ui/tr.js +68 -0
- package/dist/cjs/version.json +5 -0
- package/dist/es2019/body.js +66 -0
- package/dist/es2019/head-cell.js +31 -0
- package/dist/es2019/hooks/selection-provider.js +33 -0
- package/dist/es2019/hooks/use-row-id.js +13 -0
- package/dist/es2019/hooks/use-selectable.js +158 -0
- package/dist/es2019/hooks/use-sorting.js +37 -0
- package/dist/es2019/hooks/use-table-body.js +13 -0
- package/dist/es2019/hooks/use-table.js +34 -0
- package/dist/es2019/index.js +7 -0
- package/dist/es2019/row.js +41 -0
- package/dist/es2019/selectable-cell.js +25 -0
- package/dist/es2019/sortable-column.js +109 -0
- package/dist/es2019/table.js +38 -0
- package/dist/es2019/thead.js +46 -0
- package/dist/es2019/ui/base-cell.js +54 -0
- package/dist/es2019/ui/bulk-action-overlay.js +27 -0
- package/dist/es2019/ui/index.js +11 -0
- package/dist/es2019/ui/selectable-cell.js +28 -0
- package/dist/es2019/ui/table.js +22 -0
- package/dist/es2019/ui/tbody.js +26 -0
- package/dist/es2019/ui/td.js +21 -0
- package/dist/es2019/ui/th.js +26 -0
- package/dist/es2019/ui/thead.js +31 -0
- package/dist/es2019/ui/tr.js +55 -0
- package/dist/es2019/version.json +5 -0
- package/dist/esm/body.js +84 -0
- package/dist/esm/head-cell.js +31 -0
- package/dist/esm/hooks/selection-provider.js +32 -0
- package/dist/esm/hooks/use-row-id.js +15 -0
- package/dist/esm/hooks/use-selectable.js +171 -0
- package/dist/esm/hooks/use-sorting.js +46 -0
- package/dist/esm/hooks/use-table-body.js +13 -0
- package/dist/esm/hooks/use-table.js +36 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/row.js +46 -0
- package/dist/esm/selectable-cell.js +31 -0
- package/dist/esm/sortable-column.js +114 -0
- package/dist/esm/table.js +39 -0
- package/dist/esm/thead.js +51 -0
- package/dist/esm/ui/base-cell.js +58 -0
- package/dist/esm/ui/bulk-action-overlay.js +28 -0
- package/dist/esm/ui/index.js +11 -0
- package/dist/esm/ui/selectable-cell.js +28 -0
- package/dist/esm/ui/table.js +21 -0
- package/dist/esm/ui/tbody.js +27 -0
- package/dist/esm/ui/td.js +26 -0
- package/dist/esm/ui/th.js +28 -0
- package/dist/esm/ui/thead.js +30 -0
- package/dist/esm/ui/tr.js +55 -0
- package/dist/esm/version.json +5 -0
- package/dist/types/body.d.ts +15 -0
- package/dist/types/head-cell.d.ts +10 -0
- package/dist/types/hooks/selection-provider.d.ts +25 -0
- package/dist/types/hooks/use-row-id.d.ts +10 -0
- package/dist/types/hooks/use-selectable.d.ts +14 -0
- package/dist/types/hooks/use-sorting.d.ts +6 -0
- package/dist/types/hooks/use-table-body.d.ts +7 -0
- package/dist/types/hooks/use-table.d.ts +21 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/row.d.ts +14 -0
- package/dist/types/selectable-cell.d.ts +3 -0
- package/dist/types/sortable-column.d.ts +17 -0
- package/dist/types/table.d.ts +26 -0
- package/dist/types/thead.d.ts +7 -0
- package/dist/types/ui/base-cell.d.ts +30 -0
- package/dist/types/ui/bulk-action-overlay.d.ts +8 -0
- package/dist/types/ui/index.d.ts +11 -0
- package/dist/types/ui/selectable-cell.d.ts +10 -0
- package/dist/types/ui/table.d.ts +23 -0
- package/dist/types/ui/tbody.d.ts +8 -0
- package/dist/types/ui/td.d.ts +11 -0
- package/dist/types/ui/th.d.ts +11 -0
- package/dist/types/ui/thead.d.ts +10 -0
- package/dist/types/ui/tr.d.ts +16 -0
- package/package.json +101 -0
- package/primitives/package.json +15 -0
- package/report.api.md +139 -0
- package/tmp/api-report-tmp.d.ts +99 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/* eslint-disable no-unused-vars */
|
|
2
|
+
|
|
3
|
+
/** @jsx jsx */
|
|
4
|
+
import { jsx } from '@emotion/react';
|
|
5
|
+
import { UNSAFE_Text as Text } from '@atlaskit/ds-explorations';
|
|
6
|
+
import { TH } from './ui/th';
|
|
7
|
+
/**
|
|
8
|
+
* __HeadCell__
|
|
9
|
+
*
|
|
10
|
+
* HeadCell element
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
var Column = function Column(_ref) {
|
|
14
|
+
var children = _ref.children,
|
|
15
|
+
align = _ref.align,
|
|
16
|
+
testId = _ref.testId,
|
|
17
|
+
backgroundColor = _ref.backgroundColor,
|
|
18
|
+
_ref$scope = _ref.scope,
|
|
19
|
+
scope = _ref$scope === void 0 ? 'col' : _ref$scope;
|
|
20
|
+
return jsx(TH, {
|
|
21
|
+
scope: scope,
|
|
22
|
+
align: align,
|
|
23
|
+
testId: testId,
|
|
24
|
+
backgroundColor: backgroundColor
|
|
25
|
+
}, jsx(Text, {
|
|
26
|
+
color: "color.text",
|
|
27
|
+
fontWeight: "500"
|
|
28
|
+
}, children));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export default Column;
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
}, {}]);
|
|
11
|
+
/**
|
|
12
|
+
* __Selection provider__
|
|
13
|
+
*
|
|
14
|
+
* A selection provider injects selection specific state into the table.
|
|
15
|
+
*
|
|
16
|
+
* - [Examples](https://atlassian.design/components/{packageName}/examples)
|
|
17
|
+
* - [Code](https://atlassian.design/components/{packageName}/code)
|
|
18
|
+
* - [Usage](https://atlassian.design/components/{packageName}/usage)
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
var SelectionProvider = function SelectionProvider(_ref) {
|
|
22
|
+
var children = _ref.children;
|
|
23
|
+
var reducer = useSelectionReducer();
|
|
24
|
+
return /*#__PURE__*/React.createElement(SelectionContext.Provider, {
|
|
25
|
+
value: reducer
|
|
26
|
+
}, children);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export var useSelection = function useSelection() {
|
|
30
|
+
return useContext(SelectionContext);
|
|
31
|
+
};
|
|
32
|
+
export default SelectionProvider;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
var RowContext = /*#__PURE__*/createContext(undefined);
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export var useRowId = function useRowId() {
|
|
8
|
+
return useContext(RowContext);
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* __Row provider__
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export var RowProvider = RowContext.Provider;
|
|
@@ -0,0 +1,171 @@
|
|
|
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
|
+
import { useCallback, useReducer } from 'react';
|
|
9
|
+
var defaultState = {
|
|
10
|
+
checked: [],
|
|
11
|
+
allChecked: false,
|
|
12
|
+
anyChecked: false,
|
|
13
|
+
maxChecked: 0,
|
|
14
|
+
selectionStart: -1,
|
|
15
|
+
previousSelection: []
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
var arrayFromRange = function arrayFromRange(from, to) {
|
|
19
|
+
var startIdx = from;
|
|
20
|
+
var stopIdx = to;
|
|
21
|
+
var increment = 1;
|
|
22
|
+
|
|
23
|
+
if (from > to) {
|
|
24
|
+
startIdx = to;
|
|
25
|
+
stopIdx = from;
|
|
26
|
+
increment = 0;
|
|
27
|
+
} // Create an array with values between `from` and `to` - either ascending or descending
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
return Array.from({
|
|
31
|
+
length: stopIdx - startIdx
|
|
32
|
+
}, function (_, i) {
|
|
33
|
+
return startIdx + i + increment;
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
function reducer(_ref, action) {
|
|
38
|
+
var checked = _ref.checked,
|
|
39
|
+
anyChecked = _ref.anyChecked,
|
|
40
|
+
maxChecked = _ref.maxChecked,
|
|
41
|
+
selectionStart = _ref.selectionStart,
|
|
42
|
+
previousSelection = _ref.previousSelection;
|
|
43
|
+
|
|
44
|
+
switch (action.type) {
|
|
45
|
+
case 'toggle_selection':
|
|
46
|
+
{
|
|
47
|
+
var _action$value = action.value,
|
|
48
|
+
id = _action$value.id,
|
|
49
|
+
shiftHeld = _action$value.shiftHeld;
|
|
50
|
+
var updated = checked.slice();
|
|
51
|
+
var newSelectionStart = selectionStart;
|
|
52
|
+
var newPreviousSelection = previousSelection.slice();
|
|
53
|
+
|
|
54
|
+
if (shiftHeld) {
|
|
55
|
+
if (checked.length > 0) {
|
|
56
|
+
var newIds = arrayFromRange(selectionStart, id); // create an array of the new ids
|
|
57
|
+
// Uncheck ids from the previous selection.
|
|
58
|
+
// This is done to maintain consistency with Shift-select behaviour elsewhere (e.g. macOS)
|
|
59
|
+
// TODO: Code could be improved to only remove ids that are not included in the new range, avoiding needing to re-add them below
|
|
60
|
+
|
|
61
|
+
updated = updated.filter(function (id) {
|
|
62
|
+
return !previousSelection.includes(id);
|
|
63
|
+
});
|
|
64
|
+
newIds.forEach(function (id) {
|
|
65
|
+
return updated.indexOf(id) === -1 && updated.push(id);
|
|
66
|
+
} // If the new id is not already checked, check it
|
|
67
|
+
);
|
|
68
|
+
newPreviousSelection = newIds; // Maintain an array of the previous selection to allow for consistent Shift-select behaviour
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
var checkedIndex = checked.indexOf(id); // is index already checked
|
|
72
|
+
|
|
73
|
+
if (checkedIndex !== -1) {
|
|
74
|
+
updated.splice(checkedIndex, 1); // if index is already checked, uncheck
|
|
75
|
+
} else {
|
|
76
|
+
updated.push(id); // if index is not checked, check
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
newSelectionStart = id; // Reset selection start id to this non-shift-selected id
|
|
80
|
+
|
|
81
|
+
newPreviousSelection = []; // Reset previous selection as it is no longer relevant once a new non-shift-selected id is added
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
var _anyChecked = updated.length > 0;
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
checked: updated,
|
|
88
|
+
allChecked: updated.length === maxChecked,
|
|
89
|
+
anyChecked: _anyChecked || Boolean(updated[id]),
|
|
90
|
+
maxChecked: maxChecked,
|
|
91
|
+
selectionStart: newSelectionStart,
|
|
92
|
+
previousSelection: newPreviousSelection
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
case 'set_root':
|
|
97
|
+
return {
|
|
98
|
+
checked: action.value ? Array.from(Array(maxChecked).keys()) : [],
|
|
99
|
+
allChecked: action.value,
|
|
100
|
+
anyChecked: Boolean(action.value),
|
|
101
|
+
maxChecked: maxChecked,
|
|
102
|
+
selectionStart: selectionStart,
|
|
103
|
+
previousSelection: previousSelection
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
case 'set_max':
|
|
107
|
+
{
|
|
108
|
+
var max = action.value;
|
|
109
|
+
return {
|
|
110
|
+
maxChecked: max,
|
|
111
|
+
allChecked: checked.length === max,
|
|
112
|
+
anyChecked: anyChecked,
|
|
113
|
+
checked: checked,
|
|
114
|
+
selectionStart: selectionStart,
|
|
115
|
+
previousSelection: previousSelection
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
default:
|
|
120
|
+
throw new Error();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
var initialiseState = function initialiseState() {
|
|
125
|
+
return _objectSpread(_objectSpread({}, defaultState), {}, {
|
|
126
|
+
checked: []
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
function useSelectable() {
|
|
131
|
+
var _useReducer = useReducer(reducer, initialiseState()),
|
|
132
|
+
_useReducer2 = _slicedToArray(_useReducer, 2),
|
|
133
|
+
state = _useReducer2[0],
|
|
134
|
+
dispatch = _useReducer2[1];
|
|
135
|
+
|
|
136
|
+
var toggleSelection = useCallback(function (id, shiftHeld) {
|
|
137
|
+
dispatch({
|
|
138
|
+
type: 'toggle_selection',
|
|
139
|
+
value: {
|
|
140
|
+
id: id,
|
|
141
|
+
shiftHeld: shiftHeld
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}, []);
|
|
145
|
+
var setAll = useCallback(function () {
|
|
146
|
+
dispatch({
|
|
147
|
+
type: 'set_root',
|
|
148
|
+
value: true
|
|
149
|
+
});
|
|
150
|
+
}, []);
|
|
151
|
+
var removeAll = useCallback(function () {
|
|
152
|
+
dispatch({
|
|
153
|
+
type: 'set_root',
|
|
154
|
+
value: false
|
|
155
|
+
});
|
|
156
|
+
}, []);
|
|
157
|
+
var setMax = useCallback(function (max) {
|
|
158
|
+
dispatch({
|
|
159
|
+
type: 'set_max',
|
|
160
|
+
value: max
|
|
161
|
+
});
|
|
162
|
+
}, []);
|
|
163
|
+
return [state, {
|
|
164
|
+
setAll: setAll,
|
|
165
|
+
removeAll: removeAll,
|
|
166
|
+
toggleSelection: toggleSelection,
|
|
167
|
+
setMax: setMax
|
|
168
|
+
}];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export default useSelectable;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import { useCallback, useState } from 'react';
|
|
3
|
+
export var useSorting = function useSorting(sortKey) {
|
|
4
|
+
var _useState = useState(sortKey),
|
|
5
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
6
|
+
localSortKey = _useState2[0],
|
|
7
|
+
setLocalSortKey = _useState2[1];
|
|
8
|
+
|
|
9
|
+
var _useState3 = useState(),
|
|
10
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
11
|
+
localSortDirection = _useState4[0],
|
|
12
|
+
setLocalSortDirection = _useState4[1];
|
|
13
|
+
|
|
14
|
+
var toggleSortDirection = useCallback(function () {
|
|
15
|
+
setLocalSortDirection(function (oldLocalSortDirection) {
|
|
16
|
+
switch (oldLocalSortDirection) {
|
|
17
|
+
case undefined:
|
|
18
|
+
return 'ascending';
|
|
19
|
+
|
|
20
|
+
case 'ascending':
|
|
21
|
+
return 'descending';
|
|
22
|
+
|
|
23
|
+
case 'descending':
|
|
24
|
+
return 'ascending';
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}, []);
|
|
28
|
+
var setSortState = useCallback(function (key) {
|
|
29
|
+
setLocalSortKey(function (localSortKey) {
|
|
30
|
+
if (key !== localSortKey) {
|
|
31
|
+
// sorting by different column
|
|
32
|
+
setLocalSortDirection('ascending');
|
|
33
|
+
return key;
|
|
34
|
+
} else {
|
|
35
|
+
toggleSortDirection();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return localSortKey;
|
|
39
|
+
});
|
|
40
|
+
}, [toggleSortDirection]);
|
|
41
|
+
return {
|
|
42
|
+
sortKey: localSortKey,
|
|
43
|
+
sortDirection: localSortDirection,
|
|
44
|
+
setSortState: setSortState
|
|
45
|
+
};
|
|
46
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
import invariant from 'tiny-invariant';
|
|
3
|
+
var TableBodyContext = /*#__PURE__*/createContext(false);
|
|
4
|
+
/**
|
|
5
|
+
* __Table body provider__
|
|
6
|
+
* Ensures correct nesting of table elements.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export var TableBodyProvider = TableBodyContext.Provider;
|
|
10
|
+
export var useTableBody = function useTableBody() {
|
|
11
|
+
var hasTableBody = useContext(TableBodyContext);
|
|
12
|
+
invariant(hasTableBody, '<Row /> must be nested inside a <TableBody>');
|
|
13
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import React, { createContext, useContext } from 'react';
|
|
2
|
+
import __noop from '@atlaskit/ds-lib/noop';
|
|
3
|
+
|
|
4
|
+
function generateContext() {
|
|
5
|
+
return /*#__PURE__*/createContext({
|
|
6
|
+
isSelectable: false,
|
|
7
|
+
sortKey: 'unset',
|
|
8
|
+
setSortState: __noop
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
var TableContext = generateContext();
|
|
13
|
+
/**
|
|
14
|
+
* __Table state provider__
|
|
15
|
+
*
|
|
16
|
+
* The table context provides the data required for more complex functionality.
|
|
17
|
+
*
|
|
18
|
+
* - [Examples](https://atlassian.design/components/table/examples)
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export function TableProvider(_ref) {
|
|
22
|
+
var children = _ref.children,
|
|
23
|
+
state = _ref.state;
|
|
24
|
+
return (
|
|
25
|
+
/*#__PURE__*/
|
|
26
|
+
// @ts-expect-error
|
|
27
|
+
React.createElement(TableContext.Provider, {
|
|
28
|
+
value: state
|
|
29
|
+
}, children)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
export var useTable = function useTable() {
|
|
33
|
+
return (// @ts-expect-error
|
|
34
|
+
useContext(TableContext)
|
|
35
|
+
);
|
|
36
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default } from './table';
|
|
2
|
+
export { default as TBody } from './body';
|
|
3
|
+
export { default as Row } from './row';
|
|
4
|
+
export { TD as Cell } from './ui';
|
|
5
|
+
export { default as HeadCell } from './head-cell';
|
|
6
|
+
export { default as SortableColumn } from './sortable-column';
|
|
7
|
+
export { default as THead } from './thead';
|
package/dist/esm/row.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
|
|
3
|
+
/** @jsx jsx */
|
|
4
|
+
import { memo } from 'react';
|
|
5
|
+
import { jsx } from '@emotion/react';
|
|
6
|
+
import { useSelection } from './hooks/selection-provider';
|
|
7
|
+
import { useRowId } from './hooks/use-row-id';
|
|
8
|
+
import { useTable } from './hooks/use-table';
|
|
9
|
+
import { useTableBody } from './hooks/use-table-body';
|
|
10
|
+
import SelectableCell from './selectable-cell';
|
|
11
|
+
import * as Primitives from './ui';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* __Row__
|
|
15
|
+
*
|
|
16
|
+
* A table row.
|
|
17
|
+
*
|
|
18
|
+
* - [Examples](https://atlassian.design/components/table/examples)
|
|
19
|
+
*/
|
|
20
|
+
var Row = /*#__PURE__*/memo(function (_ref) {
|
|
21
|
+
var children = _ref.children,
|
|
22
|
+
testId = _ref.testId;
|
|
23
|
+
// To ensure valid nesting
|
|
24
|
+
useTableBody(); // to access table state
|
|
25
|
+
|
|
26
|
+
var table = useTable();
|
|
27
|
+
|
|
28
|
+
var _useSelection = useSelection(),
|
|
29
|
+
_useSelection2 = _slicedToArray(_useSelection, 1),
|
|
30
|
+
selectionState = _useSelection2[0];
|
|
31
|
+
|
|
32
|
+
var rowId = useRowId();
|
|
33
|
+
|
|
34
|
+
if (!table.isSelectable) {
|
|
35
|
+
return jsx(Primitives.TR, {
|
|
36
|
+
testId: testId
|
|
37
|
+
}, children);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
var isChecked = selectionState.allChecked || selectionState.checked.includes(rowId);
|
|
41
|
+
return jsx(Primitives.TR, {
|
|
42
|
+
isSelected: !!isChecked,
|
|
43
|
+
testId: testId
|
|
44
|
+
}, jsx(SelectableCell, null), children);
|
|
45
|
+
});
|
|
46
|
+
export default Row;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
|
|
3
|
+
/** @jsx jsx */
|
|
4
|
+
import { memo, useCallback } from 'react';
|
|
5
|
+
import { jsx } from '@emotion/react';
|
|
6
|
+
import Checkbox from '@atlaskit/checkbox';
|
|
7
|
+
import { useSelection } from './hooks/selection-provider';
|
|
8
|
+
import { useRowId } from './hooks/use-row-id';
|
|
9
|
+
import * as Primitives from './ui';
|
|
10
|
+
|
|
11
|
+
var SelectableCell = function SelectableCell() {
|
|
12
|
+
var _useSelection = useSelection(),
|
|
13
|
+
_useSelection2 = _slicedToArray(_useSelection, 2),
|
|
14
|
+
state = _useSelection2[0],
|
|
15
|
+
toggleSelection = _useSelection2[1].toggleSelection;
|
|
16
|
+
|
|
17
|
+
var idx = useRowId();
|
|
18
|
+
var isChecked = state.allChecked || state.checked.includes(idx);
|
|
19
|
+
var onChange = useCallback(function (e) {
|
|
20
|
+
return toggleSelection(idx, e.nativeEvent.shiftKey);
|
|
21
|
+
}, // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
22
|
+
[idx]);
|
|
23
|
+
return jsx(Primitives.SelectableCell, {
|
|
24
|
+
as: "td"
|
|
25
|
+
}, jsx(Checkbox, {
|
|
26
|
+
isChecked: isChecked,
|
|
27
|
+
onChange: onChange
|
|
28
|
+
}));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export default /*#__PURE__*/memo(SelectableCell);
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/extends";
|
|
2
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
|
3
|
+
var _excluded = ["name", "testId", "onClick", "children"];
|
|
4
|
+
|
|
5
|
+
/* eslint-disable no-unused-vars */
|
|
6
|
+
|
|
7
|
+
/** @jsx jsx */
|
|
8
|
+
import { css, jsx } from '@emotion/react';
|
|
9
|
+
import Button from '@atlaskit/button';
|
|
10
|
+
import ArrowDownIcon from '@atlaskit/icon/glyph/arrow-down';
|
|
11
|
+
import ArrowUpIcon from '@atlaskit/icon/glyph/arrow-up';
|
|
12
|
+
import Tooltip from '@atlaskit/tooltip';
|
|
13
|
+
import { useTable } from './hooks/use-table';
|
|
14
|
+
import { TH } from './ui';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* TODO these need to be i18n supported
|
|
18
|
+
*/
|
|
19
|
+
var sortingMessages = {
|
|
20
|
+
unsorted: {
|
|
21
|
+
string: 'Sort from A to Z',
|
|
22
|
+
number: 'Sort from 0 to 9'
|
|
23
|
+
},
|
|
24
|
+
ascending: {
|
|
25
|
+
string: 'Sort from A to Z',
|
|
26
|
+
number: 'Sort from 0 to 9'
|
|
27
|
+
},
|
|
28
|
+
descending: {
|
|
29
|
+
string: 'Sort from Z to A',
|
|
30
|
+
number: 'Sort from 9 to 0'
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var overrideStyles = css({
|
|
34
|
+
// eslint-disable-next-line @atlaskit/design-system/ensure-design-token-usage-spacing
|
|
35
|
+
margin: '0 -2px !important',
|
|
36
|
+
gap: "var(--ds-space-050, 4px)",
|
|
37
|
+
// eslint-disable-next-line @atlaskit/design-system/ensure-design-token-usage-spacing
|
|
38
|
+
paddingInline: "2px !important"
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* __SortableColumn__
|
|
42
|
+
*
|
|
43
|
+
* SortableColumn is used in place of the default Column when sorting is desired.
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
var SortableColumn = function SortableColumn(_ref) {
|
|
47
|
+
var name = _ref.name,
|
|
48
|
+
testId = _ref.testId,
|
|
49
|
+
onClick = _ref.onClick,
|
|
50
|
+
children = _ref.children,
|
|
51
|
+
other = _objectWithoutProperties(_ref, _excluded);
|
|
52
|
+
|
|
53
|
+
var _useTable = useTable(),
|
|
54
|
+
sortKey = _useTable.sortKey,
|
|
55
|
+
sortDirection = _useTable.sortDirection,
|
|
56
|
+
setSortState = _useTable.setSortState;
|
|
57
|
+
|
|
58
|
+
var getSortMessage = function getSortMessage() {
|
|
59
|
+
if (sortKey === name) {
|
|
60
|
+
// TODO: Change message depending on data type (string/number)
|
|
61
|
+
return sortingMessages[sortDirection || 'unsorted'].string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return sortingMessages.unsorted.string;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
var getSortIcon = function getSortIcon() {
|
|
68
|
+
if (sortKey === name) {
|
|
69
|
+
switch (sortDirection) {
|
|
70
|
+
case undefined:
|
|
71
|
+
return undefined;
|
|
72
|
+
|
|
73
|
+
case 'ascending':
|
|
74
|
+
return jsx(ArrowUpIcon, {
|
|
75
|
+
size: "small",
|
|
76
|
+
label: "",
|
|
77
|
+
primaryColor: "inherit"
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
case 'descending':
|
|
81
|
+
return jsx(ArrowDownIcon, {
|
|
82
|
+
size: "small",
|
|
83
|
+
label: "",
|
|
84
|
+
primaryColor: "inherit"
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return undefined;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
return (// eslint-disable-next-line @repo/internal/react/no-unsafe-spread-props
|
|
93
|
+
jsx(TH, _extends({
|
|
94
|
+
testId: testId
|
|
95
|
+
}, other), jsx(Tooltip, {
|
|
96
|
+
content: getSortMessage(),
|
|
97
|
+
position: "top"
|
|
98
|
+
}, function (tooltipProps) {
|
|
99
|
+
return jsx(Button, _extends({
|
|
100
|
+
spacing: "compact",
|
|
101
|
+
appearance: "subtle",
|
|
102
|
+
iconAfter: getSortIcon()
|
|
103
|
+
}, tooltipProps, {
|
|
104
|
+
// @ts-expect-error
|
|
105
|
+
onClick: function onClick() {
|
|
106
|
+
return setSortState(name);
|
|
107
|
+
},
|
|
108
|
+
css: overrideStyles
|
|
109
|
+
}), children);
|
|
110
|
+
}))
|
|
111
|
+
);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export default SortableColumn;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** @jsx jsx */
|
|
2
|
+
import { jsx } from '@emotion/react';
|
|
3
|
+
import SelectionProvider from './hooks/selection-provider';
|
|
4
|
+
import { useSorting } from './hooks/use-sorting';
|
|
5
|
+
import { TableProvider } from './hooks/use-table';
|
|
6
|
+
import * as Primitives from './ui';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* __Table__
|
|
10
|
+
*
|
|
11
|
+
* A data table is used to display dynamic data.
|
|
12
|
+
*
|
|
13
|
+
* - [Examples](https://atlassian.design/components/table/examples)
|
|
14
|
+
*/
|
|
15
|
+
function Table(_ref) {
|
|
16
|
+
var children = _ref.children,
|
|
17
|
+
isSelectable = _ref.isSelectable,
|
|
18
|
+
_ref$sortKey = _ref.sortKey,
|
|
19
|
+
sortKey = _ref$sortKey === void 0 ? 'unset' : _ref$sortKey,
|
|
20
|
+
testId = _ref.testId;
|
|
21
|
+
|
|
22
|
+
var _useSorting = useSorting(sortKey),
|
|
23
|
+
localSortKey = _useSorting.sortKey,
|
|
24
|
+
sortDirection = _useSorting.sortDirection,
|
|
25
|
+
setSortState = _useSorting.setSortState;
|
|
26
|
+
|
|
27
|
+
return jsx(TableProvider, {
|
|
28
|
+
state: {
|
|
29
|
+
isSelectable: isSelectable,
|
|
30
|
+
sortKey: localSortKey,
|
|
31
|
+
sortDirection: sortDirection,
|
|
32
|
+
setSortState: setSortState
|
|
33
|
+
}
|
|
34
|
+
}, jsx(Primitives.Table, {
|
|
35
|
+
testId: testId
|
|
36
|
+
}, isSelectable ? jsx(SelectionProvider, null, children) : children));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default Table;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
|
|
3
|
+
/** @jsx jsx */
|
|
4
|
+
import { jsx } from '@emotion/react';
|
|
5
|
+
import Checkbox from '@atlaskit/checkbox';
|
|
6
|
+
import { UNSAFE_Inline as Inline, UNSAFE_Text as Text } from '@atlaskit/ds-explorations';
|
|
7
|
+
import VisuallyHidden from '@atlaskit/visually-hidden';
|
|
8
|
+
import { useSelection } from './hooks/selection-provider';
|
|
9
|
+
import { useTable } from './hooks/use-table';
|
|
10
|
+
import * as Primitives from './ui';
|
|
11
|
+
|
|
12
|
+
var THead = function THead(_ref) {
|
|
13
|
+
var actions = _ref.actions,
|
|
14
|
+
children = _ref.children;
|
|
15
|
+
var table = useTable();
|
|
16
|
+
|
|
17
|
+
var _useSelection = useSelection(),
|
|
18
|
+
_useSelection2 = _slicedToArray(_useSelection, 2),
|
|
19
|
+
state = _useSelection2[0],
|
|
20
|
+
_useSelection2$ = _useSelection2[1],
|
|
21
|
+
setAll = _useSelection2$.setAll,
|
|
22
|
+
removeAll = _useSelection2$.removeAll;
|
|
23
|
+
|
|
24
|
+
var isChecked = state.allChecked || state.anyChecked;
|
|
25
|
+
|
|
26
|
+
if (!table.isSelectable) {
|
|
27
|
+
return jsx(Primitives.THead, null, jsx(Primitives.TR, {
|
|
28
|
+
isBodyRow: false
|
|
29
|
+
}, children));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return jsx(Primitives.THead, null, jsx(Primitives.TR, {
|
|
33
|
+
isBodyRow: false
|
|
34
|
+
}, jsx(Primitives.SelectableCell, {
|
|
35
|
+
as: "th"
|
|
36
|
+
}, jsx(Checkbox, {
|
|
37
|
+
"aria-labelledby": "select-all",
|
|
38
|
+
onChange: isChecked ? removeAll : setAll,
|
|
39
|
+
isChecked: isChecked,
|
|
40
|
+
isIndeterminate: state.anyChecked && !state.allChecked
|
|
41
|
+
}), jsx(VisuallyHidden, {
|
|
42
|
+
id: "select-all"
|
|
43
|
+
}, "Select all rows")), children, isChecked && jsx(Primitives.BulkActionOverlay, null, jsx(Text, {
|
|
44
|
+
color: "color.text",
|
|
45
|
+
fontWeight: "500"
|
|
46
|
+
}, state.checked.length, " selected"), actions && jsx(Inline, {
|
|
47
|
+
gap: "scale.100"
|
|
48
|
+
}, actions(state.checked)))));
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export default THead;
|