Skip to content

Commit 858968c

Browse files
authored
add ref only when support (#174)
1 parent 97b9a86 commit 858968c

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

examples/simple.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,27 @@ function getPopupContainer(trigger) {
3535
return trigger.parentNode;
3636
}
3737

38-
const InnerTarget = React.forwardRef((props, ref) => {
38+
const InnerTarget = props => (
39+
<div
40+
style={{
41+
margin: 20,
42+
display: 'inline-block',
43+
background: 'rgba(255, 0, 0, 0.05)',
44+
}}
45+
tabIndex={0}
46+
role="button"
47+
{...props}
48+
>
49+
<p>This is a example of trigger usage.</p>
50+
<p>You can adjust the value above</p>
51+
<p>which will also change the behaviour of popup.</p>
52+
</div>
53+
);
54+
55+
const RefTarget = React.forwardRef((props, ref) => {
3956
React.useImperativeHandle(ref, () => ({}));
4057

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-
);
58+
return <InnerTarget {...props} />;
5359
});
5460

5561
class Test extends React.Component {
@@ -243,7 +249,11 @@ class Test extends React.Component {
243249
</label>
244250
&nbsp;&nbsp;&nbsp;&nbsp;
245251
<label>
246-
<input checked={!!this.state.mask} type="checkbox" onChange={this.onMask} />
252+
<input
253+
checked={!!this.state.mask}
254+
type="checkbox"
255+
onChange={this.onMask}
256+
/>
247257
mask
248258
</label>
249259
&nbsp;&nbsp;&nbsp;&nbsp;
@@ -258,12 +268,20 @@ class Test extends React.Component {
258268
<br />
259269
<label>
260270
offsetX:
261-
<input type="text" onChange={this.onOffsetXChange} style={{ width: 50 }} />
271+
<input
272+
type="text"
273+
onChange={this.onOffsetXChange}
274+
style={{ width: 50 }}
275+
/>
262276
</label>
263277
&nbsp;&nbsp;&nbsp;&nbsp;
264278
<label>
265279
offsetY:
266-
<input type="text" onChange={this.onOffsetYChange} style={{ width: 50 }} />
280+
<input
281+
type="text"
282+
onChange={this.onOffsetYChange}
283+
style={{ width: 50 }}
284+
/>
267285
</label>
268286
&nbsp;&nbsp;&nbsp;&nbsp;
269287
<button type="button" onClick={this.destroy}>
@@ -294,7 +312,7 @@ class Test extends React.Component {
294312
popup={<div>i am a popup</div>}
295313
popupTransitionName={state.transitionName}
296314
>
297-
<InnerTarget />
315+
<RefTarget />
298316
</Trigger>
299317
</div>
300318
</div>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@
6363
"raf": "^3.4.1",
6464
"rc-align": "^3.0.0-rc.0",
6565
"rc-animate": "^2.10.2",
66-
"rc-util": "^4.15.2"
66+
"rc-util": "^4.20.0"
6767
}
6868
}

src/index.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { HTMLAttributes } from 'react';
22
import ReactDOM from 'react-dom';
33
import contains from 'rc-util/lib/Dom/contains';
44
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
5-
import { composeRef } from 'rc-util/lib/ref';
5+
import { composeRef, supportRef } from 'rc-util/lib/ref';
66
import addEventListener from 'rc-util/lib/Dom/addEventListener';
77
import Portal from 'rc-util/lib/Portal';
88
import classNames from 'classnames';
@@ -779,10 +779,14 @@ export function generateTrigger(
779779
if (childrenClassName) {
780780
newChildProps.className = childrenClassName;
781781
}
782-
const trigger = React.cloneElement(child, {
782+
783+
const cloneProps: any = {
783784
...newChildProps,
784-
ref: composeRef(this.triggerRef, (child as any).ref),
785-
});
785+
};
786+
if (supportRef(child)) {
787+
cloneProps.ref = composeRef(this.triggerRef, (child as any).ref);
788+
}
789+
const trigger = React.cloneElement(child, cloneProps);
786790

787791
let portal: React.ReactElement;
788792
// prevent unmounting after it's rendered

0 commit comments

Comments
 (0)