Skip to content

Commit 30b3954

Browse files
authored
feat: fallback to original findDOMNode if using function component (#145)
* fallback to original findDOMNode if fc * update demo * clean up
1 parent 8c2ad74 commit 30b3954

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

examples/simple.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,27 @@ const builtinPlacements = {
3131
},
3232
};
3333

34-
function preventDefault(e) {
35-
e.preventDefault();
36-
}
37-
3834
function getPopupContainer(trigger) {
3935
return trigger.parentNode;
4036
}
4137

38+
const InnerTarget = React.forwardRef((props, ref) => {
39+
React.useImperativeHandle(ref, () => ({}));
40+
41+
return (
42+
<div
43+
style={{ margin: 20, display: 'inline-block', background: 'rgba(255, 0, 0, 0.05)' }}
44+
tabIndex={0}
45+
role="button"
46+
{...props}
47+
>
48+
<p>This is a example of trigger usage.</p>
49+
<p>You can adjust the value above</p>
50+
<p>which will also change the behaviour of popup.</p>
51+
</div>
52+
);
53+
});
54+
4255
class Test extends React.Component {
4356
state = {
4457
mask: false,
@@ -259,7 +272,7 @@ class Test extends React.Component {
259272
</div>
260273
<div style={{ margin: 120, position: 'relative' }}>
261274
<Trigger
262-
getPopupContainer={undefined && getPopupContainer}
275+
getPopupContainer={getPopupContainer}
263276
popupAlign={this.getPopupAlign()}
264277
popupPlacement={state.placement}
265278
destroyPopupOnHide={this.state.destroyPopupOnHide}
@@ -281,15 +294,7 @@ class Test extends React.Component {
281294
popup={<div>i am a popup</div>}
282295
popupTransitionName={state.transitionName}
283296
>
284-
<a
285-
style={{ margin: 20, display: 'inline-block', background: 'rgba(255, 0, 0, 0.05)' }}
286-
href="#"
287-
onClick={preventDefault}
288-
>
289-
<p>This is a example of trigger usage.</p>
290-
<p>You can adjust the value above</p>
291-
<p>which will also change the behaviour of popup.</p>
292-
</a>
297+
<InnerTarget />
293298
</Trigger>
294299
</div>
295300
</div>

src/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { HTMLAttributes } from 'react';
2+
import ReactDOM from 'react-dom';
23
import contains from 'rc-util/lib/Dom/contains';
34
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
45
import { composeRef } from 'rc-util/lib/ref';
@@ -394,12 +395,22 @@ export function generateTrigger(PortalComponent: any): React.ComponentClass<Trig
394395
return null;
395396
}
396397

397-
getRootDomNode = () => {
398+
getRootDomNode = (): HTMLElement => {
398399
const { getTriggerDOMNode } = this.props;
399400
if (getTriggerDOMNode) {
400401
return getTriggerDOMNode(this.triggerRef.current);
401402
}
402-
return findDOMNode<HTMLElement>(this.triggerRef.current);
403+
404+
try {
405+
const domNode = findDOMNode<HTMLElement>(this.triggerRef.current);
406+
if (domNode) {
407+
return domNode;
408+
}
409+
} catch (err) {
410+
// Do nothing
411+
}
412+
413+
return ReactDOM.findDOMNode(this) as HTMLElement;
403414
};
404415

405416
getPopupClassNameFromAlign = align => {

tests/basic.test.jsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,31 @@ describe('Trigger.Basic', () => {
512512
'target className-in-trigger-1 className-in-trigger-2',
513513
);
514514
});
515+
516+
it('support function component', () => {
517+
const NoRef = React.forwardRef((props, ref) => {
518+
React.useImperativeHandle(ref, () => null);
519+
return (
520+
<div className="target" {...props}>
521+
click
522+
</div>
523+
);
524+
});
525+
526+
const wrapper = mount(
527+
<Trigger
528+
action={['click']}
529+
popupAlign={placementAlignMap.left}
530+
popup={<strong className="x-content">tooltip2</strong>}
531+
>
532+
<NoRef />
533+
</Trigger>,
534+
);
535+
536+
wrapper.trigger();
537+
expect(wrapper.isHidden()).toBeFalsy();
538+
539+
wrapper.trigger();
540+
expect(wrapper.isHidden()).toBeTruthy();
541+
});
515542
});

0 commit comments

Comments
 (0)