adjust Carousel children change goto Logic (#16756)

This commit is contained in:
zombieJ 2019-05-23 23:08:03 +08:00 committed by GitHub
parent 7884ea72f0
commit 5ab2783ff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View File

@ -106,12 +106,28 @@ describe('Carousel', () => {
warnSpy.mockRestore();
});
it('should active when children change', () => {
const wrapper = mount(<Carousel />);
wrapper.setProps({
children: <div />,
describe('should active when children change', () => {
it('should active', () => {
const wrapper = mount(<Carousel />);
wrapper.setProps({
children: <div />,
});
wrapper.update();
expect(wrapper.find('.slick-active').length).toBeTruthy();
});
it('should keep initialSlide', () => {
const wrapper = mount(<Carousel initialSlide={1} />);
wrapper.setProps({
children: [<div key="1" />, <div key="2" />, <div key="3" />],
});
wrapper.update();
expect(
wrapper
.find('.slick-dots li')
.at(1)
.hasClass('slick-active'),
).toBeTruthy();
});
wrapper.update();
expect(wrapper.find('.slick-active').length).toBeTruthy();
});
});

View File

@ -73,7 +73,7 @@ export default class Carousel extends React.Component<CarouselProps, {}> {
componentDidUpdate(prevProps: CarouselProps) {
if (React.Children.count(this.props.children) !== React.Children.count(prevProps.children)) {
this.goTo(0, false);
this.goTo(this.props.initialSlide || 0, false);
}
}