Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-implement row selection #1762

Merged
merged 35 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
502ee11
Re-implement row selection
amanmahajan7 Oct 2, 2019
97c78fa
Re-enable jsx-curly-brace-presence
nstepien Oct 2, 2019
f5f9e86
onSelectedRowsChanged -> onSelectedRowsChange
nstepien Oct 2, 2019
c7bf8fe
Small cleanup in Canvas.tsx
nstepien Oct 2, 2019
f50d526
small tweaks
nstepien Oct 2, 2019
70c9718
Delete RowUtils tests
nstepien Oct 2, 2019
7aad306
Fix Canvas tests
nstepien Oct 2, 2019
a15cab4
Get shift selection almost working
nstepien Oct 2, 2019
f5da8d4
Simplify a few exports
nstepien Oct 2, 2019
2afa28f
Focus CellMask when clicking on a cell
nstepien Oct 2, 2019
e238aac
Working shift selection
nstepien Oct 2, 2019
90034bb
use a ref for lastSelectedRowIdx
nstepien Oct 2, 2019
a41b1c8
Add class methods to avoid recomputation
amanmahajan7 Oct 2, 2019
18d3334
Do it the react way
nstepien Oct 3, 2019
68bd0d8
Simplify 1 type
nstepien Oct 3, 2019
a6bac01
Fix selection
nstepien Oct 3, 2019
5e00be3
Merge remote-tracking branch 'origin/am-improve-props' into am-ns-sel…
nstepien Oct 3, 2019
e62c354
tweaks
nstepien Oct 3, 2019
1242b30
Remove references to `enableRowSelect`
nstepien Oct 3, 2019
b303f93
More cleanup
nstepien Oct 3, 2019
3df4710
fix some tests
nstepien Oct 3, 2019
edb4fa7
fix tests
nstepien Oct 3, 2019
9438985
Remove @testing-library/react/cleanup-after-each
nstepien Oct 3, 2019
c79be1f
Fix some examples
nstepien Oct 3, 2019
604cb90
Fix another example
nstepien Oct 3, 2019
2a0d7fd
optimize all-features example
nstepien Oct 3, 2019
785f0d7
Cleanup viewport events
amanmahajan7 Oct 3, 2019
9f6762d
Cleanup props, remove PureComponent
amanmahajan7 Oct 3, 2019
ba576e8
Fix draggable header example
amanmahajan7 Oct 3, 2019
b92761a
Commit reordering fixes (not working)
amanmahajan7 Oct 3, 2019
3519c43
Remove row-reordering components
amanmahajan7 Oct 4, 2019
5ce39f9
Fix eslint errors
amanmahajan7 Oct 4, 2019
ce14794
Remove re-ordering refrences
amanmahajan7 Oct 4, 2019
2e1a989
Remove tests for the deleted file
amanmahajan7 Oct 4, 2019
390434b
Fix types
amanmahajan7 Oct 4, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ const rules = {
'react/jsx-no-undef': 0,
'react/jsx-no-useless-fragment': 0,
'react/jsx-one-expression-per-line': 0,
'react/jsx-curly-brace-presence': 0,
'react/jsx-curly-brace-presence': 1,
'react/jsx-fragments': 1,
'react/jsx-pascal-case': 1,
'react/jsx-props-no-multi-spaces': 1,
Expand Down
25 changes: 9 additions & 16 deletions examples/scripts/example16-row-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,21 @@ class Example extends React.Component {
count: i * 1000
});
}
this.state = { rows, selectedIndexes: [] };
this.state = {
rows,
selectedRows: new Set()
};
}

rowGetter = (i) => {
return this.state.rows[i];
};

onRowsSelected = (rows) => {
this.setState({ selectedIndexes: this.state.selectedIndexes.concat(rows.map(r => r.rowIdx)) });
};

onRowsDeselected = (rows) => {
const rowIndexes = rows.map(r => r.rowIdx);
this.setState({ selectedIndexes: this.state.selectedIndexes.filter(i => rowIndexes.indexOf(i) === -1) });
};

