Skip to content

Commit db8ef93

Browse files
authored
Merge pull request #766 from microsoftgraph/nmetulev/pickercleanup
[PeoplePicker] Fixed showLoading and minor refactoring
2 parents 144202b + 78b8e12 commit db8ef93

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

packages/mgt/src/components/mgt-people-picker/mgt-people-picker.ts

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
9292
return this.renderRoot.querySelector('.flyout');
9393
}
9494

95+
/**
96+
* Gets the input element
97+
*
98+
* @protected
99+
* @type {MgtFlyout}
100+
* @memberof MgtLogin
101+
*/
102+
protected get input(): HTMLInputElement {
103+
return this.renderRoot.querySelector('.search-box__input');
104+
}
105+
95106
/**
96107
* value determining if search is filtered to a group.
97108
* @type {string}
@@ -309,12 +320,11 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
309320
*/
310321
public focus(options?: FocusOptions) {
311322
this.gainedFocus();
312-
const peopleInput = this.renderRoot.querySelector('.search-box__input') as HTMLInputElement;
313-
if (!peopleInput) {
323+
if (!this.input) {
314324
return;
315325
}
316-
peopleInput.focus(options);
317-
peopleInput.select();
326+
this.input.focus(options);
327+
this.input.select();
318328
}
319329

320330
/**
@@ -420,7 +430,6 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
420430
label="people-picker-input"
421431
aria-label="people-picker-input"
422432
role="input"
423-
.value="${this.userInput}"
424433
@keydown="${this.onUserKeyDown}"
425434
@keyup="${this.onUserKeyUp}"
426435
@blur=${this.lostFocus}
@@ -780,7 +789,7 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
780789
*/
781790
protected addPerson(person: IDynamicPerson): void {
782791
if (person) {
783-
this.userInput = '';
792+
this.clearInput();
784793
const duplicatePeople = this.selectedPeople.filter(p => {
785794
if (!person.id) {
786795
return p.displayName === person.displayName;
@@ -798,6 +807,11 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
798807
}
799808
}
800809

810+
private clearInput() {
811+
this.input.value = '';
812+
this.userInput = '';
813+
}
814+
801815
private handleFlyout() {
802816
// handles hiding control if default people have no more selections available
803817
const peopleLeft = this.filterPeople(this.defaultPeople);
@@ -816,9 +830,8 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
816830

817831
private gainedFocus() {
818832
this._isFocused = true;
819-
const input = this.renderRoot.querySelector('.search-box__input') as HTMLInputElement;
820-
if (input) {
821-
input.focus();
833+
if (this.input) {
834+
this.input.focus();
822835
}
823836
this._showLoading = true;
824837
this.loadState();
@@ -881,35 +894,30 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
881894
const input = event.target as HTMLInputElement;
882895

883896
if (event.code === 'Escape') {
884-
input.value = '';
885-
this.userInput = '';
897+
this.clearInput();
886898
this._foundPeople = [];
887-
return;
888-
}
889-
if (event.code === 'Backspace' && this.userInput.length === 0 && this.selectedPeople.length > 0) {
890-
input.value = '';
891-
this.userInput = '';
899+
} else if (event.code === 'Backspace' && this.userInput.length === 0 && this.selectedPeople.length > 0) {
892900
// remove last person in selected list
893901
this.selectedPeople = this.selectedPeople.splice(0, this.selectedPeople.length - 1);
894902
this.loadState();
895903
this.hideFlyout();
896904
// fire selected people changed event
897905
this.fireCustomEvent('selectionChanged', this.selectedPeople);
898-
return;
906+
} else {
907+
this.userInput = input.value;
908+
this.handleUserSearch();
899909
}
900-
901-
this.handleUserSearch(input);
902910
}
903911

904912
private onPersonClick(person: IDynamicPerson): void {
905913
this.addPerson(person);
906914
this.hideFlyout();
907915

908-
const peopleInput = this.renderRoot.querySelector('.search-box__input') as HTMLInputElement;
909-
if (!peopleInput) {
916+
if (!this.input) {
910917
return;
911918
}
912-
peopleInput.focus();
919+
920+
this.input.focus();
913921
this._isFocused = true;
914922
this.hideFlyout();
915923
if (this.selectionMode === 'single') {
@@ -921,20 +929,12 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
921929
* Tracks event on user input in search
922930
* @param input - input text
923931
*/
924-
private handleUserSearch(input: HTMLInputElement) {
932+
private handleUserSearch() {
925933
if (!this._debouncedSearch) {
926-
this._showLoading = true;
927934
this._debouncedSearch = debounce(async () => {
928-
// Wait a few milliseconds before showing the flyout.
929-
// This helps prevent loading state flickering while the user is actively changing the query.
930-
931935
const loadingTimeout = setTimeout(() => {
932-
if (!this.userInput.length) {
933-
this._foundPeople = [];
934-
this.hideFlyout();
935-
}
936-
}, 400);
937-
this.userInput = input.value;
936+
this._showLoading = true;
937+
}, 50);
938938

939939
await this.loadState();
940940
clearTimeout(loadingTimeout);
@@ -945,9 +945,7 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
945945
}, 400);
946946
}
947947

948-
if (this.userInput !== input.value) {
949-
this._debouncedSearch();
950-
}
948+
this._debouncedSearch();
951949
}
952950

953951
/**
@@ -961,7 +959,7 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
961959
if (event.keyCode === 40 || event.keyCode === 38) {
962960
// keyCodes capture: down arrow (40) and up arrow (38)
963961
this.handleArrowSelection(event);
964-
if (this.userInput.length > 0) {
962+
if (this.input.value.length > 0) {
965963
event.preventDefault();
966964
}
967965
}
@@ -1006,9 +1004,11 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
10061004

10071005
const peopleList = this.renderRoot.querySelector('.people-list');
10081006
// reset background color
1009-
// tslint:disable-next-line: prefer-for-of
1010-
for (let i = 0; i < peopleList.children.length; i++) {
1011-
peopleList.children[i].classList.remove('focused');
1007+
if (peopleList) {
1008+
// tslint:disable-next-line: prefer-for-of
1009+
for (let i = 0; i < peopleList.children.length; i++) {
1010+
peopleList.children[i].classList.remove('focused');
1011+
}
10121012
}
10131013
// set selected background
10141014
peopleList.children[this._arrowSelectionCount].classList.add('focused');

0 commit comments

Comments
 (0)