Skip to content

Commit

Permalink
feat: fallback to original findDOMNode if using function component (#145
Browse files Browse the repository at this point in the history
)

* fallback to original findDOMNode if fc

* update demo

* clean up
  • Loading branch information
zombieJ authored Oct 11, 2019
1 parent 8c2ad74 commit 30b3954
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
33 changes: 19 additions & 14 deletions examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,27 @@ const builtinPlacements = {
},
};

function preventDefault(e) {
e.preventDefault();
}

function getPopupContainer(trigger) {
return trigger.parentNode;
}

const InnerTarget = React.forwardRef((props, ref) => {
React.useImperativeHandle(ref, () => ({}));

return (
<div
style={{ margin: 20, display: 'inline-block', background: 'rgba(255, 0, 0, 0.05)' }}
tabIndex={0}
role="button"
{...props}
>
<p>This is a example of trigger usage.</p>
<p>You can adjust the value above</p>
<p>which will also change the behaviour of popup.</p>
</div>
);
});

class Test extends React.Component {
state = {
mask: false,
Expand Down Expand Up @@ -259,7 +272,7 @@ class Test extends React.Component {
</div>
<div style={{ margin: 120, position: 'relative' }}>
<Trigger
getPopupContainer={undefined && getPopupContainer}
getPopupContainer={getPopupContainer}
popupAlign={this.getPopupAlign()}
popupPlacement={state.placement}
destroyPopupOnHide={this.state.destroyPopupOnHide}
Expand All @@ -281,15 +294,7 @@ class Test extends React.Component {
popup={<div>i am a popup</div>}
popupTransitionName={state.transitionName}
>
<a
style={{ margin: 20, display: 'inline-block', background: 'rgba(255, 0, 0, 0.05)' }}
href="#"
onClick={preventDefault}
>
<p>This is a example of trigger usage.</p>
<p>You can adjust the value above</p>
<p>which will also change the behaviour of popup.</p>
</a>
<InnerTarget />
</Trigger>
</div>
</div>
Expand Down
15 changes: 13 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { HTMLAttributes } from 'react';
import ReactDOM from 'react-dom';
import contains from 'rc-util/lib/Dom/contains';
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
import { composeRef } from 'rc-util/lib/ref';
Expand Down Expand Up @@ -394,12 +395,22 @@ export function generateTrigger(PortalComponent: any): React.ComponentClass<Trig
return null;
}

getRootDomNode = () => {
getRootDomNode = (): HTMLElement => {
const { getTriggerDOMNode } = this.props;
if (getTriggerDOMNode) {
return getTriggerDOMNode(this.triggerRef.current);
}
return findDOMNode<HTMLElement>(this.triggerRef.current);

try {
const domNode = findDOMNode<HTMLElement>(this.triggerRef.current);
if (domNode) {
return domNode;
}
} catch (err) {
// Do nothing
}

return ReactDOM.findDOMNode(this) as HTMLElement;
};

getPopupClassNameFromAlign = align => {
Expand Down
27 changes: 27 additions & 0 deletions tests/basic.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,31 @@ describe('Trigger.Basic', () => {
'target className-in-trigger-1 className-in-trigger-2',
);
});

it('support function component', () => {
const NoRef = React.forwardRef((props, ref) => {
React.useImperativeHandle(ref, () => null);
return (
<div className="target" {...props}>
click
</div>
);
});

const wrapper = mount(
<Trigger
action={['click']}
popupAlign={placementAlignMap.left}
popup={<strong className="x-content">tooltip2</strong>}
>
<NoRef />
</Trigger>,
);

wrapper.trigger();
expect(wrapper.isHidden()).toBeFalsy();

wrapper.trigger();
expect(wrapper.isHidden()).toBeTruthy();
});
});

1 comment on commit 30b3954

@vercel
Copy link

@vercel vercel bot commented on 30b3954 Oct 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.