render() {
const rowText = this.state.selectedIndexes.length === 1 ? 'row' : 'rows';
const rowText = this.state.selectedRows.size === 1 ? 'row' : 'rows';
return (
<div>
<span>{this.state.selectedIndexes.length} {rowText} selected</span>
<span>{this.state.selectedRows.size} {rowText} selected</span>
<ReactDataGrid
rowKey="id"
columns={this._columns}
Expand All @@ -59,10 +53,9 @@ class Example extends React.Component {
rowSelection={{
showCheckbox: true,
enableShiftSelect: true,
onRowsSelected: this.onRowsSelected,
onRowsDeselected: this.onRowsDeselected,
selectBy: {
indexes: this.state.selectedIndexes
selectedRows: this.state.selectedRows,
onSelectedRowsChange: (selectedRows) => {
this.setState({ selectedRows });
}
}}
/>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.14.0",
"eslint": "^6.5.1",
"eslint-plugin-react": "^7.15.0",
"eslint-plugin-react": "^7.15.1",
"eslint-plugin-react-hooks": "^2.1.1",
"eslint-plugin-sonarjs": "^0.4.0",
"faker": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-data-grid-addons/src/editors/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { SimpleTextEditor, CheckboxEditor } from 'react-data-grid';
export { SimpleTextEditor } from 'react-data-grid';
export { default as DropDownEditor } from './DropDownEditor';
23 changes: 3 additions & 20 deletions packages/react-data-grid/src/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Row from './Row';
import RowsContainerDefault from './RowsContainer';
import RowGroup from './RowGroup';
import { InteractionMasks } from './masks';
import * as rowUtils from './RowUtils';
import { getColumnScrollPosition, isPositionStickySupported } from './utils';
import { EventTypes } from './common/enums';
import { CalculatedColumn, Position, ScrollPosition, SubRowDetails, RowRenderer, RowRendererProps, RowData } from './common/types';
Expand All @@ -22,7 +21,6 @@ type SharedViewportProps<R> = Pick<ViewportProps<R>,
| 'rowHeight'
| 'scrollToRowIndex'
| 'contextMenu'
| 'rowSelection'
| 'getSubRowDetails'
| 'rowGroupRenderer'
| 'enableCellSelect'
Expand Down Expand Up @@ -146,23 +144,8 @@ export default class Canvas<R> extends React.PureComponent<CanvasProps<R>> {
return scrollVariation > 0 ? rowHeight - scrollVariation : 0;
}

isRowSelected(idx: number, row: R) {
// Use selectedRows if set
if (this.props.selectedRows) {
const selectedRow = this.props.selectedRows.find(r => {
const rowKeyValue = rowUtils.get(row, this.props.rowKey);
return r[this.props.rowKey] === rowKeyValue;
});
return !!(selectedRow && selectedRow.isSelected);
}

// Else use new rowSelection props
if (this.props.rowSelection) {
const { keys, indexes, isSelectedKey } = this.props.rowSelection as { [key: string]: unknown };
return rowUtils.isRowSelected(keys as { rowKey?: string; values?: string[] } | null, indexes as number[] | null, isSelectedKey as string | null, row, idx);
}

return false;
isRowSelected(row: R): boolean {
return this.props.selectedRows !== undefined && this.props.selectedRows.has(row[this.props.rowKey]);
}

setScrollLeft(scrollLeft: number) {
Expand Down Expand Up @@ -286,7 +269,7 @@ export default class Canvas<R> extends React.PureComponent<CanvasProps<R>> {
row,
height: rowHeight,
columns,
isSelected: this.isRowSelected(rowIdx, row),
isSelected: this.isRowSelected(row),
cellMetaData,
subRowDetails,
colVisibleStartIdx,
Expand Down
6 changes: 2 additions & 4 deletions packages/react-data-grid/src/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isValidElementType } from 'react-is';

import Header, { HeaderHandle, HeaderProps } from './Header';
import Viewport, { ScrollState } from './Viewport';
import { HeaderRowData, CellMetaData, RowSelection, InteractionMasksMetaData, SelectedRow, ColumnMetrics } from './common/types';
import { HeaderRowData, CellMetaData, InteractionMasksMetaData, ColumnMetrics } from './common/types';
import { DEFINE_SORT } from './common/enums';
import { ReactDataGridProps } from './ReactDataGrid';
import { EventBus } from './masks';
Expand Down Expand Up @@ -42,8 +42,7 @@ type SharedDataGridProps<R> = Pick<ReactDataGridProps<R>,
export interface GridProps<R> extends SharedDataGridProps<R> {
headerRows: [HeaderRowData<R>, HeaderRowData<R> | undefined];
cellMetaData: CellMetaData<R>;
selectedRows?: SelectedRow<R>[];
rowSelection?: RowSelection;
selectedRows?: ReadonlySet<R[keyof R]>;
rowOffsetHeight: number;
eventBus: EventBus;
interactionMasksMetaData: InteractionMasksMetaData<R>;
Expand Down Expand Up @@ -107,7 +106,6 @@ export default function Grid<R>({ emptyRowsView, headerRows, viewportWidth, ...p
minHeight={props.minHeight}
scrollToRowIndex={props.scrollToRowIndex}
contextMenu={props.contextMenu}
rowSelection={props.rowSelection}
getSubRowDetails={props.getSubRowDetails}
rowGroupRenderer={props.rowGroupRenderer}
enableCellSelect={props.enableCellSelect}
Expand Down
Loading