Skip to content

Commit c5b07a3

Browse files
authored
fix(tree): mouse click selection generates extra event (#DS-2900) (#332)
1 parent 3b09b96 commit c5b07a3

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

packages/components/tree-select/tree-select.component.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ import {
6767
KbqTreeSelectionChange
6868
} from '@koobiq/components/tree';
6969
import { Observable, Subject, Subscription, of } from 'rxjs';
70-
import { map } from 'rxjs/operators';
7170
import { KbqTreeSelect, KbqTreeSelectChange, KbqTreeSelectModule } from './index';
7271

7372
const TREE_DATA = {
@@ -2216,12 +2215,12 @@ describe('KbqTreeSelect', () => {
22162215
it('should consider the selection a result of a user action when closed', fakeAsync(() => {
22172216
const option = fixture.componentInstance.options.first;
22182217
const spy = jest.fn();
2219-
const subscription = option.onSelectionChange.pipe(map((e) => e.isUserInput)).subscribe(spy);
2218+
const subscription = option.userInteraction.subscribe(spy);
22202219

22212220
dispatchKeyboardEvent(select, 'keydown', DOWN_ARROW);
22222221
tick(10);
22232222

2224-
expect(spy).toHaveBeenCalledWith(true);
2223+
expect(spy).toHaveBeenCalled();
22252224

22262225
subscription.unsubscribe();
22272226
}));
@@ -2323,6 +2322,8 @@ describe('KbqTreeSelect', () => {
23232322
fixture.detectChanges();
23242323
flush();
23252324

2325+
expect(fixture.componentInstance.select.panelOpen).toBe(true);
2326+
23262327
const option = overlayContainerElement.querySelector('kbq-tree-option') as HTMLElement;
23272328
option.click();
23282329
fixture.detectChanges();

packages/components/tree-select/tree-select.component.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,21 @@ export class KbqTreeSelect
296296
);
297297
}) as Observable<KbqTreeSelectChange>;
298298

299+
/** Combined stream of all of the child options userInteraction events. */
300+
readonly userInteractionChanges: Observable<void> = defer(() => {
301+
if (this.options) {
302+
return this.options.changes.pipe(
303+
startWith(this.options),
304+
switchMap(() => merge(...this.options.map((option) => option.userInteraction)))
305+
);
306+
}
307+
308+
return this.ngZone.onStable.asObservable().pipe(
309+
take(1),
310+
switchMap(() => this.userInteractionChanges)
311+
);
312+
});
313+
299314
@Input()
300315
get placeholder(): string {
301316
return this._placeholder;
@@ -549,8 +564,8 @@ export class KbqTreeSelect
549564
this.tempValues = null;
550565
}
551566

552-
this.optionSelectionChanges.pipe(takeUntil(this.destroy)).subscribe((event) => {
553-
if (!this.multiple && this.panelOpen && event.isUserInput) {
567+
this.userInteractionChanges.pipe(takeUntil(this.destroy)).subscribe(() => {
568+
if (!this.multiple && this.panelOpen) {
554569
this.close();
555570

556571
Promise.resolve().then(() => this.focus());

packages/components/tree/tree-option.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export class KbqTreeOption extends KbqTreeNode<KbqTreeOption> implements AfterCo
138138
private _showCheckbox: boolean;
139139

140140
@Output() readonly onSelectionChange = new EventEmitter<KbqTreeOptionChange>();
141+
readonly userInteraction = new EventEmitter<void>();
141142

142143
get selected(): boolean {
143144
return this._selected;
@@ -359,7 +360,8 @@ export class KbqTreeOption extends KbqTreeNode<KbqTreeOption> implements AfterCo
359360
}
360361

361362
this.markForCheck();
362-
this.emitSelectionChangeEvent(true);
363+
364+
this.userInteraction.emit();
363365

364366
const shiftKey = $event ? hasModifierKey($event, 'shiftKey') : false;
365367
const ctrlKey = $event ? hasModifierKey($event, 'ctrlKey') : false;

tools/public_api_guard/components/tree-select.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export class KbqTreeSelect extends KbqTreeSelectMixinBase implements AfterConten
200200
get triggerValue(): string;
201201
// (undocumented)
202202
triggerValues: ITriggerValue[];
203+
readonly userInteractionChanges: Observable<void>;
203204
// (undocumented)
204205
get value(): any;
205206
readonly valueChange: EventEmitter<any>;

tools/public_api_guard/components/tree.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,8 @@ export class KbqTreeOption extends KbqTreeNode<KbqTreeOption> implements AfterCo
514514
// (undocumented)
515515
updateParentsCheckboxState(node: any): void;
516516
// (undocumented)
517+
readonly userInteraction: EventEmitter<void>;
518+
// (undocumented)
517519
get value(): any;
518520
set value(value: any);
519521
// (undocumented)

0 commit comments

Comments
 (0)