Skip to content

Commit c1c72e0

Browse files
committed
feat(carousel-item): add prop type-checking for the interval prop
Adds the proper prop type-check for the `interval` prop, improving overall developer experience. Also comes with a new export to be able to extend from the `CarouselItemProps` interface, so that our end-users (developers) who are using TS can properly type-check their encapsulated components.
1 parent 30cae9d commit c1c72e0

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/CarouselItem.tsx

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1-
import createWithBsPrefix from './createWithBsPrefix';
1+
import classNames from 'classnames';
2+
import React from 'react';
3+
import PropTypes from 'prop-types';
4+
import { useBootstrapPrefix } from './ThemeProvider';
5+
import {
6+
BsPrefixPropsWithChildren,
7+
BsPrefixRefForwardingComponent,
8+
} from './helpers';
29

3-
export default createWithBsPrefix('carousel-item');
10+
export interface CarouselItemProps extends BsPrefixPropsWithChildren {
11+
interval?: number;
12+
}
13+
14+
type CarouselItem = BsPrefixRefForwardingComponent<'div', CarouselItemProps>;
15+
16+
const propTypes = {
17+
/** Set a custom element for this component */
18+
as: PropTypes.elementType,
19+
20+
/** @default 'carousel-item' */
21+
bsPrefix: PropTypes.string,
22+
23+
/** The amount of time to delay between automatically cycling this specific item. Will default to the Carousel's `interval` prop value if none is specified. */
24+
interval: PropTypes.number,
25+
};
26+
27+
const CarouselItem = (React.forwardRef(
28+
(
29+
{
30+
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
31+
as: Component = 'div',
32+
bsPrefix,
33+
children,
34+
className,
35+
...props
36+
}: CarouselItemProps,
37+
ref,
38+
) => {
39+
const finalClassName = classNames(
40+
className,
41+
useBootstrapPrefix(bsPrefix, 'carousel-item'),
42+
);
43+
return (
44+
<Component ref={ref} {...props} className={finalClassName}>
45+
{children}
46+
</Component>
47+
);
48+
},
49+
) as unknown) as CarouselItem;
50+
51+
CarouselItem.displayName = 'CarouselItem';
52+
CarouselItem.propTypes = propTypes;
53+
54+
export default CarouselItem;

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export { default as Carousel } from './Carousel';
4646
export type { CarouselProps } from './Carousel';
4747

4848
export { default as CarouselItem } from './CarouselItem';
49+
export type { CarouselItemProps } from './CarouselItem';
50+
4951
export { default as CloseButton } from './CloseButton';
5052
export type { CloseButtonProps } from './CloseButton';
5153

0 commit comments

Comments
 (0)