Skip to content

Commit

Permalink
feat(Carousel): add indicatorLabels prop and fix indicator styling (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kyletsang committed May 20, 2021
1 parent aba14d9 commit 0f7b39d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
25 changes: 20 additions & 5 deletions src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface CarouselProps
fade?: boolean;
controls?: boolean;
indicators?: boolean;
indicatorLabels?: string[];
activeIndex?: number;
onSelect?: (eventKey: number, event: Record<string, unknown> | null) => void;
defaultActiveIndex?: number;
Expand Down Expand Up @@ -85,6 +86,11 @@ const propTypes = {
*/
indicators: PropTypes.bool,

/**
* An array of labels for the indicators. Defaults to "Slide #" if not provided.
*/
indicatorLabels: PropTypes.array,

/**
* Controls the current visible slide
*
Expand Down Expand Up @@ -174,7 +180,7 @@ const defaultProps = {
fade: false,
controls: true,
indicators: true,

indicatorLabels: [],
defaultActiveIndex: 0,
interval: 5000,
keyboard: true,
Expand Down Expand Up @@ -220,6 +226,7 @@ const Carousel: BsPrefixRefForwardingComponent<
fade,
controls,
indicators,
indicatorLabels,
activeIndex,
onSelect,
onSlide,
Expand Down Expand Up @@ -524,15 +531,23 @@ const Carousel: BsPrefixRefForwardingComponent<
)}
>
{indicators && (
<ol className={`${prefix}-indicators`}>
{map(children, (_child, index) => (
<li
<div className={`${prefix}-indicators`}>
{map(children, (_, index) => (
<button
key={index}
type="button"
data-bs-target="" // Bootstrap requires this in their css.
aria-label={
indicatorLabels?.length
? indicatorLabels[index]
: `Slide ${index + 1}`
}
className={index === renderedActiveIndex ? 'active' : undefined}
onClick={indicatorOnClicks ? indicatorOnClicks[index] : undefined}
aria-current={index === renderedActiveIndex}
/>
))}
</ol>
</div>
)}

<div className={`${prefix}-inner`}>
Expand Down
30 changes: 23 additions & 7 deletions test/CarouselSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('<Carousel>', () => {

expect(carouselItems.at(0).is('.active')).to.be.true;
expect(carouselItems.at(1).is('.active')).to.be.false;
expect(wrapper.find('.carousel-indicators > li')).to.have.lengthOf(
expect(wrapper.find('.carousel-indicators > button')).to.have.lengthOf(
items.length,
);
});
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('<Carousel>', () => {

expect(carouselItems.at(0).is('.active')).to.be.true;
expect(carouselItems.at(0).text()).to.equal('Item 1 content');
expect(wrapper.find('.carousel-indicators > li')).to.have.lengthOf(2);
expect(wrapper.find('.carousel-indicators > button')).to.have.lengthOf(2);
});

it('should call onSelect when indicator selected', (done) => {
Expand All @@ -76,7 +76,23 @@ describe('<Carousel>', () => {
</Carousel>,
);

wrapper.find('.carousel-indicators li').first().simulate('click');
wrapper.find('.carousel-indicators button').first().simulate('click');
});

it('should render custom indicator labels', () => {
const labels = ['custom1', 'custom2', 'custom3'];

const wrapper = mount(
<Carousel activeIndex={1} interval={null} indicatorLabels={labels}>
{items}
</Carousel>,
);

const indicators = wrapper.find('.carousel-indicators button');
for (let i = 0; i < labels.length; i++) {
const node = indicators.at(i).getDOMNode();
expect(node.getAttribute('aria-label')).to.equal(labels[i]);
}
});

it('should render variant', () => {
Expand Down Expand Up @@ -137,7 +153,7 @@ describe('<Carousel>', () => {
</Carousel>,
);

wrapper.find('.carousel-indicators li').first().simulate('click');
wrapper.find('.carousel-indicators button').first().simulate('click');
});

it(`should call ${eventName} with next index and direction`, (done) => {
Expand All @@ -159,7 +175,7 @@ describe('<Carousel>', () => {
</Carousel>,
);

wrapper.find('.carousel-indicators li').last().simulate('click');
wrapper.find('.carousel-indicators button').last().simulate('click');
});
});

Expand Down Expand Up @@ -289,15 +305,15 @@ describe('<Carousel>', () => {
<Carousel defaultActiveIndex={items.length - 1}>{items}</Carousel>,
);

expect(wrapper.find('.carousel-indicators > li')).to.have.lengthOf(
expect(wrapper.find('.carousel-indicators > button')).to.have.lengthOf(
items.length,
);

let fewerItems = items.slice(2);

wrapper.setProps({ children: fewerItems });

expect(wrapper.find('.carousel-indicators > li')).to.have.lengthOf(
expect(wrapper.find('.carousel-indicators > button')).to.have.lengthOf(
fewerItems.length,
);
expect(wrapper.find('div.carousel-item')).to.have.lengthOf(
Expand Down

0 comments on commit 0f7b39d

Please sign in to comment.