|
| 1 | +import type { FunctionComponent, ReactNode } from 'react'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import { ActionList, ActionListItem, Bullseye, Button, ButtonProps, Flex, FlexItem, FlexProps } from '@patternfly/react-core'; |
| 4 | + |
| 5 | +/** |
| 6 | + * extends PatternFly's ButtonProps |
| 7 | + */ |
| 8 | +export interface DeckButton extends ButtonProps { |
| 9 | + /** Automatically navigate to next/previous page or close the deck when clicked */ |
| 10 | + navigation?: 'next' | 'previous' | 'close'; |
| 11 | +} |
| 12 | + |
| 13 | +export interface DeckPage { |
| 14 | + /** Content to display on this page */ |
| 15 | + content: ReactNode; |
| 16 | + /** Array of button configurations for this page */ |
| 17 | + buttons?: DeckButton[]; |
| 18 | +} |
| 19 | + |
| 20 | +export interface DeckProps { |
| 21 | + /** Array of pages to display in the deck */ |
| 22 | + pages: DeckPage[]; |
| 23 | + /** Deck className */ |
| 24 | + className?: string; |
| 25 | + /** Custom OUIA ID */ |
| 26 | + ouiaId?: string; |
| 27 | + /** Hide the progress dots indicator */ |
| 28 | + hideProgressDots?: boolean; |
| 29 | + /** Initial page index to display (0-based) */ |
| 30 | + initialPage?: number; |
| 31 | + /** Callback when page changes */ |
| 32 | + onPageChange?: (pageIndex: number) => void; |
| 33 | + /** Callback when deck is closed/cancelled */ |
| 34 | + onClose?: () => void; |
| 35 | + /** Additional props for the Flex layout containing content, progress dots, and buttons */ |
| 36 | + contentFlexProps?: FlexProps; |
| 37 | + /** Text alignment for content (uses PatternFly utility classes). Set to false to disable. */ |
| 38 | + textAlign?: 'center' | 'left' | 'right' | false; |
| 39 | + /** Accessible label for the deck region */ |
| 40 | + ariaLabel?: string; |
| 41 | + /** Accessible role description for the deck */ |
| 42 | + ariaRoleDescription?: string; |
| 43 | + /** Function to generate accessible page info label. Receives (currentPage, totalPages) and returns a string. */ |
| 44 | + getPageLabel?: (currentPage: number, totalPages: number) => string; |
| 45 | +} |
| 46 | + |
| 47 | +export const Deck: FunctionComponent<DeckProps> = ({ |
| 48 | + pages, |
| 49 | + className, |
| 50 | + ouiaId = 'Deck', |
| 51 | + hideProgressDots = false, |
| 52 | + initialPage = 0, |
| 53 | + onPageChange, |
| 54 | + onClose, |
| 55 | + contentFlexProps, |
| 56 | + textAlign = 'center', |
| 57 | + ariaLabel = 'Information deck', |
| 58 | + ariaRoleDescription = 'sequential information deck', |
| 59 | + getPageLabel = (current, total) => `Page ${current} of ${total}`, |
| 60 | + ...props |
| 61 | +}: DeckProps) => { |
| 62 | + const [currentPageIndex, setCurrentPageIndex] = useState(initialPage); |
| 63 | + |
| 64 | + const handlePageChange = (newIndex: number) => { |
| 65 | + if (newIndex >= 0 && newIndex < pages.length) { |
| 66 | + setCurrentPageIndex(newIndex); |
| 67 | + onPageChange?.(newIndex); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + const currentPage = pages[currentPageIndex]; |
| 72 | + |
| 73 | + // Generate text alignment class if specified |
| 74 | + const textAlignClass = textAlign ? `pf-v6-u-text-align-${textAlign}` : ''; |
| 75 | + |
| 76 | + // Generate accessible label with page information |
| 77 | + const pageInfo = getPageLabel(currentPageIndex + 1, pages.length); |
| 78 | + |
| 79 | + return ( |
| 80 | + <Bullseye |
| 81 | + className={className} |
| 82 | + data-ouia-component-id={ouiaId} |
| 83 | + {...props} |
| 84 | + > |
| 85 | + <Flex |
| 86 | + direction={{ default: 'column' }} |
| 87 | + spaceItems={{ default: 'spaceItemsLg' }} |
| 88 | + alignItems={{ default: 'alignItemsCenter' }} |
| 89 | + role="region" |
| 90 | + aria-label={ariaLabel} |
| 91 | + aria-roledescription={ariaRoleDescription} |
| 92 | + {...contentFlexProps} |
| 93 | + > |
| 94 | + {/* Current page content */} |
| 95 | + <FlexItem |
| 96 | + className={textAlignClass} |
| 97 | + data-ouia-component-id={`${ouiaId}-content`} |
| 98 | + aria-live="polite" |
| 99 | + aria-atomic="true" |
| 100 | + aria-label={pageInfo} |
| 101 | + > |
| 102 | + {currentPage?.content} |
| 103 | + </FlexItem> |
| 104 | + |
| 105 | + {/* Progress dots */} |
| 106 | + {!hideProgressDots && pages.length > 1 && ( |
| 107 | + <FlexItem data-ouia-component-id={`${ouiaId}-progress-dots`}> |
| 108 | + <Flex spaceItems={{ default: 'spaceItemsSm' }} alignItems={{ default: 'alignItemsCenter' }}> |
| 109 | + {pages.map((_, index) => ( |
| 110 | + <FlexItem key={index}> |
| 111 | + <span |
| 112 | + style={{ |
| 113 | + width: '6px', |
| 114 | + height: '6px', |
| 115 | + display: 'inline-block', |
| 116 | + borderRadius: 'var(--pf-t--global--border--radius--pill)', |
| 117 | + backgroundColor: index === currentPageIndex |
| 118 | + ? 'var(--pf-t--global--background--color--inverse--default)' |
| 119 | + : 'transparent', |
| 120 | + border: index === currentPageIndex |
| 121 | + ? 'none' |
| 122 | + : '1px solid var(--pf-t--global--background--color--inverse--default)', |
| 123 | + transition: 'all 0.2s ease' |
| 124 | + }} |
| 125 | + aria-hidden="true" |
| 126 | + /> |
| 127 | + </FlexItem> |
| 128 | + ))} |
| 129 | + </Flex> |
| 130 | + </FlexItem> |
| 131 | + )} |
| 132 | + |
| 133 | + {/* Page buttons */} |
| 134 | + {currentPage?.buttons && currentPage.buttons.length > 0 && ( |
| 135 | + <FlexItem> |
| 136 | + <ActionList data-ouia-component-id={`${ouiaId}-buttons`}> |
| 137 | + {currentPage.buttons.map((buttonConfig, index) => { |
| 138 | + const { navigation, onClick, ...buttonProps } = buttonConfig; |
| 139 | + |
| 140 | + // Auto-wire navigation if specified |
| 141 | + const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { |
| 142 | + // Call user's custom onClick first if provided |
| 143 | + onClick?.(event); |
| 144 | + |
| 145 | + // Then handle navigation |
| 146 | + if (navigation === 'next') { |
| 147 | + handlePageChange(currentPageIndex + 1); |
| 148 | + } else if (navigation === 'previous') { |
| 149 | + handlePageChange(currentPageIndex - 1); |
| 150 | + } else if (navigation === 'close') { |
| 151 | + onClose?.(); |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + return ( |
| 156 | + <ActionListItem key={index}> |
| 157 | + <Button |
| 158 | + {...buttonProps} |
| 159 | + onClick={navigation || onClick ? handleClick : undefined} |
| 160 | + /> |
| 161 | + </ActionListItem> |
| 162 | + ); |
| 163 | + })} |
| 164 | + </ActionList> |
| 165 | + </FlexItem> |
| 166 | + )} |
| 167 | + </Flex> |
| 168 | + </Bullseye> |
| 169 | + ); |
| 170 | +}; |
| 171 | + |
| 172 | +export default Deck; |
0 commit comments