Skip to content

Commit b1811a2

Browse files
committed
feat: Add getTriggerDOMNode prop for FC
1 parent 0132a3f commit b1811a2

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/index.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ export interface TriggerProps {
8888
maskTransitionName?: TransitionNameType;
8989
/** @deprecated Please us `maskMotion` instead. */
9090
maskAnimation?: string;
91+
92+
/**
93+
* @private Get trigger DOM node.
94+
* Used for some component is function component which can not access by `findDOMNode`
95+
*/
96+
getTriggerDOMNode?: (node: React.ReactInstance) => HTMLElement;
9197
}
9298

9399
interface TriggerState {
@@ -128,7 +134,7 @@ export function generateTrigger(PortalComponent: any): React.ComponentClass<Trig
128134

129135
popupRef = React.createRef<Popup>();
130136

131-
triggerRef = React.createRef<Popup>();
137+
triggerRef = React.createRef<React.ReactInstance>();
132138

133139
clickOutsideHandler: CommonEventHandler;
134140

@@ -363,7 +369,7 @@ export function generateTrigger(PortalComponent: any): React.ComponentClass<Trig
363369
}
364370

365371
const { target } = event;
366-
const root = findDOMNode(this.triggerRef.current);
372+
const root = this.getRootDomNode();
367373
if (!contains(root, target) && !this.hasPopupMouseDown) {
368374
this.close();
369375
}
@@ -388,7 +394,13 @@ export function generateTrigger(PortalComponent: any): React.ComponentClass<Trig
388394
return null;
389395
}
390396

391-
getRootDomNode = () => findDOMNode<HTMLElement>(this.triggerRef.current);
397+
getRootDomNode = () => {
398+
const { getTriggerDOMNode } = this.props;
399+
if (getTriggerDOMNode) {
400+
return getTriggerDOMNode(this.triggerRef.current);
401+
}
402+
return findDOMNode<HTMLElement>(this.triggerRef.current);
403+
};
392404

393405
getPopupClassNameFromAlign = align => {
394406
const className = [];
@@ -490,7 +502,7 @@ export function generateTrigger(PortalComponent: any): React.ComponentClass<Trig
490502
popupContainer.style.left = '0';
491503
popupContainer.style.width = '100%';
492504
const mountNode = props.getPopupContainer
493-
? props.getPopupContainer(findDOMNode(this.triggerRef.current))
505+
? props.getPopupContainer(this.getRootDomNode())
494506
: props.getDocument().body;
495507
mountNode.appendChild(popupContainer);
496508
return popupContainer;

0 commit comments

Comments
 (0)