Skip to content

Commit

Permalink
fix(FormLabel): unhandled runtime error, elem is null (#4354)
Browse files Browse the repository at this point in the history
I think it makes sense to add the and statement:
https://github.com/dnbexperience/eufemia/pull/4354/files#diff-f476207b8a837f2c0dea83ca374c5d383bcd895b0f6f10e033e12d7c0a7a3f84R142
As all following code in the if statement is dependent on that
`ref.current` is existing.

Fixes #4352

---------

Co-authored-by: Tobias Høegh <[email protected]>
  • Loading branch information
langz and tujoworker authored Dec 2, 2024
1 parent 85445b4 commit a3414f1
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/dnb-eufemia/src/components/form-label/FormLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export default function FormLabel(localProps: FormLabelAllProps) {
forElem?.closest('.dnb-input__border--root') ||
forElem?.closest('.dnb-input__border')

if (target) {
if (target && ref.current) {
const elem = ref.current

const buttonEnter = () => {
Expand All @@ -166,20 +166,21 @@ export default function FormLabel(localProps: FormLabelAllProps) {
const leave = () => {
target.classList.remove('hover')

elem
.querySelector('button')
?.removeEventListener?.('mouseenter', buttonEnter)
const button = elem.querySelector('button')
button?.removeEventListener?.('mouseenter', buttonEnter)
}

elem?.addEventListener?.('mouseenter', enter)
elem?.addEventListener?.('mouseleave', leave)
elem.addEventListener('mouseenter', enter)
elem.addEventListener('mouseleave', leave)

return () => {
elem?.removeEventListener?.('mouseenter', enter)
elem?.removeEventListener?.('mouseleave', leave)
elem
.querySelector('button')
?.removeEventListener?.('mouseleave', buttonLeave)
if (elem) {
elem.removeEventListener('mouseenter', enter)
elem.removeEventListener('mouseleave', leave)

const button = elem.querySelector('button')
button?.removeEventListener?.('mouseleave', buttonLeave)
}
}
}
}, [forId, ref])
Expand Down

0 comments on commit a3414f1

Please sign in to comment.