Skip to content

Commit f738af8

Browse files
authored
fix: find dom node method deprecation
1 parent c3c0efb commit f738af8

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/Accordion/AccordionBody.tsx

+32-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import classNames from 'classnames';
2-
import React, { ElementType, PropsWithChildren, useCallback, useState } from 'react';
2+
import React, { ElementType, PropsWithChildren, useCallback, useRef, useState } from 'react';
33
import { Transition } from 'react-transition-group';
44
import type { TransitionProps } from 'react-transition-group/Transition';
55
import { TransitionTimeouts, TransitionsKeys } from '../transitions';
@@ -39,43 +39,53 @@ export const AccordionBody = ({
3939
...attributes
4040
}: PropsWithChildren<AccordionBodyProps>) => {
4141
const [height, setHeight] = useState<null | number>(null);
42-
42+
const nodeRef = useRef<HTMLElement>(null);
4343
const onEntering = useCallback(
44-
(node: HTMLElement, isAppearing: boolean) => {
45-
setHeight(getHeight(node));
46-
attributes.onEntering?.(node, isAppearing);
44+
(isAppearing: boolean) => {
45+
if (nodeRef.current) {
46+
setHeight(getHeight(nodeRef.current));
47+
attributes.onEntering?.(nodeRef.current, isAppearing);
48+
}
4749
},
4850
[attributes.onEntering]
4951
);
5052
const onEntered = useCallback(
51-
(node: HTMLElement, isAppearing: boolean) => {
53+
(isAppearing: boolean) => {
5254
setHeight(null);
53-
attributes.onEntered?.(node, isAppearing);
55+
if (nodeRef.current) {
56+
attributes.onEntered?.(nodeRef.current, isAppearing);
57+
}
5458
},
5559
[attributes.onEntered]
5660
);
5761
const onExit = useCallback(
58-
(node: HTMLElement) => {
59-
setHeight(getHeight(node));
60-
attributes.onExit?.(node);
62+
() => {
63+
if (nodeRef.current) {
64+
setHeight(getHeight(nodeRef.current));
65+
attributes.onExit?.(nodeRef.current);
66+
}
6167
},
6268
[attributes.onExit]
6369
);
6470
const onExiting = useCallback(
65-
(node: HTMLElement) => {
66-
// getting this variable triggers a reflow
67-
// @ts-expect-error variabile non usata
68-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
69-
const _unused = node.offsetHeight;
70-
setHeight(0);
71-
attributes.onExiting?.(node);
71+
() => {
72+
if (nodeRef.current) {
73+
// getting this variable triggers a reflow
74+
// @ts-expect-error variabile non usata
75+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
76+
const _unused = nodeRef.current.offsetHeight;
77+
setHeight(0);
78+
attributes.onExiting?.(nodeRef.current);
79+
}
7280
},
7381
[attributes.onExiting]
7482
);
7583
const onExited = useCallback(
76-
(node: HTMLElement) => {
84+
() => {
7785
setHeight(null);
78-
attributes.onExited?.(node);
86+
if (nodeRef.current) {
87+
attributes.onExited?.(nodeRef.current);
88+
}
7989
},
8090
[attributes.onExited]
8191
);
@@ -86,14 +96,15 @@ export const AccordionBody = ({
8696

8797
return (
8898
<Transition
89-
{...transitionProps}
99+
nodeRef={nodeRef}
90100
timeout={timeout}
91101
in={active}
92102
onEntering={onEntering}
93103
onEntered={onEntered}
94104
onExit={onExit}
95105
onExiting={onExiting}
96106
onExited={onExited}
107+
{...transitionProps}
97108
>
98109
{(status) => {
99110
const transitionClass = getTransitionClass(status);
@@ -102,12 +113,11 @@ export const AccordionBody = ({
102113
const style = height == null ? null : { height };
103114

104115
return (
105-
<Tag className={classes} style={{ ...childProps.style, ...style }} {...childProps}>
116+
<Tag className={classes} ref={nodeRef} style={{ ...childProps.style, ...style }} {...childProps}>
106117
<div className={listClasses}>{children}</div>
107118
</Tag>
108119
);
109120
}}
110121
</Transition>
111122
);
112123
};
113-
// }

0 commit comments

Comments
 (0)