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 #6000: Improved closeOnEscape handling #6012

Merged
merged 1 commit into from
Feb 19, 2024

Conversation

melloware
Copy link
Member

@melloware melloware commented Feb 19, 2024

Fix #6000: Improved closeOnEscape handling

  1. Removed the unnecessary check if (!(group in groupToDisplayedElements)) since we're checking if groupToDisplayedElements[group] exists directly.
  2. Simplified the calculation of newDisplayOrder by using the push method, which returns the new length of the array.
  3. Removed the deletion of elements by index and replaced it with finding the index of the element using indexOf and then removing it using splice.
  4. Removed the cleanup logic to reduce the length of the array since it seems unnecessary considering we are setting setDisplayOrder(undefined).

This version should be cleaner and more concise while maintaining the same functionality.

@melloware melloware added the Core Team Issue or pull request has been *opened* by a member of Core Team label Feb 19, 2024
Copy link

vercel bot commented Feb 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
primereact ⬜️ Ignored (Inspect) Visit Preview Feb 19, 2024 3:53pm
primereact-v9 ⬜️ Ignored (Inspect) Visit Preview Feb 19, 2024 3:53pm

Copy link

@Manizuca Manizuca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi!

This looks cleaner. It still has a problem though.

Consider this case:

  • useDisplayOrder is called for a Dialog with uid "pr-1". It returns 1.
  • useDisplayOrder is called for a Dialog with uid "pr-2". It returns 2.
  • useDisplayOrder is called for a Dialog with uid "pr-3". It returns 3.

At this point, groupToDisplayedElements[group] will be ["pr-1", "pr-2", "pr-3"].

  • Now, consider the case the Dialog with uid "pr-2" unmounts. Since Array.splice() removes the given index, and moves the items after that. That means that after unmounting groupToDisplayedElements[group] will be ["pr-1", "pr-3"].

  • Finally, useDisplayOrder is called for a Dialog with uid "pr-4". It returns 3, since there are currently 2 items at the array. At this point, you will have 2 items with displayOrder 3, which could cause issues with the global esc key listener.

I think the cleanup function should be like:

const index = groupToDisplayedElements[group].indexOf(uid);
delete groupToDisplayedElements[group][index]; // or just newDisplayOrder - 1

// Reduce array length, by removing undefined elements at the end of array:
const lastIndex = groupToDisplayedElements[group].length - 1:
const lastOrder = groupToDisplayedElements[group].findLastIndex((el) => el !== undefined);
if (lastOrder !== lastIndex)  groupToDisplayedElements[group].splice(lastOrder + 1);

@melloware
Copy link
Member Author

OK PR updated.

@melloware melloware requested a review from Manizuca February 19, 2024 15:54
Copy link

@Manizuca Manizuca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@melloware melloware merged commit 325a451 into primefaces:master Feb 19, 2024
6 checks passed
@melloware melloware deleted the PR6000-2 branch February 19, 2024 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Team Issue or pull request has been *opened* by a member of Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useDisplayOrder: clears incorrect
2 participants