Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Carousel): fix index not being reset when children change #3185

Merged
merged 2 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Carousel/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
ReactChildren,
useTimeout,
mergeRefs,
useControlled
useControlled,
useUpdateEffect
} from '../utils';
import { WithAsProps, RsRefForwardingComponent } from '../@types/common';

Expand Down Expand Up @@ -67,10 +68,22 @@ const Carousel: RsRefForwardingComponent<'div', CarouselProps> = React.forwardRe
const vertical = placement === 'left' || placement === 'right';
const lengthKey = vertical ? 'height' : 'width';

const [activeIndex, setActiveIndex] = useControlled(activeIndexProp, defaultActiveIndex);
const [activeIndex, setActiveIndex, isControlled] = useControlled(
activeIndexProp,
defaultActiveIndex
);
const [lastIndex, setLastIndex] = useState(0);
const rootRef = useRef<HTMLDivElement>(null);

useUpdateEffect(() => {
// When the index is controlled, the index is not updated when the number of children changes.
if (isControlled) {
return;
}
// Reset the index when the number of children changes.
setActiveIndex(0);
}, [children, isControlled]);

// Set a timer for automatic playback.
// `autoplay` needs to be cast to boolean type to avoid undefined parameters.
const { clear, reset } = useTimeout(
Expand Down
23 changes: 22 additions & 1 deletion src/Carousel/test/CarouselSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Simulate } from 'react-dom/test-utils';
import sinon from 'sinon';
import { getDOMNode } from '@test/testUtils';
import { render, act, waitFor, fireEvent, screen } from '@testing-library/react';
import { render, screen, act, waitFor, fireEvent } from '@testing-library/react';
import { testStandardProps } from '@test/commonCases';
import Carousel from '../Carousel';

Expand Down Expand Up @@ -164,4 +164,25 @@ describe('Carousel', () => {
expect(screen.getByText('3')).to.have.attr('aria-hidden', 'true');
expect(screen.getByText('4')).to.have.attr('aria-hidden', 'false');
});

it('Should reset index when children change', () => {
const { rerender } = render(
<Carousel defaultActiveIndex={2}>
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
</Carousel>
);

expect(screen.getByText('3')).to.have.attribute('aria-hidden', 'false');

rerender(
<Carousel defaultActiveIndex={2}>
<button>1</button>
</Carousel>
);

expect(screen.getByText('1')).to.have.attribute('aria-hidden', 'false');
});
});