fix(ui): usePreventLeave should not show alert for exceptions #12722
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When using 3rd party custom components in an edit form there exists a possibility that a non-navigational click event will propagate through to payload.
In this case the
findClosestAnchor
function inusePreventLeave
may find an anchor without href, resulting in thenewUrlObj = new URL(newUrl)
inisAnchorOfCurrentUrl
throwing the exception:As a result a native alert is shown to the user, with no real explanation as to what is going on. This is not a good experience.
I suggest moving it to a console log which is less "in your face" for users who do not know what to do about it anyway.
I discovered this while using a data grid component with a context menu. Clicking on menu items (which are
<a>
tags without href in this component) triggers the error.(Another on-liner fix would ofc be to not attempt to create an URL object if there is no href
if (anchor?.href) {
, but I opted for this version since usingalert()
in production code is not a preferred practice anyway)