File tree Expand file tree Collapse file tree 1 file changed +21
-10
lines changed
src/core/hooks/useIsInsideFrom Expand file tree Collapse file tree 1 file changed +21
-10
lines changed Original file line number Diff line number Diff line change 1- import { useMemo } from 'react' ;
1+ import { useEffect , useState } from 'react' ;
22
33/**
44 * Checks whether the given element is inside a <form>
55 * @param element The DOM element to check
66 * @returns true if the element is inside a form
77 */
88export function useIsInsideForm ( element : HTMLElement | null ) : boolean {
9- return useMemo ( ( ) => {
10- if ( ! element ) return false ;
9+ const [ insideForm , setInsideForm ] = useState ( false ) ;
1110
12- let current : HTMLElement | null = element ;
13- while ( current ) {
14- if ( current . tagName === 'FORM' ) return true ;
15- current = current . parentElement ;
16- }
17- return false ;
18- } , [ element ] ) ;
11+ useEffect ( ( ) => {
12+ if ( ! element ) {
13+ setInsideForm ( false ) ;
14+ return ;
15+ }
16+
17+ let current : HTMLElement | null = element ;
18+ while ( current ) {
19+ if ( current . tagName === 'FORM' ) {
20+ setInsideForm ( true ) ;
21+ return ;
22+ }
23+ current = current . parentElement ;
24+ }
25+
26+ setInsideForm ( false ) ;
27+ } , [ element ] ) ;
28+
29+ return insideForm ;
1930}
2031
2132export default useIsInsideForm ;
You can’t perform that action at this time.
0 commit comments