refactor: carousel ref strings to callback (#7486)

pull/7512/head
Wei Zhu 8 years ago committed by Benjy Cui
parent 825a3d5e95
commit 32b7a3f752

@ -6,7 +6,7 @@ describe('Carousel', () => {
it('should has innerSlider', () => { it('should has innerSlider', () => {
const wrapper = mount(<Carousel><div /></Carousel>); const wrapper = mount(<Carousel><div /></Carousel>);
const innerSlider = wrapper.node.innerSlider; const innerSlider = wrapper.node.innerSlider;
const innerSliderFromRefs = wrapper.node.refs.slick.innerSlider; const innerSliderFromRefs = wrapper.node.slick.innerSlider;
expect(innerSlider).toBe(innerSliderFromRefs); expect(innerSlider).toBe(innerSliderFromRefs);
expect(typeof innerSlider.slickNext).toBe('function'); expect(typeof innerSlider.slickNext).toBe('function');
}); });

@ -74,9 +74,7 @@ export default class Carousel extends React.Component<CarouselProps, any> {
draggable: false, draggable: false,
}; };
refs: { slick: any;
slick: any,
};
innerSlider: any; innerSlider: any;
@ -92,9 +90,8 @@ export default class Carousel extends React.Component<CarouselProps, any> {
if (autoplay) { if (autoplay) {
window.addEventListener('resize', this.onWindowResized); window.addEventListener('resize', this.onWindowResized);
} }
const { slick } = this.refs;
// https://github.com/ant-design/ant-design/issues/7191 // https://github.com/ant-design/ant-design/issues/7191
this.innerSlider = slick && slick.innerSlider; this.innerSlider = this.slick && this.slick.innerSlider;
} }
componentWillUnmount() { componentWillUnmount() {
@ -107,13 +104,16 @@ export default class Carousel extends React.Component<CarouselProps, any> {
onWindowResized = () => { onWindowResized = () => {
// Fix https://github.com/ant-design/ant-design/issues/2550 // Fix https://github.com/ant-design/ant-design/issues/2550
const { slick } = this.refs;
const { autoplay } = this.props; const { autoplay } = this.props;
if (autoplay && slick && slick.innerSlider && slick.innerSlider.autoPlay) { if (autoplay && this.slick && this.slick.innerSlider && this.slick.innerSlider.autoPlay) {
slick.innerSlider.autoPlay(); this.slick.innerSlider.autoPlay();
} }
} }
saveSlick = (node) => {
this.slick = node;
}
render() { render() {
let props = { let props = {
...this.props, ...this.props,
@ -130,7 +130,7 @@ export default class Carousel extends React.Component<CarouselProps, any> {
return ( return (
<div className={className}> <div className={className}>
<SlickCarousel ref="slick" {...props} /> <SlickCarousel ref={this.saveSlick} {...props} />
</div> </div>
); );
} }

Loading…
Cancel
Save