@vitessce/feature-list 2.0.3-beta.0 → 3.0.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.
Files changed (32) hide show
  1. package/dist/index.js +26866 -2
  2. package/dist-tsc/FeatureList.d.ts +2 -0
  3. package/dist-tsc/FeatureList.d.ts.map +1 -0
  4. package/{dist → dist-tsc}/FeatureList.js +17 -8
  5. package/dist-tsc/FeatureListSubscriber.d.ts +32 -0
  6. package/dist-tsc/FeatureListSubscriber.d.ts.map +1 -0
  7. package/{dist → dist-tsc}/FeatureListSubscriber.js +8 -7
  8. package/dist-tsc/index.d.ts +3 -0
  9. package/dist-tsc/index.d.ts.map +1 -0
  10. package/dist-tsc/index.js +2 -0
  11. package/dist-tsc/selectable-table/SelectableTable.d.ts +21 -0
  12. package/dist-tsc/selectable-table/SelectableTable.d.ts.map +1 -0
  13. package/{dist → dist-tsc}/selectable-table/SelectableTable.js +12 -9
  14. package/dist-tsc/selectable-table/SelectableTable.test.d.ts +2 -0
  15. package/dist-tsc/selectable-table/SelectableTable.test.d.ts.map +1 -0
  16. package/{dist → dist-tsc}/selectable-table/SelectableTable.test.js +29 -7
  17. package/dist-tsc/selectable-table/index.d.ts +2 -0
  18. package/dist-tsc/selectable-table/index.d.ts.map +1 -0
  19. package/dist-tsc/selectable-table/index.js +1 -0
  20. package/dist-tsc/selectable-table/styles.d.ts +2 -0
  21. package/dist-tsc/selectable-table/styles.d.ts.map +1 -0
  22. package/{dist → dist-tsc}/selectable-table/styles.js +6 -2
  23. package/package.json +23 -11
  24. package/src/FeatureList.js +108 -0
  25. package/src/FeatureListSubscriber.js +119 -0
  26. package/src/index.js +2 -0
  27. package/src/selectable-table/SelectableTable.js +221 -0
  28. package/src/selectable-table/SelectableTable.test.jsx +145 -0
  29. package/src/selectable-table/index.js +1 -0
  30. package/src/selectable-table/styles.js +75 -0
  31. package/dist/selectable-table/index.js +0 -1
  32. package/dist/selectable-table/style.js +0 -1
