Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(buttonGroup): issue with MutationObserver wrongly deselecting but… #14072

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,6 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
return;
}

this.updateSelected(index);

const button = this.buttons[index];
button.select();
}
Expand Down Expand Up @@ -387,12 +385,28 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
this.buttons.forEach((_, i) => {
if (i !== index && this.selectedIndexes.indexOf(i) !== -1) {
this.deselectButton(i);
this.updateDeselected(i);
}
});
}

}

public updateDeselected(index: number) {
const button = this.buttons[index];
if (this.selectedIndexes.indexOf(index) !== -1) {
this.selectedIndexes.splice(this.selectedIndexes.indexOf(index), 1);
}

this._renderer.setAttribute(button.nativeElement, 'aria-pressed', 'false');
this._renderer.removeClass(button.nativeElement, 'igx-button-group__item--selected');

const indexInViewButtons = this.viewButtons.toArray().indexOf(button);
if (indexInViewButtons !== -1) {
this.values[indexInViewButtons].selected = false;
}
}

/**
* Deselects a button by its index.
* ```typescript
Expand All @@ -412,16 +426,7 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
}

const button = this.buttons[index];
this.selectedIndexes.splice(this.selectedIndexes.indexOf(index), 1);

this._renderer.setAttribute(button.nativeElement, 'aria-pressed', 'false');
this._renderer.removeClass(button.nativeElement, 'igx-button-group__item--selected');
button.deselect();

const indexInViewButtons = this.viewButtons.toArray().indexOf(button);
if (indexInViewButtons !== -1) {
this.values[indexInViewButtons].selected = false;
}
}

/**
Expand Down Expand Up @@ -497,8 +502,6 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
* @hidden
*/
public _clickHandler(index: number) {
this.mutationObserver.disconnect();

const button = this.buttons[index];
const args: IButtonGroupEventArgs = { owner: this, button, index };

Expand All @@ -519,8 +522,6 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
this.deselected.emit(args);
}
}

this.mutationObserver?.observe(this._el.nativeElement, this.observerConfig);
}

private setMutationsObserver() {
Expand All @@ -544,7 +545,7 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
observer.observe(this._el.nativeElement, this.observerConfig);

// Cleanup function
this._renderer.listen(this._el.nativeElement, 'DOMNodeRemoved', () => {
this._renderer.listen(this._el.nativeElement, 'DOMAttrModified', () => {
observer.disconnect();
});
});
Expand All @@ -567,11 +568,11 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
}

private updateButtonSelectionState(index: number, args: IButtonGroupEventArgs) {
if (this.selectedIndexes.indexOf(index) === -1) {
this.selectButton(index);
if (this.buttons[index].selected) {
this.updateSelected(index);
this.selected.emit(args);
} else {
this.deselectButton(index);
this.updateDeselected(index);
this.deselected.emit(args);
}
}
Expand Down
Loading
Loading