Skip to content

Commit

Permalink
feat(tree-select): use useHostFocusControl #INFR-8464
Browse files Browse the repository at this point in the history
  • Loading branch information
minlovehua committed Aug 22, 2023
1 parent 0a4e82e commit e6b91c0
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/tree-select/tree-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
mixinTabIndex,
ThyCanDisable,
ThyClickDispatcher,
ThyHasTabIndex
ThyHasTabIndex,
useHostFocusControl
} from 'ngx-tethys/core';
import { ThyTreeNode } from 'ngx-tethys/tree';
import { elementMatchClosest, isArray, isObject, produce, warnDeprecation } from 'ngx-tethys/util';
Expand Down Expand Up @@ -42,7 +43,7 @@ import { ThyEmptyComponent } from 'ngx-tethys/empty';
import { ThyIconComponent } from 'ngx-tethys/icon';
import { ThySelectControlComponent, ThyStopPropagationDirective } from 'ngx-tethys/shared';
import { ThyTreeSelectNode, ThyTreeSelectType } from './tree-select.class';
import { FocusMonitor, FocusOrigin } from '@angular/cdk/a11y';
import { FocusOrigin } from '@angular/cdk/a11y';

type InputSize = 'xs' | 'sm' | 'md' | 'lg' | '';

Expand Down Expand Up @@ -139,6 +140,8 @@ export class ThyTreeSelectComponent extends _MixinBase implements OnInit, OnDest

private focusOrigin: FocusOrigin;

private hostFocusControl = useHostFocusControl();

originTreeNodes: ThyTreeSelectNode[];

@ContentChild('thyTreeSelectTriggerDisplay')
Expand Down Expand Up @@ -347,8 +350,7 @@ export class ThyTreeSelectComponent extends _MixinBase implements OnInit, OnDest
private ref: ChangeDetectorRef,
@Inject(PLATFORM_ID) private platformId: string,
private thyClickDispatcher: ThyClickDispatcher,
private viewportRuler: ViewportRuler,
private focusMonitor: FocusMonitor
private viewportRuler: ViewportRuler
) {
super();
}
Expand Down Expand Up @@ -385,15 +387,12 @@ export class ThyTreeSelectComponent extends _MixinBase implements OnInit, OnDest
this.init();
});

this.focusMonitor
.monitor(this.elementRef, true)
.pipe(takeUntil(this.destroy$))
.subscribe((origin: FocusOrigin) => {
this.focusOrigin = origin;
if (origin && origin !== 'mouse') {
this.openSelectPop();
}
});
this.hostFocusControl.focusChanged = (origin: FocusOrigin) => {
this.focusOrigin = origin;
if (origin && origin !== 'mouse') {
this.openSelectPop();
}
};
}

onBlur(event: FocusEvent) {
Expand All @@ -407,7 +406,7 @@ export class ThyTreeSelectComponent extends _MixinBase implements OnInit, OnDest
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
this.focusMonitor.stopMonitoring(this.elementRef);
this.hostFocusControl.destroy();
}

get selectedValueObject() {
Expand Down

0 comments on commit e6b91c0

Please sign in to comment.