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

Multiple selection via shift-click for controlled selection #219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 12 additions & 5 deletions lib/DataSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,16 @@ var DataSheet = function (_PureComponent) {
var isNowEditingSameCell = !isEmpty(this.state.editing) && this.state.editing.i === i && this.state.editing.j === j;
var editing = isEmpty(this.state.editing) || this.state.editing.i !== i || this.state.editing.j !== j ? {} : this.state.editing;

var _getState8 = this.getState(),
start = _getState8.start;

if (!e.shiftKey) {
start = { i: i, j: j };
}

this._setState({
selecting: !isNowEditingSameCell,
start: e.shiftKey ? this.state.start : { i: i, j: j },
start: start,
end: { i: i, j: j },
editing: editing,
forceEdit: !!isNowEditingSameCell
Expand Down Expand Up @@ -702,9 +709,9 @@ var DataSheet = function (_PureComponent) {
}, {
key: 'isSelected',
value: function isSelected(i, j) {
var _getState8 = this.getState(),
start = _getState8.start,
end = _getState8.end;
var _getState9 = this.getState(),
start = _getState9.start,
end = _getState9.end;

var posX = j >= start.j && j <= end.j;
var negX = j <= start.j && j >= end.j;
Expand Down Expand Up @@ -846,4 +853,4 @@ DataSheet.defaultProps = {
cellRenderer: _Cell2.default,
valueViewer: _ValueViewer2.default,
dataEditor: _DataEditor2.default
};
};
7 changes: 6 additions & 1 deletion src/DataSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,14 @@ export default class DataSheet extends PureComponent {
? {}
: this.state.editing;

let { start } = this.getState();
if (!e.shiftKey) {
start = { i, j };
}

this._setState({
selecting: !isNowEditingSameCell,
start: e.shiftKey ? this.state.start : { i, j },
start: start,
end: { i, j },
editing: editing,
forceEdit: !!isNowEditingSameCell,
Expand Down
67 changes: 67 additions & 0 deletions test/Datasheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,73 @@ describe('Component', () => {
expect(wrapper.state('selecting')).toEqual(false);
});

it('selects multiple field properly 2x2 (click and shift click)', () => {
expect(wrapper.find('td.cell.selected').length).toEqual(0);
wrapper.find('td').at(0).simulate('mouseDown');
wrapper.find('td').at(0).simulate('mouseUp');
wrapper.find('td').at(3).simulate('mouseDown', {
shiftKey: true,
});
wrapper.find('td').at(3).simulate('mouseUp', {
shiftKey: true,
});
expect(wrapper.find('td.cell.selected').length).toEqual(4);
expect(
wrapper.find('td.cell.selected span').nodes.map(n => n.innerHTML),
).toEqual(['4', '2', '0', '5']);

expect(wrapper.state('selecting')).toEqual(true);
expect(wrapper.state('editing')).toEqual({});
expect(wrapper.state('start')).toEqual({
i: 0,
j: 0,
});
expect(wrapper.state('end')).toEqual({
i: 1,
j: 1,
});
});

it('selects multiple field properly 2x2 (click and shift click) and the selection is controlled', () => {
customWrapper = mount(
<DataSheet
data={data}
selected={null}
onSelect={selected => customWrapper.setProps({ selected })}
valueRenderer={cell => cell.data}
/>,
);
expect(customWrapper.find('td.cell.selected').length).toEqual(0);
customWrapper.find('td').at(0).simulate('mouseDown');
customWrapper.find('td').at(0).simulate('mouseUp');
customWrapper.find('td').at(3).simulate('mouseDown', {
shiftKey: true,
});
customWrapper.find('td').at(3).simulate('mouseUp', {
shiftKey: true,
});
expect(customWrapper.find('td.cell.selected').length).toEqual(4);
expect(
customWrapper
.find('td.cell.selected span')
.nodes.map(n => n.innerHTML),
).toEqual(['4', '2', '0', '5']);

expect(customWrapper.state('selecting')).toEqual(true);
expect(customWrapper.state('editing')).toEqual({});
// Start and end are stored in props for a controlled component
expect(customWrapper.state('start')).toEqual({});
expect(customWrapper.state('end')).toEqual({});
expect(customWrapper.props().selected.start).toEqual({
i: 0,
j: 0,
});
expect(customWrapper.props().selected.end).toEqual({
i: 1,
j: 1,
});
});

it('calls onSelect prop when a new element is selected', done => {
customWrapper = mount(
<DataSheet
Expand Down