@@ -0,0 +1,221 @@
1
+ /* eslint-disable jsx-a11y/no-noninteractive-element-to-interactive-role */
2
+ /* eslint-disable jsx-a11y/click-events-have-key-events */
3
+ /* eslint-disable max-len */
4
+ import React, { useEffect, useCallback, useState } from 'react';
5
+ // The CommonJS imports for react-virtualized are needed
6
+ // because the ESM version is broken.
7
+ // Reference: https://github.com/bvaughn/react-virtualized/issues/1632
8
+ import { AutoSizer } from 'react-virtualized/dist/commonjs/AutoSizer/index.js';
9
+ import { Table } from 'react-virtualized/dist/commonjs/Table/index.js';
10
+ import { v4 as uuidv4 } from 'uuid';
11
+ import { union, difference, isEqual } from 'lodash-es';
12
+ import clsx from 'clsx';
13
+ import { useStyles } from './styles.js';
14
+
15
+ const SHIFT_KEYCODE = 16;
16
+
17
+ /**
18
+ * A table with "selectable" rows.
19
+ * @prop {string[]} columns An array of column names, corresponding to data object properties.
20
+ * @prop {string[]} columnLabels An array of labels to be used for the columns, corresponding to `columns`.
21
+ * columnLabels.length must be equal to columns.length.
22
+ * @prop {object[]} data An array of data objects used to populate table rows.
23
+ * @prop {function} onChange Callback function,
24
+ * passed a selection object when `allowMultiple` is false (and `null` if `allowUncheck` is true),
25
+ * or passed an array of selection objects when `allowMultiple` is true.
26
+ * @prop {string} idKey The key for a unique identifier property of `data` objects.
27
+ * @prop {string} valueKey If initially-selected rows are required,
28
+ * this key specifies a boolean property of the `data` objects
29
+ * indicating those rows that should be initially selected.
30
+ * @prop {boolean} allowMultiple Whether to allow multiple rows to be selected.
31
+ * @prop {boolean} allowUncheck Whether to allow selected rows to be un-checked. By default, false.
32
+ * @prop {boolean} showTableHead Whether to show the table header element. By default, true.
33
+ * @prop {boolean} showTableInputs Whether to show the table input elements for each row.
34
+ * By default, false.
35
+ */
36
+ export default function SelectableTable(props) {
37
+ const {
38
+ hasColorEncoding,
39
+ columns,
40
+ columnLabels,
41
+ data,
42
+ onChange,
43
+ idKey = 'id',
44
+ valueKey = 'value',
45
+ allowMultiple = false,
46
+ allowUncheck = false,
47
+ showTableHead = true,
48
+ showTableInputs = false,
49
+ testHeight = undefined,
50
+ testWidth = undefined,
51
+ } = props;
52
+
53
+ const [selectedRows, setSelectedRows] = useState(null);
54
+ const [isCheckingMultiple, setIsCheckingMultiple] = useState(false);
55
+
56
+ // Enable selecting multiple rows while the shift key is down.
57
+ useEffect(() => {
58
+ function onKeyDown(event) {
59
+ if (allowMultiple && event.keyCode === SHIFT_KEYCODE) {
60
+ setIsCheckingMultiple(true);
61
+ }
62
+ }
63
+
64
+ function onKeyUp(event) {
65
+ if (allowMultiple && event.keyCode === SHIFT_KEYCODE) {
66
+ setIsCheckingMultiple(false);
67
+ }
68
+ }
69
+
70
+ window.addEventListener('keydown', onKeyDown);
71
+ window.addEventListener('keyup', onKeyUp);
72
+
73
+ return () => {
74
+ window.removeEventListener('keydown', onKeyDown);
75
+ window.removeEventListener('keyup', onKeyUp);
76
+ };
77
+ }, [allowMultiple]);
78
+
79
+ // Callback function to update the `selectedRows` state.
80
+ const onSelectRow = useCallback((value, checked) => {
81
+ if (checked || allowUncheck) {
82
+ if (!isCheckingMultiple
83
+ && (checked || (!checked && allowMultiple && selectedRows.length > 1))
84
+ ) {
85
+ setSelectedRows([value]);
86
+ } else if (!allowMultiple && !checked) {
87
+ setSelectedRows([]);
88
+ } else {
89
+ setSelectedRows(
90
+ checked
91
+ ? union(selectedRows || [], [value])
92
+ : difference(selectedRows || [], [value]),
93
+ );
94
+ }
95
+ }
96
+ }, [allowMultiple, isCheckingMultiple, allowUncheck, selectedRows]);
97
+
98
+ // Handler for checkbox input elements.
99
+ const handleInputChange = useCallback((event) => {
100
+ const { target } = event;
101
+ const { checked } = target;
102
+ const { value } = target;
103
+ onSelectRow(value, checked);
104
+ }, [onSelectRow]);
105
+
106
+ // Function to map row IDs to corresponding objects
107
+ // to pass to the `onChange` callback.
108
+ const getDataFromIds = useCallback(ids => ids.map(id => ({
109
+ [idKey]: id,
110
+ data: data.find(item => item[idKey] === id),
111
+ })), [data, idKey]);
112
+
113
+ // Function to check if a row ID has been selected.
114
+ const isSelected = useCallback(id => (
115
+ Array.isArray(selectedRows) && selectedRows.includes(id)
116
+ ), [selectedRows]);
117
+
118
+ /* eslint-disable react-hooks/exhaustive-deps */
119
+ useEffect(() => {
120
+ // Check whether an initial set of rows should be selected.
121
+ const initialSelectedRows = data
122
+ .map((d) => {
123
+ if (d[valueKey]) {
124
+ return d[idKey];
125
+ }
126
+ return null;
127
+ })
128
+ .filter(Boolean);
129
+ if (!isEqual(initialSelectedRows, selectedRows)) {
130
+ if (initialSelectedRows.length > 0) {
131
+ setSelectedRows(initialSelectedRows);
132
+ } else {
133
+ setSelectedRows(null);
134
+ }
135
+ }
136
+ }, [data, idKey, valueKey]);
137
+
138
+ /* eslint-disable react-hooks/exhaustive-deps */
139
+ useEffect(() => {
140
+ // Call the `onChange` prop function with an updated row or set of rows.
141
+ if (!onChange || !selectedRows) {
142
+ return;
143
+ }
144
+ const selectedRowData = getDataFromIds(selectedRows);
145
+ if (allowMultiple) {
146
+ onChange(selectedRowData);
147
+ } else if (selectedRows.length === 1) {
148
+ onChange(selectedRowData[0]);
149
+ } else if (selectedRows.length === 0) {
150
+ onChange(null);
151
+ }
152
+ }, [selectedRows, allowMultiple]);
153
+
154
+ const classes = useStyles();
155
+
156
+ // Generate a unique ID to use in (for, id) label-input pairs.
157
+ const inputUuid = uuidv4();
158
+
159
+ const rowRenderer = ({ index, style }) => (
160
+ // eslint-disable-next-line jsx-a11y/interactive-supports-focus
161
+ <div
162
+ key={data[index][idKey]}
163
+ className={clsx(classes.tableItem, classes.tableRow, { 'row-checked': isSelected(data[index][idKey]) })}
164
+ style={style}
165
+ role="button"
166
+ onClick={() => onSelectRow(
167
+ data[index][idKey],
168
+ !isSelected(data[index][idKey]) || !hasColorEncoding,
169
+ )}
170
+ >
171
+ <div className={clsx(classes.inputContainer, classes.tableCell, { [classes.hiddenInputColumn]: !showTableInputs })}>
172
+ <label htmlFor={`${inputUuid}_${data[index][idKey]}`}>
173
+ <input
174
+ id={`${inputUuid}_${data[index][idKey]}`}
175
+ type="checkbox"
176
+ className={clsx(classes.radioOrCheckbox, isCheckingMultiple ? classes.checkbox : classes.radio)}
177
+ name={inputUuid}
178
+ value={data[index][idKey]}
179
+ onChange={handleInputChange}
180
+ checked={isSelected(data[index][idKey])}
181
+ />
182
+ </label>
183
+ </div>
184
+ {columns.map(column => (
185
+ <div
186
+ className={classes.tableCell}
187
+ key={column}
188
+ >
189
+ {data[index][column]}
190
+ </div>
191
+ ))}
192
+ </div>
193
+ );
194
+
195
+ const headerRowRenderer = ({ style }) => (
196
+ <div className={classes.tableRow} style={style}>
197
+ {columnLabels.map(columnLabel => (<div key={columnLabel} className={classes.tableCell} style={{ fontWeight: 'bold' }}>{columnLabel}</div>))}
198
+ </div>
199
+ );
200
+
201
+ return (
202
+ <div className={classes.selectableTable}>
203
+ <AutoSizer>
204
+ {({ width, height }) => (
205
+ <Table
206
+ height={testHeight || height}
207
+ gridStyle={{ outline: 'none' }}
208
+ rowCount={data.length}
209
+ // 24 is 1 em + padding in either direction (see _selectable_table.scss).
210
+ rowHeight={24}
211
+ headerHeight={showTableHead ? 24 : undefined}
212
+ rowRenderer={rowRenderer}
213
+ width={testWidth || width}
214
+ headerRowRenderer={showTableHead ? headerRowRenderer : undefined}
215
+ rowGetter={({ index }) => data[index]}
216
+ />
217
+ )}
218
+ </AutoSizer>
219
+ </div>
220
+ );
221
+ }
@@ -0,0 +1,145 @@
1
+ import '@testing-library/jest-dom';
2
+ import {
3
+ cleanup, render, screen,
4
+ } from '@testing-library/react';
5
+ import userEvent from '@testing-library/user-event';
6
+ import { afterEach } from 'vitest';
7
+ import React from 'react';
8
+
9
+ import SelectableTable from './SelectableTable.js';
10
+
11
+ const user = userEvent.setup();
12
+
13
+ afterEach(() => {
14
+ cleanup();
15
+ });
16
+
17
+ const tableData = [
18
+ {
19
+ X: 'X1',
20
+ Y: 'Y1',
21
+ Name: 'Tile 1',
22
+ },
23
+ {
24
+ X: 'X1',
25
+ Y: 'Y2',
26
+ Name: 'Tile 2',
27
+ },
28
+ {
29
+ X: 'X3',
30
+ Y: 'Y3',
31
+ Name: 'Tile 3',
32
+ },
33
+ ];
34
+ const tableIdKey = 'Name';
35
+ const tableColumns = [
36
+ 'X',
37
+ 'Y',
38
+ 'Name',
39
+ ];
40
+ const columnLabels = ['Label 1', 'Label 2', 'Name'];
41
+
42
+ describe('SelectableTable.js', () => {
43
+ describe('<SelectableTable />', () => {
44
+ it('renders column values', async () => {
45
+ render(
46
+ <SelectableTable
47
+ data={tableData}
48
+ columns={tableColumns}
49
+ columnLabels={columnLabels}
50
+ idKey={tableIdKey}
51
+ testHeight={500}
52
+ testWidth={500}
53
+ />,
54
+ );
55
+ expect(await screen.findByText('Label 1'));
56
+ expect(await screen.findByText('Label 2'));
57
+ expect(await screen.findByText('Name'));
58
+ // Since the button (although hidden) is also a table-cell, it has a blank string.
59
+ expect(await screen.findAllByText('X1'));
60
+ expect(await screen.findByText('Y2'));
61
+ expect(await screen.findByText('Tile 3'));
62
+ });
63
+
64
+ it('emits single selected object when allowMultiple is false', async () => {
65
+ const p = new Promise((resolve) => {
66
+ render(
67
+ <SelectableTable
68
+ data={tableData}
69
+ columns={tableColumns}
70
+ columnLabels={columnLabels}
71
+ idKey={tableIdKey}
72
+ onChange={(selection) => {
73
+ expect(Array.isArray(selection)).toBe(false);
74
+ expect(selection.Name).toEqual('Tile 1');
75
+ resolve();
76
+ }}
77
+ testHeight={500}
78
+ testWidth={500}
79
+ />,
80
+ );
81
+ screen.findByText('Tile 1').then((row) => {
82
+ user.click(row);
83
+ });
84
+ });
85
+ await p;
86
+ });
87
+
88
+ it('emits an array of selected objects when allowMultiple is true', async () => {
89
+ const p = new Promise((resolve) => {
90
+ render(
91
+ <SelectableTable
92
+ data={tableData}
93
+ columns={tableColumns}
94
+ columnLabels={columnLabels}
95
+ idKey={tableIdKey}
96
+ allowMultiple
97
+ onChange={(selection) => {
98
+ expect(Array.isArray(selection)).toBe(true);
99
+ expect(selection.length).toEqual(1);
100
+ expect(selection[0].Name).toEqual('Tile 2');
101
+ resolve();
102
+ }}
103
+ testHeight={500}
104
+ testWidth={500}
105
+ />,
106
+ );
107
+ screen.findByText('Tile 2').then((row) => {
108
+ user.click(row);
109
+ });
110
+ });
111
+ await p;
112
+ });
113
+
114
+ it('renders single column values', async () => {
115
+ const customTableColumns = [
116
+ 'Name',
117
+ ];
118
+ const customColumnLabels = ['Name'];
119
+
120
+ render(
121
+ <SelectableTable
122
+ data={tableData}
123
+ columns={customTableColumns}
124
+ columnLabels={customColumnLabels}
125
+ idKey={tableIdKey}
126
+ testHeight={500}
127
+ testWidth={500}
128
+ />,
129
+ );
130
+ // write an expect statement to check that Label 1 is not rendered
131
+ const label1 = screen.queryByText('Label 1');
132
+ const label2 = screen.queryByText('Label 2');
133
+ const X1 = screen.queryByText('X1');
134
+ const Y2 = screen.queryByText('Y2');
135
+
136
+ expect(label1).toBeNull();
137
+ expect(label2).toBeNull();
138
+ expect(X1).toBeNull();
139
+ expect(Y2).toBeNull();
140
+
141
+ expect(await screen.findByText('Tile 3'));
142
+ expect(await screen.findByText('Name'));
143
+ });
144
+ });
145
+ });
@@ -0,0 +1 @@
1
+ export { default as SelectableTable } from './SelectableTable.js';
@@ -0,0 +1,75 @@
1
+ /* eslint-disable max-len */
2
+ import { makeStyles } from '@material-ui/core';
3
+
4
+ export const useStyles = makeStyles(theme => ({
5
+ selectableTable: {
6
+ flex: '1 1 auto',
7
+ outline: 'none',
8
+ },
9
+ tableRow: {
10
+ display: 'flex',
11
+ flexDirection: 'row',
12
+ borderBottom: `1px solid ${theme.palette.secondaryBackgroundDim}`, // map-get($theme-colors, "secondary-background-dim");
13
+ },
14
+ tableItem: {
15
+ cursor: 'pointer',
16
+ userSelect: 'none',
17
+
18
+ '&.row-checked': {
19
+ backgroundColor: theme.palette.secondaryBackgroundDim, // map-get($theme-colors, "secondary-background-dim");
20
+ },
21
+ '&:not(.row-checked):hover': {
22
+ /*
23
+ @if $theme-name == dark {
24
+ background-color: darken(map-get($theme-colors, "secondary-background-dim"), 10%);
25
+ } @else {
26
+ background-color: lighten(map-get($theme-colors, "secondary-background-dim"), 10%);
27
+ }
28
+ */
29
+ },
30
+ },
31
+ tableCell: {
32
+ textAlign: 'left',
33
+ flexBasis: 0,
34
+ flexGrow: 1,
35
+ textOverflow: 'ellipsis',
36
+ whiteSpace: 'nowrap',
37
+ },
38
+ inputContainer: {
39
+ width: '1em',
40
+ '& label': {
41
+ display: 'block',
42
+ margin: '0',
43
+ cursor: 'pointer',
44
+ },
45
+ '& input': {
46
+ cursor: 'pointer',
47
+ },
48
+ },
49
+ hiddenInputColumn: {
50
+ // Class for first column of inputs, to hide them if desired.
51
+ display: 'none',
52
+ },
53
+ radioOrCheckbox: {
54
+ appearance: 'none',
55
+ /* create custom radiobutton appearance */
56
+ display: 'inline-block',
57
+ width: '1em',
58
+ height: '1em',
59
+ margin: '0.3em 0.5em 0.0em 0.5em',
60
+ padding: '6px',
61
+ /* background-color only for content */
62
+ backgroundClip: 'content-box',
63
+ border: `2px solid ${theme.palette.grayLight}`, // map-get($global-colors, "gray-light");
64
+ backgroundColor: theme.palette.grayLight, // map-get($global-colors, "gray-light");
65
+ '&:checked': {
66
+ backgroundClip: 'unset',
67
+ },
68
+ },
69
+ radio: {
70
+ borderRadius: '50%',
71
+ },
72
+ checkbox: {
73
+ borderRadius: '2px',
74
+ },
75
+ }));
@@ -1 +0,0 @@
1
- export { default as SelectableTable } from './SelectableTable';
@@ -1 +0,0 @@
1
- "use strict";