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

TreeSelect popup closes when unchecking nodes #13698

Closed
joel-matthias opened this issue Sep 18, 2023 · 11 comments · Fixed by #15037
Closed

TreeSelect popup closes when unchecking nodes #13698

joel-matthias opened this issue Sep 18, 2023 · 11 comments · Fixed by #15037
Assignees
Labels
LTS-PORTABLE Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Milestone

Comments

@joel-matthias
Copy link

Describe the bug

When selectionMode is set to 'checkbox', unchecking a checked tree node causes the popup to close prematurely

Environment

Issue can be reproduced in PrimeNG demo app https://primeng.org/treeselect#checkbox

Reproducer

No response

Angular version

16

PrimeNG version

16.3.1

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

16.20.1

Browser(s)

Chrome, Firefox

Steps to reproduce the behavior

Open PrimeNG demo page at https://primeng.org/treeselect#checkbox
Open select
Check an item
Uncheck that item
Result: Popup tree closes
Expected: Popup tree should stay open

Expected behavior

Popup tree should stay open

@joel-matthias joel-matthias added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Sep 18, 2023
@Romain-Luengo
Copy link

Romain-Luengo commented Oct 24, 2023

Same behavior here

https://primeng.org/multiselect

@fkrajcar-cirtuo
Copy link

fkrajcar-cirtuo commented Mar 5, 2024

+1

also happens on PrimeNG v17.7.0, seems like it was working as expected on PrimeNG v15 - https://www.primefaces.org/primeng-v15-lts/treeselect#checkbox

@fkrajcar-cirtuo
Copy link

There is a workaround for this bug:

<p-treeSelect #treeselect class="w-full md:w-20rem" containerStyleClass="w-full" [(ngModel)]="selectedNodes" [options]="nodes" display="chip" [metaKeySelection]="false" selectionMode="checkbox" placeholder="Select Item" (onHide)="onHide($event)"></p-treeSelect>

Add [metaKeySelection]="false" and (onHide)="onHide($event)" on <p-treeSelect>

In your .ts:

  onHide(event) {
    if (event === undefined) {
      this.treeselect.overlayVisible = true;
    }
  }

Credit goes to Berkin Sansal - SO thread - https://stackoverflow.com/questions/76976298/how-to-prevent-overlay-of-treeselect-checkbox-auto-collapse-when-click-child-nod

@GitHubStan
Copy link

onHide(event) {
if (event === undefined) {
this.treeselect.overlayVisible = true;
}
}

Thanks for the fix, @fkrajcar-cirtuo. However, it's a partial fix. With this fix, they can't close the treeselect by clicking on the select box. Since event is undefined, we can't tell what they clicked on to conditionally hide the overlay.

@GitHubStan
Copy link

Note that this only happens when you click on the checkbox. If you click on the label for the checkbox, things work as expected.

@fkrajcar-cirtuo
Copy link

onHide(event) {
if (event === undefined) {
this.treeselect.overlayVisible = true;
}
}

Thanks for the fix, @fkrajcar-cirtuo. However, it's a partial fix. With this fix, they can't close the treeselect by clicking on the select box. Since event is undefined, we can't tell what they clicked on to conditionally hide the overlay.

Yes, that is correct, but that was not a problem in our case since clicking outside the panel (and label itself) and ESC closes it as expected.

@GitHubStan
Copy link

Here's my monkey patch of onClick() that allows med to close the overlay by clicking on the dropdown box.

  @ViewChild(TreeSelect)
  treeselect: TreeSelect;

  ngAfterViewInit(): void {
    this.treeselect.onClick = this.monkeyPatchTreeSelectOnClick.bind(this);
  }

private monkeyPatchTreeSelectOnClick(event: any) {
  if (this.treeselect.disabled) {
    return;
  }

  if (
    !this.treeselect.overlayViewChild?.el?.nativeElement?.contains(event.target) &&
    !DomHandler.hasClass(event.target, 'p-treeselect-close') &&
    this.indicatesDropDownBox(event) // This is the line added for the fix
  ) {
    if (this.treeselect.overlayVisible) {
      this.treeselect.hide();
    } else {
      this.treeselect.show();
    }

    this.treeselect.focusInput?.nativeElement.focus();
  }
}

private indicatesDropDownBox(event: any) {
  const classesIndicatingDropdownWasClicked = [
    'p-treeselect-label',
    'p-treeselect-label-container',
    'p-treeselect-trigger',
    'p-treeselect-trigger-icon',
  ];
  const indicatesDropDownBox = classesIndicatingDropdownWasClicked.some((c) => DomHandler.hasClass(event.target, c));
  return indicatesDropDownBox;
}

@cetincakiroglu cetincakiroglu added this to the 17.11.0 milestone Mar 8, 2024
@cetincakiroglu cetincakiroglu added Type: Bug Issue contains a bug related to a specific component. Something about the component is not working and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Mar 13, 2024
@cetincakiroglu cetincakiroglu self-assigned this Mar 13, 2024
cetincakiroglu added a commit that referenced this issue Mar 13, 2024
Fixed #13698 - TreeSelect | Update condition of onClick
@zfloyd
Copy link

zfloyd commented Apr 1, 2024

@cetincakiroglu , this update has caused the TreeSelect to not close when in "single" mode after making a selection. I'm not sure why this change was made, but its almost assuredly the reason for the break.

if (this.selectionMode === 'single') {
            // this.hide();
            this.focusInput?.nativeElement.focus();
        }

@cetincakiroglu
Copy link
Contributor

cetincakiroglu commented Apr 1, 2024

@cetincakiroglu , this update has caused the TreeSelect to not close when in "single" mode after making a selection. I'm not sure why this change was made, but its almost assuredly the reason for the break.

if (this.selectionMode === 'single') {
            // this.hide();
            this.focusInput?.nativeElement.focus();
        }

Hi,

Thanks for letting us know, we'll check it. Could you please also share a stackblitz example and tag me so we can better understand the issue?

@zfloyd
Copy link

zfloyd commented Apr 1, 2024

@cetincakiroglu just compare the "Basic" example of Tree Select in PrimeNG 16 with the latest one in the demo site:
https://www.primefaces.org/primeng-v16-lts/treeselect#basic
https://primeng.org/treeselect#basic

Selecting an item is supposed to close the control.

@mrkubad
Copy link

mrkubad commented Apr 19, 2024

Hi,

As @zfloyd said this fix has broken behavior that caused to close overlay in a single selection mode.

control behavior before fix

before fix

control behavior after fix

after fix

Is someone working on fixing this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LTS-PORTABLE Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants