Skip to content

Commit da97f2d

Browse files
authored
feat: add forwardRef (#298)
* feat: add forwardRef * update
1 parent d43752a commit da97f2d

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/Collapse.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function getActiveKeysArray(activeKey: React.Key | React.Key[]) {
1515
return currentActiveKey.map((key) => String(key));
1616
}
1717

18-
function Collapse(props: CollapseProps) {
18+
const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) => {
1919
const {
2020
prefixCls = 'rc-collapse',
2121
destroyInactivePanel = false,
@@ -111,10 +111,15 @@ function Collapse(props: CollapseProps) {
111111

112112
// ======================== Render ========================
113113
return (
114-
<div className={collapseClassName} style={style} role={accordion ? 'tablist' : undefined}>
114+
<div
115+
ref={ref}
116+
className={collapseClassName}
117+
style={style}
118+
role={accordion ? 'tablist' : undefined}
119+
>
115120
{children}
116121
</div>
117122
);
118-
}
123+
});
119124

120125
export default Object.assign(Collapse, { Panel: CollapsePanel });

src/Panel.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import KeyCode from 'rc-util/lib/KeyCode';
55
import CSSMotion from 'rc-motion';
66
import PanelContent from './PanelContent';
77

8-
function CollapsePanel(props: CollapsePanelProps) {
8+
const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((props, ref) => {
99
const {
1010
showArrow = true,
1111
headerClass,
@@ -88,7 +88,7 @@ function CollapsePanel(props: CollapsePanelProps) {
8888

8989
// ======================== Render ========================
9090
return (
91-
<div {...resetProps} className={collapsePanelClassNames}>
91+
<div {...resetProps} ref={ref} className={collapsePanelClassNames}>
9292
<div {...headerProps}>
9393
{showArrow && iconNode}
9494
<span
@@ -106,10 +106,10 @@ function CollapsePanel(props: CollapsePanelProps) {
106106
forceRender={forceRender}
107107
removeOnLeave={destroyInactivePanel}
108108
>
109-
{({ className: motionClassName, style: motionStyle }, ref) => {
109+
{({ className: motionClassName, style: motionStyle }, motionRef) => {
110110
return (
111111
<PanelContent
112-
ref={ref}
112+
ref={motionRef}
113113
prefixCls={prefixCls}
114114
className={motionClassName}
115115
style={motionStyle}
@@ -124,6 +124,6 @@ function CollapsePanel(props: CollapsePanelProps) {
124124
</CSSMotion>
125125
</div>
126126
);
127-
}
127+
});
128128

129129
export default CollapsePanel;

tests/index.spec.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,18 @@ describe('collapse', () => {
638638

639639
expect(container.querySelectorAll('.rc-collapse-item')).toHaveLength(2);
640640
});
641+
642+
it('ref should work', () => {
643+
const ref = React.createRef<any>();
644+
const panelRef = React.createRef<any>();
645+
const { container } = render(
646+
<Collapse ref={ref}>
647+
<Panel header="collapse 1" key="1" ref={panelRef}>
648+
first
649+
</Panel>
650+
</Collapse>,
651+
);
652+
expect(ref.current).toBe(container.firstChild);
653+
expect(panelRef.current).toBe(container.querySelector('.rc-collapse-item'));
654+
});
641655
});

0 commit comments

Comments
 (0)