import { Button } from '@wordpress/components'; import { Icon, arrowRight } from '@wordpress/icons'; import clsx from 'clsx'; import { useTranslate, useRtl } from 'i18n-calypso'; import { times } from 'lodash'; import { Children, useState, useEffect, ReactNode } from 'react'; import { Swipeable } from '../swipeable'; import './style.scss'; type ControlsProps = { showControlLabels?: boolean; currentPage: number; numberOfPages: number; setCurrentPage: ( page: number ) => void; navArrowSize: number; tracksPrefix: string; tracksFn: ( eventName: string, data?: any ) => void; }; const Controls = ( { showControlLabels = false, currentPage, numberOfPages, setCurrentPage, navArrowSize, tracksPrefix, tracksFn, }: ControlsProps ) => { const translate = useTranslate(); const isRtl = useRtl(); if ( numberOfPages < 2 ) { return null; } const canGoBack = currentPage > 0; const canGoForward = currentPage < numberOfPages - 1; return ( ); }; type DotPagerProps = { showControlLabels?: boolean; hasDynamicHeight?: boolean; children: ReactNode; className?: string; onPageSelected?: ( index: number ) => void; isClickEnabled?: boolean; rotateTime?: number; navArrowSize?: number; tracksPrefix?: string; tracksFn?: ( eventName: string, data?: Record< string, unknown > ) => void; includePreviousButton?: boolean; includeNextButton?: boolean; includeFinishButton?: boolean; onFinish?: () => void; }; const DotPager = ( { showControlLabels = false, hasDynamicHeight = false, children, className = '', onPageSelected, isClickEnabled = false, rotateTime = 0, navArrowSize = 18, tracksPrefix = '', tracksFn = () => {}, includePreviousButton = false, includeNextButton = false, includeFinishButton = false, onFinish = () => {}, ...props }: DotPagerProps ) => { const translate = useTranslate(); // Filter out the empty children const normalizedChildren = Children.toArray( children ).filter( Boolean ); const [ currentPage, setCurrentPage ] = useState( 0 ); const numPages = Children.count( normalizedChildren ); useEffect( () => { if ( currentPage >= numPages ) { setCurrentPage( numPages - 1 ); } }, [ numPages, currentPage ] ); useEffect( () => { if ( rotateTime > 0 && numPages > 1 ) { const timerId = setTimeout( () => { setCurrentPage( ( currentPage + 1 ) % numPages ); }, rotateTime ); return () => clearTimeout( timerId ); } }, [ currentPage, numPages, rotateTime ] ); const handleSelectPage = ( index: number ) => { setCurrentPage( index ); onPageSelected?.( index ); }; return (
{ normalizedChildren } { includePreviousButton && currentPage !== 0 && ( ) } { includeNextButton && currentPage < numPages - 1 && ( ) } { includeFinishButton && currentPage === numPages - 1 && ( ) }
); }; export default DotPager;