import React, { Component } from 'react'; import { TabView, TabPanel } from '../../components/tabview/TabView'; import { CodeHighlight } from '../codehighlight/CodeHighlight'; import { LiveEditor } from '../liveeditor/LiveEditor'; export class CarouselDoc extends Component { constructor(props) { super(props); this.sources = { 'class': { tabName: 'Class Source', content: ` import React, { Component } from 'react'; import { Carousel } from 'primereact/carousel'; import { Button } from 'primereact/button'; import ProductService from '../service/ProductService'; import './CarouselDemo.css'; export class CarouselDemo extends Component { constructor(props) { super(props); this.state = { products: [] }; this.responsiveOptions = [ { breakpoint: '1024px', numVisible: 3, numScroll: 3 }, { breakpoint: '600px', numVisible: 2, numScroll: 2 }, { breakpoint: '480px', numVisible: 1, numScroll: 1 } ]; this.productService = new ProductService(); this.productTemplate = this.productTemplate.bind(this); } componentDidMount() { this.productService.getProductsSmall().then(data => this.setState({ products: data.slice(0,9) })); } productTemplate(product) { return (
e.target.src='https://www.primefaces.org/wp-content/uploads/2020/05/placeholder.png'} alt={product.name} className="product-image" />

{product.name}

\${product.price}
{product.inventoryStatus}
); } render() { return (
Basic} />
Circular, AutoPlay, 3 Items per Page and Scroll by 1} />
Vertical} style={{maxWidth: '400px', marginTop: '2em'}} />
); } } ` }, 'hooks': { tabName: 'Hooks Source', content: ` import React, { useState, useEffect } from 'react'; import { Carousel } from 'primereact/carousel'; import { Button } from 'primereact/button'; import ProductService from '../service/ProductService'; import './CarouselDemo.css'; const CarouselDemo = () => { const [products, setProducts] = useState([]); const responsiveOptions = [ { breakpoint: '1024px', numVisible: 3, numScroll: 3 }, { breakpoint: '600px', numVisible: 2, numScroll: 2 }, { breakpoint: '480px', numVisible: 1, numScroll: 1 } ]; const productService = new ProductService(); useEffect(() => { productService.getProductsSmall().then(data => setProducts(data.slice(0,9))); }, []); // eslint-disable-line react-hooks/exhaustive-deps const productTemplate = (product) => { return (
e.target.src='https://www.primefaces.org/wp-content/uploads/2020/05/placeholder.png'} alt={product.name} className="product-image" />

{product.name}

\${product.price}
{product.inventoryStatus}
); } return (
Basic} />
Circular, AutoPlay, 3 Items per Page and Scroll by 1} />
Vertical} style={{maxWidth: '400px', marginTop: '2em'}} />
); } ` }, 'ts': { tabName: 'TS Source', content: ` import React, { useState, useEffect } from 'react'; import { Carousel } from 'primereact/carousel'; import { Button } from 'primereact/button'; import ProductService from '../service/ProductService'; import './CarouselDemo.css'; const CarouselDemo = () => { const [products, setProducts] = useState([]); const responsiveOptions = [ { breakpoint: '1024px', numVisible: 3, numScroll: 3 }, { breakpoint: '600px', numVisible: 2, numScroll: 2 }, { breakpoint: '480px', numVisible: 1, numScroll: 1 } ]; const productService = new ProductService(); useEffect(() => { productService.getProductsSmall().then(data => setProducts(data.slice(0,9))); }, []); // eslint-disable-line react-hooks/exhaustive-deps const productTemplate = (product) => { return (
e.target.src='https://www.primefaces.org/wp-content/uploads/2020/05/placeholder.png'} alt={product.name} className="product-image" />

{product.name}

\${product.price}
{product.inventoryStatus}
); } return (
Basic} />
Circular, AutoPlay, 3 Items per Page and Scroll by 1} />
Vertical} style={{maxWidth: '400px', marginTop: '2em'}} />
); } ` } } this.extFiles = { 'src/demo/CarouselDemo.css': { content: ` .carousel-demo .product-item .product-item-content { border: 1px solid var(--surface-d); border-radius: 3px; margin: .3rem; text-align: center; padding: 2rem 0; } .carousel-demo .product-item .product-image { width: 50%; box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23); } ` } } } shouldComponentUpdate() { return false; } render() { return (
Import
{` import { Carousel } from 'primereact/carousel'; `}
Getting Started

Carousel requires a collection of items as its value along with a template to render each item.

{` `} {` itemTemplate(car) { // return content; } `}
Items per page and Scroll Items

Number of items per page is defined using the numVisible property whereas number of items to scroll is defined with the numScroll property.

{` `}
Responsive

For responsive design, numVisible and numScroll can be defined using the responsiveOptions property that should be an array of objects whose breakpoint defines the max-width to apply the settings.

{` `} {` const responsiveOptions = [ { breakpoint: '1024px', numVisible: 3, numScroll: 3 }, { breakpoint: '768px', numVisible: 2, numScroll: 2 }, { breakpoint: '560px', numVisible: 1, numScroll: 1 } ]; `}
Header and Footer

Custom content projection is available using the header and footer properties.

{` Header}> `}
Orientation

Default layout of the Carousel is horizontal, other possible option is the vertical mode that is configured with the orientation property.

{` `}
AutoPlay and Circular

When autoplayInterval is defined in milliseconds, items are scrolled automatically. In addition, for infinite scrolling circular property needs to be enabled. Note that in autoplay mode, circular is enabled by default.

Controlled vs Uncontrolled

In controlled mode, page and onPageChange properties need to be defined to control the first visible item.

{` this.setState({page: e.page})}> `}
Uncontrolled

In uncontrolled mode, no additional properties are required. Initial page can be provided using the page property in uncontrolled mode however it is evaluated at initial rendering and ignored in further updates. If you programmatically need to update the first visible item index, prefer to use the component as controlled.

{` `}
Properties
Name Type Default Description
id string null Unique identifier of the element.
value array null An array of objects to display.
page number null Index of the first item.
header any null Label of header.
footer any null Label of footer.
style string null Inline style of the component.
className string null Style class of the component.
itemTemplate function null Function that gets an item in the value and returns the content for it.
circular boolean false Defines if scrolling would be infinite.
autoplayInterval number null Time in milliseconds to scroll items automatically.
numVisible number 1 Number of items per page.
numScroll number 1 Number of items to scroll.
responsiveOptions any null An array of options for responsive design.
orientation string horizontal Specifies the layout of the component, valid values are "horizontal" and "vertical".
verticalViewPortHeight string 300px Height of the viewport in vertical layout.
contentClassName string null Style class of main content.
containerClassName string null Style class of the viewport container.
indicatorsContentClassName string null Style class of the paginator items.
Events
Name Parameters Description
onPageChange event.page = Value of the new page. Callback to invoke after scroll.
Styling

Following is the list of structural style classes

Name Element
p-carousel Container element.
p-carousel-header Header section.
p-carousel-footer Footer section.
p-carousel-content Main content element. It contains the container of the viewport.
p-carousel-container Container of the viewport. It contains navigation buttons and viewport.
p-carousel-items-content Viewport.
p-carousel-indicators Container of the indicators.
p-carousel-indicator Indicator element.
Dependencies

None.

); } }