Skip to content

Commit

Permalink
feat(lib): add additive behavior
Browse files Browse the repository at this point in the history
This commit adds additive behavior, mimicking the behavior when holding the Shift key.
Updated app to be able to select addictive behavior.
Updated Cypress tests.
Updated documentation.
  • Loading branch information
vascocc committed Oct 28, 2019
1 parent a0d0d54 commit 8c8398b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ shortcuts: {

**Inputs**

<<<<<<< HEAD
| Input | Type | Default | Description |
| --------------------- | ---------- | ------- | ------------------------------------------------------------------------------------------------------------- |
| selectedItems | Array<any> | / | Collection of items that are currently selected |
Expand All @@ -247,7 +248,8 @@ shortcuts: {
| disableDrag | Boolean | `false` | Disable selection by dragging with the mouse. May be useful for mobile. |
| selectMode | Boolean | `false` | If set to `true`, a _toggle_ mode is activated similar to the `toggleSingleItem` shortcut. Useful for mobile. |
| custom | Boolean | `false` | If set to `true`, all default styles for selected items will not be applied. |
| selectWithShortcut | Boolean | `false` | If set to `true`, items can only be selected when single clicking and applying a keyboard shortcut |
| selectWithShortcut | Boolean | `false` | If set to `true`, items can only be selected when single clicking and applying a keyboard shortcut | |
| additive | Boolean | `false` | If set to `true`, will add items to selection the same way as holding the `shift` key. |

Here's an example of all inputs in action:

Expand All @@ -259,7 +261,8 @@ Here's an example of all inputs in action:
[disableDrag]="true"
[selectMode]="true"
[custom]="true"
[selectWithShortcut]="false">
[selectWithShortcut]="false"
[additive]="true">
...
</dts-select-container>
```
Expand Down
31 changes: 31 additions & 0 deletions cypress/integration/dragging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
disableSelection,
disableSelectOnDrag,
enableSelectMode,
enableAdditive,
getDesktopExample,
shouldBeInvisible,
shouldBeVisible,
Expand Down Expand Up @@ -372,6 +373,36 @@ describe('Dragging', () => {
});
});

describe('Additive', () => {
beforeEach(() => {
enableAdditive();
});

it('should extend selection after mouseup', () => {
getDesktopExample().within(() => {
cy.getSelectItem(0)
.dispatch('mousedown', { button: 0 })
.getSelectItem(5)
.dispatch('mousemove')
.dispatch('mouseup')
.shouldSelect([1, 2, 5, 6])
.getSelectItem(2)
.dispatch('mousedown', { button: 0 })
.getSelectItem(7)
.as('end')
.dispatch('mousemove')
.shouldSelect([1, 2, 5, 6])
.get(`.${SELECTED_CLASS}`)
.should('have.length', 8)
.get('@end')
.dispatch('mouseup')
.shouldSelect([1, 2, 3, 4, 5, 6, 7, 8])
.get(`.${SELECTED_CLASS}`)
.should('have.length', 8);
});
});
});

describe('Shortcuts', () => {
it('should toggle single items', () => {
getDesktopExample().within(() => {
Expand Down
4 changes: 4 additions & 0 deletions cypress/support/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export const enableSelectWithShortcut = () => {
return cy.get('[data-cy="selectWithShortcut"]').click();
};

export const enableAdditive = () => {
return cy.get('[data-cy="additive"]').click();
};

export const selectAll = () => {
return getSelectAllButton().click();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class SelectContainerComponent implements AfterViewInit, OnDestroy, After
@Input() disableRangeSelection = false;
@Input() selectMode = false;
@Input() selectWithShortcut = false;
@Input() additive = false;

@Input()
@HostBinding('class.dts-custom')
Expand Down Expand Up @@ -466,7 +467,7 @@ export class SelectContainerComponent implements AfterViewInit, OnDestroy, After
const itemRect = item.getBoundingClientRect();
const withinBoundingBox = inBoundingBox(mousePoint, itemRect);

if (this.shortcuts.extendedSelectionShortcut(event) && this.disableRangeSelection) {
if (this.shortcuts.extendedSelectionShortcut(event) && this.disableRangeSelection || this.additive) {
return;
}

Expand Down Expand Up @@ -540,7 +541,7 @@ export class SelectContainerComponent implements AfterViewInit, OnDestroy, After
}

private _isExtendedSelection(event: Event) {
return this.shortcuts.extendedSelectionShortcut(event) && this.selectOnDrag;
return (this.shortcuts.extendedSelectionShortcut(event) || this.additive) && this.selectOnDrag;
}

private _normalSelectionMode(selectBox: BoundingBox, item: SelectItemDirective, event: Event) {
Expand All @@ -567,15 +568,18 @@ export class SelectContainerComponent implements AfterViewInit, OnDestroy, After
(inSelection && item.selected && this.shortcuts.removeFromSelection(event) && !this._tmpItems.has(item));

const shouldRemove =
(!inSelection && item.selected && this.shortcuts.addToSelection(event) && this._tmpItems.has(item)) ||
(!inSelection &&
item.selected &&
(this.shortcuts.addToSelection(event) || this.additive) &&
this._tmpItems.has(item)) ||
(!inSelection && !item.selected && this.shortcuts.removeFromSelection(event) && this._tmpItems.has(item));

if (shoudlAdd) {
item.selected ? item._deselect() : item._select();

const action = this.shortcuts.removeFromSelection(event)
? Action.Delete
: this.shortcuts.addToSelection(event)
: this.shortcuts.addToSelection(event) || this.additive
? Action.Add
: Action.None;

Expand Down
2 changes: 2 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ <h3>
>
<mat-checkbox data-cy="selectMode" [(ngModel)]="selectMode">Select Mode</mat-checkbox>
<mat-checkbox data-cy="selectWithShortcut" [(ngModel)]="selectWithShortcut">Select with Shortcut</mat-checkbox>
<mat-checkbox data-cy="additive" [(ngModel)]="additive">Additive</mat-checkbox>

<div class="action-buttons">
<button data-cy="selectAll" mat-raised-button (click)="selectContainer.selectAll()">Select All</button>
Expand All @@ -44,6 +45,7 @@ <h3>
[disableRangeSelection]="disableRangeSelection"
[selectOnDrag]="selectOnDrag"
[selectWithShortcut]="selectWithShortcut"
[additive]="additive"
>
<mat-grid-list cols="4" rowHeight="200px" gutterSize="20px">
<mat-grid-tile [dtsSelectItem]="document" *ngFor="let document of documents">
Expand Down
1 change: 1 addition & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class AppComponent implements OnInit {
disableRangeSelection = false;
isDesktop = false;
selectWithShortcut = false;
additive = false;

constructor(
private titleService: Title,
Expand Down

0 comments on commit 8c8398b

Please sign in to comment.