Skip to content

Commit

Permalink
feat(lib): add input to control select on click (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-eah committed Sep 19, 2021
1 parent ec1116e commit abeeb1b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ shortcuts: {
| --------------------- | ---------- | ------- | ------------------------------------------------------------------------------------------------------------- |
| selectedItems | Array<any> | / | Collection of items that are currently selected |
| selectOnDrag | Boolean | `true` | Whether items should be selected while dragging |
| selectOnClick | Boolean | `true` | Whether items should be selected when clicked |
| dragOverItems | Boolean | `true` | Whether drag selection is allowed to start from inside an item |
| disabled | Boolean | `false` | Disable selection |
| disableRangeSelection | Boolean | `false` | Disable range selection |
Expand Down
14 changes: 14 additions & 0 deletions cypress/integration/clicking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
disableEvenItems,
disableRangeSelection,
disableSelectOnDrag,
disableSelectOnClick,
enableSelectWithShortcut,
getDesktopExample,
selectAll,
Expand Down Expand Up @@ -190,6 +191,19 @@ describe('Clicking', () => {
});
});

it('should not select a single item when selectOnClick is false', () => {
getDesktopExample().within(() => {
disableSelectOnClick();

cy.getSelectItem(0)
.dispatch('mousedown', { button: 0 })
.dispatch('mouseup')
.shouldSelect([])
.get(`.${SELECTED_CLASS}`)
.should('have.length', 0);
});
});

it('should not clear selected item when clicked outside of draggable area', () => {
const mousePosition = { pageX: 0, pageY: 0 };

Expand Down
4 changes: 4 additions & 0 deletions cypress/support/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export const disableSelectOnDrag = () => {
return cy.get('[data-cy="selectOnDrag"]').click();
};

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

export const disableRangeSelection = () => {
return cy.get('[data-cy="disableRangeSelection"]').click();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class SelectContainerComponent implements AfterViewInit, OnDestroy, After
@Input() selectOnDrag = true;
@Input() disabled = false;
@Input() disableDrag = false;
@Input() selectOnClick = true;
@Input() dragOverItems = true;
@Input() disableRangeSelection = false;
@Input() selectMode = false;
Expand Down Expand Up @@ -161,6 +162,7 @@ export class SelectContainerComponent implements AfterViewInit, OnDestroy, After
const mousedown$ = fromEvent<MouseEvent>(this.host, 'mousedown').pipe(
filter((event) => event.button === 0), // only emit left mouse
filter(() => !this.disabled),
filter((event) => this.selectOnClick || event.target === this.host),
tap((event) => this._onMouseDown(event)),
share()
);
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 @@ -19,6 +19,7 @@ <h3>
<span>Desktop Demo</span> <a href="https://github.com/d3lm/ngx-drag-to-select/blob/master/src/app">SOURCE</a>
</h3>

<mat-checkbox data-cy="selectOnClick" [(ngModel)]="selectOnClick">Select on Click</mat-checkbox>
<mat-checkbox data-cy="selectOnDrag" [(ngModel)]="selectOnDrag">Select on Drag</mat-checkbox>
<mat-checkbox data-cy="dragOverItems" [(ngModel)]="dragOverItems">Drag Over Items</mat-checkbox>
<mat-checkbox data-cy="disable" [(ngModel)]="disable">Disable</mat-checkbox>
Expand All @@ -44,6 +45,7 @@ <h3>
[(selectedItems)]="selectedDocuments"
[disabled]="disable"
[disableRangeSelection]="disableRangeSelection"
[selectOnClick]="selectOnClick"
[selectOnDrag]="selectOnDrag"
[selectWithShortcut]="selectWithShortcut"
[dragOverItems]="dragOverItems"
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 @@ -13,6 +13,7 @@ const json = require('../../projects/ngx-drag-to-select/package.json');
export class AppComponent implements OnInit {
documents: Array<any> = [];
selectedDocuments: Array<any> = [];
selectOnClick = true;
selectOnDrag = true;
selectMode = false;
disable = false;
Expand Down

0 comments on commit abeeb1b

Please sign in to comment.