Skip to content

Commit 7032e88

Browse files
gavinbarronmusale
andauthored
fix: people picker single select mode (#2541)
Rephrase max selection placeholder and fix typo Set the input to disabled after single-select Reset input value when you search for a user in single select mode disable input when maxSelections reached by keyboard selection --------- Signed-off-by: Martin Musale <[email protected]> Co-authored-by: Martin Musale <[email protected]>
1 parent b89f045 commit 7032e88

File tree

4 files changed

+40
-26
lines changed

4 files changed

+40
-26
lines changed

index.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ <h2>mgt-login</h2>
5656
<!-- <h2>mgt-person me query two lines card on click with presence</h2> -->
5757
<!-- <mgt-person person-query="me" view="twoLines" person-card="hover" show-presence></mgt-person> -->
5858
<!-- <mgt-person-card person-query="me"></mgt-person-card> -->
59-
<h2>mgt-people-picker</h2>
60-
<mgt-people-picker show-max="6"></mgt-people-picker>
61-
<h2>mgt-teams-channel-picker</h2>
62-
<mgt-teams-channel-picker></mgt-teams-channel-picker>
59+
<!-- <h2>mgt-people-picker</h2>
60+
<mgt-people-picker selection-mode="single" show-max="6"></mgt-people-picker>
61+
<mgt-people-picker show-max="6"></mgt-people-picker> -->
62+
<!-- <h2>mgt-teams-channel-picker</h2>
63+
<mgt-teams-channel-picker></mgt-teams-channel-picker> -->
6364
<!-- <h2>mgt-tasks</h2>
6465
<mgt-tasks></mgt-tasks> -->
6566
<!-- <h2>mgt-agenda group-by-day</h2>

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,10 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
606606
this.addEventListener('paste', this.handlePaste);
607607
}
608608

609+
private get hasMaxSelections(): boolean {
610+
return this.selectionMode === 'single' && this.selectedPeople.length >= 1;
611+
}
612+
609613
/**
610614
* Focuses the input element when focus is called
611615
*
@@ -739,18 +743,8 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
739743
* @memberof MgtPeoplePicker
740744
*/
741745
protected renderInput(selectedPeopleTemplate: TemplateResult): TemplateResult {
742-
const placeholder = !this.disabled
743-
? this.placeholder
744-
? this.placeholder
745-
: this.strings.inputPlaceholderText
746-
: this.placeholder || '';
747-
748-
const selectionMode = this.selectionMode ? this.selectionMode : 'multiple';
749-
750-
if (selectionMode === 'single' && this.selectedPeople.length >= 1) {
751-
this.lostFocus();
752-
return html``;
753-
}
746+
const placeholder = this.disabled ? '' : this.placeholder || this.strings.inputPlaceholderText;
747+
const maxSelectionsAriaLabel = this.hasMaxSelections ? this.strings.maxSelectionsAriaLabel : '';
754748

755749
const searchIcon = html`<span class="search-icon">${getSvg(SvgIcon.Search)}</span>`;
756750
const startSlot = this.selectedPeople?.length > 0 ? selectedPeopleTemplate : searchIcon;
@@ -761,17 +755,17 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
761755
slot="anchor"
762756
id="people-picker-input"
763757
role="combobox"
758+
placeholder=${this.hasMaxSelections ? this.strings.maxSelectionsPlaceHolder : placeholder}
759+
aria-label=${this.ariaLabel || maxSelectionsAriaLabel || placeholder || this.strings.selectContact}
764760
aria-expanded=${this.flyout?.isOpen ?? false}
765-
placeholder=${placeholder}
766-
aria-label=${this.ariaLabel || placeholder || this.strings.selectContact}
767-
@click="${this.handleInputClick}"
768-
@focus="${this.gainedFocus}"
769-
@keydown="${this.onUserKeyDown}"
770-
@keyup="${this.onUserKeyUp}"
771-
@input="${this.onUserInput}"
761+
@click="${this.hasMaxSelections ? undefined : this.handleInputClick}"
762+
@focus="${this.hasMaxSelections ? undefined : this.gainedFocus}"
763+
@keydown="${this.hasMaxSelections ? undefined : this.onUserKeyDown}"
764+
@input="${this.hasMaxSelections ? undefined : this.onUserInput}"
772765
@blur="${this.lostFocus}"
773-
?disabled=${this.disabled}>
774-
<span slot="start">${startSlot}</span>
766+
?disabled=${this.disabled}
767+
>
768+
<span slot="start">${startSlot}</span>
775769
</fluent-text-field>
776770
`;
777771
}
@@ -1296,10 +1290,14 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
12961290
}
12971291
return p.id !== person.id;
12981292
});
1293+
const inputControl = this.input.shadowRoot.querySelector<HTMLInputElement>('input');
1294+
if (this.hasMaxSelections && inputControl) {
1295+
inputControl.removeAttribute('disabled');
1296+
}
12991297
this.selectedPeople = filteredPersonArr;
13001298
void this.loadState();
13011299
this.fireCustomEvent('selectionChanged', this.selectedPeople);
1302-
this.input?.focus();
1300+
inputControl?.focus();
13031301
}
13041302

13051303
/**
@@ -1473,6 +1471,11 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
14731471
// handle suggestion list item click
14741472
private handleSuggestionClick(person: IDynamicPerson): void {
14751473
this.addPerson(person);
1474+
const inputControl = this.input.shadowRoot.querySelector<HTMLInputElement>('input');
1475+
if (this.hasMaxSelections && inputControl) {
1476+
inputControl.setAttribute('disabled', 'true');
1477+
this.input.value = inputControl.value = '';
1478+
}
14761479
this.hideFlyout();
14771480
}
14781481

@@ -1559,6 +1562,10 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
15591562
this.addPerson(foundPerson);
15601563
this.hideFlyout();
15611564
this.input.value = '';
1565+
const inputControl = this.input.shadowRoot.querySelector<HTMLInputElement>('input');
1566+
if (this.hasMaxSelections && inputControl) {
1567+
inputControl.setAttribute('disabled', 'true');
1568+
}
15621569
}
15631570
} else if (this.allowAnyEmail) {
15641571
this.handleAnyEmail();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
export const strings = {
99
inputPlaceholderText: 'Start typing a name',
10+
maxSelectionsPlaceHolder: 'Max contacts added',
11+
maxSelectionsAriaLabel: 'Maximum contact selections reached',
1012
noResultsFound: "We didn't find any matches.",
1113
loadingMessage: 'Loading...',
1214
selected: 'selected',

stories/components/peoplePicker/peoplePicker.properties.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export const groupId = () => html`
1818
<mgt-people-picker group-id="02bd9fd6-8f93-4758-87c3-1fb73740a315"></mgt-people-picker>
1919
`;
2020

21+
export const singleSelectMode = () => html`
22+
<mgt-people-picker selection-mode="single"></mgt-people-picker>
23+
`;
24+
2125
export const dynamicGroupId = () => html`
2226
<mgt-people-picker id="picker"></mgt-people-picker>
2327
<div>

0 commit comments

Comments
 (0)