Skip to content

Commit

Permalink
fix(tree): mc-tree multi select is not reset by first click #UIM-8 (#152
Browse files Browse the repository at this point in the history
)
  • Loading branch information
lskramarov authored and pimenovoleg committed Jun 28, 2019
1 parent ef06e63 commit c982895
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/mosaic/tree-select/tree-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ export class McTreeSelect extends McTreeSelectMixinBase implements
} else if ((keyCode === ENTER || keyCode === SPACE) && this.tree.keyManager.activeItem) {
event.preventDefault();

this.tree.keyManager.activeItem.selectViaInteraction();
this.tree.keyManager.activeItem.selectViaInteraction(event);
} else if (this.multiple && keyCode === A && event.ctrlKey) {
event.preventDefault();

Expand All @@ -925,7 +925,7 @@ export class McTreeSelect extends McTreeSelectMixinBase implements

if (this.multiple && isArrowKey && event.shiftKey && this.tree.keyManager.activeItem &&
this.tree.keyManager.activeItemIndex !== previouslyFocusedIndex) {
this.tree.keyManager.activeItem.selectViaInteraction();
this.tree.keyManager.activeItem.selectViaInteraction(event);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/mosaic/tree/tree-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let uniqueIdCounter: number = 0;
'[class.mc-selected]': 'selected',
'[class.mc-active]': 'active',

'(click)': 'selectViaInteraction()'
'(click)': 'selectViaInteraction($event)'
},
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
Expand Down Expand Up @@ -222,13 +222,13 @@ export class McTreeOption extends CdkTreeNode<McTreeOption> implements CanDisabl
}
}

selectViaInteraction(): void {
selectViaInteraction($event?: KeyboardEvent): void {
if (!this.disabled) {
this.changeDetectorRef.markForCheck();
this.emitSelectionChangeEvent(true);

if (this.parent.setFocusedOption) {
this.parent.setFocusedOption(this);
this.parent.setFocusedOption(this, $event);
}
}
}
Expand Down
31 changes: 17 additions & 14 deletions packages/mosaic/tree/tree-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ import {
import { NodeDef, ViewData } from '@angular/core/esm2015/src/view';
import { ControlValueAccessor, NgControl } from '@angular/forms';
import { ActiveDescendantKeyManager } from '@ptsecurity/cdk/a11y';
import { END, ENTER, HOME, LEFT_ARROW, PAGE_DOWN, PAGE_UP, RIGHT_ARROW, SPACE } from '@ptsecurity/cdk/keycodes';
import {
END,
ENTER,
hasModifierKey,
HOME,
LEFT_ARROW,
PAGE_DOWN,
PAGE_UP,
RIGHT_ARROW,
SPACE
} from '@ptsecurity/cdk/keycodes';
import { CdkTree, CdkTreeNodeOutlet } from '@ptsecurity/cdk/tree';
import {
CanDisable,
Expand Down Expand Up @@ -96,10 +106,6 @@ export class McTreeSelection extends McTreeSelectionBaseMixin<McTreeOption>
autoSelect: boolean;
noUnselect: boolean;

// todo temporary solution
withShift: boolean;
withCtrl: boolean;

@Output() readonly navigationChange = new EventEmitter<McTreeNavigationChange>();

@Output() readonly selectionChange = new EventEmitter<McTreeSelectionChange>();
Expand Down Expand Up @@ -189,8 +195,6 @@ export class McTreeSelection extends McTreeSelectionBaseMixin<McTreeOption>
onKeyDown(event: KeyboardEvent) {
// tslint:disable-next-line: deprecation
const keyCode = event.keyCode;
this.withShift = event.shiftKey;
this.withCtrl = event.ctrlKey;

switch (keyCode) {
case LEFT_ARROW:
Expand Down Expand Up @@ -246,14 +250,17 @@ export class McTreeSelection extends McTreeSelectionBaseMixin<McTreeOption>
this.keyManager.withScrollSize(Math.floor(this.getHeight() / this.options.first.getHeight()));
}

setFocusedOption(option: McTreeOption) {
setFocusedOption(option: McTreeOption, $event?: KeyboardEvent) {
this.keyManager.setActiveItem(option);

const withShift = $event ? hasModifierKey($event, 'shiftKey') : false;
const withCtrl = $event ? hasModifierKey($event, 'ctrlKey') : false;

if (this.multiple) {
if (!this.canDeselectLast(option)) { return; }

option.toggle();
} else if (this.withShift) {
} else if (withShift) {
const previousIndex = this.keyManager.previousActiveItemIndex;
const activeIndex = this.keyManager.activeItemIndex;

Expand All @@ -266,11 +273,7 @@ export class McTreeSelection extends McTreeSelectionBaseMixin<McTreeOption>
if (index >= activeIndex && index <= previousIndex) { item.setSelected(true); }
});
}

this.withShift = false;
} else if (this.withCtrl) {
this.withCtrl = false;

} else if (withCtrl) {
if (!this.canDeselectLast(option)) { return; }

option.toggle();
Expand Down

0 comments on commit c982895

Please sign in to comment.