Skip to content

Commit

Permalink
fix: enable the input control when selectedPeople is cleared via code. (
Browse files Browse the repository at this point in the history
#2792)

handle disabling the input when setting the selectedPeople to an empty array via code
fix stylelint redundant longhand property names error
add a storybook to demo people picker in modal with single selection mode

---------

Signed-off-by: Martin Musale <martinmusale@microsoft.com>
Co-authored-by: Nickii Miaro <miaronkirote@gmail.com>
  • Loading branch information
musale and Mnickii authored Oct 31, 2023
1 parent 40e9e18 commit f54dc81
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
if (!value) value = [];
if (!arraysAreEqual(this._selectedPeople, value)) {
this._selectedPeople = value;
this.fireCustomEvent('selectionChanged', this._selectedPeople);
this.requestUpdate();
}
}
Expand Down Expand Up @@ -612,8 +613,40 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
this.addEventListener('copy', this.handleCopy);
this.addEventListener('cut', this.handleCut);
this.addEventListener('paste', this.handlePaste);
this.addEventListener('selectionChanged', this.handleSelectionChanged);
}

/**
* Disable the inner input of the fluent-text-field.
*/
private disableTextInput() {
const inputControl = this.input.shadowRoot.querySelector<HTMLInputElement>('input');
if (inputControl) {
inputControl.setAttribute('disabled', 'true');
inputControl.value = '';
}
}

/**
* Enable the inner input of the fluent-text-field.
*/
private enableTextInput() {
const inputControl = this.input.shadowRoot.querySelector<HTMLInputElement>('input');
if (inputControl) {
inputControl.removeAttribute('disabled');
inputControl.focus();
}
}

/**
* Clears the disabled property on the people picker when used in single mode.
*/
private readonly handleSelectionChanged = () => {
if (this.selectedPeople.length === 0 && !this.disabled) {
this.enableTextInput();
}
};

private get hasMaxSelections(): boolean {
return this.selectionMode === 'single' && this.selectedPeople.length >= 1;
}
Expand Down Expand Up @@ -1125,7 +1158,6 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {

this.selectedPeople = [...this.defaultSelectedUsers, ...this.defaultSelectedGroups];
this.requestUpdate();
this.fireCustomEvent('selectionChanged', this.selectedPeople);
}

if (input) {
Expand Down Expand Up @@ -1299,14 +1331,11 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
}
return p.id !== person.id;
});
const inputControl = this.input.shadowRoot.querySelector<HTMLInputElement>('input');
if (this.hasMaxSelections && inputControl) {
inputControl.removeAttribute('disabled');
if (this.hasMaxSelections) {
this.enableTextInput();
}
this.selectedPeople = filteredPersonArr;
void this.loadState();
this.fireCustomEvent('selectionChanged', this.selectedPeople);
inputControl?.focus();
}

/**
Expand Down Expand Up @@ -1341,7 +1370,6 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {

if (duplicatePeople.length === 0) {
this.selectedPeople = [...this.selectedPeople, person];
this.fireCustomEvent('selectionChanged', this.selectedPeople);
void this.loadState();
this._foundPeople = [];
this._arrowSelectionCount = -1;
Expand Down Expand Up @@ -1429,8 +1457,6 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
this.selectedPeople = this.selectedPeople.splice(0, this.selectedPeople.length - 1);
void this.loadState();
this.hideFlyout();
// fire selected people changed event
this.fireCustomEvent('selectionChanged', this.selectedPeople);
return;
}

Expand Down Expand Up @@ -1480,10 +1506,9 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
// handle suggestion list item click
private handleSuggestionClick(person: IDynamicPerson): void {
this.addPerson(person);
const inputControl = this.input.shadowRoot.querySelector<HTMLInputElement>('input');
if (this.hasMaxSelections && inputControl) {
inputControl.setAttribute('disabled', 'true');
this.input.value = inputControl.value = '';
if (this.hasMaxSelections) {
this.disableTextInput();
this.input.value = '';
}
this.hideFlyout();
}
Expand Down Expand Up @@ -1571,9 +1596,8 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
this.addPerson(foundPerson);
this.hideFlyout();
this.input.value = '';
const inputControl = this.input.shadowRoot.querySelector<HTMLInputElement>('input');
if (this.hasMaxSelections && inputControl) {
inputControl.setAttribute('disabled', 'true');
if (this.hasMaxSelections) {
this.disableTextInput();
}
}
} else if (this.allowAnyEmail) {
Expand Down Expand Up @@ -1700,7 +1724,6 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
this._currentHighlightedUserPos = 0;
void this.loadState();
this.hideFlyout();
this.fireCustomEvent('selectionChanged', this.selectedPeople);
}
/**
* Changes the color class to show which people are selected for copy/cut-paste
Expand Down
35 changes: 35 additions & 0 deletions stories/components/peoplePicker/peoplePicker.properties.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,41 @@ export const groupId = () => html`

export const singleSelectMode = () => html`
<mgt-people-picker selection-mode="single"></mgt-people-picker>
<h2>Render in a modal and clear on opening the modal</h2>
<button aria-label="open modal" id="modal">Open modal</button>
<div id="modal-content">
<mgt-people-picker id="modal-picker" selection-mode="single"></mgt-people-picker>
<button aria-label="close modal" id="close-modal">X</button>
</div>
<style>
#modal-content {
height: 200px;
width: 100%;
background-color: beige;
display: none;
align-items: center;
justify-content: center;
gap: 6px;
}
</style>
<script>
const modal = document.getElementById("modal")
const closeModal = document.getElementById("close-modal")
const modalContent = document.getElementById("modal-content")
const modalPicker = document.getElementById("modal-picker")
modal.addEventListener('click', () => {
modalContent.style.display = "flex"
modalPicker.selectedPeople = []
})
closeModal.addEventListener('click', () => {
modalContent.style.display = "none"
})
</script>
`;

export const dynamicGroupId = () => html`
Expand Down

0 comments on commit f54dc81

Please sign in to comment.