Skip to content

Commit a16ff4c

Browse files
committed
add currentRef and childRefs
1 parent f492a5f commit a16ff4c

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ ReactDom.render(
5858
| --- | --- | --- | --- | --- |
5959
| IE 10+ ✔ | Chrome 31.0+ ✔ | Firefox 31.0+ ✔ | Opera 30.0+ ✔ | Safari 7.0+ ✔ |
6060

61+
### 1.7.x add childRefs and currentRef;
62+
63+
```jsx
64+
<QueueAnim
65+
component={Row}
66+
ref={(c) => {
67+
this.ref = c;
68+
}}
69+
onEnd={() => {
70+
// this..currentRef = <Row />
71+
// this..childRefs.a = <Col key="a">1212</Col>
72+
}}
73+
>
74+
<Col key="a">1212</Col>
75+
</QueueAnim>
76+
```
77+
6178
## API
6279

6380
> You must provide the key attribute for all children of QueueAnim, children would not peform any animation without key.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-queue-anim",
3-
"version": "1.6.12",
3+
"version": "1.7.0",
44
"description": "Queue animation component for react",
55
"keywords": [
66
"react",
@@ -83,7 +83,7 @@
8383
"dependencies": {
8484
"babel-runtime": "6.x",
8585
"prop-types": "^15.6.0",
86-
"rc-tween-one": "^2.3.2"
86+
"rc-tween-one": "^2.5.0"
8787
},
8888
"types": "index.d.ts",
8989
"pre-commit": [

src/QueueAnim.jsx

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from './utils';
1111
import AnimTypes from './animTypes';
1212

13-
const noop = () => {};
13+
const noop = () => { };
1414

1515
const typeDefault = [
1616
'displayName',
@@ -104,6 +104,16 @@ class QueueAnim extends React.Component {
104104
* 进场时 deley 的 timeout 记录;
105105
*/
106106
this.placeholderTimeoutIds = {};
107+
/**
108+
* @param childRefs;
109+
* 储存 children 的 ref;
110+
*/
111+
this.childRefs = {};
112+
/**
113+
* @param currentRef;
114+
* 记录 component 是组件时的 ref;
115+
*/
116+
this.currentRef = null
107117
// 第一次进入,默认进场
108118
const children = toArrayChildren(getChildrenFromProps(props));
109119
const childrenShow = {};
@@ -381,6 +391,9 @@ class QueueAnim extends React.Component {
381391
}
382392
});
383393
}
394+
let ref = () => {
395+
delete this.childRefs[key];
396+
};
384397
// 处理出场
385398
if (i >= 0) {
386399
if (this.leaveUnfinishedChild.indexOf(key) >= 0) {
@@ -396,6 +409,9 @@ class QueueAnim extends React.Component {
396409
} else {
397410
// 处理进场;
398411
i = this.keysToEnterToCallback.indexOf(key);
412+
ref = (c) => {
413+
this.childRefs[key] = c && c.currentRef ? c.currentRef : c;
414+
}
399415
// appear=false 时,设定 childrenShow 和 tweenToEnter 都为 true, 这里不渲染 animation;
400416
if (this.tweenToEnter[key] && !forcedReplay) {
401417
// 如果是已进入的,将直接返回标签。。
@@ -404,6 +420,7 @@ class QueueAnim extends React.Component {
404420
component: child.type,
405421
forcedJudg,
406422
componentProps: child.props,
423+
ref,
407424
});
408425
} else if (!this.tweenToEnter[key]) {
409426
animation = this.enterAnimation[key] || this.getTweenEnterOrLeaveData(key, i, 0, 'enter');
@@ -418,6 +435,7 @@ class QueueAnim extends React.Component {
418435
forcedJudg,
419436
componentProps: child.props,
420437
animation,
438+
ref,
421439
});
422440
this.saveTweenOneTag[key] = tag;
423441
return tag;
@@ -506,24 +524,28 @@ class QueueAnim extends React.Component {
506524
};
507525

508526
render() {
509-
const { ...tagProps } = this.props;
510-
[
511-
'component',
512-
'componentProps',
513-
'interval',
514-
'duration',
515-
'delay',
516-
'type',
517-
'animConfig',
518-
'ease',
519-
'leaveReverse',
520-
'animatingClassName',
521-
'forcedReplay',
522-
'onEnd',
523-
'appear',
524-
].forEach(key => delete tagProps[key]);
527+
const {
528+
component,
529+
componentProps,
530+
interval,
531+
duration,
532+
delay,
533+
type,
534+
animConfig,
535+
ease,
536+
leaveReverse,
537+
animatingClassName,
538+
forcedReplay,
539+
onEnd,
540+
appear,
541+
...tagProps
542+
} = this.props;
525543
const childrenToRender = toArrayChildren(this.state.children).map(this.getChildrenToRender);
526-
const props = { ...tagProps, ...this.props.componentProps };
544+
const props = {
545+
...tagProps,
546+
...this.props.componentProps,
547+
ref: (c) => { this.currentRef = c; }
548+
};
527549
return createElement(this.props.component, props, childrenToRender);
528550
}
529551
}

0 commit comments

Comments
 (0)