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 (
Carousel requires a collection of items as its value along with a template to render each item.
Number of items per page is defined using the numVisible property whereas number of items to scroll is defined with the numScroll property.
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.
Custom content projection is available using the header and footer properties.
Default layout of the Carousel is horizontal, other possible option is the vertical mode that is configured with the orientation property.
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.
In controlled mode, page and onPageChange properties need to be defined to control the first visible item.
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.
| 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. |
| Name | Parameters | Description |
|---|---|---|
| onPageChange | event.page = Value of the new page. | Callback to invoke after scroll. |
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. |
None.